Skip to content

Commit 81506f8

Browse files
pvdlggr2m
authored andcommitted
feat: remove githubToken from options
BREAKING CHANGE: The `githubToken` options has been removed. The Github token can now be configured only via the `GH_TOKEN` or `GITHUB_TOKEN` environment variables.
1 parent f473cdb commit 81506f8

File tree

6 files changed

+120
-156
lines changed

6 files changed

+120
-156
lines changed

README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
# @semantic-release/github
22

3-
Set of [semantic-release](https://github.com/semantic-release/semantic-release) plugins for publishing a [Github release](https://help.github.com/articles/about-releases).
3+
Set of [semantic-release](https://github.com/semantic-release/semantic-release) plugins for publishing a
4+
[Github release](https://help.github.com/articles/about-releases).
45

56
[![Travis](https://img.shields.io/travis/semantic-release/github.svg)](https://travis-ci.org/semantic-release/github)
67
[![Codecov](https://img.shields.io/codecov/c/github/semantic-release/github.svg)](https://codecov.io/gh/semantic-release/github)
78
[![Greenkeeper badge](https://badges.greenkeeper.io/semantic-release/github.svg)](https://greenkeeper.io/)
89

910
## verifyConditions
1011

11-
Verify the presence and the validity of the `githubToken` (set via option or environment variable) and the `assets` option configuration.
12+
Verify the presence and the validity of the authentication (set via [environment variables](#environment-variables)) and
13+
the [assets](#assets) option configuration.
1214

1315
## publish
1416

15-
Publish a [Github release](https://help.github.com/articles/about-releases), optionnaly uploading files.
17+
Publish a [Github release](https://help.github.com/articles/about-releases), optionally uploading files.
1618

1719
## Configuration
1820

1921
### Github Repository authentication
2022

21-
The `Github` authentication configuration is **required** and can be set via [environment variables](#environment-variables).
23+
The `Github` authentication configuration is **required** and can be set via
24+
[environment variables](#environment-variables).
2225

23-
Only the [personal token](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line) authentication is supported.
26+
Only the [personal token](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line)
27+
authentication is supported.
2428

2529
### Environment variables
2630

2731
| Variable | Description |
28-
| ------------------------------ | ----------------------------------------------------------|
32+
| ------------------------------ | --------------------------------------------------------- |
2933
| `GH_TOKEN` or `GITHUB_TOKEN` | **Required.** The token used to authenticate with GitHub. |
3034
| `GH_URL` or `GITHUB_URL` | The GitHub Enterprise endpoint. |
3135
| `GH_PREFIX` or `GITHUB_PREFIX` | The GitHub Enterprise API prefix. |
@@ -34,22 +38,24 @@ Only the [personal token](https://help.github.com/articles/creating-a-personal-a
3438

3539
| Option | Description | Default |
3640
| --------------------- | ------------------------------------------------------------------ | ---------------------------------------------------- |
37-
| `githubToken` | **Required.** The token used to authenticate with GitHub. | `GH_TOKEN` or `GITHUB_TOKEN` environment variable. |
3841
| `githubUrl` | The GitHub Enterprise endpoint. | `GH_URL` or `GITHUB_URL` environment variable. |
3942
| `githubApiPathPrefix` | The GitHub Enterprise API prefix. | `GH_PREFIX` or `GITHUB_PREFIX` environment variable. |
4043
| `assets` | An array of files to upload to the release. See [assets](#assets). | - |
4144

4245
#### `assets`
4346

44-
Can be a [glob](https://github.com/isaacs/node-glob#glob-primer) or and `Array` of [globs](https://github.com/isaacs/node-glob#glob-primer) and `Object`s with the following properties
47+
Can be a [glob](https://github.com/isaacs/node-glob#glob-primer) or and `Array` of
48+
[globs](https://github.com/isaacs/node-glob#glob-primer) and `Object`s with the following properties
4549

4650
| Property | Description | Default |
4751
| -------- | -------------------------------------------------------------------------------------------------------- | ------------------------------------ |
4852
| `path` | **Required.** A [glob](https://github.com/isaacs/node-glob#glob-primer) to identify the files to upload. | - |
4953
| `name` | The name of the downloadable file on the Github release. | File name extracted from the `path`. |
5054
| `label` | Short description of the file displayed on the Github release. | - |
5155

52-
Each entry in the `assets` `Array` is globbed individually. A [glob](https://github.com/isaacs/node-glob#glob-primer) can be a `String` (`"dist/**/*.js"` or `"dist/mylib.js"`) or an `Array` of `String`s that will be globbed together (`["dist/**", "!**/*.css"]`).
56+
Each entry in the `assets` `Array` is globbed individually. A [glob](https://github.com/isaacs/node-glob#glob-primer)
57+
can be a `String` (`"dist/**/*.js"` or `"dist/mylib.js"`) or an `Array` of `String`s that will be globbed together
58+
(`["dist/**", "!**/*.css"]`).
5359

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

@@ -59,15 +65,21 @@ Files can be included enven if they have a match in `.gitignore`.
5965

6066
`'dist/*.js'`: include all the `js` files in the `dist` directory, but not in its sub-directories.
6167

62-
`[['dist', '!**/*.css']]`: include all the files in the `dist` directory and its sub-directories excluding the `css` files.
68+
`[['dist', '!**/*.css']]`: include all the files in the `dist` directory and its sub-directories excluding the `css`
69+
files.
6370

64-
`[{path: 'dist/MyLibrary.js', label: 'MyLibrary JS distribution'}, {path: 'dist/MyLibrary.css', label: 'MyLibrary CSS distribution'}]`: include the `dist/MyLibrary.js` and `dist/MyLibrary.css` files, and label them `MyLibrary JS distribution` and `MyLibrary CSS distribution` in the Github release.
71+
`[{path: 'dist/MyLibrary.js', label: 'MyLibrary JS distribution'}, {path: 'dist/MyLibrary.css', label: 'MyLibrary CSS
72+
distribution'}]`: include the `dist/MyLibrary.js` and `dist/MyLibrary.css` files, and label them `MyLibrary JS
73+
distribution` and `MyLibrary CSS distribution` in the Github release.
6574

66-
`[['dist/**/*.{js,css}', '!**/*.min.*'], {path: 'build/MyLibrary.zip', label: 'MyLibrary'}]`: include all the `js` and `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.
75+
`[['dist/**/*.{js,css}', '!**/*.min.*'], {path: 'build/MyLibrary.zip', label: 'MyLibrary'}]`: include all the `js` and
76+
`css` files in the `dist` directory and its sub-directories excluding the minified version, plus the
77+
`build/MyLibrary.zip` file and label it `MyLibrary` in the Github release.
6778

6879
### Usage
6980

70-
The plugins are used by default by [semantic-release](https://github.com/semantic-release/semantic-release) so no specific configuration is requiered if `githubToken`, `githubUrl` and `githubApiPathPrefix` are set via environment variable.
81+
The plugins are used by default by [semantic-release](https://github.com/semantic-release/semantic-release) so no
82+
specific configuration is required if `githubUrl` and `githubApiPathPrefix` are set via environment variable.
7183

7284
Each individual plugin can be disabled, replaced or used with other plugins in the `package.json`:
7385

lib/resolve-config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const {castArray} = require('lodash');
22

3-
module.exports = ({githubToken, githubUrl, githubApiPathPrefix, assets}) => ({
4-
githubToken: githubToken || process.env.GH_TOKEN || process.env.GITHUB_TOKEN,
3+
module.exports = ({githubUrl, githubApiPathPrefix, assets}) => ({
4+
githubToken: process.env.GH_TOKEN || process.env.GITHUB_TOKEN,
55
githubUrl: githubUrl || process.env.GH_URL || process.env.GITHUB_URL,
66
githubApiPathPrefix: githubApiPathPrefix || process.env.GH_PREFIX || process.env.GITHUB_PREFIX,
77
assets: assets ? castArray(assets) : assets,

test/helpers/mock-github.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,32 @@ import nock from 'nock';
33
/**
44
* Retun a `nock` object setup to respond to a github authentication request. Other expectation and responses can be chained.
55
*
6-
* @param {String} [githubToken='GH_TOKEN'] The github token to return in the authentication response.
7-
* @param {String} [githubUrl='https://api.github.com'] The url on which to intercept http requests.
8-
* @param {String} [githubApiPathPrefix] The GitHub Enterprise API prefix.
6+
* @param {String} [githubToken=process.env.GH_TOKEN || process.env.GITHUB_TOKEN || 'GH_TOKEN'] The github token to return in the authentication response.
7+
* @param {String} [githubUrl=process.env.GH_URL || process.env.GITHUB_URL || 'https://api.github.com'] The url on which to intercept http requests.
8+
* @param {String} [githubApiPathPrefix=process.env.GH_PREFIX || process.env.GITHUB_PREFIX || ''] The GitHub Enterprise API prefix.
99
* @return {Object} A `nock` object ready to respond to a github authentication request.
1010
*/
1111
export function authenticate({
12-
githubToken = 'GH_TOKEN',
13-
githubUrl = 'https://api.github.com',
14-
githubApiPathPrefix = '',
12+
githubToken = process.env.GH_TOKEN || process.env.GITHUB_TOKEN || 'GH_TOKEN',
13+
githubUrl = process.env.GH_URL || process.env.GITHUB_URL || 'https://api.github.com',
14+
githubApiPathPrefix = process.env.GH_PREFIX || process.env.GITHUB_PREFIX || '',
1515
} = {}) {
1616
return nock(`${githubUrl}/${githubApiPathPrefix}`, {reqheaders: {Authorization: `token ${githubToken}`}});
1717
}
1818

1919
/**
2020
* Retun a `nock` object setup to respond to a github release upload request. Other expectation and responses can be chained.
2121
*
22-
* @param {String} [githubToken='GH_TOKEN'] The github token to return in the authentication response.
22+
* @param {String} [githubToken=process.env.GH_TOKEN || process.env.GITHUB_TOKEN || 'GH_TOKEN'] The github token to return in the authentication response.
2323
* @param {String} [uploadUrl] The url on which to intercept http requests.
2424
* @return {Object} A `nock` object ready to respond to a github file upload request.
2525
*/
26-
export function upload({githubToken = 'GH_TOKEN', uploadUrl, contentType = 'text/plain', contentLength} = {}) {
26+
export function upload({
27+
githubToken = process.env.GH_TOKEN || process.env.GITHUB_TOKEN || 'GH_TOKEN',
28+
uploadUrl,
29+
contentType = 'text/plain',
30+
contentLength,
31+
} = {}) {
2732
return nock(uploadUrl, {
2833
reqheaders: {Authorization: `token ${githubToken}`, 'content-type': contentType, 'content-length': contentLength},
2934
});

test/integration.test.js

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ test.serial('Verify Github auth', async t => {
4141
const owner = 'test_user';
4242
const repo = 'test_repo';
4343
const options = {repositoryUrl: `git+https://othertesturl.com/${owner}/${repo}.git`};
44-
45-
const github = authenticate({githubToken: process.env.GITHUB_TOKEN})
44+
const github = authenticate()
4645
.get(`/repos/${owner}/${repo}`)
4746
.reply(200, {permissions: {push: true}});
4847

@@ -59,8 +58,7 @@ test.serial('Verify Github auth with publish options', async t => {
5958
publish: {path: '@semantic-release/github'},
6059
repositoryUrl: `git+https://othertesturl.com/${owner}/${repo}.git`,
6160
};
62-
63-
const github = authenticate({githubToken: process.env.GITHUB_TOKEN})
61+
const github = authenticate()
6462
.get(`/repos/${owner}/${repo}`)
6563
.reply(200, {permissions: {push: true}});
6664

@@ -84,8 +82,7 @@ test.serial('Verify Github auth and assets config', async t => {
8482
publish: [{path: '@semantic-release/npm'}, {path: '@semantic-release/github', assets}],
8583
repositoryUrl: `git+https://othertesturl.com/${owner}/${repo}.git`,
8684
};
87-
88-
const github = authenticate({githubToken: process.env.GH_TOKEN})
85+
const github = authenticate()
8986
.get(`/repos/${owner}/${repo}`)
9087
.reply(200, {permissions: {push: true}});
9188

@@ -113,7 +110,7 @@ test.serial('Throw SemanticReleaseError if invalid config', async t => {
113110
test.serial('Publish a release with an array of assets', async t => {
114111
const owner = 'test_user';
115112
const repo = 'test_repo';
116-
const githubToken = 'github_token';
113+
process.env.GH_TOKEN = 'github_token';
117114
const assets = [
118115
'test/fixtures/upload.txt',
119116
{path: ['test/fixtures/*.txt', '!**/*_other.txt'], name: 'upload_file_name.txt'},
@@ -127,8 +124,7 @@ test.serial('Publish a release with an array of assets', async t => {
127124
const releaseId = 1;
128125
const uploadUri = `/api/uploads/repos/${owner}/${repo}/releases/${releaseId}/assets`;
129126
const uploadUrl = `https://github.com${uploadUri}{?name,label}`;
130-
131-
const github = authenticate({githubToken})
127+
const github = authenticate()
132128
.get(`/repos/${owner}/${repo}`)
133129
.reply(200, {permissions: {push: true}})
134130
.get(`/repos/${owner}/${repo}/git/refs/tags/${nextRelease.gitTag}`)
@@ -142,24 +138,20 @@ test.serial('Publish a release with an array of assets', async t => {
142138
body: nextRelease.notes,
143139
})
144140
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});
145-
146141
const githubUpload1 = upload({
147-
githubToken,
148142
uploadUrl: 'https://github.com',
149143
contentLength: (await stat('test/fixtures/upload.txt')).size,
150144
})
151145
.post(`${uploadUri}?name=${escape('upload_file_name.txt')}`)
152146
.reply(200, {browser_download_url: assetUrl});
153-
154147
const githubUpload2 = upload({
155-
githubToken,
156148
uploadUrl: 'https://github.com',
157149
contentLength: (await stat('test/fixtures/upload_other.txt')).size,
158150
})
159151
.post(`${uploadUri}?name=${escape('upload_other.txt')}&label=${escape('Other File')}`)
160152
.reply(200, {browser_download_url: otherAssetUrl});
161153

162-
await t.context.m.publish({githubToken, assets}, {nextRelease, options, logger: t.context.logger});
154+
await t.context.m.publish({assets}, {nextRelease, options, logger: t.context.logger});
163155

164156
t.deepEqual(t.context.log.args[0], ['Verify Github authentication']);
165157
t.deepEqual(t.context.log.args[1], ['Published Github release: %s', releaseUrl]);
@@ -190,8 +182,7 @@ test.serial('Verify Github auth and release', async t => {
190182
const releaseId = 1;
191183
const uploadUri = `/api/uploads/repos/${owner}/${repo}/releases/${releaseId}/assets`;
192184
const uploadUrl = `https://github.com${uploadUri}{?name,label}`;
193-
194-
const github = authenticate({githubToken: process.env.GH_TOKEN})
185+
const github = authenticate()
195186
.get(`/repos/${owner}/${repo}`)
196187
.reply(200, {permissions: {push: true}})
197188
.get(`/repos/${owner}/${repo}/git/refs/tags/${nextRelease.gitTag}`)
@@ -205,17 +196,13 @@ test.serial('Verify Github auth and release', async t => {
205196
body: nextRelease.notes,
206197
})
207198
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});
208-
209199
const githubUpload1 = upload({
210-
githubToken: process.env.GH_TOKEN,
211200
uploadUrl: 'https://github.com',
212201
contentLength: (await stat('test/fixtures/upload.txt')).size,
213202
})
214203
.post(`${uploadUri}?name=${escape('upload.txt')}`)
215204
.reply(200, {browser_download_url: assetUrl});
216-
217205
const githubUpload2 = upload({
218-
githubToken: process.env.GH_TOKEN,
219206
uploadUrl: 'https://github.com',
220207
contentLength: (await stat('test/fixtures/upload_other.txt')).size,
221208
})

0 commit comments

Comments
 (0)