Skip to content

Commit 97d09ce

Browse files
committed
move githubApiClient into the separate class. Implement tests
1 parent 1b1203b commit 97d09ce

File tree

9 files changed

+224
-99
lines changed

9 files changed

+224
-99
lines changed

dist/index.js

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,46 @@ const parseInputs = () => {
1818
return {
1919
rootIssue,
2020
accessToken: "",
21+
sectionTitle: "Spec graph",
2122
};
2223
};
2324
exports.parseInputs = parseInputs;
2425

2526

27+
/***/ }),
28+
29+
/***/ 1886:
30+
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {
31+
32+
"use strict";
33+
34+
Object.defineProperty(exports, "__esModule", ({ value: true }));
35+
exports.GitHubApiClient = void 0;
36+
const github_1 = __nccwpck_require__(5438);
37+
class GitHubApiClient {
38+
constructor(accessToken) {
39+
this.client = (0, github_1.getOctokit)(accessToken);
40+
}
41+
async getIssue(issueRef) {
42+
const response = await this.client.rest.issues.get({
43+
owner: issueRef.repoOwner,
44+
repo: issueRef.repoName,
45+
issue_number: issueRef.issueNumber,
46+
});
47+
return response.data;
48+
}
49+
async updateIssueContent(issueRef, body) {
50+
await this.client.rest.issues.update({
51+
owner: issueRef.repoOwner,
52+
repo: issueRef.repoName,
53+
issue_number: issueRef.issueNumber,
54+
body: body,
55+
});
56+
}
57+
}
58+
exports.GitHubApiClient = GitHubApiClient;
59+
60+
2661
/***/ }),
2762

2863
/***/ 3289:
@@ -32,7 +67,7 @@ exports.parseInputs = parseInputs;
3267

