Skip to content

Commit

Permalink
Merge pull request #593 from Zemnmez/deploy
Browse files Browse the repository at this point in the history
deploy
  • Loading branch information
Zemnmez authored Jul 22, 2022
2 parents 49ecd6d + fc4f381 commit 3ef84df
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 59 deletions.
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

0 comments on commit 3ef84df

Please sign in to comment.