-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgithub.ts
446 lines (406 loc) Β· 12.5 KB
/
github.ts
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import { Octokit, RestEndpointMethodTypes } from '@octokit/rest';
import { createReadStream, promises, statSync } from 'fs';
import { basename } from 'path';
import { getConfiguration } from '../config';
import {
ChangelogPolicy,
GitHubGlobalConfig,
TargetConfig,
} from '../schemas/project_config';
import {
Changeset,
DEFAULT_CHANGELOG_PATH,
findChangeset,
} from '../utils/changelog';
import { getGitHubClient } from '../utils/githubApi';
import { isDryRun } from '../utils/helpers';
import {
isPreviewRelease,
parseVersion,
versionGreaterOrEqualThan,
versionToTag,
} from '../utils/version';
import { BaseTarget } from './base';
import { BaseArtifactProvider } from '../artifact_providers/base';
import { logger } from '../logger';
/**
* Default content type for GitHub release assets.
*/
export const DEFAULT_CONTENT_TYPE = 'application/octet-stream';
/**
* Configuration options for the GitHub target.
*/
export interface GitHubTargetConfig extends GitHubGlobalConfig {
/** Path to changelog inside the repository */
changelog: string;
/** Prefix that will be used to generate tag name */
tagPrefix: string;
/** Mark release as pre-release, if the version looks like a non-public release */
previewReleases: boolean;
/** Do not create a full GitHub release, only push a git tag */
tagOnly: boolean;
}
/**
* An interface that represents a minimal GitHub release as returned by the
* GitHub API.
*/
interface GitHubRelease {
/** Release id */
id: number;
/** Tag name */
tag_name: string;
/** Upload URL */
upload_url: string;
}
type ReposListAssetsForReleaseResponseItem = RestEndpointMethodTypes['repos']['listReleaseAssets']['response']['data'][0];
/**
* Target responsible for publishing releases on GitHub.
*/
export class GitHubTarget extends BaseTarget {
/** Target name */
public readonly name = 'github';
/** Target options */
public readonly githubConfig: GitHubTargetConfig;
/** GitHub client */
public readonly github: Octokit;
/** GitHub repo configuration */
public readonly githubRepo: GitHubGlobalConfig;
public constructor(
config: TargetConfig,
artifactProvider: BaseArtifactProvider,
githubRepo: GitHubGlobalConfig
) {
super(config, artifactProvider, githubRepo);
this.githubRepo = githubRepo;
const owner = config.owner || githubRepo.owner;
const repo = config.repo || githubRepo.repo;
const changelog = getConfiguration().changelog || DEFAULT_CHANGELOG_PATH;
this.githubConfig = {
owner,
repo,
changelog,
previewReleases:
this.config.previewReleases === undefined ||
!!this.config.previewReleases,
tagPrefix: this.config.tagPrefix || '',
tagOnly: !!this.config.tagOnly,
};
this.github = getGitHubClient();
}
/**
* Create a draft release for the given version.
*
* The release name and description body is brought in from `changes`
* respective tag, if present. Otherwise, the release name defaults to the
* tag and the body to the commit it points to.
*
* @param version The version to release
* @param revision Git commit SHA to be published
* @param changes The changeset information for this release
* @returns The newly created release
*/
public async createDraftRelease(
version: string,
revision: string,
changes?: Changeset
): Promise<GitHubRelease> {
const tag = versionToTag(version, this.githubConfig.tagPrefix);
this.logger.info(`Git tag: "${tag}"`);
const isPreview =
this.githubConfig.previewReleases && isPreviewRelease(version);
if (isDryRun()) {
this.logger.info(`[dry-run] Not creating the draft release`);
return {
id: 0,
tag_name: tag,
upload_url: '',
};
}
let latestRelease: { tag_name: string } | undefined = undefined;
try {
latestRelease = (
await this.github.repos.getLatestRelease({
owner: this.githubConfig.owner,
repo: this.githubConfig.repo,
})
).data;
} catch (error) {
// if the error is a 404 error, it means that no release exists yet
// all other errors should be rethrown
if (error.status !== 404) {
throw error;
}
}
const isLatest = isPreview
? false
: isLatestRelease(latestRelease, version);
const { data } = await this.github.repos.createRelease({
draft: true,
name: tag,
owner: this.githubConfig.owner,
prerelease: isPreview,
repo: this.githubConfig.repo,
tag_name: tag,
make_latest: isLatest ? 'true' : 'false',
target_commitish: revision,
...changes,
});
return data;
}
public async getChangelog(version: string): Promise<Changeset> {
let changelog;
try {
changelog = (
await promises.readFile(this.githubConfig.changelog)
).toString();
} catch (err) {
logger.error('Cannot read changelog, moving on without one', err);
}
const changes = (changelog && findChangeset(changelog, version)) || {
name: version,
body: '',
};
this.logger.debug('Changes extracted from changelog.');
this.logger.trace(changes);
return changes;
}
/**
* Deletes the provided asset from its respective release
*
* Can also be used to delete orphaned (unfinished) releases
*
* @param asset Asset to delete
*/
public async deleteAsset(
asset: ReposListAssetsForReleaseResponseItem
): Promise<boolean> {
this.logger.debug(`Deleting asset: "${asset.name}"...`);
if (isDryRun()) {
this.logger.info(`[dry-run] Not deleting "${asset.name}"`);
return false;
}
return (
(
await this.github.repos.deleteReleaseAsset({
asset_id: asset.id,
...this.githubConfig,
})
).status === 204
);
}
/**
* Fetches a list of all assets for the given release
*
* The result includes unfinished asset uploads.
*
* @param release Release to fetch assets from
*/
public async getAssetsForRelease(
release_id: number
): Promise<ReposListAssetsForReleaseResponseItem[]> {
const assetsResponse = await this.github.repos.listReleaseAssets({
owner: this.githubConfig.owner,
per_page: 50,
release_id,
repo: this.githubConfig.repo,
});
return assetsResponse.data;
}
/**
* Deletes the asset with the given name from the specific release
*
* @param release Release object ID
* @param assetName Asset name to be deleted
*/
public async deleteAssetByName(
release_id: number,
assetName: string
): Promise<boolean> {
const assets = await this.getAssetsForRelease(release_id);
const assetToDelete = assets.find(({ name }) => name === assetName);
if (!assetToDelete) {
logger.warn(
`No such asset with the name "${assetToDelete}", moving on. We have these instead: ${assets.map(
({ name }) => name
)}`
);
return false;
}
return this.deleteAsset(assetToDelete);
}
/**
* Uploads the file from the provided path to the specific release
*
* @param release Release object
* @param path Filesystem (local) path of the file to upload
* @param contentType Optional content-type for uploading
*/
public async uploadAsset(
release: GitHubRelease,
path: string,
contentType?: string
): Promise<string | undefined> {
const name = basename(path);
if (isDryRun()) {
this.logger.info(`[dry-run] Not uploading asset "${name}"`);
return;
}
process.stderr.write(
`Uploading asset "${name}" to ${this.githubConfig.owner}/${this.githubConfig.repo}:${release.tag_name}\n`
);
try {
const { url } = await this.handleGitHubUpload(release, path, contentType);
process.stderr.write(`β Uploaded asset "${name}".\n`);
return url;
} catch (e) {
process.stderr.write(`β Cannot upload asset "${name}".\n`);
throw e;
}
}
private async handleGitHubUpload(
release: GitHubRelease,
path: string,
contentType?: string,
retries = 3
): Promise<{ url: string; size: number }> {
const contentTypeProcessed = contentType || DEFAULT_CONTENT_TYPE;
const stats = statSync(path);
const name = basename(path);
// this must be recreated each attempt to prevent fd reuse
const file = createReadStream(path);
const params = {
...this.githubConfig,
headers: {
'Content-Length': stats.size,
'Content-Type': contentTypeProcessed,
},
release_id: release.id,
name,
// XXX: Octokit types this out as string, but in fact it also
// accepts a `Buffer` here. In fact passing a string is not what we
// want as we upload binary data.
data: file as any,
request: {
// we are handling retries -- octokit-retries will resuse our fd and
// hang forever
retries: 0,
timeout: 10 * 1000,
},
};
this.logger.trace('Upload parameters:', params);
try {
const ret = (await this.github.repos.uploadReleaseAsset(params)).data;
if (ret.size != stats.size) {
throw new Error(
`Uploaded asset size (${ret.size} bytes) does not match local asset size (${stats.size} bytes) for "${name}".`
);
}
return ret;
} catch (err) {
if (retries <= 0) {
throw new Error(
`Reached maximum retries for trying to upload asset "${params.name}.`
);
}
logger.info(
'Got an error when trying to upload an asset, deleting and retrying...'
);
await this.deleteAssetByName(params.release_id, params.name);
return this.handleGitHubUpload(release, path, contentType, --retries);
} finally {
file.destroy();
}
}
/**
* Publishes the draft release.
*
* @param release Release object
*/
public async publishRelease(release: GitHubRelease) {
if (isDryRun()) {
this.logger.info(`[dry-run] Not publishing the draft release`);
return;
}
await this.github.repos.updateRelease({
...this.githubConfig,
release_id: release.id,
draft: false,
});
}
/**
* Creates a git tag in the remote repository for the version.
*
* The function currently creates a lightweight (not annotated) tag.
* "tagPrefix" is respected when creating a tag name.
*
* @param version New version to be released
* @param revision Git commit SHA to be published
*/
protected async createGitTag(
version: string,
revision: string
): Promise<any> {
const tag = versionToTag(version, this.githubConfig.tagPrefix);
const tagRef = `refs/tags/${tag}`;
if (isDryRun()) {
this.logger.info(`[dry-run] Not pushing the tag reference: "${tagRef}"`);
} else {
this.logger.info(`Pushing the tag reference: "${tagRef}"...`);
await this.github.rest.git.createRef({
owner: this.githubConfig.owner,
repo: this.githubConfig.repo,
ref: tagRef,
sha: revision,
});
}
}
/**
* Creates a new GitHub release and publish all available artifacts.
*
* It also creates a tag if it doesn't exist
*
* @param version New version to be released
* @param revision Git commit SHA to be published
*/
public async publish(version: string, revision: string): Promise<any> {
if (this.githubConfig.tagOnly) {
this.logger.info(
`Not creating a GitHub release because "tagOnly" flag was set.`
);
return this.createGitTag(version, revision);
}
const config = getConfiguration();
let changelog;
if (config.changelogPolicy !== ChangelogPolicy.None) {
changelog = await this.getChangelog(version);
}
const artifacts = await this.getArtifactsForRevision(revision);
const localArtifacts = await Promise.all(
artifacts.map(async artifact => ({
mimeType: artifact.mimeType,
path: await this.artifactProvider.downloadArtifact(artifact),
}))
);
const draftRelease = await this.createDraftRelease(
version,
revision,
changelog
);
await Promise.all(
localArtifacts.map(({ path, mimeType }) =>
this.uploadAsset(draftRelease, path, mimeType)
)
);
await this.publishRelease(draftRelease);
}
}
export function isLatestRelease(
githubRelease: { tag_name: string } | undefined,
version: string
) {
const latestVersion = githubRelease && parseVersion(githubRelease.tag_name);
const versionToPublish = parseVersion(version);
return latestVersion && versionToPublish
? versionGreaterOrEqualThan(versionToPublish, latestVersion)
: true; // By default, we tag as latest
}