Add a dry run CLI to help debug

This commit is contained in:
Barry Gordon
2021-05-26 17:25:03 +01:00
parent 5dd06f4151
commit 339a588190
3 changed files with 92 additions and 0 deletions

1
.env.example Normal file
View File

@@ -0,0 +1 @@
LOCAL_GITHUB_ACCESS_TOKEN=XXXX

9
src/dependabot/util.ts Normal file
View File

@@ -0,0 +1,9 @@
export function parseNwo (nwo: string): {owner: string; repo: string} {
const [owner, name] = nwo.split('/')
if (!owner || !name) {
throw new Error(`'${nwo}' does not appear to be a valid repository NWO`)
}
return { owner: owner, repo: name }
}

82
src/dry-run.ts Executable file
View File

@@ -0,0 +1,82 @@
/* 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
},
repository: {
owner: {
login: repoDetails.owner
},
name: repoDetails.repo
}
}
// Bypass the actor check for purpose of a dry run
actionContext.actor = 'dependabot[bot]'
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