Skip to content

Commit 49c66ef

Browse files
No more any, code improvements
1 parent 0447227 commit 49c66ef

File tree

6 files changed

+62
-63
lines changed

6 files changed

+62
-63
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
},
1515
"devDependencies": {
1616
"@neoconfetti/svelte": "^2.2.1",
17+
"@octokit/graphql-schema": "^15.25.0",
1718
"@shikijs/rehype": "^1.22.0",
1819
"@sveltejs/adapter-vercel": "^5.4.5",
1920
"@sveltejs/kit": "^2.7.2",

pnpm-lock.yaml

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/components/Steps.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
type Props = {
66
class?: string | undefined | null;
77
children?: Snippet;
8-
[key: string]: any;
8+
[key: string]: unknown;
99
};
1010
1111
let { class: className = undefined, children, ...rest }: Props = $props();

src/lib/components/renderers/BodyRenderer.svelte

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,19 @@
1313
const repo = $page.data.repo;
1414
1515
$effect(() => {
16-
if (data) {
17-
let replaced = data.innerHTML;
18-
const issuesCandidates = replaced.matchAll(/ #(\d+)/g) || [];
19-
for (let candidate of issuesCandidates) {
20-
const wholeMatch = candidate[0];
21-
const matchedGroup = candidate[1];
22-
if (!wholeMatch || !matchedGroup) continue;
23-
replaced = replaced.replace(
24-
wholeMatch,
25-
` <a href="https://github.com/${org}/${repo}/issues/${matchedGroup}">${wholeMatch.trim()}</a>`
26-
);
27-
}
28-
data.innerHTML = replaced;
16+
if (!data) return;
17+
let replaced = data.innerHTML;
18+
const issuesCandidates = replaced.matchAll(/ #(\d+)/g) || [];
19+
for (let candidate of issuesCandidates) {
20+
const wholeMatch = candidate[0];
21+
const matchedGroup = candidate[1];
22+
if (!wholeMatch || !matchedGroup) continue;
23+
replaced = replaced.replace(
24+
wholeMatch,
25+
` <a href="https://github.com/${org}/${repo}/issues/${matchedGroup}">${wholeMatch.trim()}</a>`
26+
);
2927
}
28+
data.innerHTML = replaced;
3029
});
3130
</script>
3231

src/routes/[pullOrIssue=poi]/[org]/[repo]/[id=number]/+page.svelte

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
<script lang="ts">
2+
import type { Issue, Repository } from "@octokit/graphql-schema";
23
import { LoaderCircle } from "lucide-svelte";
34
import { getOctokit } from "$lib/octokit";
4-
import type { Issues, LinkedEntity, Pulls } from "./types";
5+
import type { Issues, Pulls } from "./types";
56
import PageRenderer from "./PageRenderer.svelte";
67
78
let { data } = $props();
89
let { org: owner, id, repo, pullOrIssue } = $derived(data);
910
1011
const octokit = getOctokit();
1112
12-
async function linkedIssuesForPR(
13-
owner: string,
14-
repo: string,
15-
pr: number
16-
): Promise<LinkedEntity[]> {
13+
async function linkedIssuesForPR(owner: string, repo: string, pr: number) {
1714
return octokit
18-
.graphql(
15+
.graphql<{ repository: Repository }>(
1916
`
2017
query closingIssues($number: Int!, $owner: String!, $repo: String!) {
2118
repository(owner: $owner, name: $repo) {
@@ -42,15 +39,16 @@
4239
number: pr
4340
}
4441
)
45-
.then(
46-
/* eslint-disable @typescript-eslint/no-explicit-any */ (response: any) =>
47-
response.repository.pullRequest.closingIssuesReferences.nodes
48-
)
49-
.catch(() => []);
42+
.then(({ repository }) => {
43+
const n = repository.pullRequest?.closingIssuesReferences?.nodes;
44+
if (!n) return [] as Issue[];
45+
return n.filter(Boolean);
46+
})
47+
.catch(() => [] as Issue[]);
5048
}
5149
5250
// PR issues or issue PRs
53-
let linkedPRsOrIssues = $state<LinkedEntity[]>();
51+
let linkedPRsOrIssues = $state<(Issue | Awaited<ReturnType<Pulls["get"]>>["data"])[]>();
5452
5553
// PR
5654
let prInfo = $state<{
@@ -123,10 +121,7 @@
123121
124122
// Fetch closing issues
125123
linkedIssuesForPR(owner, repo, id).then(response => (linkedPRsOrIssues = response));
126-
}
127-
});
128-
$effect(() => {
129-
if (pullOrIssue === "issues") {
124+
} else {
130125
linkedPRsOrIssues = [];
131126
prInfo = {
132127
info: undefined,
@@ -172,30 +167,29 @@
172167
.then(async crEvents => {
173168
const prEvents = [];
174169
for (let event of crEvents) {
175-
const anyEvent = event as any;
170+
if (!("source" in event)) continue;
171+
if (!event.source.issue) continue;
176172
const doesPRExist = await octokit.rest.pulls
177173
.get({
178174
owner,
179175
repo,
180-
pull_number: anyEvent.source.issue.number
176+
pull_number: event.source.issue.number
181177
})
182178
.then(() => true)
183179
.catch(() => false);
184180
if (!doesPRExist) continue;
185181
186-
const containedInPr = await linkedIssuesForPR(
187-
owner,
188-
repo,
189-
anyEvent.source.issue.number
190-
);
182+
const containedInPr = await linkedIssuesForPR(owner, repo, event.source.issue.number);
191183
if (containedInPr.map(pr => pr.number).includes(id)) {
192184
prEvents.push(event);
193185
}
194186
}
195187
return prEvents;
196188
})
197189
.then(prEvents => {
198-
prsToFetch = prEvents.map(event => (event as any).source.issue.number);
190+
prsToFetch = prEvents
191+
.map(event => event.source.issue?.number || -1)
192+
.filter(pr => pr !== -1);
199193
})
200194
.catch(() => (linkedPRsOrIssues = []));
201195
}
@@ -219,20 +213,8 @@
219213
if (linkedPRsOrIssues.map(i => i.number).includes(data.number)) {
220214
continue;
221215
}
222-
linkedPRsOrIssues.push({
223-
title: data.title,
224-
author: {
225-
login: data.user.login,
226-
avatarUrl: data.user.avatar_url
227-
},
228-
body: data.body ?? "",
229-
createdAt: data.created_at,
230-
number: data.number
231-
});
216+
linkedPRsOrIssues.push(data);
232217
}
233-
linkedPRsOrIssues = linkedPRsOrIssues.sort(
234-
(a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime()
235-
);
236218
prsToFetch = prsToFetch.filter(
237219
prNumber => !prs.map(pr => pr.data.number).includes(prNumber)
238220
);
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
11
import type { Octokit } from "octokit";
22

3-
export type LinkedEntity = {
4-
createdAt: string;
5-
author: {
6-
login: string;
7-
avatarUrl: string;
8-
};
9-
number: number;
10-
body: string;
11-
title: string;
12-
};
13-
143
export type Issues = InstanceType<typeof Octokit>["rest"]["issues"];
154
export type Pulls = InstanceType<typeof Octokit>["rest"]["pulls"];

0 commit comments

Comments
 (0)