🐛 fix: #21 correct issue caching logic

Cache was incorrectly using issue.id and not issue.number, which led to no issue requests being cached.
This commit is contained in:
Nathan Smith 2024-03-16 21:29:09 -04:00
parent 63566edd6c
commit 40247cd3e4
5 changed files with 22 additions and 9 deletions

View file

@ -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);

View file

@ -18,7 +18,6 @@ class CacheEntry<T> {
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<IssueResponse>(issue);
issueCache[issue.number] = new CacheEntry<IssueResponse>(issue);
}
}

View file

@ -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);

View file

@ -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);

View file

@ -119,6 +119,12 @@ export const DateFormat = {
}),
};
export function sleep(ms: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
export class RequestError implements Error {
name: string;
message: string;