/
dry-run.ts
executable file
·83 lines (72 loc) · 2.43 KB
/
dry-run.ts
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
/* eslint-disable no-console, @typescript-eslint/no-var-requires, no-unused-expressions */
import * as github from '@actions/github'
import { Context } from '@actions/github/lib/context'
import * as dotenv from 'dotenv'
import { Argv } from 'yargs'
import { hideBin } from 'yargs/helpers'
import { getMessage } from './dependabot/verified_commits'
import { parse } from './dependabot/update_metadata'
import { parseNwo } from './dependabot/util'
async function check (args: any): Promise<void> {
try {
const githubToken = process.env.LOCAL_GITHUB_ACCESS_TOKEN
if (!githubToken) {
console.log('You must set LOCAL_GITHUB_ACCESS_TOKEN to perform dry runs.')
process.exit(1)
}
const repoDetails = parseNwo(args.nwo)
const actionContext = new Context()
// Convert the CLI args into a stubbed Webhook payload
actionContext.payload = {
pull_request: {
number: args.prNumber,
user: {
login: 'dependabot[bot]'
}
},
repository: {
owner: {
login: repoDetails.owner
},
name: repoDetails.repo
}
}
const githubClient = github.getOctokit(githubToken)
// Retries the commit message if the PR is from Dependabot
const commitMessage = await getMessage(githubClient, actionContext)
if (commitMessage) {
console.log('This appears to be a valid Dependabot Pull Request.')
const updatedDependencies = parse(commitMessage)
if (updatedDependencies.length > 0) {
console.log('Updated dependencies:')
console.log(JSON.stringify(updatedDependencies, undefined, 2))
} else {
console.log('No dependabot metadata found!')
}
} else {
console.log('This is not a Dependabot Pull Request.')
process.exit(1)
}
} catch (exception) {
console.log(exception.message)
process.exit(1)
}
}
dotenv.config()
require('yargs')(hideBin(process.argv))
.command('check <nwo> <pr-number>', 'Perform a dry run on the given PR number of the NWO repo', (yargs) => {
yargs.positional('nwo', {
describe: 'The name with owner representation of a target repository, e.g. github/github',
type: 'string'
})
yargs.positional('pr-number', {
describe: 'The integer number of the target pull request to dry run',
type: 'number'
})
yargs.demandOption(['nwo', 'pr-number'])
}, (args: Argv) => {
check(args)
})
.demandCommand(1)
.help()
.argv