Skip to content

Commit a73321f

Browse files
committed
refactor: move github comment code in a separate module
fix: fix a typo introduce in the previous fix...
1 parent f88da4a commit a73321f

File tree

3 files changed

+97
-42
lines changed

3 files changed

+97
-42
lines changed

dist/index.js

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13856,6 +13856,39 @@ async function isMainBranch(octokit, owner, repo) {
1385613856
module.exports = { isBranch, isMainBranch };
1385713857

1385813858

13859+
/***/ }),
13860+
13861+
/***/ 427:
13862+
/***/ ((module) => {
13863+
13864+
async function addComment(octokit, owner, repo, issue_number, body) {
13865+
return await octokit.rest.issues.createComment({
13866+
owner,
13867+
repo,
13868+
issue_number,
13869+
body,
13870+
});
13871+
}
13872+
13873+
async function deleteExistingComments(octokit, owner, repo, issue_number) {
13874+
let comments = await octokit.rest.issues.listComments({
13875+
owner,
13876+
repo,
13877+
issue_number,
13878+
});
13879+
13880+
for (const comment of comments.data) {
13881+
if (comment.body.includes(MARKER)) {
13882+
await octokit.rest.issues.deleteComment({
13883+
comment_id: comment.id,
13884+
});
13885+
}
13886+
}
13887+
}
13888+
13889+
module.exports = { addComment, deleteExistingComments };
13890+
13891+
1385913892
/***/ }),
1386013893

1386113894
/***/ 109:
@@ -14201,6 +14234,7 @@ const { isBranch, isMainBranch } = __nccwpck_require__(6381);
1420114234
const { getShieldURL, getJSONBadge } = __nccwpck_require__(3673);
1420214235
const { average } = __nccwpck_require__(8978);
1420314236
const { renderDiff } = __nccwpck_require__(1397);
14237+
const { addComment, deleteExistingComments } = __nccwpck_require__(427);
1420414238

1420514239
const { context } = github;
1420614240

@@ -14209,7 +14243,6 @@ const MARKER = "<!-- This comment was produced by coverage-diff-action -->";
1420914243
const WIKI_PATH = path.join(process.env.GITHUB_WORKSPACE, "wiki");
1421014244

