diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..d2b965f --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +LOCAL_GITHUB_ACCESS_TOKEN=XXXX diff --git a/src/dependabot/util.ts b/src/dependabot/util.ts new file mode 100644 index 0000000..cf503e0 --- /dev/null +++ b/src/dependabot/util.ts @@ -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 } +} diff --git a/src/dry-run.ts b/src/dry-run.ts new file mode 100755 index 0000000..b8397a4 --- /dev/null +++ b/src/dry-run.ts @@ -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 { + 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 ', '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