summaryrefslogtreecommitdiff
path: root/archived/projt-launcher/ci/github-script/run
blob: 4f296f56339d33599bad58815062038a71c59523 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env -S node --import ./run
/**
 * ProjT Launcher - CI Script Runner
 * CLI tool for running CI automation scripts locally
 */
import { execSync } from 'node:child_process'
import { closeSync, mkdtempSync, openSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { program } from 'commander'
import * as core from '@actions/core'
import { getOctokit } from '@actions/github'

/**
 * Run a CI action locally
 */
async function run(action, owner, repo, pull_number, options = {}) {
  // Get GitHub token from gh CLI
  const token = execSync('gh auth token', { encoding: 'utf-8' }).trim()
  const github = getOctokit(token)

  // Build payload
  const payload = !pull_number ? {} : {
    pull_request: (await github.rest.pulls.get({
      owner,
      repo,
      pull_number,
    })).data
  }

  process.env['INPUT_GITHUB-TOKEN'] = token

  // Set up step summary file
  closeSync(openSync('step-summary.md', 'w'))
  process.env.GITHUB_STEP_SUMMARY = 'step-summary.md'

  await action({
    github,
    context: {
      payload,
      repo: {
        owner,
        repo,
      },
    },
    core,
    dry: true,
    ...options,
  })
}

program
  .name('projt-ci')
  .description('ProjT Launcher CI automation script runner')
  .version('1.0.0')

program
  .command('prepare')
  .description('Prepare and validate a pull request')
  .argument('<owner>', 'Repository owner (e.g., Project-Tick)')
  .argument('<repo>', 'Repository name (e.g., ProjT-Launcher)')
  .argument('<pr>', 'Pull Request number')
  .option('--no-dry', 'Make actual modifications')
  .action(async (owner, repo, pr, options) => {
    const prepare = (await import('./prepare.js')).default
    await run(prepare, owner, repo, pr, options)
  })

program
  .command('commits')
  .description('Validate commit messages and structure')
  .argument('<owner>', 'Repository owner')
  .argument('<repo>', 'Repository name')
  .argument('<pr>', 'Pull Request number')
  .option('--no-dry', 'Make actual modifications')
  .action(async (owner, repo, pr, options) => {
    const commits = (await import('./commits.js')).default
    await run(commits, owner, repo, pr, options)
  })

program
  .command('get-teams')
  .description('Fetch team information from GitHub organization')
  .argument('<owner>', 'Organization/owner name')
  .argument('<repo>', 'Repository name')
  .argument('[outFile]', 'Output file path (prints to stdout if omitted)')
  .action(async (owner, repo, outFile, options) => {
    const getTeams = (await import('./get-teams.js')).default
    await run(getTeams, owner, repo, undefined, { ...options, outFile })
  })

program
  .command('reviewers')
  .description('Assign reviewers to a pull request')
  .argument('<owner>', 'Repository owner')
  .argument('<repo>', 'Repository name')
  .argument('<pr>', 'Pull Request number')
  .option('--no-dry', 'Make actual modifications')
  .action(async (owner, repo, pr, options) => {
    const { handleReviewers } = await import('./reviewers.js')
    const token = execSync('gh auth token', { encoding: 'utf-8' }).trim()
    const github = getOctokit(token)
    
    const pull_request = (await github.rest.pulls.get({
      owner,
      repo,
      pull_number: pr,
    })).data

    const reviews = await github.paginate(github.rest.pulls.listReviews, {
      owner,
      repo,
      pull_number: pr,
    })

    await handleReviewers({
      github,
      context: { repo: { owner, repo } },
      core,
      log: console.log,
      dry: options.dry ?? true,
      pull_request,
      reviews,
      maintainers: [],
      owners: [],
      getTeamMembers: async () => [],
      getUser: async (id) => ({ login: `user-${id}`, id }),
    })
  })

// Parse CLI arguments
await program.parse()