1421114245
async function run() {
14212-
const { repo, owner } = context.repo;
1421314246
const githubToken = core.getInput("github-token");
1421414247
const baseSummaryFilename = core.getInput("base-summary-filename");
1421514248
const coverageFilename = core.getInput("coverage-filename");
@@ -14231,7 +14264,10 @@ async function run() {
1423114264
0
1423214265
);
1423314266

14234-
if (isBranch() && (await isMainBranch(octokit, owner, repo))) {
14267+
if (
14268+
isBranch() &&
14269+
(await isMainBranch(octokit, context.repo.owner, context.repo.repo))
14270+
) {
1423514271
core.info("Running on default branch");
1423614272
const BadgeEnabled = core.getBooleanInput("badge-enabled");
1423714273
const badgeFilename = core.getInput("badge-filename");
@@ -14266,7 +14302,7 @@ async function run() {
1426614302
return;
1426714303
}
1426814304

14269-
const issue_number = context?.payload?.pull_request?.issue_number;
14305+
const issue_number = context?.payload?.pull_request?.number;
1427014306
const allowedToFail = core.getBooleanInput("allowed-to-fail");
1427114307
const base = JSON.parse(
1427214308
await readFile(path.join(WIKI_PATH, baseSummaryFilename), "utf8")
@@ -14275,27 +14311,22 @@ async function run() {
1427514311
const diff = coverageDiff.diff(base, head);
1427614312

1427714313
if (issue_number) {
14278-
let comments = await octokit.rest.issues.listComments({
14279-
issue_number,
14280-
});
14281-
github;
14282-
14283-
for (const comment of comments.data) {
14284-
if (comment.body.includes(MARKER)) {
14285-
await octokit.rest.issues.deleteComment({
14286-
...context.repo,
14287-
comment_id: comment.id,
14288-
});
14289-
}
14290-
}
14314+
await deleteExistingComments(
14315+
octokit,
14316+
context.repo.owner,
14317+
context.repo.repo,
14318+
issue_number
14319+
);
1429114320

1429214321
core.info("Add a comment with the diff coverage report");
14293-
await octokit.rest.issues.createComment({
14294-
...context.repo,
14322+
await addComment(
14323+
octokit,
14324+
context.repo.owner,
14325+
context.repo.repo,
1429514326
issue_number,
14296-
body: `${renderDiff(base, head, diff, { allowedToFail })}
14297-
${MARKER}`,
14298-
});
14327+
`${renderDiff(base, head, diff, { allowedToFail })}
14328+
${MARKER}`
14329+
);
1429914330
} else {
1430014331
core.info(diff.results);
1430114332
}

src/comment.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
async function addComment(octokit, owner, repo, issue_number, body) {
2+
return await octokit.rest.issues.createComment({
3+
owner,
4+
repo,
5+
issue_number,
6+
body,
7+
});
8+
}
9+
10+
async function deleteExistingComments(octokit, owner, repo, issue_number) {
11+
let comments = await octokit.rest.issues.listComments({
12+
owner,
13+
repo,
14+
issue_number,
15+
});
16+
17+
for (const comment of comments.data) {
18+
if (comment.body.includes(MARKER)) {
19+
await octokit.rest.issues.deleteComment({
20+
comment_id: comment.id,
21+
});
22+
}
23+
}
24+
}
25+
26+
module.exports = { addComment, deleteExistingComments };

src/index.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const { isBranch, isMainBranch } = require("./branch");
1010
const { getShieldURL, getJSONBadge } = require("./badge");
1111
const { average } = require("./math");
1212
const { renderDiff } = require("./render");
13+
const { addComment, deleteExistingComments } = require("./comment");
1314

1415
const { context } = github;
1516

@@ -18,7 +19,6 @@ const MARKER = "<!-- This comment was produced by coverage-diff-action -->";
1819
const WIKI_PATH = path.join(process.env.GITHUB_WORKSPACE, "wiki");
1920

2021
async function run() {
21-
const { repo, owner } = context.repo;
2222
const githubToken = core.getInput("github-token");
2323
const baseSummaryFilename = core.getInput("base-summary-filename");
2424
const coverageFilename = core.getInput("coverage-filename");
@@ -40,7 +40,10 @@ async function run() {
4040
0
4141
);
4242

43-
if (isBranch() && (await isMainBranch(octokit, owner, repo))) {
43+
if (
44+
isBranch() &&
45+
(await isMainBranch(octokit, context.repo.owner, context.repo.repo))
46+
) {
4447
core.info("Running on default branch");
4548
const BadgeEnabled = core.getBooleanInput("badge-enabled");
4649
const badgeFilename = core.getInput("badge-filename");
@@ -75,7 +78,7 @@ async function run() {
7578
return;
7679
}
7780

78-
const issue_number = context?.payload?.pull_request?.issue_number;
81+
const issue_number = context?.payload?.pull_request?.number;
7982
const allowedToFail = core.getBooleanInput("allowed-to-fail");
8083
const base = JSON.parse(
8184
await readFile(path.join(WIKI_PATH, baseSummaryFilename), "utf8")
@@ -84,27 +87,22 @@ async function run() {
8487
const diff = coverageDiff.diff(base, head);
8588

8689
if (issue_number) {
87-
let comments = await octokit.rest.issues.listComments({
88-
issue_number,
89-
});
90-
github;
91-
92-
for (const comment of comments.data) {
93-
if (comment.body.includes(MARKER)) {
94-
await octokit.rest.issues.deleteComment({
95-
...context.repo,
96-
comment_id: comment.id,
97-
});
98-
}
99-
}
90+
await deleteExistingComments(
91+
octokit,
92+
context.repo.owner,
93+
context.repo.repo,
94+
issue_number
95+
);
10096

10197
core.info("Add a comment with the diff coverage report");
102-
await octokit.rest.issues.createComment({
103-
...context.repo,
98+
await addComment(
99+
octokit,
100+
context.repo.owner,
101+
context.repo.repo,
104102
issue_number,
105-
body: `${renderDiff(base, head, diff, { allowedToFail })}
106-
${MARKER}`,
107-
});
103+
`${renderDiff(base, head, diff, { allowedToFail })}
104+
${MARKER}`
105+
);
108106
} else {
109107
core.info(diff.results);
110108
}

0 commit comments

Comments
 (0)