Skip to content

Commit ca918b6

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

File tree

5 files changed

+147
-2
lines changed

5 files changed

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

0 commit comments

Comments
 (0)