Skip to content

Commit 1e2f2a6

Browse files
committed
feat: delete old linter comments
1 parent b67088a commit 1e2f2a6

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ inputs:
1515
comment:
1616
description: Add a lint report as a comment on the PR
1717
default: true
18+
delete_comment:
19+
description: Whether to scrub old lint reports from comments
20+
default: true
1821

1922
runs:
2023
using: node12

dist/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,7 @@ async function run() {
15381538
try {
15391539
const token = core.getInput('token', { required: true });
15401540
const createComment = core.getBooleanInput('comment');
1541+
const deleteComment = core.getBooleanInput('delete_comment');
15411542

15421543
// load rules from file, if there
15431544
const configPath = core.getInput('config_path', { required: true });
@@ -1655,6 +1656,36 @@ ${warningReportText}
16551656
core.setFailed(`Action failed with ${countErrors} errors (and ${countWarnings} warnings)`);
16561657
}
16571658

1659+
if (deleteComment) {
1660+
const perPage = 100;
1661+
let page = 1;
1662+
let hasMore = true;
1663+
do {
1664+
core.debug(`Fetching page #${page} (max: ${perPage}) of existing comments...`);
1665+
const res = await octokit.rest.issues.listComments({
1666+
owner,
1667+
repo: repo.name,
1668+
issue_number: num,
1669+
per_page: 100,
1670+
page,
1671+
});
1672+
const relevantComments = res.data.filter(comment => comment.body.includes('## Commit Message Lint Report'));
1673+
core.debug(`Fetched ${relevantComments.length} relevant (previous lint report) comments...`);
1674+
for (let i = 0; i < relevantComments.length; i++) {
1675+
core.debug(`Deleting comment #${relevantComments[i].id}...`);
1676+
await octokit.rest.issues.deleteComment({
1677+
owner,
1678+
repo: repo.name,
1679+
comment_id: relevantComments[i].id,
1680+
});
1681+
}
1682+
page++;
1683+
if (res.data.length < perPage) {
1684+
hasMore = false;
1685+
}
1686+
} while (hasMore);
1687+
}
1688+
16581689
if (createComment) {
16591690
if (countErrors || countWarnings) {
16601691
const finalReport = `

index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ async function run() {
1919
try {
2020
const token = core.getInput('token', { required: true });
2121
const createComment = core.getBooleanInput('comment');
22+
const deleteComment = core.getBooleanInput('delete_comment');
2223

2324
// load rules from file, if there
2425
const configPath = core.getInput('config_path', { required: true });
@@ -136,6 +137,36 @@ ${warningReportText}
136137
core.setFailed(`Action failed with ${countErrors} errors (and ${countWarnings} warnings)`);
137138
}
138139

140+
if (deleteComment) {
141+
const perPage = 100;
142+
let page = 1;
143+
let hasMore = true;
144+
do {
145+
core.debug(`Fetching page #${page} (max: ${perPage}) of existing comments...`);
146+
const res = await octokit.rest.issues.listComments({
147+
owner,
148+
repo: repo.name,
149+
issue_number: num,
150+
per_page: 100,
151+
page,
152+
});
153+
const relevantComments = res.data.filter(comment => comment.body.includes('## Commit Message Lint Report'));
154+
core.debug(`Fetched ${relevantComments.length} relevant (previous lint report) comments...`);
155+
for (let i = 0; i < relevantComments.length; i++) {
156+
core.debug(`Deleting comment #${relevantComments[i].id}...`);
157+
await octokit.rest.issues.deleteComment({
158+
owner,
159+
repo: repo.name,
160+
comment_id: relevantComments[i].id,
161+
});
162+
}
163+
page++;
164+
if (res.data.length < perPage) {
165+
hasMore = false;
166+
}
167+
} while (hasMore);
168+
}
169+
139170
if (createComment) {
140171
if (countErrors || countWarnings) {
141172
const finalReport = `

0 commit comments

Comments
 (0)