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, dry = true) {
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,
39 })
40}
41
42program
43 .command('commits')
44 .description('Check commit structure of a pull request.')
45 .argument('<owner>', 'Owner of the GitHub repository to check (Example: NixOS)')
46 .argument('<repo>', 'Name of the GitHub repository to check (Example: nixpkgs)')
47 .argument('<pr>', 'Number of the Pull Request to check')
48 .action(async (owner, repo, pr) => {
49 const commits = (await import('./commits.js')).default
50 run(commits, owner, repo, pr)
51 })
52
53program
54 .command('labels')
55 .description('Manage labels on pull requests.')
56 .argument('<owner>', 'Owner of the GitHub repository to label (Example: NixOS)')
57 .argument('<repo>', 'Name of the GitHub repository to label (Example: nixpkgs)')
58 .argument('[pr]', 'Number of the Pull Request to label')
59 .option('--no-dry', 'Make actual modifications')
60 .action(async (owner, repo, pr, options) => {
61 const labels = (await import('./labels.js')).default
62 const tmp = mkdtempSync(join(tmpdir(), 'github-script-'))
63 try {
64 process.env.GITHUB_WORKSPACE = tmp
65 process.chdir(tmp)
66 run(labels, owner, repo, pr, options.dry)
67 } finally {
68 rmSync(tmp, { recursive: true })
69 }
70 })
71
72await program.parse()