Skip to content

Commit 8304914

Browse files
committed
Build: deleting im-pipeline-bot PR comments before adding new ones
1 parent 8c238cf commit 8304914

File tree

5 files changed

+148
-2
lines changed

5 files changed

+148
-2
lines changed

package-lock.json

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

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,14 @@
8080
"eslint-plugin-react-hooks": "^4.2.0",
8181
"fs-extra": "^9.0.1",
8282
"husky": "^4.3.0",
83+
"issue-parser": "^6.0.0",
8384
"jest": "^26.6.3",
8485
"jest-environment-jsdom": "^26.6.2",
8586
"lint-staged": "^10.5.2",
87+
"lodash": "^4.17.20",
8688
"msw": "^0.22.3",
89+
"octokit": "^1.0.0-hello-world",
90+
"p-filter": "^2.1.0",
8791
"prettier": "^2.2.0",
8892
"react": "^17.0.1",
8993
"react-dom": "^17.0.1",

release.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ You can test it by using:
8787
npm install scriptloader-component@\${nextRelease.version}
8888
\`\`\`
8989
`,
90+
releasedLabels: null,
9091
},
9192
],
9293
],

release/create-github-prerelease.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ const originalPackage = path.dirname(
66
);
77
fs.copySync(originalPackage, path.join(__dirname, "github-prerelease"));
88

9-
const replaceFileText = (file, ...replaceArgs) => {
9+
const replaceFileText = (file, newFile, ...replaceArgs) => {
1010
const fileText = fs.readFileSync(file, { encoding: "utf8" });
1111
const replacedText = fileText.replace(...replaceArgs);
12-
fs.writeFileSync(file, replacedText);
12+
fs.writeFileSync(newFile, replacedText);
1313
};
1414

1515
replaceFileText(
1616
path.join(__dirname, "github-prerelease", "lib", "success.js"),
17+
path.join(__dirname, "github-prerelease", "lib", "new-success.js"),
1718
"type:pr+is:merged",
1819
"type:pr+is:open"
1920
);
21+
22+
fs.copySync(
23+
require.resolve("./success"),
24+
path.join(__dirname, "github-prerelease", "lib", "success.js")
25+
);

release/success.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
const { isNil, uniqBy, flatten } = require("lodash");
2+
const pFilter = require("p-filter");
3+
const issueParser = require("issue-parser");
4+
5+
// this file will be copied two folders deeper
6+
const resolveConfig = require("./resolve-config");
7+
const getClient = require("./resolve-config");
8+
const parseGithubUrl = require("./parse-github-url");
9+
const getSearchQueries = require("./get-search-queries");
10+
const success = require("./new-success");
11+
12+
module.exports = async (pluginConfig, context) => {
13+
const {
14+
options: { repositoryUrl },
15+
commits,
16+
logger,
17+
} = context;
18+
const {
19+
githubToken,
20+
githubUrl,
21+
githubApiPathPrefix,
22+
proxy,
23+
successComment,
24+
} = resolveConfig(pluginConfig, context);
25+
const github = getClient({
26+
githubToken,
27+
githubUrl,
28+
githubApiPathPrefix,
29+
proxy,
30+
});
31+
const [owner, repo] = (
32+
await github.repos.get(parseGithubUrl(repositoryUrl))
33+
).data.full_name.split("/");
34+
35+
if (successComment === false) {
36+
logger.log("Skipping old comment deletion.");
37+
} else {
38+
const parser = issueParser(
39+
"github",
40+
githubUrl ? { hosts: [githubUrl] } : {}
41+
);
42+
const shas = commits.map(({ hash }) => hash);
43+
44+
const searchQueries = getSearchQueries(
45+
`repo:${owner}/${repo}+type:pr+is:merged`,
46+
shas
47+
).map(
48+
async (q) => (await github.search.issuesAndPullRequests({ q })).data.items
49+
);
50+
51+
const prs = await pFilter(
52+
uniqBy(flatten(await Promise.all(searchQueries)), "number"),
53+
async ({ number }) =>
54+
(
55+
await github.pulls.listCommits({ owner, repo, pull_number: number })
56+
).data.find(({ sha }) => shas.includes(sha)) ||
57+
shas.includes(
58+
(await github.pulls.get({ owner, repo, pull_number: number })).data
59+
.merge_commit_sha
60+
)
61+
);
62+
63+
// Parse the release commits message and PRs body to find resolved issues/PRs via comment keyworkds
64+
const issues = [
65+
...prs.map((pr) => pr.body),
66+
...commits.map((commit) => commit.message),
67+
].reduce((issues, message) => {
68+
return message
69+
? issues.concat(
70+
parser(message)
71+
.actions.close.filter(
72+
(action) =>
73+
isNil(action.slug) || action.slug === `${owner}/${repo}`
74+
)
75+
.map((action) => ({ number: Number.parseInt(action.issue, 10) }))
76+
)
77+
: issues;
78+
}, []);
79+
80+
await Promise.all(
81+
uniqBy([...prs, ...issues], "number").map(async (issue) => {
82+
try {
83+
const comments = { owner, repo, issue_number: issue.number };
84+
const foundComments = (
85+
await github.issues.listComments(comments)
86+
).filter((comment) => comment.user.login === "im-pipeline-bot");
87+
88+
for (let c in foundComments) {
89+
await github.issues.deleteComment({
90+
owner,
91+
repo,
92+
comment_id: c.id,
93+
});
94+
logger.log("Removed comment %c from issue #%d", c.id, issue.number);
95+
}
96+
} catch (error) {
97+
if (error.status === 403) {
98+
logger.error(
99+
"Not allowed to delete a comment to the issue #%d.",
100+
issue.number
101+
);
102+
} else if (error.status === 404) {
103+
logger.error(
104+
"Failed to delete a comment to the issue #%d as it doesn't exist.",
105+
issue.number
106+
);
107+
} else {
108+
logger.error(
109+
"Failed to delete a comment to the issue #%d.",
110+
issue.number
111+
);
112+
}
113+
}
114+
})
115+
);
116+
}
117+
118+
await success(pluginConfig, context);
119+
};

0 commit comments

Comments
 (0)