1#!/usr/bin/env -S node --import ./run
2import { execSync } from 'node:child_process'
3import { closeSync, mkdtempSync, openSync, rmSync } from 'node:fs'
4import { tmpdir } from 'node:os'
5import { join } from 'node:path'
6import { program } from 'commander'
7import * as core from '@actions/core'
8import { getOctokit } from '@actions/github'
9
10async function run(action, owner, repo, pull_number, options = {}) {
11 const token = execSync('gh auth token', { encoding: 'utf-8' }).trim()
12
13 const github = getOctokit(token)
14
15 const payload = !pull_number ? {} : {
16 pull_request: (await github.rest.pulls.get({
17 owner,
18 repo,
19 pull_number,
20 })).data
21 }
22
23 process.env['INPUT_GITHUB-TOKEN'] = token
24
25 closeSync(openSync('step-summary.md', 'w'))
26 process.env.GITHUB_STEP_SUMMARY = 'step-summary.md'
27
28 await action({
29 github,
30 context: {
31 payload,
32 repo: {
33 owner,
34 repo,
35 },
36 },
37 core,
38 dry: true,
39 ...options,
40 })
41}
42
43program
44 .command('prepare')
45 .description('Prepare relevant information of a pull request.')
46 .argument('<owner>', 'Owner of the GitHub repository to check (Example: NixOS)')
47 .argument('<repo>', 'Name of the GitHub repository to check (Example: nixpkgs)')
48 .argument('<pr>', 'Number of the Pull Request to check')
49 .option('--no-dry', 'Make actual modifications')
50 .action(async (owner, repo, pr, options) => {
51 const prepare = (await import('./prepare.js')).default
52 await run(prepare, owner, repo, pr, options)
53 })
54
55program
56 .command('commits')
57 .description('Check commit structure of a pull request.')
58 .argument('<owner>', 'Owner of the GitHub repository to check (Example: NixOS)')
59 .argument('<repo>', 'Name of the GitHub repository to check (Example: nixpkgs)')
60 .argument('<pr>', 'Number of the Pull Request to check')
61 .option('--no-cherry-picks', 'Do not expect cherry-picks.')
62 .action(async (owner, repo, pr, options) => {
63 const commits = (await import('./commits.js')).default
64 await run(commits, owner, repo, pr, options)
65 })
66
67program
68 .command('labels')
69 .description('Manage labels on pull requests.')
70 .argument('<owner>', 'Owner of the GitHub repository to label (Example: NixOS)')
71 .argument('<repo>', 'Name of the GitHub repository to label (Example: nixpkgs)')
72 .argument('[pr]', 'Number of the Pull Request to label')
73 .option('--no-dry', 'Make actual modifications')
74 .action(async (owner, repo, pr, options) => {
75 const labels = (await import('./labels.js')).default
76 const tmp = mkdtempSync(join(tmpdir(), 'github-script-'))
77 try {
78 process.env.GITHUB_WORKSPACE = tmp
79 process.chdir(tmp)
80 await run(labels, owner, repo, pr, options)
81 } finally {
82 rmSync(tmp, { recursive: true })
83 }
84 })
85
86await program.parse()