1
+ const { LABEL_PENDING_RELEASE , LABEL_RELEASED } = require ( "./constants" ) ;
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
+
15
+ const fetchIssues = async ( {
16
+ gh_client,
17
+ org,
18
+ repository,
19
+ state = "all" ,
20
+ label = LABEL_PENDING_RELEASE ,
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
+ } catch ( error ) {
32
+ console . error ( error ) ;
33
+ throw new Error ( "Failed to fetch issues" ) ;
34
+ }
35
+
36
+ } ;
37
+
38
+ /**
39
+ * Notify new release and close staged GitHub issue
40
+ *
41
+ * @param {object } gh_client - Pre-authenticated REST client (Octokit)
42
+ * @param {string } owner - GitHub Organization
43
+ * @param {string } repository - GitHub repository
44
+ * @param {string } release_version - GitHub Release version
45
+ * @see {@link https://octokit.github.io/rest.js/v18#usage|Octokit client }
46
+ */
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 != LABEL_PENDING_RELEASE )
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 , LABEL_RELEASED ] ,
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
+
103
+ module . exports = async ( { github, context } ) => {
104
+ const { RELEASE_VERSION } = process . env ;
105
+ console . log ( `Running post-release script for ${ RELEASE_VERSION } version` ) ;
106
+
107
+ await notifyRelease ( {
108
+ gh_client : github ,
109
+ owner : context . repo . owner ,
110
+ repository : context . repo . repo ,
111
+ release_version : RELEASE_VERSION ,
112
+ } ) ;
113
+ } ;
0 commit comments