Skip to content

Commit 433928d

Browse files
committed
feat(ci): auto-notify & close issues on release
1 parent 22f8719 commit 433928d

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

.github/workflows/post_release.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
const STAGED_LABEL = "status/staged-next-release";
2+
3+
/**
4+
* Fetch issues using GitHub REST API
5+
*
6+
* @param {object} gh_client - Pre-authenticated REST client (Octokit)
7+
* @param {string} org - GitHub Organization
8+
* @param {string} repository - GitHub repository
9+
* @param {string} state - GitHub issue state (open, closed)
10+
* @param {string} label - Comma-separated issue labels to fetch
11+
* @return {Object[]} issues - Array of issues matching params
12+
* @see {@link https://octokit.github.io/rest.js/v18#usage|Octokit client}
13+
*/
14+
const fetchIssues = async ({
15+
gh_client,
16+
org,
17+
repository,
18+
state = "open",
19+
label = STAGED_LABEL,
20+
}) => {
21+
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+
});
29+
30+
return issues;
31+
32+
} catch (error) {
33+
console.error(error);
34+
throw new Error("Failed to fetch issues")
35+
}
36+
37+
};
38+
39+
/**
40+
* Notify new release and close staged GitHub issue
41+
*
42+
* @param {object} gh_client - Pre-authenticated REST client (Octokit)
43+
* @param {string} owner - GitHub Organization
44+
* @param {string} repository - GitHub repository
45+
* @param {string} release_version - GitHub Release version
46+
* @see {@link https://octokit.github.io/rest.js/v18#usage|Octokit client}
47+
*/
48+
const notifyRelease = async ({
49+
gh_client,
50+
owner,
51+
repository,
52+
release_version,
53+
}) => {
54+
const release_url = `https://github.com/${owner}/${repository}/releases/tag/v${release_version}`;
55+
56+
const issues = await fetchIssues({
57+
gh_client: gh_client,
58+
org: owner,
59+
repository: repository,
60+
});
61+
62+
issues.forEach(async (issue) => {
63+
console.info(`Updating issue number ${issue.number}`);
64+
65+
const comment = `This is now released under [${release_version}](${release_url}) version!`;
66+
try {
67+
await gh_client.rest.issues.createComment({
68+
owner: owner,
69+
repo: repository,
70+
body: comment,
71+
issue_number: issue.number,
72+
});
73+
} catch (error) {
74+
console.error(error);
75+
throw new Error(`Failed to update issue ${issue.number} about ${release_version} release`)
76+
}
77+
78+
79+
// Close issue and remove staged label; keep existing ones
80+
const labels = issue.labels
81+
.filter((label) => label.name != STAGED_LABEL)
82+
.map((label) => label.name);
83+
84+
try {
85+
await gh_client.rest.issues.update({
86+
repo: repository,
87+
owner: owner,
88+
issue_number: issue.number,
89+
state: "closed",
90+
labels: labels,
91+
});
92+
} catch (error) {
93+
console.error(error);
94+
throw new Error("Failed to close issue")
95+
}
96+
97+
console.info(`Issue number ${issue.number} closed and updated`);
98+
});
99+
};
100+
101+
// context: https://github.com/actions/toolkit/blob/main/packages/github/src/context.ts
102+
module.exports = async ({ github, context }) => {
103+
const { RELEASE_TAG_VERSION } = process.env;
104+
console.log(`Running post-release script for ${RELEASE_TAG_VERSION} version`);
105+
106+
await notifyRelease({
107+
gh_client: github,
108+
owner: context.repo.owner,
109+
repository: context.repo.repo,
110+
release_version: RELEASE_TAG_VERSION,
111+
});
112+
};

.github/workflows/publish.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ name: Publish to PyPi
2222
# 8. Builds a fresh version of docs including Changelog updates
2323
# 9. Push latest release source code to master using release title as the commit message
2424
# 10. Builds latest documentation for new release, and update latest alias pointing to the new release tag
25+
# 11. Close and notify all issues labeled "status/staged-next-release" about the release details
2526

2627
#
2728
# === Fallback mechanism due to external failures ===
@@ -107,6 +108,12 @@ jobs:
107108
publish_dir: ./api
108109
keep_files: true
109110
destination_dir: latest/api
111+
- name: Close issues related to this release
112+
uses: actions/github-script@v5
113+
with:
114+
script: |
115+
const post_release = require('.github/workflows/post_release.js')
116+
await post_release({github, context, core})
110117
111118
sync_master:
112119
needs: release

0 commit comments

Comments
 (0)