Skip to content

Commit ae2e0d8

Browse files
committed
feat: prioritize PAT over installation token
1 parent 36d3c43 commit ae2e0d8

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

__tests__/auth.test.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('authenticate', () => {
99
const mockFetch = jest.spyOn(global, 'fetch')
1010

1111
beforeEach(() => {
12+
process.env.GITHUB_TOKEN = 'ghp_default_token'
1213
mockFetch.mockClear()
1314
})
1415

@@ -21,7 +22,7 @@ describe('authenticate', () => {
2122

2223
const result = await authenticate(
2324
{ owner: 'dunder-mifflin', repo: 'website' },
24-
'fallback_token'
25+
'ghp_default_token'
2526
)
2627

2728
expect(result).toBe('ghs_app_123')
@@ -43,20 +44,32 @@ describe('authenticate', () => {
4344

4445
const result = await authenticate(
4546
{ owner: 'dunder-mifflin', repo: 'website' },
46-
'fallback_token'
47+
'ghp_default_token'
4748
)
4849

49-
expect(result).toBe('fallback_token')
50+
expect(result).toBe('ghp_default_token')
5051
})
5152

5253
it('should fall back to standard authentication when service is unavailable', async () => {
5354
mockFetch.mockRejectedValue(new Error('Network error'))
5455

5556
const result = await authenticate(
5657
{ owner: 'dunder-mifflin', repo: 'website' },
57-
'fallback_token'
58+
'ghp_default_token'
5859
)
5960

60-
expect(result).toBe('fallback_token')
61+
expect(result).toBe('ghp_default_token')
62+
})
63+
64+
it('should use user-provided PAT when different from GITHUB_TOKEN', async () => {
65+
const customPAT = 'ghp_custom_pat'
66+
67+
const result = await authenticate(
68+
{ owner: 'owner', repo: 'repo' },
69+
customPAT
70+
)
71+
72+
expect(result).toBe(customPAT)
73+
expect(mockFetch).not.toHaveBeenCalled()
6174
})
6275
})

dist/index.js

Lines changed: 9 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/auth.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as core from '@actions/core'
22
import type { Context } from '@actions/github/lib/context'
33

4+
// TODO: check production URL?
45
export const GITHUB_AUTH_SERVICE_URL =
56
'https://github-auth.staging.code-pushup.dev'
67

@@ -13,8 +14,12 @@ type TokenResponse = {
1314

1415
export async function authenticate(
1516
{ owner, repo }: Context['repo'],
16-
fallbackToken: string
17+
token: string
1718
): Promise<string> {
19+
if (token !== process.env.GITHUB_TOKEN) {
20+
core.info('Using user-provided PAT')
21+
return token
22+
}
1823
try {
1924
const response = await fetch(
2025
`${GITHUB_AUTH_SERVICE_URL}/github/${owner}/${repo}/installation-token`,
@@ -27,7 +32,7 @@ export async function authenticate(
2732
)
2833
const data = await response.json()
2934
if (response.ok && isTokenResponse(data)) {
30-
core.info('Using Code PushUp GitHub App authentication')
35+
core.info('Using Code PushUp GitHub App installation token')
3136
return data.token
3237
}
3338
handleErrorResponse(response.status)
@@ -36,8 +41,8 @@ export async function authenticate(
3641
`Unable to contact Code PushUp authentication service: ${error}`
3742
)
3843
}
39-
core.info('Using standard token authentication')
40-
return fallbackToken
44+
core.info('Using default GITHUB_TOKEN')
45+
return token
4146
}
4247

4348
function isTokenResponse(res: unknown): res is TokenResponse {

0 commit comments

Comments
 (0)