-
Notifications
You must be signed in to change notification settings - Fork 56
/
travis_to_github_upload.js
216 lines (185 loc) · 6.05 KB
/
travis_to_github_upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
require("dotenv").config();
// TODO: electron builder might already contain this
// https://www.electron.build/configuration/publish
// takes file to upload as script param
// knows release name & tag name from ENV var
const packageJson = require("./package.json");
const fetch = require("node-fetch");
var UPLOADTOOL_SUFFIX = packageJson.name;
var TRAVIS_TAG = packageJson.version;
console.log("UPLOADTOOL_SUFFIX ", UPLOADTOOL_SUFFIX);
// TEMP Patch for dev, delete following line when done
process.env.TRAVIS_OS_NAME = "osx";
console.log("process.env.TRAVIS_OS_NAME", process.env.TRAVIS_OS_NAME);
const RELEASE_NAME = `${UPLOADTOOL_SUFFIX}-${TRAVIS_TAG}-${process.env.TRAVIS_OS_NAME}`;
const RELEASE_TITLE = `${UPLOADTOOL_SUFFIX} ${TRAVIS_TAG} ${process.env.TRAVIS_OS_NAME}`;
const IS_PRERELEASE = process.env.IS_PRERELEASE;
// :owner/:repo
// const TRAVIS_REPO_SLUG = process.env.TRAVIS_REPO_SLUG;
// TEMP place holder, for development, delete subsequent line for production
const TRAVIS_REPO_SLUG =
process.env.TRAVIS_REPO_SLUG || "OpenNewsLabs/autoEdit_2";
console.log("RELEASE_NAME ", RELEASE_NAME);
console.log("RELEASE_TITLE ", RELEASE_TITLE);
// console.log('IS_PRERELEASE ',IS_PRERELEASE);
console.log("------");
// GITHUB: get list of release from githuhb
// if the tag name is in the list of releases
// GITHUB: delete release
// tag name is not in list of releases
// GITHUB: create new release
// in both cases - add asset to this release
// mac
// ./dist/autoEdit2-1.0.13-mac.zip
// linux
// ./autoEdit2-1.0.13-x86_64.AppImage
// windows
// autoEdit2 1.0.13.exe
// adobe CEP
//npm run adobe-panel-package-sign-build
// ./dist/com.autoedit2.it.zxp
/**
* Helper function - get list of release from githuhb
* https://developer.github.com/v3/repos/releases/#list-releases-for-a-repository
* GET /repos/:owner/:repo/releases
* https://api.github.com/repos/OpenNewsLabs/autoEdit_2/releases
* .tag_name
*/
function getListOfReleases(TRAVIS_REPO_SLUG) {
return new Promise(function(resolve, reject) {
fetch(`https://api.github.com/repos/${TRAVIS_REPO_SLUG}/releases`)
.then(res => {
return res.text();
})
.then(body => {
return JSON.parse(body);
})
.then(releases => {
resolve(releases);
// returns array of releases
// do something with releases
releases.forEach(individualRelease => {
if (individualRelease.tag_name === RELEASE_NAME) {
// console.log('true');
return true;
} else {
// console.log('false');
return false;
}
});
})
.catch(err => {
reject(err);
});
});
}
/**
* Helper function - create new release
* https://developer.github.com/v3/repos/releases/#create-a-release
* POST /repos/:owner/:repo/releases
*/
function createNewRelease(RELEASE_NAME, TRAVIS_REPO_SLUG) {
return new Promise(function(resolve, reject) {
var body = {
tag_name: RELEASE_NAME,
target_commitish: "master",
name: "test prerelease draft",
body: "some text for body",
draft: false,
prerelease: true
};
fetch(`https://api.github.com/repos/${TRAVIS_REPO_SLUG}/releases`, {
method: "POST",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
// https://help.github.com/articles/authorizing-a-personal-access-token-for-use-with-a-saml-single-sign-on-organization/
Authorization: `token ${process.env.GITHUB_TOKEN}`
}
})
.then(res => res.json())
.then(release => {
resolve(release);
// return upload url for uploading binary to release
// as well as release id
console.log("test", release.upload_url, release.id);
})
.catch(err => {
reject(err);
});
});
}
/**
* Helper funciton - add assets to a release
* https://developer.github.com/v3/repos/releases/#upload-a-release-asset
*/
function adAssetsToRelease(
upload_url,
TRAVIS_REPO_SLUG,
RELEASE_ID,
assetName
) {
return new Promise(function(resolve, reject) {
var body = {
tag_name: RELEASE_NAME,
target_commitish: "master",
name: "test prerelease draft",
body: "some text for body",
draft: false,
prerelease: true
};
fetch(
`https://${upload_url}/repos/${TRAVIS_REPO_SLUG}/releases/${RELEASE_ID}/assets?name=${assetName}`,
{
method: "POST",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
// https://help.github.com/articles/authorizing-a-personal-access-token-for-use-with-a-saml-single-sign-on-organization/
Authorization: `token ${process.env.GITHUB_TOKEN}`
}
}
)
.then(res => res.json())
.then(json => {
resolve(json);
// get upload url for uploading binary to release
console.log("test", json.upload_url);
})
.catch(err => {
reject(err);
});
});
}
/**
* Helper funciton -delete release
* https://developer.github.com/v3/repos/releases/#delete-a-release
*/
function deleteRelease() {
// NA
}
const ghReleaseAssets = require("gh-release-assets");
async function main() {
const releases = await getListOfReleases(TRAVIS_REPO_SLUG);
const lastRelease = releases[0];
console.log('lastRelease', JSON.stringify(lastRelease,null,2))
const uploadUrl = lastRelease.upload_url;
console.log('uploadUrl', uploadUrl)
ghReleaseAssets(
{
url: uploadUrl, //'https://uploads.github.com/repos/octocat/Hello-World/releases/1197692/assets{?name}',
token: [process.env.GITHUB_TOKEN],
assets: ["./dist/com.autoedit2.it.zxp"]
},
function(err, assets) {
console.error("err::", err);
console.log('assets::',assets);
}
);
// const newReleaseResp = await createNewRelease(RELEASE_NAME,TRAVIS_REPO_SLUG)
// console.log('newReleaseResp', newReleaseResp);
// const uploadUrl = newReleaseResp.upload_url;
// const releaseId = newReleaseResp.id;
// await adAssetsToRelease(uploadUrl,TRAVIS_REPO_SLUG,releaseId, assetName)
}
main();