Skip to content

Commit d772cdf

Browse files
committed
Improve robustness of GitHub repo URL parsing
For example this works now: https://github.com/MichaelAquilina/zsh-auto-notify/pull/67/files Previously `67` was taken as the repo owner and `files` as the repo name.
1 parent aae254f commit d772cdf

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

website/src/queries-init.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,15 @@ function getQueryOrDefault(defaultVal) {
166166
if (!JQ_REPO_FIELD.val()) {
167167
JQ_REPO_FIELD.val(defaultVal);
168168
}
169-
return JQ_REPO_FIELD.val();
169+
170+
const val = JQ_REPO_FIELD.val();
171+
172+
const isShorthand = /^[\w\.-]+\/[\w\.-]+$/;
173+
if (isShorthand.test(val)) {
174+
return val;
175+
} else {
176+
return new URL(val).pathname;
177+
}
170178
}
171179

172180
function hideFilterContainer() {

website/src/queries-logic.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,11 @@ function initiate_search() {
471471
clear_old_data();
472472

473473
let queryString = getQueryOrDefault("payne911/PieMenu");
474-
let queryValues = queryString.split('/').filter(Boolean);
474+
let queryValues = queryString.split('/').filter(s => s.length > 0);
475475

476476
let len = queryValues.length;
477477
if (len < 2) {
478-
setMsg('Please enter a valid query: it should contain two strings separated by a "/"');
478+
setMsg('Please enter a valid query: it should contain two strings separated by a "/", or the full URL to a GitHub repo');
479479
ga_faultyQuery(queryString);
480480
return; // abort
481481
}
@@ -486,8 +486,8 @@ function initiate_search() {
486486
hideFilterContainer();
487487
setMsg(UF_MSG_SCANNING);
488488

489-
const user = queryValues[len - 2];
490-
const repo = queryValues[len - 1];
489+
const user = queryValues[0];
490+
const repo = queryValues[1];
491491
if (history.replaceState) {
492492
history.replaceState({}, document.title, `?repo=${user}/${repo}`); // replace current URL param
493493
}

0 commit comments

Comments
 (0)