forked from cncjs/cncjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-release.js
executable file
·199 lines (177 loc) · 5.94 KB
/
github-release.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
#!/usr/bin/env node
/* eslint no-console: 0 */
/* eslint max-len: 0 */
import path from 'path';
import _ from 'lodash';
import GitHub from 'github';
import program from 'commander';
import pkg from './package.json';
program
.version(pkg.version)
.option('-T, --token <token>', 'OAuth2 token')
.option('-o, --owner <owner>', 'owner')
.option('-r, --repo <repo>', 'repo')
.option('-t, --tag <tag>', 'tag')
.option('-n, --name <name>', 'name')
.option('-b, --body <body>', 'body', false)
.parse(process.argv);
const github = new GitHub({
version: '3.0.0',
timeout: 5000,
headers: {
'user-agent': 'GitHub-Release-App'
}
});
const files = program.args;
github.authenticate({
type: 'oauth',
token: program.token || process.env.GITHUB_TOKEN
});
const listReleases = (options) => {
return new Promise((resolve, reject) => {
github.releases.listReleases(options, (err, res) => {
err ? reject(err) : resolve(res);
});
});
};
const createRelease = (options) => {
return new Promise((resolve, reject) => {
github.releases.createRelease(options, (err, res) => {
err ? reject(err) : resolve(res);
});
});
};
const editRelease = (options) => {
return new Promise((resolve, reject) => {
github.releases.editRelease(options, (err, res) => {
err ? reject(err) : resolve(res);
});
});
};
const listAssets = (options) => {
return new Promise((resolve, reject) => {
github.releases.listAssets(options, (err, res) => {
err ? reject(err) : resolve(res);
});
});
};
const deleteAsset = (options) => {
return new Promise((resolve, reject) => {
github.releases.deleteAsset(options, (err, res) => {
err ? reject(err) : resolve(res);
});
});
};
const uploadAsset = (options) => {
return new Promise((resolve, reject) => {
github.releases.uploadAsset(options, (err, res) => {
err ? reject(err) : resolve(res);
});
});
};
const main = async () => {
const { owner, repo, tag, name, body } = program;
try {
console.log('> releases#listReleases');
let releases = await listReleases({
owner: owner,
repo: repo
});
console.log('releases=%d', releases.length);
let release = _.find(releases, { tag_name: tag });
if (!release) {
console.log('> releases#createRelease');
release = await createRelease({
owner: owner,
repo: repo,
tag_name: tag,
name: name || tag,
body: body || ''
});
console.log('ok', release);
} else if (release.body !== body) {
console.log('> releases#editRelease');
let releaseOptions = {
owner: owner,
repo: repo,
id: release.id,
tag_name: tag,
name: name || tag
};
if (body) {
releaseOptions.body = body || '';
}
release = await editRelease(releaseOptions);
console.log('ok', release);
}
console.log('> releases#listAssets');
let assets = await listAssets({
owner: owner,
repo: repo,
id: release.id
});
console.log('assets=%d', assets.length);
assets = _.filter(assets, (asset) => {
// Example:
// 'cnc-1.1.0-latest-08c256a-linux-x64.tar.gz'
// ["cnc-1.1.0-latest-08c256a-linux-x64.tar.gz", "cnc", "1.1.0-latest-08c256a", "linux", "x64", "tar.gz"]
const pattern = new RegExp(/([a-zA-Z0-9][a-zA-Z0-9\-]*)\-(\d+\.\d+\.\d+(?:\-[a-zA-Z0-9][a-zA-Z0-9\-]*)?)(?:\-(linux|osx|win))(?:\-([a-zA-Z0-9_\-]+)\.(.*))/);
return _.some(files, (file) => {
const r1 = asset.name.match(pattern);
const r2 = path.basename(file).match(pattern);
if ((r1 === null) || (r2 === null)) {
console.error('Unable to match file: asset="%s", file="%s"', asset.name, path.basename(file));
return false;
}
// 0: full
// 1: name
// 2: version
// 3: platform
// 4: arch
// 5: extname
// Skip checking for #0 (full) and #2 (version)
r1[0] = r1[2] = undefined;
r2[0] = r2[2] = undefined;
return _.isEqual(_.compact(r1), _.compact(r2));
});
});
if (assets.length > 0) {
console.log('> releases#deleteAsset');
for (let i = 0; i < assets.length; ++i) {
const asset = assets[i];
console.log('#%d', i + 1, {
id: asset.id,
name: asset.name,
label: asset.label,
state: asset.state,
size: asset.size,
download_count: asset.download_count,
created_at: asset.created_at,
updated_at: asset.updated_at
});
await deleteAsset({
owner: owner,
repo: repo,
id: asset.id
});
}
}
if (files.length > 0) {
console.log('> releases#uploadAsset');
for (let i = 0; i < files.length; ++i) {
const file = files[i];
console.log('#%d name="%s" filePath="%s"', i + 1, path.basename(file), file);
await uploadAsset({
owner: owner,
repo: repo,
id: release.id,
name: path.basename(file),
filePath: file
});
}
}
} catch (err) {
console.error(err);
}
};
main();