Skip to content

Commit

Permalink
fix: improve perf of getToken
Browse files Browse the repository at this point in the history
  • Loading branch information
tdurieux committed Jun 19, 2024
1 parent 532c094 commit f81c63d
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/core/GitHubUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Octokit } from "@octokit/rest";
import Repository from "./Repository";
import UserModel from "./model/users/users.model";
import config from "../config";
import { RepositoryStatus } from "./types";

export function octokit(token: string) {
return new Octokit({
Expand All @@ -28,15 +29,14 @@ export async function getToken(repository: Repository) {
const span = trace.getTracer("ano-file").startSpan("GHUtils.getToken");
span.setAttribute("repoId", repository.repoId);
try {
// only check the token if the repo has been visited more than one day ago
// if (
// repository.model.source.accessToken &&
// repository.model.lastView > new Date(Date.now() - 1000 * 60 * 60 * 24)
// ) {
// return repository.model.source.accessToken;
// }
if (repository.model.source.accessToken) {
if (await checkToken(repository.model.source.accessToken)) {
// only check the token if the repo has been visited less than 10 minutes ago
if (
repository.status == RepositoryStatus.READY &&
repository.model.lastView > new Date(Date.now() - 1000 * 60 * 10)
) {
return repository.model.source.accessToken;
} else if (await checkToken(repository.model.source.accessToken)) {
return repository.model.source.accessToken;
}
}
Expand All @@ -53,7 +53,11 @@ export async function getToken(repository: Repository) {
const ownerAccessToken = repository.owner.model.accessTokens?.github;
if (ownerAccessToken) {
const tokenAge = repository.owner.model.accessTokenDates?.github;
if (!tokenAge || tokenAge < new Date(Date.now() - 1000 * 60 * 60 * 24)) {
// if the token is older than 7 days, refresh it
if (
!tokenAge ||
tokenAge < new Date(Date.now() - 1000 * 60 * 60 * 24 * 7)
) {
const url = `https://api.github.com/applications/${config.CLIENT_ID}/token`;
const headers = {
Accept: "application/vnd.github+json",
Expand Down

0 comments on commit f81c63d

Please sign in to comment.