Skip to content

Commit 9a9adfe

Browse files
authored
Fallback to GITHUB_TOKEN if pat is not available (#554)
1 parent 00be90e commit 9a9adfe

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/commands/fix/open-pr.mts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ let _octokit: Octokit | undefined
2828
function getOctokit() {
2929
if (_octokit === undefined) {
3030
_octokit = new Octokit({
31-
// Lazily access constants.ENV.SOCKET_SECURITY_GITHUB_PAT.
32-
auth: constants.ENV.SOCKET_SECURITY_GITHUB_PAT
31+
// Lazily access constants.ENV properties.
32+
auth:
33+
constants.ENV.SOCKET_SECURITY_GITHUB_PAT || constants.ENV.GITHUB_TOKEN
3334
})
3435
}
3536
return _octokit
@@ -40,8 +41,8 @@ export function getOctokitGraphql() {
4041
if (!_octokitGraphql) {
4142
_octokitGraphql = OctokitGraphql.defaults({
4243
headers: {
43-
// Lazily access constants.ENV.SOCKET_SECURITY_GITHUB_PAT.
44-
authorization: `token ${constants.ENV.SOCKET_SECURITY_GITHUB_PAT}`
44+
// Lazily access constants.ENV properties.
45+
authorization: `token ${constants.ENV.SOCKET_SECURITY_GITHUB_PAT || constants.ENV.GITHUB_TOKEN}`
4546
}
4647
})
4748
}
@@ -364,12 +365,10 @@ export async function openPr(
364365
} as OpenPrOptions
365366
// Lazily access constants.ENV.GITHUB_ACTIONS.
366367
if (constants.ENV.GITHUB_ACTIONS) {
367-
// Lazily access constants.ENV.SOCKET_SECURITY_GITHUB_PAT.
368-
const pat = constants.ENV.SOCKET_SECURITY_GITHUB_PAT
369-
if (!pat) {
370-
throw new Error('Missing SOCKET_SECURITY_GITHUB_PAT environment variable')
371-
}
372-
const url = `https://x-access-token:${pat}@github.com/${owner}/${repo}`
368+
// Lazily access constants.ENV properties.
369+
const token =
370+
constants.ENV.SOCKET_SECURITY_GITHUB_PAT || constants.ENV.GITHUB_TOKEN
371+
const url = `https://x-access-token:${token}@github.com/${owner}/${repo}`
373372
await spawn('git', ['remote', 'set-url', 'origin', url], {
374373
cwd
375374
})

src/constants.mts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type ENV = Remap<
5050
GITHUB_REF_NAME: string
5151
GITHUB_REF_TYPE: string
5252
GITHUB_REPOSITORY: string
53+
GITHUB_TOKEN: string
5354
INLINED_CYCLONEDX_CDXGEN_VERSION: string
5455
INLINED_SOCKET_CLI_HOMEPAGE: string
5556
INLINED_SOCKET_CLI_LEGACY_BUILD: string
@@ -239,6 +240,10 @@ const LAZY_ENV = () => {
239240
// The owner and repository name. For example, octocat/Hello-World.
240241
// https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#default-environment-variables
241242
GITHUB_REPOSITORY: envAsString(env['GITHUB_REPOSITORY']),
243+
// The GITHUB_TOKEN secret is a GitHub App installation access token. The token's
244+
// permissions are limited to the repository that contains the workflow.
245+
// https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication#about-the-github_token-secret
246+
GITHUB_TOKEN: envAsString(env['GITHUB_TOKEN']),
242247
// Comp-time inlined @cyclonedx/cdxgen package version.
243248
// The '@rollup/plugin-replace' will replace "process.env['INLINED_CYCLONEDX_CDXGEN_VERSION']".
244249
INLINED_CYCLONEDX_CDXGEN_VERSION: envAsString(

0 commit comments

Comments
 (0)