3368
Object.defineProperty(exports, "__esModule", ({ value: true }));
3469
exports.GraphBuilder = void 0;
35-
const mermaid_node_1 = __nccwpck_require__(7537);
70+
const mermaid_node_1 = __nccwpck_require__(235);
3671
class GraphBuilder {
3772
constructor() {
3873
this.nodes = new Map();
@@ -92,28 +127,8 @@ exports.GraphBuilder = GraphBuilder;
92127

93128
Object.defineProperty(exports, "__esModule", ({ value: true }));
94129
exports.IssueContentParser = void 0;
95-
const github_1 = __nccwpck_require__(5438);
96130
const utils_1 = __nccwpck_require__(918);
97131
class IssueContentParser {
98-
constructor(accessToken) {
99-
this.client = (0, github_1.getOctokit)(accessToken);
100-
}
101-
async getIssue(issueRef) {
102-
const response = await this.client.rest.issues.get({
103-
owner: issueRef.repoOwner,
104-
repo: issueRef.repoName,
105-
issue_number: issueRef.issueNumber,
106-
});
107-
return response.data;
108-
}
109-
async updateIssueContent(issueRef, body) {
110-
await this.client.rest.issues.update({
111-
owner: issueRef.repoOwner,
112-
repo: issueRef.repoName,
113-
issue_number: issueRef.issueNumber,
114-
body: body,
115-
});
116-
}
117132
extractIssueTasklist(issue) {
118133
var _a;
119134
const issueContent = (_a = issue.body) !== null && _a !== void 0 ? _a : "";
@@ -195,20 +210,22 @@ var __importStar = (this && this.__importStar) || function (mod) {
195210
Object.defineProperty(exports, "__esModule", ({ value: true }));
196211
const core = __importStar(__nccwpck_require__(2186));
197212
const config_1 = __nccwpck_require__(88);
213+
const github_api_client_1 = __nccwpck_require__(1886);
198214
const graph_builder_1 = __nccwpck_require__(3289);
199215
const issue_content_parser_1 = __nccwpck_require__(8352);
200-
const mermaid_node_1 = __nccwpck_require__(7537);
216+
const mermaid_node_1 = __nccwpck_require__(235);
201217
const mermaid_render_1 = __nccwpck_require__(6288);
202218
const run = async () => {
203219
try {
204220
const config = (0, config_1.parseInputs)();
205-
const issueContentParser = new issue_content_parser_1.IssueContentParser(config.accessToken);
221+
const githubApiClient = new github_api_client_1.GitHubApiClient(config.accessToken);
222+
const issueContentParser = new issue_content_parser_1.IssueContentParser();
206223
const mermaidRender = new mermaid_render_1.MermaidRender();
207-
const rootIssue = await issueContentParser.getIssue(config.rootIssue);
224+
const rootIssue = await githubApiClient.getIssue(config.rootIssue);
208225
const rootIssueTasklist = issueContentParser.extractIssueTasklist(rootIssue);
209226
const graphBuilder = new graph_builder_1.GraphBuilder();
210227
for (const issueRef of rootIssueTasklist) {
211-
const issue = await issueContentParser.getIssue(issueRef);
228+
const issue = await githubApiClient.getIssue(issueRef);
212229
const issueDetails = mermaid_node_1.MermaidNode.fromGitHubIssue(issue);
213230
graphBuilder.addIssue(issueRef, issueDetails);
214231
const issueDependencies = issueContentParser.extractIssueDependencies(issue);
@@ -217,10 +234,13 @@ const run = async () => {
217234
const graph = graphBuilder.getGraph();
218235
const renderedContent = mermaidRender.render(graph);
219236
console.log(renderedContent);
237+
const updatedIssueContent = issueContentParser.replaceIssueContent(rootIssue, config.sectionTitle, renderedContent);
238+
await githubApiClient.updateIssueContent(config.rootIssue, updatedIssueContent);
220239
}
221240
catch (error) {
222241
if (error instanceof Error) {
223242
core.setFailed(error.message);
243+
throw error;
224244
}
225245
}
226246
};
@@ -229,7 +249,7 @@ run();
229249

230250
/***/ }),
231251

232-
/***/ 7537:
252+
/***/ 235:
233253
/***/ ((__unused_webpack_module, exports) => {
234254

235255
"use strict";
@@ -256,10 +276,10 @@ class MermaidNode {
256276
return "notstarted";
257277
}
258278
static getStartNode() {
259-
return new MermaidNode('start', 'Start', "notstarted");
279+
return new MermaidNode("start", "Start", "notstarted");
260280
}
261281
static getFinishNode() {
262-
return new MermaidNode('finish', 'Finish', "notstarted");
282+
return new MermaidNode("finish", "Finish", "notstarted");
263283
}
264284
}
265285
exports.MermaidNode = MermaidNode;
@@ -8074,7 +8094,7 @@ conversions["RegExp"] = function (V, opts) {
80748094

80758095
/***/ }),
80768096

8077-
/***/ 1654:
8097+
/***/ 7537:
80788098
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {
80798099

80808100
"use strict";
@@ -8290,7 +8310,7 @@ exports.implementation = class URLImpl {
82908310

82918311
const conversions = __nccwpck_require__(4886);
82928312
const utils = __nccwpck_require__(3185);
8293-
const Impl = __nccwpck_require__(1654);
8313+
const Impl = __nccwpck_require__(7537);
82948314

82958315
const impl = utils.implSymbol;
82968316

src/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { parseIssueUrl } from "./utils";
33

44
export interface Config {
55
rootIssue: GitHubIssueReference;
6+
sectionTitle: string;
67
accessToken: string;
78
}
89

@@ -16,5 +17,6 @@ export const parseInputs = (): Config => {
1617
return {
1718
rootIssue,
1819
accessToken: "",
20+
sectionTitle: "Spec graph",
1921
};
2022
};

src/github-api-client.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { getOctokit } from "@actions/github";
2+
import type { GitHub } from "@actions/github/lib/utils";
3+
import { GitHubIssue, GitHubIssueReference } from "./models";
4+
5+
export class GitHubApiClient {
6+
private readonly client: InstanceType<typeof GitHub>;
7+
8+
constructor(accessToken: string) {
9+
this.client = getOctokit(accessToken);
10+
}
11+
12+
public async getIssue(issueRef: GitHubIssueReference): Promise<GitHubIssue> {
13+
const response = await this.client.rest.issues.get({
14+
owner: issueRef.repoOwner,
15+
repo: issueRef.repoName,
16+
issue_number: issueRef.issueNumber,
17+
});
18+
19+
return response.data;
20+
}
21+
22+
public async updateIssueContent(issueRef: GitHubIssueReference, body: string): Promise<void> {
23+
await this.client.rest.issues.update({
24+
owner: issueRef.repoOwner,
25+
repo: issueRef.repoName,
26+
issue_number: issueRef.issueNumber,
27+
body: body,
28+
});
29+
}
30+
}

src/graph-builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class GraphBuilder {
5656
const vertices = graphNodes.map(x => x.value).filter((x): x is MermaidNode => x !== null);
5757

5858
const startNode = MermaidNode.getStartNode();
59-
const finishNode = MermaidNode.getFinishNode()
59+
const finishNode = MermaidNode.getFinishNode();
6060

6161
const edgesFromStartNode: NullablePartial<GraphEdge>[] = graphNodes
6262
.filter(x => x.predecessors.length === 0)

src/issue-content-parser.ts

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,20 @@
1-
import { getOctokit } from "@actions/github";
2-
import type { GitHub } from "@actions/github/lib/utils";
31
import { GitHubIssue, GitHubIssueReference } from "./models";
42
import { parseIssueUrl } from "./utils";
53

64
export class IssueContentParser {
7-
private readonly client: InstanceType<typeof GitHub>;
8-
9-
constructor(accessToken: string) {
10-
this.client = getOctokit(accessToken);
11-
}
12-
13-
public async getIssue(issueRef: GitHubIssueReference): Promise<GitHubIssue> {
14-
const response = await this.client.rest.issues.get({
15-
owner: issueRef.repoOwner,
16-
repo: issueRef.repoName,
17-
issue_number: issueRef.issueNumber,
18-
});
19-
20-
return response.data;
21-
}
22-
23-
public async updateIssueContent(issueRef: GitHubIssueReference, body: string): Promise<void> {
24-
await this.client.rest.issues.update({
25-
owner: issueRef.repoOwner,
26-
repo: issueRef.repoName,
27-
issue_number: issueRef.issueNumber,
28-
body: body,
29-
});
30-
}
31-
325
public extractIssueTasklist(issue: GitHubIssue): GitHubIssueReference[] {
33-
const issueContent = issue.body ?? "";
34-
const issueContentLines = issueContent.split("\n");
6+
const contentLines = issue.body?.split("\n") ?? [];
357

36-
return issueContentLines
8+
return contentLines
379
.filter(x => x.startsWith("- [ ] "))
3810
.map(x => parseIssueUrl(x))
3911
.filter((x): x is GitHubIssueReference => x !== null);
4012
}
4113

4214
public extractIssueDependencies(issue: GitHubIssue): GitHubIssueReference[] {
43-
const issueContent = issue.body ?? "";
44-
const issueContentLines = issueContent.split("\n");
15+
const contentLines = issue.body?.split("\n") ?? [];
4516

46-
return issueContentLines
17+
return contentLines
4718
.filter(x => x.startsWith("Depends on"))
4819
.map(x => x.split(",").map(y => parseIssueUrl(y)))
4920
.flat()

src/main.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as core from "@actions/core";
22
import { parseInputs } from "./config";
3+
import { GitHubApiClient } from "./github-api-client";
34
import { GraphBuilder } from "./graph-builder";
45
import { IssueContentParser } from "./issue-content-parser";
56
import { MermaidNode } from "./mermaid-node";
@@ -8,15 +9,16 @@ import { MermaidRender } from "./mermaid-render";
89
const run = async (): Promise<void> => {
910
try {
1011
const config = parseInputs();
11-
const issueContentParser = new IssueContentParser(config.accessToken);
12+
const githubApiClient = new GitHubApiClient(config.accessToken);
13+
const issueContentParser = new IssueContentParser();
1214
const mermaidRender = new MermaidRender();
1315

14-
const rootIssue = await issueContentParser.getIssue(config.rootIssue);
16+
const rootIssue = await githubApiClient.getIssue(config.rootIssue);
1517
const rootIssueTasklist = issueContentParser.extractIssueTasklist(rootIssue);
1618

1719
const graphBuilder = new GraphBuilder();
1820
for (const issueRef of rootIssueTasklist) {
19-
const issue = await issueContentParser.getIssue(issueRef);
21+
const issue = await githubApiClient.getIssue(issueRef);
2022
const issueDetails = MermaidNode.fromGitHubIssue(issue);
2123
graphBuilder.addIssue(issueRef, issueDetails);
2224

@@ -26,11 +28,18 @@ const run = async (): Promise<void> => {
2628

2729
const graph = graphBuilder.getGraph();
2830
const renderedContent = mermaidRender.render(graph);
29-
3031
console.log(renderedContent);
32+
33+
const updatedIssueContent = issueContentParser.replaceIssueContent(
34+
rootIssue,
35+
config.sectionTitle,
36+
renderedContent
37+
);
38+
await githubApiClient.updateIssueContent(config.rootIssue, updatedIssueContent);
3139
} catch (error) {
3240
if (error instanceof Error) {
3341
core.setFailed(error.message);
42+
throw error;
3443
}
3544
}
3645
};

src/mermaid-node.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class MermaidNode {
1515
`issue${issue.id}`,
1616
issue.title,
1717
MermaidNode.getNodeStatusFromGitHubIssue(issue),
18-
issue.html_url,
18+
issue.html_url
1919
);
2020
}
2121

@@ -32,10 +32,10 @@ export class MermaidNode {
3232
}
3333

3434
public static getStartNode(): MermaidNode {
35-
return new MermaidNode('start', 'Start', "notstarted");
35+
return new MermaidNode("start", "Start", "notstarted");
3636
}
3737

3838
public static getFinishNode(): MermaidNode {
39-
return new MermaidNode('finish', 'Finish', "notstarted");
39+
return new MermaidNode("finish", "Finish", "notstarted");
4040
}
4141
}

0 commit comments

Comments
 (0)