diff --git a/deploy/BUILD b/deploy/BUILD index 6fbc826355..4d02942c74 100644 --- a/deploy/BUILD +++ b/deploy/BUILD @@ -7,6 +7,7 @@ ts_project( name = "release_src", srcs = [ "deploy_test.ts", + "mocks.ts", "program.ts", "release.ts", ], diff --git a/deploy/mocks.ts b/deploy/mocks.ts new file mode 100644 index 0000000000..31f11b0bee --- /dev/null +++ b/deploy/mocks.ts @@ -0,0 +1,75 @@ +export const Github = { + rest: { + repos: { + uploadReleaseAsset({ + owner, + repo, + release_id, + }: { + owner: string; + repo: string; + release_id: number; + }) { + if (!owner) + throw new Error('invalid release asset: missing owner.'); + if (!repo) + throw new Error('invalid release asset: missing repo.'); + if (!release_id) + throw new Error( + 'invalid release asset: missing release_id.' + ); + }, + + createRelease({ + owner, + repo, + tag_name, + body, + generate_release_notes, + name, + target_commitish, + }: { + owner: string; + repo: string; + tag_name: string; + body: string; + generate_release_notes: boolean; + name: string; + target_commitish: string; + }) { + for (const v of [ + owner, + repo, + tag_name, + body, + generate_release_notes, + name, + target_commitish, + ]) { + if (v === undefined || v === null || v === '') + throw new Error( + `A parameter is empty. ${JSON.stringify({ + owner, + repo, + tag_name, + body, + generate_release_notes, + name, + target_commitish, + })}` + ); + } + return { data: { id: 9000 } }; + }, + }, + }, +}; + +export const context = { + sha: '123fakesha', + ref: '123fakeref!', + repo: { + owner: 'fake!', + repo: 'fake!', + }, +}; diff --git a/deploy/program.ts b/deploy/program.ts index 5c61a3a02e..c5578b4211 100644 --- a/deploy/program.ts +++ b/deploy/program.ts @@ -5,9 +5,10 @@ import fs from 'fs/promises'; import child_process from 'child_process'; import { promisify } from 'util'; -import { context, getOctokit } from '@actions/github'; +import { context as githubCtx, getOctokit } from '@actions/github'; import { Command } from 'commander'; import { runfiles } from '@bazel/runfiles'; +import { Github as mockGithub, context as mockContext } from './mocks'; const Program = new Command(); @@ -156,15 +157,15 @@ export const program = Program.name('release') ) .option('--dryRun ', 'Perform a dry run.', false) .action(async dryRun => { + const context = dryRun ? mockContext : githubCtx; + const Github = dryRun + ? mockGithub + : getOctokit(process.env['GITHUB_TOKEN']!); + const syntheticVersion = `v0.0.0-${new Date().getTime()}-${ context.sha }`; - const Github = - dryRun == false - ? getOctokit(process.env['GITHUB_TOKEN']!) - : undefined; - const releaser = release( artifact( 'recursive_vassals.zip', @@ -178,55 +179,38 @@ export const program = Program.name('release') npmPackage('svgshot', '//ts/cmd/svgshot/npm_pkg.publish.sh') ); - if (!dryRun && Github === undefined) - throw new Error('Unable to initialize Github API.'); - releaser({ - uploadReleaseAsset: - dryRun == false && Github !== undefined - ? async ({ name, release_id, data }) => - void (await Github.rest.repos.uploadReleaseAsset({ - owner: context.repo.owner, - repo: context.repo.repo, - release_id: await release_id, - name, - // https://github.com/octokit/octokit.js/discussions/2087#discussioncomment-646569 - data: data as unknown as string, - })) - : async ({ name, release_id, data }) => { - if (name === '') throw new Error('Name is empty'); - if (!release_id) - throw new Error('Release_id is empty'); - if (!data) throw new Error('data is empty'); - }, - - createRelease: - Github !== undefined - ? async ({ body }) => ({ - release_id: ( - await Github.rest.repos.createRelease({ - // could probably use a spread operator here - // but i also think that would be uglier... - owner: context.repo.owner, - repo: context.repo.repo, - - tag_name: syntheticVersion, - - body, - - generate_release_notes: true, - - name: syntheticVersion, - - target_commitish: context.ref, - }) - ).data.id, - }) - : async ({ body }) => { - if (body === '') - throw new Error(`Release body is empty.`); - return { release_id: 0 }; - }, + uploadReleaseAsset: async ({ name, release_id, data }) => + void (await Github.rest.repos.uploadReleaseAsset({ + owner: context.repo.owner, + repo: context.repo.repo, + release_id: await release_id, + name, + // https://github.com/octokit/octokit.js/discussions/2087#discussioncomment-646569 + data: data as unknown as string, + })), + + createRelease: async ({ body }) => ({ + release_id: ( + await Github.rest.repos.createRelease({ + // could probably use a spread operator here + // but i also think that would be uglier... + owner: context.repo.owner, + repo: context.repo.repo, + + tag_name: syntheticVersion, + + body, + + generate_release_notes: true, + + name: syntheticVersion, + + target_commitish: context.ref, + }) + ).data.id, + }), + dryRun: dryRun, releaseNotes,