Skip to content

Commit 396cc36

Browse files
committed
Refactor GitHub repo URL parsing
1 parent a845ae4 commit 396cc36

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

website/src/queries-init.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,7 @@ function getQueryOrDefault(defaultVal) {
166166
if (!JQ_REPO_FIELD.val()) {
167167
JQ_REPO_FIELD.val(defaultVal);
168168
}
169-
170-
const val = JQ_REPO_FIELD.val();
171-
172-
const isShorthand = /^[\w\.-]+\/[\w\.-]+$/;
173-
if (isShorthand.test(val)) {
174-
return val; // we are dealing with "user/repo" input format
175-
} else {
176-
return new URL(val).pathname;
177-
}
169+
return JQ_REPO_FIELD.val();
178170
}
179171

180172
function hideFilterContainer() {

website/src/queries-logic.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,32 +462,55 @@ function initial_request(user, repo) {
462462
}
463463

464464
/** Extracts and sanitizes 'user' and 'repo' values from potential inputs. */
465-
function initiate_search() {
465+
function parse_query(queryString) {
466+
const shorthand = /^(?<user>[\w.-]+)\/(?<repo>[\w.-]+)$/;
467+
const shorthandMatch = shorthand.exec(queryString);
468+
469+
if (shorthandMatch) { // we are dealing with "user/repo" input format
470+
const {user, repo} = shorthandMatch.groups;
471+
return {user, repo};
472+
}
473+
474+
let pathname;
475+
try {
476+
pathname = new URL(queryString).pathname;
477+
} catch {
478+
return null;
479+
}
480+
481+
const values = pathname.split('/').filter(s => s.length > 0);
482+
if (values.length < 2)
483+
return null;
466484

485+
const [user, repo] = values;
486+
return {user, repo};
487+
}
488+
489+
490+
function initiate_search() {
467491
/* Checking if search is allowed. */
468492
if (searchNotAllowed())
469493
return; // abort
470494

471495
clear_old_data();
472496

473497
let queryString = getQueryOrDefault("payne911/PieMenu");
474-
let queryValues = queryString.split('/').filter(s => s.length > 0);
498+
const queryValues = parse_query(queryString);
475499

476-
let len = queryValues.length;
477-
if (len < 2) {
500+
if (!queryValues) {
478501
setMsg('Please enter a valid query: it should contain two strings separated by a "/", or the full URL to a GitHub repo');
479502
ga_faultyQuery(queryString);
480503
return; // abort
481504
}
482505

506+
const {user, repo} = queryValues;
507+
483508
setUpOctokitWithLatestToken();
484509

485510
setQueryFieldsAsLoading();
486511
hideFilterContainer();
487512
setMsg(UF_MSG_SCANNING);
488513

489-
const user = queryValues[0];
490-
const repo = queryValues[1];
491514
if (history.replaceState) {
492515
history.replaceState({}, document.title, `?repo=${user}/${repo}`); // replace current URL param
493516
}

0 commit comments

Comments
 (0)