Skip to content

Commit

Permalink
Merge pull request #71 from devnoname120/improve-url-parsing
Browse files Browse the repository at this point in the history
Improve robustness of GitHub repo URL parsing
  • Loading branch information
payne911 authored Jan 3, 2025
2 parents d732ed6 + 6fb7c1d commit 4742362
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
4 changes: 4 additions & 0 deletions website/src/queries-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ function getQueryOrDefault(defaultVal) {
return JQ_REPO_FIELD.val();
}

function setQuery(query) {
JQ_REPO_FIELD.val(query);
}

function hideFilterContainer() {
JQ_FILTER_CONTAINER.hide();
}
Expand Down
38 changes: 31 additions & 7 deletions website/src/queries-logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,32 +462,56 @@ function initial_request(user, repo) {
}

/** Extracts and sanitizes 'user' and 'repo' values from potential inputs. */
function initiate_search() {
function parse_query(queryString) {
const shorthand = /^(?<user>[\w.-]+)\/(?<repo>[\w.-]+)$/;
const shorthandMatch = shorthand.exec(queryString);

if (shorthandMatch) { // we are dealing with "user/repo" input format
const {user, repo} = shorthandMatch.groups;
return {user, repo};
}

let pathname;
try {
pathname = new URL(queryString).pathname;
} catch {
return null;
}

const values = pathname.split('/').filter(s => s.length > 0);
if (values.length < 2)
return null;

const [user, repo] = values;
return {user, repo};
}


function initiate_search() {
/* Checking if search is allowed. */
if (searchNotAllowed())
return; // abort

clear_old_data();

let queryString = getQueryOrDefault("payne911/PieMenu");
let queryValues = queryString.split('/').filter(Boolean);
const queryValues = parse_query(queryString);

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

const {user, repo} = queryValues;

setUpOctokitWithLatestToken();

setQuery(`${user}/${repo}`);
setQueryFieldsAsLoading();
hideFilterContainer();
setMsg(UF_MSG_SCANNING);

const user = queryValues[len - 2];
const repo = queryValues[len - 1];
if (history.replaceState) {
history.replaceState({}, document.title, `?repo=${user}/${repo}`); // replace current URL param
}
Expand Down

0 comments on commit 4742362

Please sign in to comment.