@@ -6,9 +6,12 @@ import {
66} from '@electron/github-app-auth'
77
88import * as index from '../src/index'
9+ import { GitHub } from '@actions/github/lib/utils'
910
1011jest . mock ( '@actions/core' , ( ) => {
1112 return {
13+ exportVariable : jest . fn ( ) ,
14+ getBooleanInput : jest . fn ( ) ,
1215 getInput : jest . fn ( ) ,
1316 getState : jest . fn ( ) ,
1417 info : jest . fn ( ) ,
@@ -28,15 +31,35 @@ jest.mock('@actions/github', () => {
2831 }
2932 }
3033} )
34+ jest . mock ( '@actions/github/lib/utils' )
3135jest . mock ( '@electron/github-app-auth' )
3236
3337jest
3438 . mocked ( appCredentialsFromString )
3539 . mockReturnValue ( { appId : '12345' , privateKey : 'private' } )
3640
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+
3755// Spy the action's entrypoint
3856const runSpy = jest . spyOn ( index , 'run' )
3957
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+
4063describe ( 'action' , ( ) => {
4164 beforeEach ( ( ) => {
4265 jest . clearAllMocks ( )
@@ -166,6 +189,49 @@ describe('action', () => {
166189 expect ( core . saveState ) . toHaveBeenLastCalledWith ( 'token' , token )
167190 } )
168191
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+
169235 it ( 'generates an org token' , async ( ) => {
170236 const token = 'org-token'
171237 jest . mocked ( core . getInput ) . mockImplementation ( ( name : string ) => {
@@ -202,6 +268,47 @@ describe('action', () => {
202268 expect ( core . saveState ) . toHaveBeenLastCalledWith ( 'token' , token )
203269 } )
204270
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+
205312 it ( 'handles token generate failure' , async ( ) => {
206313 jest . mocked ( core . getInput ) . mockImplementation ( ( name : string ) => {
207314 switch ( name ) {
0 commit comments