Skip to content

Commit 3312019

Browse files
authored
chore(ci): update post release workflow (#1506)
1 parent 6fe47ee commit 3312019

File tree

2 files changed

+103
-88
lines changed

2 files changed

+103
-88
lines changed

.github/scripts/label_related_issue.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ const {
77
HANDLE_MAINTAINERS_TEAM,
88
PR_IS_MERGED,
99
RELATED_ISSUE_REGEX,
10-
} = require("./constants");
10+
LABEL_RELEASED,
11+
} = require('./constants');
1112

1213
module.exports = async ({ github, context, core }) => {
1314
if (IGNORE_AUTHORS.includes(PR_AUTHOR)) {
14-
return core.notice("Author in IGNORE_AUTHORS list; skipping...");
15+
return core.notice('Author in IGNORE_AUTHORS list; skipping...');
1516
}
1617

17-
if (PR_IS_MERGED == "false") {
18-
return core.notice("Only merged PRs to avoid spam; skipping");
18+
if (PR_IS_MERGED == 'false') {
19+
return core.notice('Only merged PRs to avoid spam; skipping');
1920
}
2021

2122
const isMatch = RELATED_ISSUE_REGEX.exec(PR_BODY);
@@ -60,22 +61,28 @@ module.exports = async ({ github, context, core }) => {
6061
/**
6162
* Keep all labels except those that start with 'status/' or 'need-' or equal to 'help-wanted'
6263
* as those are contextual to issues still in progress.
64+
*
65+
* If the issue was already marked with the 'status/completed' label, then we'll keep that, otherwise
66+
* we'll add the 'status/pending-release' label.
6367
*/
68+
let hasCompletedLabel = false;
6469
const newLabels = currentLabels.data
65-
.filter(
66-
(label) =>
67-
!label.name.startsWith("status/") &&
68-
!label.name.startsWith("need-") &&
69-
label.name !== "help-wanted"
70-
)
70+
.filter((label) => {
71+
if (label.name === LABEL_RELEASED) {
72+
hasCompletedLabel = true;
73+
}
74+
return (
75+
!label.name.startsWith('status/') &&
76+
!label.name.startsWith('need-') &&
77+
label.name !== 'help-wanted'
78+
);
79+
})
7180
.map((label) => label.name);
72-
// Add the status/pending-release label
73-
newLabels.push(LABEL_PENDING_RELEASE);
81+
// Add the status/pending-release or status/completed label
82+
newLabels.push(hasCompletedLabel ? LABEL_RELEASED : LABEL_PENDING_RELEASE);
7483

7584
try {
76-
core.info(
77-
`Auto-labeling related issue ${issue} for release while removing 'status/*' and 'need-*' labels`
78-
);
85+
core.info(`Auto-labeling related issue ${issue} completed`);
7986
return await github.rest.issues.setLabels({
8087
issue_number: issue,
8188
owner: context.repo.owner,

.github/scripts/post_release.js

Lines changed: 81 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { LABEL_PENDING_RELEASE, LABEL_RELEASED } = require("./constants");
1+
const { LABEL_PENDING_RELEASE, LABEL_RELEASED } = require('./constants');
22

33
/**
44
* Fetch issues using GitHub REST API
@@ -12,26 +12,28 @@ const { LABEL_PENDING_RELEASE, LABEL_RELEASED } = require("./constants");
1212
* @see {@link https://octokit.github.io/rest.js/v18#usage|Octokit client}
1313
*/
1414
const fetchIssues = async ({
15-
gh_client,
16-
org,
17-
repository,
18-
state = "all",
19-
label = LABEL_PENDING_RELEASE,
15+
gh_client,
16+
core,
17+
org,
18+
repository,
19+
state = 'all',
20+
label = LABEL_PENDING_RELEASE,
2021
}) => {
21-
try {
22-
const { data: issues } = await gh_client.rest.issues.listForRepo({
23-
owner: org,
24-
repo: repository,
25-
state: state,
26-
labels: label,
27-
});
28-
29-
return issues;
30-
} catch (error) {
31-
console.error(error);
32-
throw new Error("Failed to fetch issues");
33-
}
22+
try {
23+
const { data: issues } = await gh_client.rest.issues.listForRepo({
24+
owner: org,
25+
repo: repository,
26+
state: state,
27+
labels: label,
28+
});
3429

30+
return issues.filter(
31+
(issue) => Object(issue).hasOwnProperty('pull_request') === false
32+
);
33+
} catch (error) {
34+
core.setFailed(error);
35+
throw new Error('Failed to fetch issues');
36+
}
3537
};
3638

3739
/**
@@ -44,68 +46,74 @@ const fetchIssues = async ({
4446
* @see {@link https://octokit.github.io/rest.js/v18#usage|Octokit client}
4547
*/
4648
const notifyRelease = async ({
47-
gh_client,
48-
owner,
49-
repository,
50-
release_version,
49+
gh_client,
50+
core,
51+
owner,
52+
repository,
53+
release_version,
5154
}) => {
52-
const release_url = `https://github.com/${owner}/${repository}/releases/tag/v${release_version.replace(/v/g, '')}`;
53-
54-
const issues = await fetchIssues({
55-
gh_client: gh_client,
56-
org: owner,
57-
repository: repository,
58-
state: "closed"
59-
});
55+
const release_url = `https://github.com/${owner}/${repository}/releases/tag/v${release_version.replace(
56+
/v/g,
57+
''
58+
)}`;
6059

61-
issues.forEach(async (issue) => {
62-
console.info(`Updating issue number ${issue.number}`);
60+
const issues = await fetchIssues({
61+
gh_client: gh_client,
62+
org: owner,
63+
repository: repository,
64+
state: 'closed',
65+
});
6366

64-
const comment = `This is now released under [${release_version}](${release_url}) version!`;
65-
try {
66-
await gh_client.rest.issues.createComment({
67-
owner: owner,
68-
repo: repository,
69-
body: comment,
70-
issue_number: issue.number,
71-
});
72-
} catch (error) {
73-
console.error(error);
74-
throw new Error(`Failed to update issue ${issue.number} about ${release_version} release`)
75-
}
67+
issues.forEach(async (issue) => {
68+
core.info(`Updating issue number ${issue.number}`);
7669

70+
const comment = `This is now released under [${release_version}](${release_url}) version!`;
71+
try {
72+
await gh_client.rest.issues.createComment({
73+
owner: owner,
74+
repo: repository,
75+
body: comment,
76+
issue_number: issue.number,
77+
});
78+
} catch (error) {
79+
core.setFailed(error);
80+
throw new Error(
81+
`Failed to update issue ${issue.number} about ${release_version} release`
82+
);
83+
}
7784

78-
// Close issue and remove staged label; keep existing ones
79-
const labels = issue.labels
80-
.filter((label) => label.name != LABEL_PENDING_RELEASE)
81-
.map((label) => label.name)
82-
.push(LABEL_RELEASED);
85+
// Remove staged label; keep existing ones
86+
const labels = issue.labels
87+
.filter((label) => label.name != LABEL_PENDING_RELEASE)
88+
.map((label) => label.name);
8389

84-
try {
85-
await gh_client.rest.issues.setLabels({
86-
repo: repository,
87-
owner,
88-
issue_number: issue.number,
89-
labels,
90-
});
91-
} catch (error) {
92-
console.error(error);
93-
throw new Error("Failed to close issue")
94-
}
90+
// Update labels including the released one
91+
try {
92+
await gh_client.rest.issues.setLabels({
93+
repo: repository,
94+
owner,
95+
issue_number: issue.number,
96+
labels: [...labels, LABEL_RELEASED],
97+
});
98+
} catch (error) {
99+
core.setFailed(error);
100+
throw new Error('Failed to label issue');
101+
}
95102

96-
console.info(`Issue number ${issue.number} closed and updated`);
97-
});
103+
core.info(`Issue number ${issue.number} labeled`);
104+
});
98105
};
99106

100107
// context: https://github.com/actions/toolkit/blob/main/packages/github/src/context.ts
101-
module.exports = async ({ github, context }) => {
102-
const { RELEASE_VERSION } = process.env;
103-
console.log(`Running post-release script for ${RELEASE_VERSION} version`);
108+
module.exports = async ({ github, context, core }) => {
109+
const { RELEASE_VERSION } = process.env;
110+
core.info(`Running post-release script for ${RELEASE_VERSION} version`);
104111

105-
await notifyRelease({
106-
gh_client: github,
107-
owner: context.repo.owner,
108-
repository: context.repo.repo,
109-
release_version: RELEASE_VERSION,
110-
});
111-
};
112+
await notifyRelease({
113+
gh_client: github,
114+
core,
115+
owner: context.repo.owner,
116+
repository: context.repo.repo,
117+
release_version: RELEASE_VERSION,
118+
});
119+
};

0 commit comments

Comments
 (0)