Skip to content

Commit

Permalink
fix: fix parsing or GitHub URL with special character prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Oct 18, 2019
1 parent 385fef9 commit dea2edc
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 13 deletions.
5 changes: 2 additions & 3 deletions lib/fail.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {template} = require('lodash');
const parseGithubUrl = require('parse-github-url');
const debug = require('debug')('semantic-release:github');
const parseGithubUrl = require('./parse-github-url');
const ISSUE_ID = require('./definitions/sr-issue-id');
const resolveConfig = require('./resolve-config');
const getClient = require('./get-client');
Expand All @@ -22,9 +22,8 @@ module.exports = async (pluginConfig, context) => {
logger.log('Skip issue creation.');
} else {
const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});
let {name: repo, owner} = parseGithubUrl(repositoryUrl);
// In case the repo changed name, get the new `repo`/`owner` as the search API will not follow redirects
[owner, repo] = (await github.repos.get({repo, owner})).data.full_name.split('/');
const [owner, repo] = (await github.repos.get(parseGithubUrl(repositoryUrl))).data.full_name.split('/');
const body = failComment ? template(failComment)({branch, errors}) : getFailComment(branch, errors);
const [srIssue] = await findSRIssues(github, failTitle, owner, repo);

Expand Down
13 changes: 13 additions & 0 deletions lib/parse-github-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const {URL} = require('url');

module.exports = repositoryUrl => {
const [match, auth, host, path] = /^(?!.+:\/\/)(?:(.*)@)?(.*?):(.*?)$/.exec(repositoryUrl) || [];
try {
const [, owner, repo] = /^\/([^/]+)?\/?(.+?)(?:\.git)?$/.exec(
new URL(match ? `ssh://${auth ? `${auth}@` : ''}${host}/${path}` : repositoryUrl).pathname
);
return {owner, repo};
} catch (_) {
return {};
}
};
4 changes: 2 additions & 2 deletions lib/publish.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const {basename, extname, resolve} = require('path');
const {stat, readFile} = require('fs-extra');
const {isPlainObject, template} = require('lodash');
const parseGithubUrl = require('parse-github-url');
const mime = require('mime');
const debug = require('debug')('semantic-release:github');
const parseGithubUrl = require('./parse-github-url');
const globAssets = require('./glob-assets.js');
const resolveConfig = require('./resolve-config');
const getClient = require('./get-client');
Expand All @@ -16,7 +16,7 @@ module.exports = async (pluginConfig, context) => {
logger,
} = context;
const {githubToken, githubUrl, githubApiPathPrefix, proxy, assets} = resolveConfig(pluginConfig, context);
const {name: repo, owner} = parseGithubUrl(repositoryUrl);
const {owner, repo} = parseGithubUrl(repositoryUrl);
const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});

const releaseData = {
Expand Down
7 changes: 2 additions & 5 deletions lib/success.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const {isNil, uniqBy, template, flatten} = require('lodash');
const parseGithubUrl = require('parse-github-url');
const pFilter = require('p-filter');
const AggregateError = require('aggregate-error');
const issueParser = require('issue-parser');
const debug = require('debug')('semantic-release:github');
const parseGithubUrl = require('./parse-github-url');
const resolveConfig = require('./resolve-config');
const getClient = require('./get-client');
const getSearchQueries = require('./get-search-queries');
Expand Down Expand Up @@ -31,11 +31,8 @@ module.exports = async (pluginConfig, context) => {
} = resolveConfig(pluginConfig, context);

const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});
const parsedUrl = parseGithubUrl(repositoryUrl);
// In case the repo changed name, get the new `repo`/`owner` as the search API will not follow redirects
const [owner, repo] = (await github.repos.get({repo: parsedUrl.name, owner: parsedUrl.owner})).data.full_name.split(
'/'
);
const [owner, repo] = (await github.repos.get(parseGithubUrl(repositoryUrl))).data.full_name.split('/');

const errors = [];

Expand Down
4 changes: 2 additions & 2 deletions lib/verify.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {isString, isPlainObject, isNil, isArray, isNumber} = require('lodash');
const parseGithubUrl = require('parse-github-url');
const urlJoin = require('url-join');
const AggregateError = require('aggregate-error');
const parseGithubUrl = require('./parse-github-url');
const resolveConfig = require('./resolve-config');
const getClient = require('./get-client');
const getError = require('./get-error');
Expand Down Expand Up @@ -47,7 +47,7 @@ module.exports = async (pluginConfig, context) => {
logger.log('Verify GitHub authentication');
}

const {name: repo, owner} = parseGithubUrl(repositoryUrl);
const {repo, owner} = parseGithubUrl(repositoryUrl);
if (!owner || !repo) {
errors.push(getError('EINVALIDGITHUBURL'));
} else if (githubToken && !errors.find(({code}) => code === 'EINVALIDPROXY')) {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"mime": "^2.4.3",
"p-filter": "^2.0.0",
"p-retry": "^4.0.0",
"parse-github-url": "^1.0.1",
"url-join": "^4.0.0"
},
"devDependencies": {
Expand Down
17 changes: 17 additions & 0 deletions test/verify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ test.serial('Verify package, token and repository access and custom URL without
t.deepEqual(t.context.log.args[0], ['Verify GitHub authentication (%s)', 'https://othertesturl.com:9090']);
});

test.serial('Verify package, token and repository access and shorthand repositoryUrl URL', async t => {
const owner = 'test_user';
const repo = 'test_repo';
const env = {GH_TOKEN: 'github_token'};
const githubUrl = 'https://othertesturl.com:9090';
const github = authenticate(env, {githubUrl})
.get(`/repos/${owner}/${repo}`)
.reply(200, {permissions: {push: true}});

await t.notThrowsAsync(
verify({githubUrl}, {env, options: {repositoryUrl: `github:${owner}/${repo}`}, logger: t.context.logger})
);

t.true(github.isDone());
t.deepEqual(t.context.log.args[0], ['Verify GitHub authentication (%s)', 'https://othertesturl.com:9090']);
});

test.serial('Verify package, token and repository with environment variables', async t => {
const owner = 'test_user';
const repo = 'test_repo';
Expand Down

0 comments on commit dea2edc

Please sign in to comment.