1
+ import fs from 'fs' ;
2
+ import path from 'path' ;
1
3
import { Octokit } from '@octokit/rest' ;
2
- import {
3
- GetResponseTypeFromEndpointMethod ,
4
- GetResponseDataTypeFromEndpointMethod ,
5
- } from "@octokit/types" ;
6
4
import semver from 'semver' ;
7
5
8
6
/**
@@ -14,16 +12,21 @@ const REPO = Object.freeze({
14
12
} ) ;
15
13
16
14
/**
17
- * Determine if this version is releasable .
15
+ * Release mongosh to Github releases .
18
16
*
19
17
* @param {string } version - The current version.
18
+ * @param {string } artifact - The artifact path.
20
19
* @param {Octokit } octokit - The octokit instance.
21
- *
22
- * @returns {Promise } The promise of the boolean.
23
20
*/
24
- const isReleasable = async ( version : string , octokit : Octokit ) : Promise < boolean > => {
21
+ const releaseToGithub = async ( version : string , artifact : string , octokit : Octokit ) : Promise < any > => {
25
22
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
+ }
27
30
} ;
28
31
29
32
/**
@@ -48,33 +51,43 @@ const getLatestRelease = async(octokit: Octokit): Promise<any> => {
48
51
* @param {string } version - The release version.
49
52
* @param {Octokit } octokit - The octokit instance.
50
53
*/
51
- const createRelease = ( version : string , octokit : Octokit ) : Promise < any > => {
54
+ const createRelease = async ( version : string , octokit : Octokit ) : Promise < any > => {
52
55
const params = {
53
56
...REPO ,
54
57
tag_name : `v${ version } ` ,
55
58
name : version ,
56
59
body : 'TODO: Generate Release Notes'
57
60
} ;
58
- return octokit . repos . createRelease ( params ) ;
61
+ const { data } = await octokit . repos . createRelease ( params ) ;
62
+ console . log ( 'mongosh: created release:' , data ) ;
63
+ return data ;
59
64
} ;
60
65
61
66
/**
62
67
* Upload the asset to the release.
63
68
*
64
- *
69
+ * @param {string } artifact - The artifact.
70
+ * @param {number } releaseId - The release id.
71
+ * @param {Octokit } octokit - The octokit instance.
65
72
*/
66
73
const uploadAsset = ( artifact : string , releaseId : number , octokit : Octokit ) : Promise < any > => {
67
74
const params = {
68
75
...REPO ,
69
76
release_id : releaseId ,
70
- data : ''
77
+ name : path . basename ( artifact ) ,
78
+ data : artifact
71
79
} ;
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
+ } ) ;
73
86
} ;
74
87
75
- export default createRelease ;
88
+ export default releaseToGithub ;
76
89
export {
77
- isReleasable ,
90
+ getLatestRelease ,
78
91
createRelease ,
79
92
uploadAsset
80
93
} ;
0 commit comments