Skip to content

Commit dea2edc

Browse files
committed
fix: fix parsing or GitHub URL with special character prefix
1 parent 385fef9 commit dea2edc

File tree

7 files changed

+38
-13
lines changed

7 files changed

+38
-13
lines changed

lib/fail.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const {template} = require('lodash');
2-
const parseGithubUrl = require('parse-github-url');
32
const debug = require('debug')('semantic-release:github');
3+
const parseGithubUrl = require('./parse-github-url');
44
const ISSUE_ID = require('./definitions/sr-issue-id');
55
const resolveConfig = require('./resolve-config');
66
const getClient = require('./get-client');
@@ -22,9 +22,8 @@ module.exports = async (pluginConfig, context) => {
2222
logger.log('Skip issue creation.');
2323
} else {
2424
const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});
25-
let {name: repo, owner} = parseGithubUrl(repositoryUrl);
2625
// In case the repo changed name, get the new `repo`/`owner` as the search API will not follow redirects
27-
[owner, repo] = (await github.repos.get({repo, owner})).data.full_name.split('/');
26+
const [owner, repo] = (await github.repos.get(parseGithubUrl(repositoryUrl))).data.full_name.split('/');
2827
const body = failComment ? template(failComment)({branch, errors}) : getFailComment(branch, errors);
2928
const [srIssue] = await findSRIssues(github, failTitle, owner, repo);
3029

lib/parse-github-url.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const {URL} = require('url');
2+
3+
module.exports = repositoryUrl => {
4+
const [match, auth, host, path] = /^(?!.+:\/\/)(?:(.*)@)?(.*?):(.*?)$/.exec(repositoryUrl) || [];
5+
try {
6+
const [, owner, repo] = /^\/([^/]+)?\/?(.+?)(?:\.git)?$/.exec(
7+
new URL(match ? `ssh://${auth ? `${auth}@` : ''}${host}/${path}` : repositoryUrl).pathname
8+
);
9+
return {owner, repo};
10+
} catch (_) {
11+
return {};
12+
}
13+
};

lib/publish.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const {basename, extname, resolve} = require('path');
22
const {stat, readFile} = require('fs-extra');
33
const {isPlainObject, template} = require('lodash');
4-
const parseGithubUrl = require('parse-github-url');
54
const mime = require('mime');
65
const debug = require('debug')('semantic-release:github');
6+
const parseGithubUrl = require('./parse-github-url');
77
const globAssets = require('./glob-assets.js');
88
const resolveConfig = require('./resolve-config');
99
const getClient = require('./get-client');
@@ -16,7 +16,7 @@ module.exports = async (pluginConfig, context) => {
1616
logger,
1717
} = context;
1818
const {githubToken, githubUrl, githubApiPathPrefix, proxy, assets} = resolveConfig(pluginConfig, context);
19-
const {name: repo, owner} = parseGithubUrl(repositoryUrl);
19+
const {owner, repo} = parseGithubUrl(repositoryUrl);
2020
const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});
2121

2222
const releaseData = {

lib/success.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const {isNil, uniqBy, template, flatten} = require('lodash');
2-
const parseGithubUrl = require('parse-github-url');
32
const pFilter = require('p-filter');
43
const AggregateError = require('aggregate-error');
54
const issueParser = require('issue-parser');
65
const debug = require('debug')('semantic-release:github');
6+
const parseGithubUrl = require('./parse-github-url');
77
const resolveConfig = require('./resolve-config');
88
const getClient = require('./get-client');
99
const getSearchQueries = require('./get-search-queries');
@@ -31,11 +31,8 @@ module.exports = async (pluginConfig, context) => {
3131
} = resolveConfig(pluginConfig, context);
3232

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

4037
const errors = [];
4138

lib/verify.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const {isString, isPlainObject, isNil, isArray, isNumber} = require('lodash');
2-
const parseGithubUrl = require('parse-github-url');
32
const urlJoin = require('url-join');
43
const AggregateError = require('aggregate-error');
4+
const parseGithubUrl = require('./parse-github-url');
55
const resolveConfig = require('./resolve-config');
66
const getClient = require('./get-client');
77
const getError = require('./get-error');
@@ -47,7 +47,7 @@ module.exports = async (pluginConfig, context) => {
4747
logger.log('Verify GitHub authentication');
4848
}
4949

50-
const {name: repo, owner} = parseGithubUrl(repositoryUrl);
50+
const {repo, owner} = parseGithubUrl(repositoryUrl);
5151
if (!owner || !repo) {
5252
errors.push(getError('EINVALIDGITHUBURL'));
5353
} else if (githubToken && !errors.find(({code}) => code === 'EINVALIDPROXY')) {

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"mime": "^2.4.3",
3232
"p-filter": "^2.0.0",
3333
"p-retry": "^4.0.0",
34-
"parse-github-url": "^1.0.1",
3534
"url-join": "^4.0.0"
3635
},
3736
"devDependencies": {

test/verify.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,23 @@ test.serial('Verify package, token and repository access and custom URL without
113113
t.deepEqual(t.context.log.args[0], ['Verify GitHub authentication (%s)', 'https://othertesturl.com:9090']);
114114
});
115115

116+
test.serial('Verify package, token and repository access and shorthand repositoryUrl URL', async t => {
117+
const owner = 'test_user';
118+
const repo = 'test_repo';
119+
const env = {GH_TOKEN: 'github_token'};
120+
const githubUrl = 'https://othertesturl.com:9090';
121+
const github = authenticate(env, {githubUrl})
122+
.get(`/repos/${owner}/${repo}`)
123+
.reply(200, {permissions: {push: true}});
124+
125+
await t.notThrowsAsync(
126+
verify({githubUrl}, {env, options: {repositoryUrl: `github:${owner}/${repo}`}, logger: t.context.logger})
127+
);
128+
129+
t.true(github.isDone());
130+
t.deepEqual(t.context.log.args[0], ['Verify GitHub authentication (%s)', 'https://othertesturl.com:9090']);
131+
});
132+
116133
test.serial('Verify package, token and repository with environment variables', async t => {
117134
const owner = 'test_user';
118135
const repo = 'test_repo';

0 commit comments

Comments
 (0)