Skip to content

Commit 8eb8d66

Browse files
pmrcunhapvdlg
authored andcommittedMay 9, 2019
feat: append assets before release
1 parent bab986f commit 8eb8d66

File tree

3 files changed

+90
-60
lines changed

3 files changed

+90
-60
lines changed
 

‎lib/publish.js

+65-50
Original file line numberDiff line numberDiff line change
@@ -21,61 +21,76 @@ module.exports = async (pluginConfig, context) => {
2121
const {githubToken, githubUrl, githubApiPathPrefix, proxy, assets} = resolveConfig(pluginConfig, context);
2222
const {name: repo, owner} = parseGithubUrl(repositoryUrl);
2323
const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});
24-
const release = {owner, repo, tag_name: gitTag, name, body: notes, prerelease: isPrerelease(branch)}; // eslint-disable-line camelcase
24+
const release = {owner, repo, tag_name: gitTag, name, body: notes, prerelease: isPrerelease(branch)};
2525

2626
debug('release object: %O', release);
2727

28-
const {
29-
data: {html_url: url, upload_url: uploadUrl},
30-
} = await github.repos.createRelease(release);
31-
logger.log('Published GitHub release: %s', url);
28+
// When there are no assets, we publish a release directly
29+
if (!assets || assets.length === 0) {
30+
const {
31+
data: {html_url: url},
32+
} = await github.repos.createRelease(release);
3233

33-
if (assets && assets.length > 0) {
34-
const globbedAssets = await globAssets(context, assets);
35-
debug('globed assets: %o', globbedAssets);
36-
37-
await Promise.all(
38-
globbedAssets.map(async asset => {
39-
const filePath = isPlainObject(asset) ? asset.path : asset;
40-
let file;
41-
42-
try {
43-
file = await stat(resolve(cwd, filePath));
44-
} catch (error) {
45-
logger.error('The asset %s cannot be read, and will be ignored.', filePath);
46-
return;
47-
}
48-
49-
if (!file || !file.isFile()) {
50-
logger.error('The asset %s is not a file, and will be ignored.', filePath);
51-
return;
52-
}
53-
54-
const fileName = asset.name || basename(filePath);
55-
const upload = {
56-
url: uploadUrl,
57-
file: await readFile(resolve(cwd, filePath)),
58-
name: fileName,
59-
headers: {
60-
'content-type': mime.getType(extname(fileName)) || 'text/plain',
61-
'content-length': file.size,
62-
},
63-
};
64-
65-
debug('file path: %o', filePath);
66-
debug('file name: %o', fileName);
67-
68-
if (isPlainObject(asset) && asset.label) {
69-
upload.label = asset.label;
70-
}
71-
72-
const {
73-
data: {browser_download_url: downloadUrl},
74-
} = await github.repos.uploadReleaseAsset(upload);
75-
logger.log('Published file %s', downloadUrl);
76-
})
77-
);
34+
logger.log('Published GitHub release: %s', url);
35+
return {url, name: RELEASE_NAME};
7836
}
7937

38+
// We'll create a draft release, append the assets to it, and then publish it.
39+
// This is so that the assets are available when we get a Github release event.
40+
const draftRelease = {...release, draft: true};
41+
42+
const {
43+
data: {html_url: url, upload_url: uploadUrl, id: releaseId},
44+
} = await github.repos.createRelease(draftRelease);
45+
46+
// Append assets to the release
47+
const globbedAssets = await globAssets(context, assets);
48+
debug('globed assets: %o', globbedAssets);
49+
50+
await Promise.all(
51+
globbedAssets.map(async asset => {
52+
const filePath = isPlainObject(asset) ? asset.path : asset;
53+
let file;
54+
55+
try {
56+
file = await stat(resolve(cwd, filePath));
57+
} catch (error) {
58+
logger.error('The asset %s cannot be read, and will be ignored.', filePath);
59+
return;
60+
}
61+
62+
if (!file || !file.isFile()) {
63+
logger.error('The asset %s is not a file, and will be ignored.', filePath);
64+
return;
65+
}
66+
67+
const fileName = asset.name || basename(filePath);
68+
const upload = {
69+
url: uploadUrl,
70+
file: await readFile(resolve(cwd, filePath)),
71+
name: fileName,
72+
headers: {
73+
'content-type': mime.getType(extname(fileName)) || 'text/plain',
74+
'content-length': file.size,
75+
},
76+
};
77+
78+
debug('file path: %o', filePath);
79+
debug('file name: %o', fileName);
80+
81+
if (isPlainObject(asset) && asset.label) {
82+
upload.label = asset.label;
83+
}
84+
85+
const {
86+
data: {browser_download_url: downloadUrl},
87+
} = await github.repos.uploadReleaseAsset(upload);
88+
logger.log('Published file %s', downloadUrl);
89+
})
90+
);
91+
92+
await github.repos.updateRelease({owner, repo, release_id: releaseId, draft: false});
93+
94+
logger.log('Published GitHub release: %s', url);
8095
return {url, name: RELEASE_NAME};
8196
};

