Skip to content

Commit 8503939

Browse files
coderbyheartpvdlg
authored andcommitted
feat: allow to use context variables to generate assets label and name
1 parent 9d7e7a3 commit 8503939

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
| `verifyConditions` | Verify the presence and the validity of the authentication (set via [environment variables](#environment-variables)) and the [assets](#assets) option configuration. |
1616
| `publish` | Publish a [GitHub release](https://help.github.com/articles/about-releases), optionally uploading file assets. |
1717
| `success` | Add a comment to each [GitHub Issue](https://help.github.com/articles/about-issues) or [Pull Request](https://help.github.com/articles/about-pull-requests) resolved by the release and close issues previously open by the `fail` step. |
18-
| `fail` | Open or update a [GitHub Issue](https://help.github.com/articles/about-issues) with information about the errors that caused the release to fail. |
18+
| `fail` | Open or update a [GitHub Issue](https://help.github.com/articles/about-issues) with information about the errors that caused the release to fail. |
1919

2020
## Install
2121

@@ -115,6 +115,15 @@ can be a `String` (`"dist/**/*.js"` or `"dist/mylib.js"`) or an `Array` of `Stri
115115

116116
If a directory is configured, all the files under this directory and its children will be included.
117117

118+
The `name` and `label` for each assets are generated with [Lodash template](https://lodash.com/docs#template). The following variables are available:
119+
120+
| Parameter | Description |
121+
|---------------|-------------------------------------------------------------------------------------|
122+
| `branch` | The branch from which the release is done. |
123+
| `lastRelease` | `Object` with `version`, `gitTag` and `gitHead` of the last release. |
124+
| `nextRelease` | `Object` with `version`, `gitTag`, `gitHead` and `notes` of the release being done. |
125+
| `commits` | `Array` of commit `Object`s with `hash`, `subject`, `body` `message` and `author`. |
126+
118127
**Note**: If a file has a match in `assets` it will be included even if it also has a match in `.gitignore`.
119128

120129
##### assets examples
@@ -132,6 +141,10 @@ distribution` and `MyLibrary CSS distribution` in the GitHub release.
132141
`css` files in the `dist` directory and its sub-directories excluding the minified version, plus the
133142
`build/MyLibrary.zip` file and label it `MyLibrary` in the GitHub release.
134143

144+
`[{path: 'dist/MyLibrary.js', name: 'MyLibrary-${nextRelease.gitTag}.js', label: 'MyLibrary JS (${nextRelease.gitTag}) distribution'}]`: include the file `dist/MyLibrary.js` and upload it to the GitHub release with name `MyLibrary-v1.0.0.js` and label `MyLibrary JS (v1.0.0) distribution` which will generate the link:
145+
146+
> `[MyLibrary JS (v1.0.0) distribution](MyLibrary-v1.0.0.js)`
147+
135148
#### successComment
136149

137150
The message for the issue comments is generated with [Lodash template](https://lodash.com/docs#template). The following variables are available:
@@ -155,9 +168,9 @@ The `successComment` `This ${issue.pull_request ? 'pull request' : 'issue'} is i
155168

156169
The message for the issue content is generated with [Lodash template](https://lodash.com/docs#template). The following variables are available:
157170

158-
| Parameter | Description |
159-
|-----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
160-
| `branch` | The branch from which the release had failed. |
171+
| Parameter | Description |
172+
|-----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
173+
| `branch` | The branch from which the release had failed. |
161174
| `errors` | An `Array` of [SemanticReleaseError](https://github.com/semantic-release/error). Each error has the `message`, `code`, `pluginName` and `details` properties.<br>`pluginName` contains the package name of the plugin that threw the error.<br>`details` contains a information about the error formatted in markdown. |
162175

163176
##### failComment examples

lib/publish.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const {basename, extname, resolve} = require('path');
22
const {stat, readFile} = require('fs-extra');
3-
const {isPlainObject} = require('lodash');
3+
const {isPlainObject, template} = require('lodash');
44
const parseGithubUrl = require('parse-github-url');
55
const mime = require('mime');
66
const debug = require('debug')('semantic-release:github');
@@ -72,7 +72,7 @@ module.exports = async (pluginConfig, context) => {
7272
return;
7373
}
7474

75-
const fileName = asset.name || basename(filePath);
75+
const fileName = template(asset.name || basename(filePath))(context);
7676
const upload = {
7777
url: uploadUrl,
7878
file: await readFile(resolve(cwd, filePath)),
@@ -87,7 +87,7 @@ module.exports = async (pluginConfig, context) => {
8787
debug('file name: %o', fileName);
8888

8989
if (isPlainObject(asset) && asset.label) {
90-
upload.label = asset.label;
90+
upload.label = template(asset.label)(context);
9191
}
9292

9393
const {

test/integration.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,60 @@ test.serial('Publish a release with an array of assets', async t => {
182182
t.true(githubUpload2.isDone());
183183
});
184184

185+
test.serial('Publish a release with release information in assets', async t => {
186+
const owner = 'test_user';
187+
const repo = 'test_repo';
188+
const env = {GITHUB_TOKEN: 'github_token'};
189+
const assets = [
190+
{
191+
path: ['upload.txt'],
192+
name: `file_with_release_\${nextRelease.gitTag}_in_filename.txt`,
193+
label: `File with release \${nextRelease.gitTag} in label`,
194+
},
195+
];
196+
const nextRelease = {version: '1.0.0', gitHead: '123', gitTag: 'v1.0.0', notes: 'Test release note body'};
197+
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
198+
const releaseUrl = `https://github.com/${owner}/${repo}/releases/${nextRelease.version}`;
199+
const assetUrl = `https://github.com/${owner}/${repo}/releases/download/${nextRelease.version}/file_with_release_v1.0.0_in_filename.txt`;
200+
const releaseId = 1;
201+
const uploadUri = `/api/uploads/repos/${owner}/${repo}/releases/${releaseId}/assets`;
202+
const uploadUrl = `https://github.com${uploadUri}{?name,label}`;
203+
const github = authenticate(env)
204+
.get(`/repos/${owner}/${repo}`)
205+
.reply(200, {permissions: {push: true}})
206+
.post(`/repos/${owner}/${repo}/releases`, {
207+
tag_name: nextRelease.gitTag,
208+
target_commitish: options.branch,
209+
name: nextRelease.gitTag,
210+
body: nextRelease.notes,
211+
draft: true,
212+
})
213+
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl, id: releaseId})
214+
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {
215+
draft: false,
216+
})
217+
.reply(200, {html_url: releaseUrl});
218+
const githubUpload = upload(env, {
219+
uploadUrl: 'https://github.com',
220+
contentLength: (await stat(path.resolve(cwd, 'upload.txt'))).size,
221+
})
222+
.post(
223+
`${uploadUri}?name=${escape('file_with_release_v1.0.0_in_filename.txt')}&label=${escape(
224+
'File with release v1.0.0 in label'
225+
)}`
226+
)
227+
.reply(200, {browser_download_url: assetUrl});
228+
229+
const result = await t.context.m.publish({assets}, {cwd, env, options, nextRelease, logger: t.context.logger});
230+
231+
t.is(result.url, releaseUrl);
232+
t.deepEqual(t.context.log.args[0], ['Verify GitHub authentication']);
233+
t.true(t.context.log.calledWith('Published file %s', assetUrl));
234+
t.true(t.context.log.calledWith('Published GitHub release: %s', releaseUrl));
235+
t.true(github.isDone());
236+
t.true(githubUpload.isDone());
237+
});
238+
185239
test.serial('Comment and add labels on PR included in the releases', async t => {
186240
const owner = 'test_user';
187241
const repo = 'test_repo';

0 commit comments

Comments
 (0)