Skip to content

Commit 8e081bf

Browse files
committed
fix: Clean-up Issue class: Reposition 'constructor', rename 'parseIssue', cache its result, and update the cache if 'url' changes
1 parent e656d33 commit 8e081bf

File tree

1 file changed

+41
-28
lines changed

1 file changed

+41
-28
lines changed

.github/actions/file/src/Issue.ts

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,63 @@
11
import type { Issue as IssueInput } from "./types.d.js";
22

33
export class Issue implements IssueInput {
4-
/**
5-
* Extracts owner, repository, and issue number from a GitHub issue URL.
6-
* @param issueUrl A GitHub issue URL (e.g. `https://github.com/owner/repo/issues/42`).
7-
* @returns An object with `owner`, `repository`, and `issueNumber` keys.
8-
* @throws The provided URL is unparseable due to its unexpected format.
9-
*/
10-
static parseIssueUrl(issueUrl: string): {
4+
#url!: string;
5+
#parsedUrl!: {
116
owner: string;
127
repository: string;
138
issueNumber: number;
14-
} {
15-
const { owner, repository, issueNumber } =
16-
/\/(?<owner>[^/]+)\/(?<repository>[^/]+)\/issues\/(?<issueNumber>\d+)(?:[/?#]|$)/.exec(
17-
issueUrl
18-
)?.groups || {};
19-
if (!owner || !repository || !issueNumber) {
20-
throw new Error(`Could not parse issue URL: ${issueUrl}`);
21-
}
22-
return { owner, repository, issueNumber: Number(issueNumber) };
23-
}
24-
25-
url: string;
9+
};
2610
nodeId: string;
2711
id: number;
2812
title: string;
2913
state?: "open" | "reopened" | "closed";
3014

15+
constructor({ url, nodeId, id, title, state }: IssueInput) {
16+
this.url = url;
17+
this.nodeId = nodeId;
18+
this.id = id;
19+
this.title = title;
20+
this.state = state;
21+
}
22+
23+
set url(newUrl: string) {
24+
this.#url = newUrl;
25+
this.#parsedUrl = this.#parseUrl();
26+
}
27+
28+
get url(): string {
29+
return this.#url;
30+
}
31+
3132
get owner(): string {
32-
return Issue.parseIssueUrl(this.url).owner;
33+
return this.#parsedUrl.owner;
3334
}
3435

3536
get repository(): string {
36-
return Issue.parseIssueUrl(this.url).repository;
37+
return this.#parsedUrl.repository;
3738
}
3839

3940
get issueNumber(): number {
40-
return Issue.parseIssueUrl(this.url).issueNumber;
41+
return this.#parsedUrl.issueNumber;
4142
}
4243

43-
constructor({ url, nodeId, id, title, state }: IssueInput) {
44-
this.url = url;
45-
this.nodeId = nodeId;
46-
this.id = id;
47-
this.title = title;
48-
this.state = state;
44+
/**
45+
* Extracts owner, repository, and issue number from the Issue instance’s GitHub issue URL.
46+
* @returns An object with `owner`, `repository`, and `issueNumber` keys.
47+
* @throws The provided URL is unparseable due to its unexpected format.
48+
*/
49+
#parseUrl(): {
50+
owner: string;
51+
repository: string;
52+
issueNumber: number;
53+
} {
54+
const { owner, repository, issueNumber } =
55+
/\/(?<owner>[^/]+)\/(?<repository>[^/]+)\/issues\/(?<issueNumber>\d+)(?:[/?#]|$)/.exec(
56+
this.#url
57+
)?.groups || {};
58+
if (!owner || !repository || !issueNumber) {
59+
throw new Error(`Could not parse issue URL: ${this.#url}`);
60+
}
61+
return { owner, repository, issueNumber: Number(issueNumber) };
4962
}
5063
}

0 commit comments

Comments
 (0)