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 #593

Merged
merged 1 commit 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
29 changes: 26 additions & 3 deletions deploy/deploy_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
import program from './program';
import * as program from './program';

test('deploy', async () => {
test('smoke', async () => {
process.env.NPM_TOKEN = '123fake';
await program.parseAsync(['xxx', 'ok', '--dryRun', 'true']);
await program.program.parseAsync(['xxx', 'ok', '--dryRun', 'true']);
});

test('releaseNotes', () => {
expect(
program.releaseNotes([
{
kind: 'npm_publication',
buildTag: 'xxx',
package_name: 'something',
publish: async () => void 0,
},
{
kind: 'artifact',
filename: 'egg',
buildTag: 'whatever',
publish: async () => void 0,
},
])
).toEqual(`This release contains the following artifacts:
- whatever → egg
This release contains the following NPM packages:
- xxx → [something](https://npmjs.com/package/svgshot)
`);
});
121 changes: 65 additions & 56 deletions deploy/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,45 @@ interface ReleaseProps {
}): Promise<void>;
}

export function releaseNotes(notes: (NpmPackageInfo | ArtifactInfo)[]) {
const artifacts: ArtifactInfo[] = [];
const npmPackages: NpmPackageInfo[] = [];

for (const note of notes) {
if (note.kind === 'artifact') {
artifacts.push(note);
continue;
}
if (note.kind === 'npm_publication') {
npmPackages.push(note);
continue;
}
throw new Error(`Unknown kind ${(note as any).kind}`);
}

return `${
artifacts.length
? `This release contains the following artifacts:\n ${artifacts
.map(
artifact =>
` - ${artifact.buildTag} → ${artifact.filename}`
)
.join('\n')}`
: ''
}
${
npmPackages.length
? `This release contains the following NPM packages:\n ${npmPackages
.map(
pkg =>
` - ${pkg.buildTag} → [${pkg.package_name}](https://npmjs.com/package/svgshot)`
)
.join('\n')}`
: ''
}
`;
}

const release =
(...fns: (() => Promise<ArtifactInfo | NpmPackageInfo>)[]) =>
async ({
Expand Down Expand Up @@ -121,9 +160,10 @@ export const program = Program.name('release')
context.sha
}`;

const Github = !dryRun
? getOctokit(process.env['GITHUB_TOKEN']!)
: undefined;
const Github =
dryRun == false
? getOctokit(process.env['GITHUB_TOKEN']!)
: undefined;

const releaser = release(
artifact(
Expand All @@ -138,22 +178,27 @@ 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: Github
? 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');
},
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
Expand Down Expand Up @@ -183,44 +228,8 @@ export const program = Program.name('release')
return { release_id: 0 };
},
dryRun: dryRun,
releaseNotes: (notes: (NpmPackageInfo | ArtifactInfo)[]) => {
const artifacts: ArtifactInfo[] = [];
const npmPackages: NpmPackageInfo[] = [];

for (const note of notes) {
if (note.kind === 'artifact') {
artifacts.push(note);
continue;
}
if (note.kind === 'npm_publication') {
npmPackages.push(note);
continue;
}
throw new Error(`Unknown kind ${(note as any).kind}`);
}

return `${
artifacts.length
? `This release contains the following artifacts:\n ${artifacts
.map(
artifact =>
` - ${artifact.buildTag} → ${artifact.filename}`
)
.join('\n')}`
: ''
}
${
npmPackages.length
? `This release contains the following NPM packages:\n ${npmPackages
.map(
pkg =>
` - ${pkg.buildTag} → [${pkg.package_name}](https://npmjs.com/package/svgshot)`
)
.join('\n')}`
: ''
}
`;
},

releaseNotes,
});
});

Expand Down