diff --git a/src/github/api.ts b/src/github/api.ts index e78dec6..4a23ebf 100644 --- a/src/github/api.ts +++ b/src/github/api.ts @@ -14,6 +14,7 @@ import type { RequestUrlParam, RequestUrlResponse } from "obsidian"; import { RequestError } from "src/util"; import { requestUrl } from "obsidian"; +import { Logger } from "src/plugin"; const baseApi = "https://api.github.com"; @@ -35,7 +36,20 @@ export async function githubRequest(config: RequestUrlParam, token?: string): Pr config.headers.Authorization = `Bearer ${token}`; } try { + Logger.debug(`Request: ${config.url}`); + Logger.debug(config); const response = await requestUrl(config); + Logger.debug(`Response:`); + Logger.debug(response); + + // Handle rate limit + const retryAfterSeconds = response.headers["retry-after"]; + if (retryAfterSeconds) { + Logger.warn(`Got retry-after header with value ${retryAfterSeconds}`); + await sleep(parseInt(retryAfterSeconds) * 1000); + return githubRequest(config, token); + } + return response; } catch (err) { throw new RequestError(err as Error); diff --git a/src/github/cache.ts b/src/github/cache.ts index ce7c875..08f6979 100644 --- a/src/github/cache.ts +++ b/src/github/cache.ts @@ -18,7 +18,6 @@ class CacheEntry { get expired(): boolean { const expiry = this.created.getTime() + this.ttl * 60 * 1000; - return new Date().getTime() > expiry; } } @@ -60,13 +59,13 @@ export class Cache { setIssue(org: string, repo: string, issue: IssueResponse): void { const issueCache = this.getRepoCache(org, repo).issueCache; - const existingCache = issueCache[issue.id]; + const existingCache = issueCache[issue.number]; if (existingCache) { const now = new Date(); existingCache.created = now; existingCache.value = issue; } else { - issueCache[issue.id] = new CacheEntry(issue); + issueCache[issue.number] = new CacheEntry(issue); } } diff --git a/src/inline/inline.ts b/src/inline/inline.ts index 7a63dbb..b2979ff 100644 --- a/src/inline/inline.ts +++ b/src/inline/inline.ts @@ -4,7 +4,6 @@ import { getIssue, getPullRequest } from "../github/github"; import { parseUrl } from "../github/url-parse"; import { setIcon } from "obsidian"; import { setIssueIcon, setPRIcon } from "src/icon"; -import { Logger } from "src/plugin"; export async function createTag(href: string) { const parsedUrl = parseUrl(href); @@ -36,8 +35,6 @@ export async function createTag(href: string) { // Get issue info if (parsedUrl.issue !== undefined) { const issue = await getIssue(parsedUrl.org, parsedUrl.repo, parsedUrl.issue); - Logger.debug("Rendering tag for issue:"); - Logger.debug(issue); if (issue.title) { const status = getIssueStatus(issue); setIssueIcon(icon, status); @@ -51,8 +48,6 @@ export async function createTag(href: string) { // Get PR info if (parsedUrl.pr !== undefined) { const pull = await getPullRequest(parsedUrl.org, parsedUrl.repo, parsedUrl.pr); - Logger.debug("Rendering tag for pull request:"); - Logger.debug(pull); if (pull.title) { const status = getPRStatus(pull); setPRIcon(icon, status); diff --git a/src/plugin.ts b/src/plugin.ts index 46dff81..9e5036d 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -14,7 +14,6 @@ export class GithubLinkPlugin extends Plugin { async onload() { PluginSettings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); Logger = verboseFactory(PluginSettings.logLevel); - // Logger.debug(getIconIds()); this.addSettingTab(new GithubLinkPluginSettingsTab(this.app, this)); this.registerMarkdownPostProcessor(InlineRenderer); diff --git a/src/util.ts b/src/util.ts index 886f963..063d6cc 100644 --- a/src/util.ts +++ b/src/util.ts @@ -119,6 +119,12 @@ export const DateFormat = { }), }; +export function sleep(ms: number): Promise { + return new Promise((resolve) => { + setTimeout(resolve, ms); + }); +} + export class RequestError implements Error { name: string; message: string;