Skip to content

Commit

Permalink
fix: log the Github url in the verifyConditions plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Dec 17, 2017
1 parent da7a788 commit fc6bdc6
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 52 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const publishGithub = require('./lib/publish');

let verified;

async function verifyConditions(pluginConfig, {options}) {
async function verifyConditions(pluginConfig, {options, logger}) {
// 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
if (options.publish) {
const publishPlugin = (Array.isArray(options.publish) ? options.publish : [options.publish]).find(
Expand All @@ -14,13 +14,13 @@ async function verifyConditions(pluginConfig, {options}) {
}
}

await verifyGithub(pluginConfig, options);
await verifyGithub(pluginConfig, options, logger);
verified = true;
}

async function publish(pluginConfig, {nextRelease, options, logger}) {
if (!verified) {
await verifyGithub(pluginConfig, options);
await verifyGithub(pluginConfig, options, logger);
verified = true;
}
await publishGithub(pluginConfig, options, nextRelease, logger);
Expand Down
14 changes: 14 additions & 0 deletions lib/get-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const url = require('url');
const GitHubApi = require('github');

module.exports = (githubToken, githubUrl, githubApiPathPrefix) => {
const {port, protocol, hostname} = githubUrl ? url.parse(githubUrl) : {};
const github = new GitHubApi({
port,
protocol: (protocol || '').split(':')[0] || null,
host: hostname,
pathPrefix: githubApiPathPrefix,
});
github.authenticate({type: 'token', token: githubToken});
return github;
};
12 changes: 4 additions & 8 deletions lib/publish.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
const {basename, extname} = require('path');
const {parse} = require('url');
const {stat, readFile} = require('fs-extra');
const {isPlainObject} = require('lodash');
const parseGithubUrl = require('parse-github-url');
const GitHubApi = require('github');
const pReduce = require('p-reduce');
const mime = require('mime');
const debug = require('debug')('semantic-release:github');
const globAssets = require('./glob-assets.js');
const resolveConfig = require('./resolve-config');
const getClient = require('./get-client');

module.exports = async (pluginConfig, {branch, repositoryUrl}, {version, gitHead, gitTag, notes}, logger) => {
const {githubToken, githubUrl, githubApiPathPrefix, assets} = resolveConfig(pluginConfig);
const {name: repo, owner} = parseGithubUrl(repositoryUrl);
let {port, protocol, hostname: host} = githubUrl ? parse(githubUrl) : {};
protocol = (protocol || '').split(':')[0] || null;

const github = new GitHubApi({port, protocol, host, pathPrefix: githubApiPathPrefix});
github.authenticate({type: 'token', token: githubToken});

const github = getClient(githubToken, githubUrl, githubApiPathPrefix);
const release = {owner, repo, tag_name: gitTag, name: gitTag, target_commitish: branch, body: notes}; // eslint-disable-line camelcase

debug('release owner: %o', owner);
debug('release repo: %o', repo);
debug('release name: %o', gitTag);
debug('release branch: %o', branch);

const ref = `refs/tags/${gitTag}`;

try {
Expand Down
18 changes: 10 additions & 8 deletions lib/verify.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const {parse} = require('url');
const url = require('url');
const {isString, isPlainObject, isUndefined, isArray} = require('lodash');
const parseGithubUrl = require('parse-github-url');
const GitHubApi = require('github');
const SemanticReleaseError = require('@semantic-release/error');
const resolveConfig = require('./resolve-config');
const getClient = require('./get-client');

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

if (!githubToken) {
Expand Down Expand Up @@ -34,13 +34,15 @@ module.exports = async (pluginConfig, {repositoryUrl}) => {
);
}

let {port, protocol, hostname: host} = githubUrl ? parse(githubUrl) : {};
protocol = (protocol || '').split(':')[0] || null;

const github = new GitHubApi({port, protocol, host, pathPrefix: githubApiPathPrefix});
github.authenticate({type: 'token', token: githubToken});
if (githubUrl) {
logger.log('Verify Github authentication (%s)', url.resolve(githubUrl, githubApiPathPrefix));
} else {
logger.log('Verify Github authentication');
}

const github = getClient(githubToken, githubUrl, githubApiPathPrefix);
let push;

try {
({data: {permissions: {push}}} = await github.repos.get({repo, owner}));
} catch (err) {
Expand Down
27 changes: 16 additions & 11 deletions test/integration.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {escape} from 'querystring';
import test from 'ava';
import {stat} from 'fs-extra';
import nock from 'nock';
import {stub} from 'sinon';
import clearModule from 'clear-module';
import SemanticReleaseError from '@semantic-release/error';
Expand Down Expand Up @@ -31,6 +32,8 @@ test.beforeEach(t => {
test.afterEach.always(() => {
// Restore process.env
process.env = envBackup;
// Clear nock
nock.cleanAll();
});

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

await t.notThrows(t.context.m.verifyConditions({}, {options}));
await t.notThrows(t.context.m.verifyConditions({}, {options, logger: t.context.logger}));

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

await t.notThrows(t.context.m.verifyConditions({}, {options}));
await t.notThrows(t.context.m.verifyConditions({}, {options, logger: t.context.logger}));

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

await t.notThrows(t.context.m.verifyConditions({}, {options}));
await t.notThrows(t.context.m.verifyConditions({}, {options, logger: t.context.logger}));

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

const error = await t.throws(t.context.m.verifyConditions({}, {options}));
const error = await t.throws(t.context.m.verifyConditions({}, {options, logger: t.context.logger}));

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

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

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

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

t.deepEqual(t.context.log.args[0], ['Published Github release: %s', releaseUrl]);
t.deepEqual(t.context.log.args[1], ['Published file %s', otherAssetUrl]);
t.deepEqual(t.context.log.args[2], ['Published file %s', assetUrl]);
t.deepEqual(t.context.log.args[0], ['Verify Github authentication']);
t.deepEqual(t.context.log.args[1], ['Published Github release: %s', releaseUrl]);
t.deepEqual(t.context.log.args[2], ['Published file %s', otherAssetUrl]);
t.deepEqual(t.context.log.args[3], ['Published file %s', assetUrl]);
t.true(github.isDone());
t.true(githubUpload1.isDone());
t.true(githubUpload2.isDone());
Expand Down

0 comments on commit fc6bdc6

Please sign in to comment.