1
1
import fs from 'fs' ;
2
+ import { StringDecoder } from 'string_decoder' ;
2
3
import path from 'path' ;
3
4
import { Octokit } from '@octokit/rest' ;
4
5
import semver from 'semver' ;
6
+ import Platform from './platform' ;
5
7
6
8
/**
7
9
* The repo we are working on.
@@ -16,16 +18,17 @@ const REPO = Object.freeze({
16
18
*
17
19
* @param {string } version - The current version.
18
20
* @param {string } artifact - The artifact path.
21
+ * @param {string } platform - The platform.
19
22
* @param {Octokit } octokit - The octokit instance.
20
23
*/
21
- const releaseToGithub = async ( version : string , artifact : string , octokit : Octokit ) : Promise < any > => {
24
+ const releaseToGithub = async ( version : string , artifact : string , platform : string , octokit : Octokit ) : Promise < any > => {
22
25
const latestRelease = await getLatestRelease ( octokit ) ;
23
26
if ( semver . gt ( version , latestRelease . tag_name . replace ( 'v' , '' ) ) ) {
24
27
// Create a new release if our version is higher than latest.
25
28
const newRelease = await createRelease ( version , octokit ) ;
26
- await uploadAsset ( artifact , newRelease . id , octokit ) ;
29
+ await uploadAsset ( artifact , platform , newRelease . upload_url , octokit ) ;
27
30
} else {
28
- await uploadAsset ( artifact , latestRelease . id , octokit ) ;
31
+ await uploadAsset ( artifact , platform , latestRelease . upload_url , octokit ) ;
29
32
}
30
33
} ;
31
34
@@ -67,18 +70,22 @@ const createRelease = async(version: string, octokit: Octokit): Promise<any> =>
67
70
* Upload the asset to the release.
68
71
*
69
72
* @param {string } artifact - The artifact.
70
- * @param {number } releaseId - The release id.
73
+ * @param {string } platform - The platform.
74
+ * @param {string } uploadUrl - The release endpoint.
71
75
* @param {Octokit } octokit - The octokit instance.
72
76
*/
73
- const uploadAsset = ( artifact : string , releaseId : number , octokit : Octokit ) : Promise < any > => {
77
+ const uploadAsset = ( artifact : string , platform : string , uploadUrl : string , octokit : Octokit ) : Promise < any > => {
74
78
const params = {
75
- ...REPO ,
76
- release_id : releaseId ,
79
+ method : 'POST' ,
80
+ url : uploadUrl ,
81
+ headers : {
82
+ 'content-type' : platform === Platform . Linux ? 'application/gzip' : 'application/zip'
83
+ } ,
77
84
name : path . basename ( artifact ) ,
78
- data : artifact
85
+ data : fs . readFileSync ( artifact )
79
86
} ;
80
- console . log ( 'mongosh: uploading asset:' , artifact ) ;
81
- return octokit . repos . uploadReleaseAsset ( params ) . catch ( ( e ) => {
87
+ console . log ( 'mongosh: uploading asset to github :' , artifact ) ;
88
+ return octokit . request ( params ) . catch ( ( e ) => {
82
89
// If the asset already exists it will throw, but we just log
83
90
// it since we don't want to overwrite assets.
84
91
console . error ( e ) ;
0 commit comments