Skip to content

Commit c874462

Browse files
committed
Automatically upload assets to github releases
1 parent 9787632 commit c874462

File tree

3 files changed

+54
-42
lines changed

3 files changed

+54
-42
lines changed

packages/build/src/github.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { getLatestRelease, createRelease, uploadAsset } from './github';

packages/build/src/github.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1+
import fs from 'fs';
2+
import path from 'path';
13
import { Octokit } from '@octokit/rest';
2-
import {
3-
GetResponseTypeFromEndpointMethod,
4-
GetResponseDataTypeFromEndpointMethod,
5-
} from "@octokit/types";
64
import semver from 'semver';
75

86
/**
@@ -14,16 +12,21 @@ const REPO = Object.freeze({
1412
});
1513

1614
/**
17-
* Determine if this version is releasable.
15+
* Release mongosh to Github releases.
1816
*
1917
* @param {string} version - The current version.
18+
* @param {string} artifact - The artifact path.
2019
* @param {Octokit} octokit - The octokit instance.
21-
*
22-
* @returns {Promise} The promise of the boolean.
2320
*/
24-
const isReleasable = async(version: string, octokit: Octokit): Promise<boolean> => {
21+
const releaseToGithub = async(version: string, artifact: string, octokit: Octokit): Promise<any> => {
2522
const latestRelease = await getLatestRelease(octokit);
26-
return semver.gt(version, latestRelease.tag_name.replace('v', ''));
23+
if (semver.gt(version, latestRelease.tag_name.replace('v', ''))) {
24+
// Create a new release if our version is higher than latest.
25+
const newRelease = await createRelease(version, octokit);
26+
await uploadAsset(artifact, newRelease.id, octokit);
27+
} else {
28+
await uploadAsset(artifact, latestRelease.id, octokit);
29+
}
2730
};
2831

2932
/**
@@ -48,33 +51,43 @@ const getLatestRelease = async(octokit: Octokit): Promise<any> => {
4851
* @param {string} version - The release version.
4952
* @param {Octokit} octokit - The octokit instance.
5053
*/
51-
const createRelease = (version: string, octokit: Octokit): Promise<any> => {
54+
const createRelease = async(version: string, octokit: Octokit): Promise<any> => {
5255
const params = {
5356
...REPO,
5457
tag_name: `v${version}`,
5558
name: version,
5659
body: 'TODO: Generate Release Notes'
5760
};
58-
return octokit.repos.createRelease(params);
61+
const { data } = await octokit.repos.createRelease(params);
62+
console.log('mongosh: created release:', data);
63+
return data;
5964
};
6065

6166
/**
6267
* Upload the asset to the release.
6368
*
64-
*
69+
* @param {string} artifact - The artifact.
70+
* @param {number} releaseId - The release id.
71+
* @param {Octokit} octokit - The octokit instance.
6572
*/
6673
const uploadAsset = (artifact: string, releaseId: number, octokit: Octokit): Promise<any> => {
6774
const params = {
6875
...REPO,
6976
release_id: releaseId,
70-
data: ''
77+
name: path.basename(artifact),
78+
data: artifact
7179
};
72-
return octokit.repos.uploadReleaseAsset(params);
80+
console.log('mongosh: uploading asset:', artifact);
81+
return octokit.repos.uploadReleaseAsset(params).catch((e) => {
82+
// If the asset already exists it will throw, but we just log
83+
// it since we don't want to overwrite assets.
84+
console.error(e);
85+
});
7386
};
7487

75-
export default createRelease;
88+
export default releaseToGithub;
7689
export {
77-
isReleasable,
90+
getLatestRelease,
7891
createRelease,
7992
uploadAsset
8093
};

packages/build/src/release.ts

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Octokit } from '@octokit/rest';
33
import Config from './config';
44
import compileExec from './compile-exec';
55
import uploadArtifactToEvergreen from './evergreen';
6-
import createRelease, { isReleasable, uploadAsset } from './github';
6+
import releaseToGithub from './github';
77
import uploadArtifactToDownloads from './upload-artifact';
88
import uploadDownloadCenterConfig from './download-center';
99
import Platform from './platform';
@@ -48,35 +48,33 @@ const release = async(config: Config) => {
4848
config.revision
4949
);
5050

51-
if (await isReleasable(config.version, octokit)) {
52-
const releaseId = await createRelease(config.version, octokit);
53-
await uploadAsset(artifact, releaseId, octokit);
51+
// - Create release and upload assets to Github.
52+
await releaseToGithub(config.version, artifact, octokit);
5453

55-
// - Publish the .deb (only on linux)
56-
// - Publish the .rpm (only on linux)
57-
// - Create PR for Homebrew (only on macos)
54+
// - Publish the .deb (only on linux)
55+
// - Publish the .rpm (only on linux)
56+
// - Create PR for Homebrew (only on macos)
5857

59-
// - Upload the artifact to downloads.10gen.com
60-
await uploadArtifactToDownloads(
61-
artifact,
58+
// - Upload the artifact to downloads.10gen.com
59+
await uploadArtifactToDownloads(
60+
artifact,
61+
config.downloadCenterAwsKey,
62+
config.downloadCenterAwsSecret,
63+
config.project,
64+
config.revision
65+
);
66+
67+
// - Create download center config and upload.
68+
// - Publish to NPM.
69+
//
70+
// These only need to happen once so we only run them on MacOS.
71+
if (platform === Platform.MacOs) {
72+
await uploadDownloadCenterConfig(
73+
config.version,
6274
config.downloadCenterAwsKey,
63-
config.downloadCenterAwsSecret,
64-
config.project,
65-
config.revision
75+
config.downloadCenterAwsSecret
6676
);
67-
68-
// - Create download center config and upload.
69-
// - Publish to NPM.
70-
//
71-
// These only need to happen once so we only run them on MacOS.
72-
if (platform === Platform.MacOs) {
73-
await uploadDownloadCenterConfig(
74-
config.version,
75-
config.downloadCenterAwsKey,
76-
config.downloadCenterAwsSecret
77-
);
78-
// npm publish.
79-
}
77+
// npm publish.
8078
}
8179
};
8280

0 commit comments

Comments
 (0)