Skip to content

Commit

Permalink
chore: rate limit scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Mar 4, 2024
1 parent 498c806 commit 167310b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
SUPABASE_URL=
SUPABASE_REF=
SUPABASE_ANON_KEY=
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,3 @@ open http://localhost:8080
#### Mobile

![screenshot 2](https://github.com/ubiquity/devpool-directory-ui/assets/4975670/b7861ce7-1f1f-49a9-b8e2-ebb20724ee67)

25 changes: 12 additions & 13 deletions src/home/fetch-github/fetch-issues-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ export async function fetchIssuePreviews(): Promise<TaskNoFull[]> {
} catch (error) {
if (403 === error.status) {
console.error(`GitHub API rate limit exceeded.`);
const resetTime = error.response.headers["x-ratelimit-reset"];
const resetParsed = new Date(resetTime * 1000).toLocaleTimeString();

if (taskManager.getTasks().length == 0) {
if (!user || user === null) {
// only show rate limit modal if there are no issues loaded and not logged in
rateLimitModal(error);
rateLimitModal(
`You have been rate limited. Please log in to GitHub to increase your GitHub API limits, otherwise you can try again at ${resetParsed}.`
);
} else {
// otherwise we have a user and no issues loaded
// this happens processing the auth token it seems
Expand All @@ -33,10 +38,12 @@ export async function fetchIssuePreviews(): Promise<TaskNoFull[]> {
}
} else if (user && user !== null) {
// Tasks loaded and logged in
rateLimitModal(error, `You have been rate limited. Please try again at `);
rateLimitModal(`You have been rate limited. Please try again at ${resetParsed}.`);
} else {
// tasks loaded but not logged in
rateLimitModal(error);
rateLimitModal(
`You have been rate limited. Please log in to GitHub to increase your GitHub API limits, otherwise you can try again at ${resetParsed}.`
);
}
} else {
console.error(`Failed to fetch issue previews: ${error}`);
Expand All @@ -53,14 +60,6 @@ export async function fetchIssuePreviews(): Promise<TaskNoFull[]> {
return tasks;
}

function rateLimitModal(error: unknown, message?: string) {
const resetTime = error.response.headers["x-ratelimit-reset"];
const resetParsed = new Date(resetTime * 1000).toLocaleTimeString();

displayPopupMessage(
`GitHub API rate limit exceeded.`,
!message
? `You have been rate limited. Please log in to GitHub to increase your GitHub API limits, otherwise you can try again at ${resetParsed}.`
: message + `${resetParsed}.`
);
function rateLimitModal(message: string) {
displayPopupMessage(`GitHub API rate limit exceeded.`, message);
}
35 changes: 26 additions & 9 deletions src/home/getters/get-github-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ import { GitHubUser, GitHubUserResponse } from "../github-types";
import { OAuthToken } from "./get-github-access-token";
import { getLocalStore } from "./get-local-store";
import { getSupabaseRef } from "../rendering/render-github-login-button";
import { displayPopupMessage } from "../rendering/display-popup-modal";

export async function getGitHubUser(): Promise<GitHubUser | null> {
const activeSessionToken = await getSessionToken();
if (activeSessionToken) {
return getNewGitHubUser(activeSessionToken);
} else {
return null;
}
return getNewGitHubUser(activeSessionToken);
}

async function getSessionToken(): Promise<string | null> {
Expand All @@ -30,13 +27,33 @@ async function getNewSessionToken(): Promise<string | null> {
const params = new URLSearchParams(hash.substr(1)); // remove the '#' and parse
const providerToken = params.get("provider_token");
if (!providerToken) {
return null;
const err = params.get("error");
const code = params.get("error_code");
const desc = params.get("error_description");

if (err === "server_error") {
if (code === "500") {
if (desc === "Error getting user profile from external provider") {
displayPopupMessage(`GitHub Login Provider`, `Your access token may have reached it's rate limit, please try again after one hour.`);
throw new Error("GitHub login provider");
}
}
}
}
return providerToken;
}

async function getNewGitHubUser(providerToken: string): Promise<GitHubUser> {
async function getNewGitHubUser(providerToken: string | null): Promise<GitHubUser | null> {
const octokit = new Octokit({ auth: providerToken });
const response = (await octokit.request("GET /user")) as GitHubUserResponse;
return response.data;
try {
const response = (await octokit.request("GET /user")) as GitHubUserResponse;
return response.data;
} catch (err: unknown) {
// API rate limit exceeded for user ID 106.... If you reach out to GitHub Support for help, ...
if (err.status === 403 && err.message.includes("API rate limit exceeded")) {
displayPopupMessage("GitHub API rate limit exceeded", err.message);
throw new Error("GitHub API rate limit exceeded");
}
return null;
}
}
3 changes: 2 additions & 1 deletion src/home/rendering/render-github-login-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const supabaseAnonKey = process.env.SUPABASE_ANON_KEY;
if (!supabaseAnonKey) throw new Error("SUPABASE_ANON_KEY not found");

export function getSupabaseRef(): string {
const supabaseRef = process.env.SUPABASE_REF;
if (!supabaseUrl) throw new Error("SUPABASE_URL not found");
const supabaseRef = supabaseUrl?.split("//")[1].split(".")[0];
if (!supabaseRef) throw new Error("SUPABASE_REF not found");

return supabaseRef;
Expand Down

0 comments on commit 167310b

Please sign in to comment.