@@ -6,9 +6,12 @@ import {
6
6
} from '@electron/github-app-auth'
7
7
8
8
import * as index from '../src/index'
9
+ import { GitHub } from '@actions/github/lib/utils'
9
10
10
11
jest . mock ( '@actions/core' , ( ) => {
11
12
return {
13
+ exportVariable : jest . fn ( ) ,
14
+ getBooleanInput : jest . fn ( ) ,
12
15
getInput : jest . fn ( ) ,
13
16
getState : jest . fn ( ) ,
14
17
info : jest . fn ( ) ,
@@ -28,15 +31,35 @@ jest.mock('@actions/github', () => {
28
31
}
29
32
}
30
33
} )
34
+ jest . mock ( '@actions/github/lib/utils' )
31
35
jest . mock ( '@electron/github-app-auth' )
32
36
33
37
jest
34
38
. mocked ( appCredentialsFromString )
35
39
. mockReturnValue ( { appId : '12345' , privateKey : 'private' } )
36
40
41
+ const getAuthenticated = jest . fn ( )
42
+ const getByUsername = jest . fn ( )
43
+
44
+ ; ( GitHub as unknown as jest . Mock ) . mockReturnValue ( {
45
+ rest : {
46
+ apps : {
47
+ getAuthenticated
48
+ } ,
49
+ users : {
50
+ getByUsername
51
+ }
52
+ }
53
+ } )
54
+
37
55
// Spy the action's entrypoint
38
56
const runSpy = jest . spyOn ( index , 'run' )
39
57
58
+ const slug = 'my-app'
59
+ const userId = 12345
60
+ const username = `${ slug } [bot]`
61
+ const email = `${ userId } +${ slug } [bot]@users.noreply.github.com`
62
+
40
63
describe ( 'action' , ( ) => {
41
64
beforeEach ( ( ) => {
42
65
jest . clearAllMocks ( )
@@ -166,6 +189,49 @@ describe('action', () => {
166
189
expect ( core . saveState ) . toHaveBeenLastCalledWith ( 'token' , token )
167
190
} )
168
191
192
+ it ( 'can export a git user with repo token' , async ( ) => {
193
+ const token = 'repo-token'
194
+ jest . mocked ( core . getBooleanInput ) . mockReturnValue ( true )
195
+ jest . mocked ( core . getInput ) . mockImplementation ( ( name : string ) => {
196
+ switch ( name ) {
197
+ case 'creds' :
198
+ return 'foobar'
199
+ case 'owner' :
200
+ return 'electron'
201
+ case 'repo' :
202
+ return 'fake-repo'
203
+ default :
204
+ return ''
205
+ }
206
+ } )
207
+ jest . mocked ( getTokenForRepo ) . mockResolvedValue ( token )
208
+ jest . mocked ( getAuthenticated ) . mockResolvedValue ( { data : { slug } } )
209
+ jest . mocked ( getByUsername ) . mockResolvedValue ( {
210
+ data : {
211
+ id : userId
212
+ }
213
+ } )
214
+
215
+ await index . run ( )
216
+ expect ( runSpy ) . toHaveReturned ( )
217
+
218
+ // Exports git user environment variables
219
+ expect ( core . exportVariable ) . toHaveBeenCalledTimes ( 4 )
220
+ expect ( core . exportVariable ) . toHaveBeenCalledWith (
221
+ 'GIT_AUTHOR_NAME' ,
222
+ username
223
+ )
224
+ expect ( core . exportVariable ) . toHaveBeenCalledWith ( 'GIT_AUTHOR_EMAIL' , email )
225
+ expect ( core . exportVariable ) . toHaveBeenCalledWith (
226
+ 'GIT_COMMITTER_NAME' ,
227
+ username
228
+ )
229
+ expect ( core . exportVariable ) . toHaveBeenCalledWith (
230
+ 'GIT_COMMITTER_EMAIL' ,
231
+ email
232
+ )
233
+ } )
234
+
169
235
it ( 'generates an org token' , async ( ) => {
170
236
const token = 'org-token'
171
237
jest . mocked ( core . getInput ) . mockImplementation ( ( name : string ) => {
@@ -202,6 +268,47 @@ describe('action', () => {
202
268
expect ( core . saveState ) . toHaveBeenLastCalledWith ( 'token' , token )
203
269
} )
204
270
271
+ it ( 'can export a git user with org token' , async ( ) => {
272
+ const token = 'org-token'
273
+ jest . mocked ( core . getBooleanInput ) . mockReturnValue ( true )
274
+ jest . mocked ( core . getInput ) . mockImplementation ( ( name : string ) => {
275
+ switch ( name ) {
276
+ case 'creds' :
277
+ return 'foobar'
278
+ case 'org' :
279
+ return 'electron'
280
+ default :
281
+ return ''
282
+ }
283
+ } )
284
+ jest . mocked ( getTokenForOrg ) . mockResolvedValue ( token )
285
+ jest . mocked ( getAuthenticated ) . mockResolvedValue ( { data : { slug } } )
286
+ jest . mocked ( getByUsername ) . mockResolvedValue ( {
287
+ data : {
288
+ id : userId
289
+ }
290
+ } )
291
+
292
+ await index . run ( )
293
+ expect ( runSpy ) . toHaveReturned ( )
294
+
295
+ // Exports git user environment variables
296
+ expect ( core . exportVariable ) . toHaveBeenCalledTimes ( 4 )
297
+ expect ( core . exportVariable ) . toHaveBeenCalledWith (
298
+ 'GIT_AUTHOR_NAME' ,
299
+ username
300
+ )
301
+ expect ( core . exportVariable ) . toHaveBeenCalledWith ( 'GIT_AUTHOR_EMAIL' , email )
302
+ expect ( core . exportVariable ) . toHaveBeenCalledWith (
303
+ 'GIT_COMMITTER_NAME' ,
304
+ username
305
+ )
306
+ expect ( core . exportVariable ) . toHaveBeenCalledWith (
307
+ 'GIT_COMMITTER_EMAIL' ,
308
+ email
309
+ )
310
+ } )
311
+
205
312
it ( 'handles token generate failure' , async ( ) => {
206
313
jest . mocked ( core . getInput ) . mockImplementation ( ( name : string ) => {
207
314
switch ( name ) {
0 commit comments