Skip to content

Commit

Permalink
feat: allow to use context variables to generate assets label and name
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart authored and pvdlg committed Oct 2, 2019
1 parent 9d7e7a3 commit 8503939
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
| `verifyConditions` | Verify the presence and the validity of the authentication (set via [environment variables](#environment-variables)) and the [assets](#assets) option configuration. |
| `publish` | Publish a [GitHub release](https://help.github.com/articles/about-releases), optionally uploading file assets. |
| `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. |
| `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. |
| `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. |

## Install

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

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

The `name` and `label` for each assets are generated with [Lodash template](https://lodash.com/docs#template). The following variables are available:

| Parameter | Description |
|---------------|-------------------------------------------------------------------------------------|
| `branch` | The branch from which the release is done. |
| `lastRelease` | `Object` with `version`, `gitTag` and `gitHead` of the last release. |
| `nextRelease` | `Object` with `version`, `gitTag`, `gitHead` and `notes` of the release being done. |
| `commits` | `Array` of commit `Object`s with `hash`, `subject`, `body` `message` and `author`. |

**Note**: If a file has a match in `assets` it will be included even if it also has a match in `.gitignore`.

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

`[{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:

> `[MyLibrary JS (v1.0.0) distribution](MyLibrary-v1.0.0.js)`
#### successComment

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

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

| Parameter | Description |
|-----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `branch` | The branch from which the release had failed. |
| Parameter | Description |
|-----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `branch` | The branch from which the release had failed. |
| `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. |

##### failComment examples
Expand Down
6 changes: 3 additions & 3 deletions lib/publish.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {basename, extname, resolve} = require('path');
const {stat, readFile} = require('fs-extra');
const {isPlainObject} = require('lodash');
const {isPlainObject, template} = require('lodash');
const parseGithubUrl = require('parse-github-url');
const mime = require('mime');
const debug = require('debug')('semantic-release:github');
Expand Down Expand Up @@ -72,7 +72,7 @@ module.exports = async (pluginConfig, context) => {
return;
}

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

if (isPlainObject(asset) && asset.label) {
upload.label = asset.label;
upload.label = template(asset.label)(context);
}

const {
Expand Down
54 changes: 54 additions & 0 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,60 @@ test.serial('Publish a release with an array of assets', async t => {
t.true(githubUpload2.isDone());
});

test.serial('Publish a release with release information in assets', async t => {
const owner = 'test_user';
const repo = 'test_repo';
const env = {GITHUB_TOKEN: 'github_token'};
const assets = [
{
path: ['upload.txt'],
name: `file_with_release_\${nextRelease.gitTag}_in_filename.txt`,
label: `File with release \${nextRelease.gitTag} in label`,
},
];
const nextRelease = {version: '1.0.0', gitHead: '123', gitTag: 'v1.0.0', notes: 'Test release note body'};
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
const releaseUrl = `https://github.com/${owner}/${repo}/releases/${nextRelease.version}`;
const assetUrl = `https://github.com/${owner}/${repo}/releases/download/${nextRelease.version}/file_with_release_v1.0.0_in_filename.txt`;
const releaseId = 1;
const uploadUri = `/api/uploads/repos/${owner}/${repo}/releases/${releaseId}/assets`;
const uploadUrl = `https://github.com${uploadUri}{?name,label}`;
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {permissions: {push: true}})
.post(`/repos/${owner}/${repo}/releases`, {
tag_name: nextRelease.gitTag,
target_commitish: options.branch,
name: nextRelease.gitTag,
body: nextRelease.notes,
draft: true,
})
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl, id: releaseId})
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {
draft: false,
})
.reply(200, {html_url: releaseUrl});
const githubUpload = upload(env, {
uploadUrl: 'https://github.com',
contentLength: (await stat(path.resolve(cwd, 'upload.txt'))).size,
})
.post(
`${uploadUri}?name=${escape('file_with_release_v1.0.0_in_filename.txt')}&label=${escape(
'File with release v1.0.0 in label'
)}`
)
.reply(200, {browser_download_url: assetUrl});

const result = await t.context.m.publish({assets}, {cwd, env, options, nextRelease, logger: t.context.logger});

t.is(result.url, releaseUrl);
t.deepEqual(t.context.log.args[0], ['Verify GitHub authentication']);
t.true(t.context.log.calledWith('Published file %s', assetUrl));
t.true(t.context.log.calledWith('Published GitHub release: %s', releaseUrl));
t.true(github.isDone());
t.true(githubUpload.isDone());
});

test.serial('Comment and add labels on PR included in the releases', async t => {
const owner = 'test_user';
const repo = 'test_repo';
Expand Down

0 comments on commit 8503939

Please sign in to comment.