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+ } ;
0 commit comments