-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathpost_release.js
119 lines (108 loc) · 3.27 KB
/
post_release.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const { LABEL_PENDING_RELEASE, LABEL_RELEASED } = require('./constants');
/**
* Fetch issues using GitHub REST API
*
* @param {object} gh_client - Pre-authenticated REST client (Octokit)
* @param {string} org - GitHub Organization
* @param {string} repository - GitHub repository
* @param {string} state - GitHub issue state (open, closed)
* @param {string} label - Comma-separated issue labels to fetch
* @return {Object[]} issues - Array of issues matching params
* @see {@link https://octokit.github.io/rest.js/v18#usage|Octokit client}
*/
const fetchIssues = async ({
gh_client,
core,
org,
repository,
state = 'all',
label = LABEL_PENDING_RELEASE,
}) => {
try {
const { data: issues } = await gh_client.rest.issues.listForRepo({
owner: org,
repo: repository,
state: state,
labels: label,
});
return issues.filter(
(issue) => Object(issue).hasOwnProperty('pull_request') === false
);
} catch (error) {
core.setFailed(error);
throw new Error('Failed to fetch issues');
}
};
/**
* Notify new release and close staged GitHub issue
*
* @param {object} gh_client - Pre-authenticated REST client (Octokit)
* @param {string} owner - GitHub Organization
* @param {string} repository - GitHub repository
* @param {string} release_version - GitHub Release version
* @see {@link https://octokit.github.io/rest.js/v18#usage|Octokit client}
*/
const notifyRelease = async ({
gh_client,
core,
owner,
repository,
release_version,
}) => {
const release_url = `https://github.com/${owner}/${repository}/releases/tag/v${release_version.replace(
/v/g,
''
)}`;
const issues = await fetchIssues({
gh_client: gh_client,
org: owner,
repository: repository,
state: 'closed',
});
issues.forEach(async (issue) => {
core.info(`Updating issue number ${issue.number}`);
const comment = `This is now released under [${release_version}](${release_url}) version!`;
try {
await gh_client.rest.issues.createComment({
owner: owner,
repo: repository,
body: comment,
issue_number: issue.number,
});
} catch (error) {
core.setFailed(error);
throw new Error(
`Failed to update issue ${issue.number} about ${release_version} release`
);
}
// Remove staged label; keep existing ones
const labels = issue.labels
.filter((label) => label.name != LABEL_PENDING_RELEASE)
.map((label) => label.name);
// Update labels including the released one
try {
await gh_client.rest.issues.setLabels({
repo: repository,
owner,
issue_number: issue.number,
labels: [...labels, LABEL_RELEASED],
});
} catch (error) {
core.setFailed(error);
throw new Error('Failed to label issue');
}
core.info(`Issue number ${issue.number} labeled`);
});
};
// context: https://github.com/actions/toolkit/blob/main/packages/github/src/context.ts
module.exports = async ({ github, context, core }) => {
const { RELEASE_VERSION } = process.env;
core.info(`Running post-release script for ${RELEASE_VERSION} version`);
await notifyRelease({
gh_client: github,
core,
owner: context.repo.owner,
repository: context.repo.repo,
release_version: RELEASE_VERSION,
});
};