Skip to content

Commit 2abbf1f

Browse files
committed
Add pagination logic so that repos beyond the first 30 can be found
1 parent 38e6d32 commit 2abbf1f

3 files changed

Lines changed: 27 additions & 14 deletions

File tree

manifest.backup.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "CodeHub",
33
"description": "Automatically pushes your Codewars submissions to GitHub",
44
"homepage_url": "https://github.com/FebinBellamy/CodeHub",
5-
"version": "2.0.2",
5+
"version": "2.0.3",
66
"author": "Febin Bellamy",
77
"action": {
88
"default_icon": {

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "CodeHub",
33
"description": "Automatically pushes your Codewars submissions to GitHub",
44
"homepage_url": "https://github.com/FebinBellamy/CodeHub",
5-
"version": "2.0.2",
5+
"version": "2.0.3",
66
"author": "Febin Bellamy",
77
"action": {
88
"default_icon": {

scripts/helperFunctions.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
const getNextPageUrlFromLinkHeader = (linkHeader) => {
2+
if (!linkHeader) return null;
3+
const parts = linkHeader.split(",");
4+
for (const part of parts) {
5+
const match = part.trim().match(/<([^>]+)>;\s*rel="next"/);
6+
if (match) return match[1];
7+
}
8+
return null;
9+
};
10+
111
const checkIfRepoExists = async (repoName) => {
212
const { accessToken } = await chrome.storage.local.get(["accessToken"]);
3-
const url = `https://api.github.com/user/repos`;
413
const options = {
514
method: "GET",
615
headers: new Headers({
@@ -9,21 +18,25 @@ const checkIfRepoExists = async (repoName) => {
918
}),
1019
};
1120
try {
12-
const response = await fetch(url, options);
13-
if (!response.ok) {
14-
console.log(
15-
`GitHub API error: ${response.status} - ${response.statusText}`
16-
);
17-
return false;
18-
}
19-
const json = await response.json();
20-
for (let i = 0; i < json.length; i++) {
21-
let currentRepoName = json[i]["name"].toLowerCase();
22-
if (currentRepoName === repoName.toLowerCase()) return true;
21+
let url = `https://api.github.com/user/repos?per_page=100&page=1`;
22+
while (url) {
23+
const response = await fetch(url, options);
24+
if (!response.ok) {
25+
console.log(
26+
`GitHub API error: ${response.status} - ${response.statusText}`
27+
);
28+
return false;
29+
}
30+
const json = await response.json();
31+
if (json.some((repo) => repo.name.toLowerCase() === repoName.toLowerCase())) {
32+
return true;
33+
}
34+
url = getNextPageUrlFromLinkHeader(response.headers.get("Link"));
2335
}
2436
return false;
2537
} catch (e) {
2638
console.log("Error checking if repo exists:", e.message);
39+
return false;
2740
}
2841
};
2942

0 commit comments

Comments
 (0)