11import fs from 'fs' ;
2+ import { StringDecoder } from 'string_decoder' ;
23import path from 'path' ;
34import { Octokit } from '@octokit/rest' ;
45import semver from 'semver' ;
6+ import Platform from './platform' ;
57
68/**
79 * The repo we are working on.
@@ -16,16 +18,17 @@ const REPO = Object.freeze({
1618 *
1719 * @param {string } version - The current version.
1820 * @param {string } artifact - The artifact path.
21+ * @param {string } platform - The platform.
1922 * @param {Octokit } octokit - The octokit instance.
2023 */
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 > => {
2225 const latestRelease = await getLatestRelease ( octokit ) ;
2326 if ( semver . gt ( version , latestRelease . tag_name . replace ( 'v' , '' ) ) ) {
2427 // Create a new release if our version is higher than latest.
2528 const newRelease = await createRelease ( version , octokit ) ;
26- await uploadAsset ( artifact , newRelease . id , octokit ) ;
29+ await uploadAsset ( artifact , platform , newRelease . upload_url , octokit ) ;
2730 } else {
28- await uploadAsset ( artifact , latestRelease . id , octokit ) ;
31+ await uploadAsset ( artifact , platform , latestRelease . upload_url , octokit ) ;
2932 }
3033} ;
3134
@@ -67,18 +70,22 @@ const createRelease = async(version: string, octokit: Octokit): Promise<any> =>
6770 * Upload the asset to the release.
6871 *
6972 * @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.
7175 * @param {Octokit } octokit - The octokit instance.
7276 */
73- const uploadAsset = ( artifact : string , releaseId : number , octokit : Octokit ) : Promise < any > => {
77+ const uploadAsset = ( artifact : string , platform : string , uploadUrl : string , octokit : Octokit ) : Promise < any > => {
7478 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+ } ,
7784 name : path . basename ( artifact ) ,
78- data : artifact
85+ data : fs . readFileSync ( artifact )
7986 } ;
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 ) => {
8289 // If the asset already exists it will throw, but we just log
8390 // it since we don't want to overwrite assets.
8491 console . error ( e ) ;
0 commit comments