Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deploy #594

Merged
merged 2 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deploy/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ts_project(
name = "release_src",
srcs = [
"deploy_test.ts",
"mocks.ts",
"program.ts",
"release.ts",
],
Expand Down
75 changes: 75 additions & 0 deletions deploy/mocks.ts
Original file line number Diff line number Diff line change
@@ -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!',
},
};
92 changes: 38 additions & 54 deletions deploy/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -156,15 +157,15 @@ export const program = Program.name('release')
)
.option('--dryRun <bool>', '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',
Expand All @@ -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,
Expand Down