Skip to content

Commit

Permalink
feat: remove githubToken from options
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pvdlg authored and gr2m committed Dec 22, 2017
1 parent f473cdb commit 81506f8
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 156 deletions.
38 changes: 25 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
# @semantic-release/github

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

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

## verifyConditions

Verify the presence and the validity of the `githubToken` (set via option or environment variable) and the `assets` option configuration.
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), optionnaly uploading files.
Publish a [Github release](https://help.github.com/articles/about-releases), optionally uploading files.

## Configuration

### Github Repository authentication

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

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

### 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. |
Expand All @@ -34,22 +38,24 @@ Only the [personal token](https://help.github.com/articles/creating-a-personal-a

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

#### `assets`

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
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

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

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"]`).
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"]`).

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

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

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

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

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

`[['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.
`[['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.

### Usage

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.
The plugins are used by default by [semantic-release](https://github.com/semantic-release/semantic-release) so no
specific configuration is required if `githubUrl` and `githubApiPathPrefix` are set via environment variable.

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

Expand Down
4 changes: 2 additions & 2 deletions lib/resolve-config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {castArray} = require('lodash');

module.exports = ({githubToken, githubUrl, githubApiPathPrefix, assets}) => ({
githubToken: githubToken || process.env.GH_TOKEN || process.env.GITHUB_TOKEN,
module.exports = ({githubUrl, githubApiPathPrefix, assets}) => ({
githubToken: process.env.GH_TOKEN || process.env.GITHUB_TOKEN,
githubUrl: githubUrl || process.env.GH_URL || process.env.GITHUB_URL,
githubApiPathPrefix: githubApiPathPrefix || process.env.GH_PREFIX || process.env.GITHUB_PREFIX,
assets: assets ? castArray(assets) : assets,
Expand Down
21 changes: 13 additions & 8 deletions test/helpers/mock-github.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,32 @@ import nock from 'nock';
/**
* Retun a `nock` object setup to respond to a github authentication request. Other expectation and responses can be chained.
*
* @param {String} [githubToken='GH_TOKEN'] The github token to return in the authentication response.
* @param {String} [githubUrl='https://api.github.com'] The url on which to intercept http requests.
* @param {String} [githubApiPathPrefix] The GitHub Enterprise API prefix.
* @param {String} [githubToken=process.env.GH_TOKEN || process.env.GITHUB_TOKEN || 'GH_TOKEN'] The github token to return in the authentication response.
* @param {String} [githubUrl=process.env.GH_URL || process.env.GITHUB_URL || 'https://api.github.com'] The url on which to intercept http requests.
* @param {String} [githubApiPathPrefix=process.env.GH_PREFIX || process.env.GITHUB_PREFIX || ''] The GitHub Enterprise API prefix.
* @return {Object} A `nock` object ready to respond to a github authentication request.
*/
export function authenticate({
githubToken = 'GH_TOKEN',
githubUrl = 'https://api.github.com',
githubApiPathPrefix = '',
githubToken = process.env.GH_TOKEN || process.env.GITHUB_TOKEN || 'GH_TOKEN',
githubUrl = process.env.GH_URL || process.env.GITHUB_URL || 'https://api.github.com',
githubApiPathPrefix = process.env.GH_PREFIX || process.env.GITHUB_PREFIX || '',
} = {}) {
return nock(`${githubUrl}/${githubApiPathPrefix}`, {reqheaders: {Authorization: `token ${githubToken}`}});
}

/**
* Retun a `nock` object setup to respond to a github release upload request. Other expectation and responses can be chained.
*
* @param {String} [githubToken='GH_TOKEN'] The github token to return in the authentication response.
* @param {String} [githubToken=process.env.GH_TOKEN || process.env.GITHUB_TOKEN || 'GH_TOKEN'] The github token to return in the authentication response.
* @param {String} [uploadUrl] The url on which to intercept http requests.
* @return {Object} A `nock` object ready to respond to a github file upload request.
*/
export function upload({githubToken = 'GH_TOKEN', uploadUrl, contentType = 'text/plain', contentLength} = {}) {
export function upload({
githubToken = process.env.GH_TOKEN || process.env.GITHUB_TOKEN || 'GH_TOKEN',
uploadUrl,
contentType = 'text/plain',
contentLength,
} = {}) {
return nock(uploadUrl, {
reqheaders: {Authorization: `token ${githubToken}`, 'content-type': contentType, 'content-length': contentLength},
});
Expand Down
27 changes: 7 additions & 20 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ test.serial('Verify Github auth', async t => {
const owner = 'test_user';
const repo = 'test_repo';
const options = {repositoryUrl: `git+https://othertesturl.com/${owner}/${repo}.git`};

const github = authenticate({githubToken: process.env.GITHUB_TOKEN})
const github = authenticate()
.get(`/repos/${owner}/${repo}`)
.reply(200, {permissions: {push: true}});

Expand All @@ -59,8 +58,7 @@ test.serial('Verify Github auth with publish options', async t => {
publish: {path: '@semantic-release/github'},
repositoryUrl: `git+https://othertesturl.com/${owner}/${repo}.git`,
};

const github = authenticate({githubToken: process.env.GITHUB_TOKEN})
const github = authenticate()
.get(`/repos/${owner}/${repo}`)
.reply(200, {permissions: {push: true}});

Expand All @@ -84,8 +82,7 @@ test.serial('Verify Github auth and assets config', async t => {
publish: [{path: '@semantic-release/npm'}, {path: '@semantic-release/github', assets}],
repositoryUrl: `git+https://othertesturl.com/${owner}/${repo}.git`,
};

const github = authenticate({githubToken: process.env.GH_TOKEN})
const github = authenticate()
.get(`/repos/${owner}/${repo}`)
.reply(200, {permissions: {push: true}});

Expand Down Expand Up @@ -113,7 +110,7 @@ test.serial('Throw SemanticReleaseError if invalid config', async t => {
test.serial('Publish a release with an array of assets', async t => {
const owner = 'test_user';
const repo = 'test_repo';
const githubToken = 'github_token';
process.env.GH_TOKEN = 'github_token';
const assets = [
'test/fixtures/upload.txt',
{path: ['test/fixtures/*.txt', '!**/*_other.txt'], name: 'upload_file_name.txt'},
Expand All @@ -127,8 +124,7 @@ test.serial('Publish a release with an array of assets', async t => {
const releaseId = 1;
const uploadUri = `/api/uploads/repos/${owner}/${repo}/releases/${releaseId}/assets`;
const uploadUrl = `https://github.com${uploadUri}{?name,label}`;

const github = authenticate({githubToken})
const github = authenticate()
.get(`/repos/${owner}/${repo}`)
.reply(200, {permissions: {push: true}})
.get(`/repos/${owner}/${repo}/git/refs/tags/${nextRelease.gitTag}`)
Expand All @@ -142,24 +138,20 @@ test.serial('Publish a release with an array of assets', async t => {
body: nextRelease.notes,
})
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});

const githubUpload1 = upload({
githubToken,
uploadUrl: 'https://github.com',
contentLength: (await stat('test/fixtures/upload.txt')).size,
})
.post(`${uploadUri}?name=${escape('upload_file_name.txt')}`)
.reply(200, {browser_download_url: assetUrl});

const githubUpload2 = upload({
githubToken,
uploadUrl: 'https://github.com',
contentLength: (await stat('test/fixtures/upload_other.txt')).size,
})
.post(`${uploadUri}?name=${escape('upload_other.txt')}&label=${escape('Other File')}`)
.reply(200, {browser_download_url: otherAssetUrl});

await t.context.m.publish({githubToken, assets}, {nextRelease, options, logger: t.context.logger});
await t.context.m.publish({assets}, {nextRelease, options, logger: t.context.logger});

t.deepEqual(t.context.log.args[0], ['Verify Github authentication']);
t.deepEqual(t.context.log.args[1], ['Published Github release: %s', releaseUrl]);
Expand Down Expand Up @@ -190,8 +182,7 @@ test.serial('Verify Github auth and release', async t => {
const releaseId = 1;
const uploadUri = `/api/uploads/repos/${owner}/${repo}/releases/${releaseId}/assets`;
const uploadUrl = `https://github.com${uploadUri}{?name,label}`;

const github = authenticate({githubToken: process.env.GH_TOKEN})
const github = authenticate()
.get(`/repos/${owner}/${repo}`)
.reply(200, {permissions: {push: true}})
.get(`/repos/${owner}/${repo}/git/refs/tags/${nextRelease.gitTag}`)
Expand All @@ -205,17 +196,13 @@ test.serial('Verify Github auth and release', async t => {
body: nextRelease.notes,
})
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});

const githubUpload1 = upload({
githubToken: process.env.GH_TOKEN,
uploadUrl: 'https://github.com',
contentLength: (await stat('test/fixtures/upload.txt')).size,
})
.post(`${uploadUri}?name=${escape('upload.txt')}`)
.reply(200, {browser_download_url: assetUrl});

const githubUpload2 = upload({
githubToken: process.env.GH_TOKEN,
uploadUrl: 'https://github.com',
contentLength: (await stat('test/fixtures/upload_other.txt')).size,
})
Expand Down

0 comments on commit 81506f8

Please sign in to comment.