1
- import { debug , notice , getIDToken , exportVariable , info , setOutput } from '@actions/core' ;
1
+ import {
2
+ debug ,
3
+ notice ,
4
+ getIDToken ,
5
+ exportVariable ,
6
+ info ,
7
+ setOutput ,
8
+ saveState ,
9
+ getState ,
10
+ } from '@actions/core' ;
2
11
import { getInput } from '@actions/core' ;
3
- import { context } from '@actions/github' ;
12
+ import { context , getOctokit } from '@actions/github' ;
4
13
import { warn } from 'console' ;
5
14
import fs from 'fs' ;
6
15
import path from 'path' ;
@@ -30,23 +39,8 @@ export class Action {
30
39
const region = getInput ( 'region' ) || 'us-east-1' ;
31
40
const role = getInput ( 'role' ) ;
32
41
const [ owner , repo ] = GITHUB_REPOSITORY ?. split ( '/' ) || [ ] ;
33
- const [ , branchType , branchId ] = GITHUB_REF ?. split ( '/' ) || [ ] ;
34
-
35
- if ( ! branchId ) {
36
- debug ( `GITHUB_REF: ${ GITHUB_REF } ` ) ;
37
- debug ( `branchType: ${ branchType } ` ) ;
38
- debug ( `branchId: ${ branchId } ` ) ;
39
- throw new Error ( 'Unable to determine branch from GITHUB_REF' ) ;
40
- }
41
42
42
- let deploymentStage = branchId ;
43
- if ( branchType === 'pull' ) {
44
- if ( ! GITHUB_BASE_REF ) {
45
- throw new Error ( 'Unable to determine base ref from GITHUB_BASE_REF' ) ;
46
- }
47
- deploymentStage = `${ GITHUB_BASE_REF } -pr-${ branchId } ` ;
48
- }
49
- setOutput ( 'stage' , deploymentStage ) ;
43
+ setOutput ( 'stage' , this . stage ) ;
50
44
51
45
let deploy = true ;
52
46
let destroy = false ;
@@ -59,6 +53,7 @@ export class Action {
59
53
destroy = true ;
60
54
}
61
55
setOutput ( 'deploy' , deploy ) ;
56
+ saveState ( 'destroy' , destroy ) ;
62
57
setOutput ( 'destroy' , destroy ) ;
63
58
64
59
let idToken : string | undefined = undefined ;
@@ -115,11 +110,82 @@ export class Action {
115
110
}
116
111
}
117
112
113
+ get stage ( ) : string {
114
+ const [ , branchType , branchId ] = GITHUB_REF ?. split ( '/' ) || [ ] ;
115
+
116
+ if ( ! branchId ) {
117
+ debug ( `GITHUB_REF: ${ GITHUB_REF } ` ) ;
118
+ debug ( `branchType: ${ branchType } ` ) ;
119
+ debug ( `branchId: ${ branchId } ` ) ;
120
+ throw new Error ( 'Unable to determine branch from GITHUB_REF' ) ;
121
+ }
122
+
123
+ let deploymentStage = branchId ;
124
+ if ( branchType === 'pull' ) {
125
+ if ( ! GITHUB_BASE_REF ) {
126
+ throw new Error ( 'Unable to determine base ref from GITHUB_BASE_REF' ) ;
127
+ }
128
+ deploymentStage = `${ GITHUB_BASE_REF } -pr-${ branchId } ` ;
129
+ }
130
+
131
+ return deploymentStage ;
132
+ }
133
+
134
+ get token ( ) : string {
135
+ const token = getInput ( 'token' ) ;
136
+ if ( ! token ) {
137
+ throw new Error ( 'Missing GITHUB_TOKEN' ) ;
138
+ }
139
+ return token ;
140
+ }
141
+
142
+ get prComment ( ) : string {
143
+ return `
144
+ [${ this . commitSha } ](https://github.com/${ context . repo . owner } /${ context . repo . repo } /commit/${ this . commitSha } has been deployed!
145
+ - **Commit:** \`${ this . commitSha } \`
146
+ - **Stage:** \`${ this . stage } \`
147
+ - **URL:** [${ this . httpApiUrl } ](${ this . httpApiUrl } )
148
+ ` ;
149
+ }
150
+
151
+ get commitSha ( ) : string {
152
+ if ( context . eventName === 'pull_request' ) {
153
+ return `${ context . payload . pull_request ?. head . sha } ` . substring ( 0 , 7 ) ;
154
+ }
155
+ return context . sha . substring ( 0 , 7 ) ;
156
+ }
157
+
158
+ get prNumber ( ) : number | undefined {
159
+ if ( context . eventName === 'pull_request' ) {
160
+ return context . payload . pull_request ?. number ;
161
+ }
162
+ return undefined ;
163
+ }
164
+
165
+ async addPrComments ( ) : Promise < void > {
166
+ const destroy = boolean ( getState ( 'destroy' ) ) ;
167
+ if ( destroy ) {
168
+ return ;
169
+ }
170
+
171
+ const { prNumber } = this ;
172
+ if ( ! prNumber ) {
173
+ return ;
174
+ }
175
+
176
+ const octokit = getOctokit ( this . token ) ;
177
+ await octokit . rest . issues . createComment ( {
178
+ body : this . prComment ,
179
+ owner : context . repo . owner ,
180
+ repo : context . repo . repo ,
181
+ issue_number : prNumber ,
182
+ } ) ;
183
+ }
184
+
118
185
async post ( ) : Promise < void > {
119
186
const httpApiUrl = await this . httpApiUrl ;
120
187
121
- // TODO: Detect branch/PR delete/close and remove the stage
122
-
188
+ await this . addPrComments ( ) ;
123
189
notice ( `HTTP API URL: ${ httpApiUrl } ` ) ;
124
190
}
125
191
0 commit comments