‎test/integration.test.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,12 @@ test.serial('Publish a release with an array of assets', async t => {
151151
tag_name: nextRelease.gitTag,
152152
name: nextRelease.name,
153153
body: nextRelease.notes,
154+
draft: true,
154155
prerelease: false,
155156
})
156-
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});
157+
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl, id: releaseId})
158+
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {draft: false})
159+
.reply(200, {html_url: releaseUrl});
157160
const githubUpload1 = upload(env, {
158161
uploadUrl: 'https://github.com',
159162
contentLength: (await stat(path.resolve(cwd, 'upload.txt'))).size,
@@ -174,9 +177,9 @@ test.serial('Publish a release with an array of assets', async t => {
174177

175178
t.is(result.url, releaseUrl);
176179
t.deepEqual(t.context.log.args[0], ['Verify GitHub authentication']);
177-
t.deepEqual(t.context.log.args[1], ['Published GitHub release: %s', releaseUrl]);
178180
t.true(t.context.log.calledWith('Published file %s', otherAssetUrl));
179181
t.true(t.context.log.calledWith('Published file %s', assetUrl));
182+
t.true(t.context.log.calledWith('Published GitHub release: %s', releaseUrl));
180183
t.true(github.isDone());
181184
t.true(githubUpload1.isDone());
182185
t.true(githubUpload2.isDone());
@@ -319,9 +322,12 @@ test.serial('Verify, release and notify success', async t => {
319322
tag_name: nextRelease.gitTag,
320323
name: nextRelease.name,
321324
body: nextRelease.notes,
325+
draft: true,
322326
prerelease: false,
323327
})
324-
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl})
328+
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl, id: releaseId})
329+
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {draft: false})
330+
.reply(200, {html_url: releaseUrl})
325331
.get(`/repos/${owner}/${repo}`)
326332
.reply(200, {full_name: `${owner}/${repo}`})
327333
.get(
@@ -366,9 +372,9 @@ test.serial('Verify, release and notify success', async t => {
366372
);
367373

368374
t.deepEqual(t.context.log.args[0], ['Verify GitHub authentication']);
369-
t.deepEqual(t.context.log.args[1], ['Published GitHub release: %s', releaseUrl]);
370375
t.true(t.context.log.calledWith('Published file %s', otherAssetUrl));
371376
t.true(t.context.log.calledWith('Published file %s', assetUrl));
377+
t.true(t.context.log.calledWith('Published GitHub release: %s', releaseUrl));
372378
t.true(github.isDone());
373379
t.true(githubUpload1.isDone());
374380
t.true(githubUpload2.isDone());

‎test/publish.test.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ test.serial('Publish a lts release', async t => {
152152
body: nextRelease.notes,
153153
prerelease: false,
154154
})
155-
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});
155+
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl, id: releaseId});
156156

157157
const result = await publish(pluginConfig, {
158158
cwd,
@@ -195,7 +195,7 @@ test.serial('Publish a release, retrying 4 times', async t => {
195195
body: nextRelease.notes,
196196
prerelease: false,
197197
})
198-
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});
198+
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl, id: releaseId});
199199

200200
const result = await publish(pluginConfig, {
201201
cwd,
@@ -231,8 +231,11 @@ test.serial('Publish a release with one asset', async t => {
231231
tag_name: nextRelease.gitTag,
232232
name: nextRelease.name,
233233
body: nextRelease.notes,
234+
draft: true,
234235
prerelease: false,
235236
})
237+
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl, id: releaseId})
238+
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {draft: false})
236239
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});
237240

238241
const githubUpload = upload(env, {
@@ -252,7 +255,7 @@ test.serial('Publish a release with one asset', async t => {
252255
});
253256

254257
t.is(result.url, releaseUrl);
255-
t.deepEqual(t.context.log.args[0], ['Published GitHub release: %s', releaseUrl]);
258+
t.true(t.context.log.calledWith('Published GitHub release: %s', releaseUrl));
256259
t.true(t.context.log.calledWith('Published file %s', assetUrl));
257260
t.true(github.isDone());
258261
t.true(githubUpload.isDone());
@@ -278,8 +281,11 @@ test.serial('Publish a release with one asset and custom github url', async t =>
278281
tag_name: nextRelease.gitTag,
279282
name: nextRelease.name,
280283
body: nextRelease.notes,
284+
draft: true,
281285
prerelease: false,
282286
})
287+
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl, id: releaseId})
288+
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {draft: false})
283289
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});
284290

285291
const githubUpload = upload(env, {
@@ -299,7 +305,7 @@ test.serial('Publish a release with one asset and custom github url', async t =>
299305
});
300306

301307
t.is(result.url, releaseUrl);
302-
t.deepEqual(t.context.log.args[0], ['Published GitHub release: %s', releaseUrl]);
308+
t.true(t.context.log.calledWith('Published GitHub release: %s', releaseUrl));
303309
t.true(t.context.log.calledWith('Published file %s', assetUrl));
304310
t.true(github.isDone());
305311
t.true(githubUpload.isDone());
@@ -323,9 +329,12 @@ test.serial('Publish a release with an array of missing assets', async t => {
323329
tag_name: nextRelease.gitTag,
324330
name: nextRelease.name,
325331
body: nextRelease.notes,
332+
draft: true,
326333
prerelease: false,
327334
})
328-
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});
335+
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl, id: releaseId})
336+
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {draft: false})
337+
.reply(200, {html_url: releaseUrl});
329338

330339
const result = await publish(pluginConfig, {
331340
cwd,
@@ -337,7 +346,7 @@ test.serial('Publish a release with an array of missing assets', async t => {
337346
});
338347

339348
t.is(result.url, releaseUrl);
340-
t.deepEqual(t.context.log.args[0], ['Published GitHub release: %s', releaseUrl]);
349+
t.true(t.context.log.calledWith('Published GitHub release: %s', releaseUrl));
341350
t.true(t.context.error.calledWith('The asset %s cannot be read, and will be ignored.', 'missing.txt'));
342351
t.true(t.context.error.calledWith('The asset %s is not a file, and will be ignored.', emptyDirectory));
343352
t.true(github.isDone());

0 commit comments

Comments
 (0)
Please sign in to comment.