Skip to content

Commit

Permalink
fix: publish a release when env.GITHUB_URL is set to https://github.com
Browse files Browse the repository at this point in the history
… (#269)
  • Loading branch information
gr2m authored May 14, 2020
1 parent 8027e91 commit 543f40b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ If you have actions that trigger on newly created releases, please use a generat

### Environment variables

| Variable | Description |
| ------------------------------ | --------------------------------------------------------- |
| `GH_TOKEN` or `GITHUB_TOKEN` | **Required.** The token used to authenticate with GitHub. |
| `GH_URL` or `GITHUB_URL` | The GitHub Enterprise endpoint. |
| `GH_PREFIX` or `GITHUB_PREFIX` | The GitHub Enterprise API prefix. |
| Variable | Description |
| -------------------------------------------------- | --------------------------------------------------------- |
| `GH_TOKEN` or `GITHUB_TOKEN` | **Required.** The token used to authenticate with GitHub. |
| `GITHUB_API_URL` or `GH_URL` or `GITHUB_URL` | The GitHub Enterprise endpoint. |
| `GH_PREFIX` or `GITHUB_PREFIX` | The GitHub Enterprise API prefix. |

### Options

Expand Down
2 changes: 1 addition & 1 deletion lib/resolve-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = (
{env}
) => ({
githubToken: env.GH_TOKEN || env.GITHUB_TOKEN,
githubUrl: githubUrl || env.GH_URL || env.GITHUB_URL,
githubUrl: githubUrl || env.GITHUB_API_URL || env.GH_URL || env.GITHUB_URL,
githubApiPathPrefix: githubApiPathPrefix || env.GH_PREFIX || env.GITHUB_PREFIX || '',
proxy: proxy || env.HTTP_PROXY,
assets: assets ? castArray(assets) : assets,
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/mock-github.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const nock = require('nock');
*
* @param {Object} [env={}] Environment variables.
* @param {String} [githubToken=env.GH_TOKEN || env.GITHUB_TOKEN || 'GH_TOKEN'] The github token to return in the authentication response.
* @param {String} [githubUrl=env.GH_URL || env.GITHUB_URL || 'https://api.github.com'] The url on which to intercept http requests.
* @param {String} [githubUrl=env.GITHUB_API_URL || env.GH_URL || env.GITHUB_URL || 'https://api.github.com'] The url on which to intercept http requests.
* @param {String} [githubApiPathPrefix=env.GH_PREFIX || env.GITHUB_PREFIX || ''] The GitHub Enterprise API prefix.
* @return {Object} A `nock` object ready to respond to a github authentication request.
*/
function authenticate(
env = {},
{
githubToken = env.GH_TOKEN || env.GITHUB_TOKEN || 'GH_TOKEN',
githubUrl = env.GH_URL || env.GITHUB_URL || 'https://api.github.com',
githubUrl = env.GITHUB_API_URL || env.GH_URL || env.GITHUB_URL || 'https://api.github.com',
githubApiPathPrefix = env.GH_PREFIX || env.GITHUB_PREFIX || '',
} = {}
) {
Expand Down
42 changes: 42 additions & 0 deletions test/publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,45 @@ test.serial('Throw error without retries for 400 error', async (t) => {
t.is(error.status, 400);
t.true(github.isDone());
});

test.serial(
'Publish a release when env.GITHUB_URL is set to https://github.com (Default in GitHub Actions, #268)',
async (t) => {
const owner = 'test_user';
const repo = 'test_repo';
const env = {
GITHUB_TOKEN: 'github_token',
GITHUB_URL: 'https://github.com',
GITHUB_API_URL: 'https://api.github.com',
};
const pluginConfig = {};
const nextRelease = {gitTag: 'v1.0.0', name: 'v1.0.0', notes: 'Test release note body'};
const options = {repositoryUrl: `https://github.com/${owner}/${repo}.git`};
const releaseUrl = `https://github.com/${owner}/${repo}/releases/${nextRelease.version}`;
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)
.post(`/repos/${owner}/${repo}/releases`, {
tag_name: nextRelease.gitTag,
name: nextRelease.name,
body: nextRelease.notes,
prerelease: false,
})
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});

const result = await publish(pluginConfig, {
cwd,
env,
options,
branch: {type: 'release', main: true},
nextRelease,
logger: t.context.logger,
});

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

0 comments on commit 543f40b

Please sign in to comment.