Skip to content

Commit fc6bdc6

Browse files
committed
fix: log the Github url in the verifyConditions plugin
1 parent da7a788 commit fc6bdc6

File tree

6 files changed

+108
-52
lines changed

6 files changed

+108
-52
lines changed

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const publishGithub = require('./lib/publish');
33

44
let verified;
55

6-
async function verifyConditions(pluginConfig, {options}) {
6+
async function verifyConditions(pluginConfig, {options, logger}) {
77
// If the Github publish plugin is used and has `assets` configured, validate it now in order to prevent any release if the configuration is wrong
88
if (options.publish) {
99
const publishPlugin = (Array.isArray(options.publish) ? options.publish : [options.publish]).find(
@@ -14,13 +14,13 @@ async function verifyConditions(pluginConfig, {options}) {
1414
}
1515
}
1616

17-
await verifyGithub(pluginConfig, options);
17+
await verifyGithub(pluginConfig, options, logger);
1818
verified = true;
1919
}
2020

2121
async function publish(pluginConfig, {nextRelease, options, logger}) {
2222
if (!verified) {
23-
await verifyGithub(pluginConfig, options);
23+
await verifyGithub(pluginConfig, options, logger);
2424
verified = true;
2525
}
2626
await publishGithub(pluginConfig, options, nextRelease, logger);

lib/get-client.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const url = require('url');
2+
const GitHubApi = require('github');
3+
4+
module.exports = (githubToken, githubUrl, githubApiPathPrefix) => {
5+
const {port, protocol, hostname} = githubUrl ? url.parse(githubUrl) : {};
6+
const github = new GitHubApi({
7+
port,
8+
protocol: (protocol || '').split(':')[0] || null,
9+
host: hostname,
10+
pathPrefix: githubApiPathPrefix,
11+
});
12+
github.authenticate({type: 'token', token: githubToken});
13+
return github;
14+
};

lib/publish.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
const {basename, extname} = require('path');
2-
const {parse} = require('url');
32
const {stat, readFile} = require('fs-extra');
43
const {isPlainObject} = require('lodash');
54
const parseGithubUrl = require('parse-github-url');
6-
const GitHubApi = require('github');
75
const pReduce = require('p-reduce');
86
const mime = require('mime');
97
const debug = require('debug')('semantic-release:github');
108
const globAssets = require('./glob-assets.js');
119
const resolveConfig = require('./resolve-config');
10+
const getClient = require('./get-client');
1211

1312
module.exports = async (pluginConfig, {branch, repositoryUrl}, {version, gitHead, gitTag, notes}, logger) => {
1413
const {githubToken, githubUrl, githubApiPathPrefix, assets} = resolveConfig(pluginConfig);
1514
const {name: repo, owner} = parseGithubUrl(repositoryUrl);
16-
let {port, protocol, hostname: host} = githubUrl ? parse(githubUrl) : {};
17-
protocol = (protocol || '').split(':')[0] || null;
18-
19-
const github = new GitHubApi({port, protocol, host, pathPrefix: githubApiPathPrefix});
20-
github.authenticate({type: 'token', token: githubToken});
21-
15+
const github = getClient(githubToken, githubUrl, githubApiPathPrefix);
2216
const release = {owner, repo, tag_name: gitTag, name: gitTag, target_commitish: branch, body: notes}; // eslint-disable-line camelcase
17+
2318
debug('release owner: %o', owner);
2419
debug('release repo: %o', repo);
2520
debug('release name: %o', gitTag);
2621
debug('release branch: %o', branch);
22+
2723
const ref = `refs/tags/${gitTag}`;
2824

2925
try {

lib/verify.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const {parse} = require('url');
1+
const url = require('url');
22
const {isString, isPlainObject, isUndefined, isArray} = require('lodash');
33
const parseGithubUrl = require('parse-github-url');
4-
const GitHubApi = require('github');
54
const SemanticReleaseError = require('@semantic-release/error');
65
const resolveConfig = require('./resolve-config');
6+
const getClient = require('./get-client');
77

8-
module.exports = async (pluginConfig, {repositoryUrl}) => {
8+
module.exports = async (pluginConfig, {repositoryUrl}, logger) => {
99
const {githubToken, githubUrl, githubApiPathPrefix, assets} = resolveConfig(pluginConfig);
1010

1111
if (!githubToken) {
@@ -34,13 +34,15 @@ module.exports = async (pluginConfig, {repositoryUrl}) => {
3434
);
3535
}
3636

37-
let {port, protocol, hostname: host} = githubUrl ? parse(githubUrl) : {};
38-
protocol = (protocol || '').split(':')[0] || null;
39-
40-
const github = new GitHubApi({port, protocol, host, pathPrefix: githubApiPathPrefix});
41-
github.authenticate({type: 'token', token: githubToken});
37+
if (githubUrl) {
38+
logger.log('Verify Github authentication (%s)', url.resolve(githubUrl, githubApiPathPrefix));
39+
} else {
40+
logger.log('Verify Github authentication');
41+
}
4242

43+
const github = getClient(githubToken, githubUrl, githubApiPathPrefix);
4344
let push;
45+
4446
try {
4547
({data: {permissions: {push}}} = await github.repos.get({repo, owner}));
4648
} catch (err) {

test/integration.test.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {escape} from 'querystring';
22
import test from 'ava';
33
import {stat} from 'fs-extra';
4+
import nock from 'nock';
45
import {stub} from 'sinon';
56
import clearModule from 'clear-module';
67
import SemanticReleaseError from '@semantic-release/error';
@@ -31,6 +32,8 @@ test.beforeEach(t => {
3132
test.afterEach.always(() => {
3233
// Restore process.env
3334
process.env = envBackup;
35+
// Clear nock
36+
nock.cleanAll();
3437
});
3538

3639
test.serial('Verify Github auth', async t => {
@@ -43,7 +46,7 @@ test.serial('Verify Github auth', async t => {
4346
.get(`/repos/${owner}/${repo}`)
4447
.reply(200, {permissions: {push: true}});
4548

46-
await t.notThrows(t.context.m.verifyConditions({}, {options}));
49+
await t.notThrows(t.context.m.verifyConditions({}, {options, logger: t.context.logger}));
4750

4851
t.true(github.isDone());
4952
});
@@ -61,7 +64,7 @@ test.serial('Verify Github auth with publish options', async t => {
6164
.get(`/repos/${owner}/${repo}`)
6265
.reply(200, {permissions: {push: true}});
6366

64-
await t.notThrows(t.context.m.verifyConditions({}, {options}));
67+
await t.notThrows(t.context.m.verifyConditions({}, {options, logger: t.context.logger}));
6568

6669
t.true(github.isDone());
6770
});
@@ -86,7 +89,7 @@ test.serial('Verify Github auth and assets config', async t => {
8689
.get(`/repos/${owner}/${repo}`)
8790
.reply(200, {permissions: {push: true}});
8891

89-
await t.notThrows(t.context.m.verifyConditions({}, {options}));
92+
await t.notThrows(t.context.m.verifyConditions({}, {options, logger: t.context.logger}));
9093

9194
t.true(github.isDone());
9295
});
@@ -101,7 +104,7 @@ test.serial('Throw SemanticReleaseError if invalid config', async t => {
101104
repositoryUrl: `git+https://othertesturl.com/${owner}/${repo}.git`,
102105
};
103106

104-
const error = await t.throws(t.context.m.verifyConditions({}, {options}));
107+
const error = await t.throws(t.context.m.verifyConditions({}, {options, logger: t.context.logger}));
105108

106109
t.true(error instanceof SemanticReleaseError);
107110
t.is(error.code, 'EINVALIDASSETS');
@@ -158,9 +161,10 @@ test.serial('Publish a release with an array of assets', async t => {
158161

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

161-
t.deepEqual(t.context.log.args[0], ['Published Github release: %s', releaseUrl]);
162-
t.deepEqual(t.context.log.args[1], ['Published file %s', assetUrl]);
163-
t.deepEqual(t.context.log.args[2], ['Published file %s', otherAssetUrl]);
164+
t.deepEqual(t.context.log.args[0], ['Verify Github authentication']);
165+
t.deepEqual(t.context.log.args[1], ['Published Github release: %s', releaseUrl]);
166+
t.deepEqual(t.context.log.args[2], ['Published file %s', assetUrl]);
167+
t.deepEqual(t.context.log.args[3], ['Published file %s', otherAssetUrl]);
164168
t.true(github.isDone());
165169
t.true(githubUpload1.isDone());
166170
t.true(githubUpload2.isDone());
@@ -218,12 +222,13 @@ test.serial('Verify Github auth and release', async t => {
218222
.post(`${uploadUri}?name=${escape('other_file.txt')}&label=${escape('Other File')}`)
219223
.reply(200, {browser_download_url: otherAssetUrl});
220224

221-
await t.notThrows(t.context.m.verifyConditions({}, {options}));
225+
await t.notThrows(t.context.m.verifyConditions({}, {options, logger: t.context.logger}));
222226
await t.context.m.publish({assets}, {nextRelease, options, logger: t.context.logger});
223227

224-
t.deepEqual(t.context.log.args[0], ['Published Github release: %s', releaseUrl]);
225-
t.deepEqual(t.context.log.args[1], ['Published file %s', otherAssetUrl]);
226-
t.deepEqual(t.context.log.args[2], ['Published file %s', assetUrl]);
228+
t.deepEqual(t.context.log.args[0], ['Verify Github authentication']);
229+
t.deepEqual(t.context.log.args[1], ['Published Github release: %s', releaseUrl]);
230+
t.deepEqual(t.context.log.args[2], ['Published file %s', otherAssetUrl]);
231+
t.deepEqual(t.context.log.args[3], ['Published file %s', assetUrl]);
227232
t.true(github.isDone());
228233
t.true(githubUpload1.isDone());
229234
t.true(githubUpload2.isDone());

0 commit comments

Comments
 (0)