Skip to content

Commit

Permalink
Merge pull request #108 from nathonius/feat/85/refresh-errored-tags
Browse files Browse the repository at this point in the history
Show errored tags with some info
  • Loading branch information
nathonius authored Apr 22, 2024
2 parents 14ba964 + afe8a2e commit 0a15cd5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 32 deletions.
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"obsidian": "latest",
"octokit": "^3.1.2",
"prettier": "^3.2.5",
"queue": "^7.0.0",
"tslib": "2.6.2",
"typescript": "5.3.3"
},
Expand All @@ -37,6 +36,7 @@
"@octokit/core": "^5.1.0",
"@octokit/openapi-types": "^19.1.0",
"@octokit/plugin-rest-endpoint-methods": "^10.3.0",
"@octokit/request": "^8.2.0"
"@octokit/request": "^8.2.0",
"queue": "^7.0.0"
}
}
17 changes: 11 additions & 6 deletions src/github/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ export class GitHubApi {
private static rateLimitReset: Date | null = null;
private static q = new Queue({ autostart: true, concurrency: 1 });

public async queueRequest(config: RequestUrlParam, token?: string): Promise<RequestUrlResponse> {
public queueRequest(config: RequestUrlParam, token?: string): Promise<RequestUrlResponse> {
// Responses we (probably) have cached will skip the queue
if (getCache().get(config)) {
return this.githubRequest(config, token);
}

const { resolve, promise } = promiseWithResolvers<RequestUrlResponse>();
const { resolve, reject, promise } = promiseWithResolvers<RequestUrlResponse>();
GitHubApi.q.push(() => {
return this.githubRequest(config, token).then((result) => {
resolve(result);
});
return this.githubRequest(config, token)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
return promise;
}
Expand Down Expand Up @@ -175,7 +179,8 @@ export class GitHubApi {

return response;
} catch (err) {
throw new RequestError(err as Error);
logger.debug(err);
return Promise.reject(new RequestError(err as Error));
}
}

Expand Down
61 changes: 41 additions & 20 deletions src/inline/inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { parseUrl } from "../github/url-parse";
import { setIcon } from "obsidian";
import { setIssueIcon, setPRIcon, setPRMergeableIcon } from "src/icon";
import { PluginSettings } from "src/plugin";
import { RequestError } from "src/util";

interface TagConfig {
icon: HTMLSpanElement;
Expand Down Expand Up @@ -34,7 +35,7 @@ export function createTag(href: string): HTMLAnchorElement {
}

if (parsedUrl.issue !== undefined) {
createIssueSection(config, parsedUrl);
createIssueSection(config, parsedUrl, container);
} else if (parsedUrl.pr !== undefined) {
createPullRequestSection(config, parsedUrl, container);
}
Expand Down Expand Up @@ -84,7 +85,7 @@ function createRepoSection(config: TagConfig, parsedUrl: ParsedUrl) {
}
}

function createIssueSection(config: TagConfig, parsedUrl: ParsedUrl) {
function createIssueSection(config: TagConfig, parsedUrl: ParsedUrl, container: HTMLAnchorElement) {
if (parsedUrl.issue === undefined) {
return;
}
Expand All @@ -95,17 +96,21 @@ function createIssueSection(config: TagConfig, parsedUrl: ParsedUrl) {
});
config.sections.push(issueContainer);
if (parsedUrl.org && parsedUrl.repo) {
getIssue(parsedUrl.org, parsedUrl.repo, parsedUrl.issue).then((issue) => {
if (issue.title) {
const status = getIssueStatus(issue);
setIssueIcon(config.icon, status);
issueContainer.setText(issue.title);
}
});
getIssue(parsedUrl.org, parsedUrl.repo, parsedUrl.issue)
.then((issue) => {
if (issue.title) {
const status = getIssueStatus(issue);
setIssueIcon(config.icon, status);
issueContainer.setText(issue.title);
}
})
.catch((err) => {
createErrorSection(config, container, err);
});
}
}

function createPullRequestSection(config: TagConfig, parsedUrl: ParsedUrl, tag: HTMLElement) {
function createPullRequestSection(config: TagConfig, parsedUrl: ParsedUrl, container: HTMLAnchorElement) {
if (parsedUrl.pr === undefined) {
return;
}
Expand All @@ -116,28 +121,44 @@ function createPullRequestSection(config: TagConfig, parsedUrl: ParsedUrl, tag:
});
config.sections.push(prContainer);
if (parsedUrl.org && parsedUrl.repo) {
getPullRequest(parsedUrl.org, parsedUrl.repo, parsedUrl.pr).then((pr) => {
if (pr.title) {
const status = getPRStatus(pr);
setPRIcon(config.icon, status);
prContainer.setText(pr.title);
}
createPullRequestMergeableSection(config, pr, tag);
});
getPullRequest(parsedUrl.org, parsedUrl.repo, parsedUrl.pr)
.then((pr) => {
if (pr.title) {
const status = getPRStatus(pr);
setPRIcon(config.icon, status);
prContainer.setText(pr.title);
}
createPullRequestMergeableSection(config, pr, container);
})
.catch((err) => {
createErrorSection(config, container, err);
});
}
}

/**
* Note that this function is called AFTER the tag has been built, so it adds itself to the dom.
*/
function createPullRequestMergeableSection(config: TagConfig, pullRequest: PullResponse, tag: HTMLElement): void {
function createPullRequestMergeableSection(config: TagConfig, pullRequest: PullResponse, container: HTMLElement): void {
if (!PluginSettings.tagShowPRMergeable || pullRequest.mergeable === null) {
return;
}
const mergeableIcon = createSpan({ cls: ["github-link-inline-pr-mergeable-icon", "github-link-inline-icon"] });
setPRMergeableIcon(mergeableIcon, pullRequest.mergeable);
config.sections.push(mergeableIcon);
tag.appendChild(createTagSection(mergeableIcon));
container.appendChild(createTagSection(mergeableIcon));
}

function createErrorSection(config: TagConfig, container: HTMLAnchorElement, error: unknown) {
const errorIcon = createSpan({
cls: ["github-link-inline-error-icon", "github-link-inline-icon"],
});
setIcon(errorIcon, "lucide-alert-triangle");
if (error instanceof RequestError) {
errorIcon.ariaLabel = error.message;
}
config.sections.push(errorIcon);
container.appendChild(createTagSection(errorIcon));
}

export async function InlineRenderer(el: HTMLElement) {
Expand Down
5 changes: 5 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ body.theme-dark {
background-color: var(--gh-color-danger-subtle);
border-color: var(--gh-color-danger-emphasis);
}
.github-link-inline-section:has(> .github-link-inline-error-icon) {
color: var(--gh-color-closed-fg);
background-color: var(--gh-color-danger-subtle);
border-color: var(--gh-color-danger-emphasis);
}

.github-link-inline-section:has(> .github-link-inline-repo) {
background-color: var(--gh-color-accent-subtle);
Expand Down

0 comments on commit 0a15cd5

Please sign in to comment.