diff --git a/.gitignore b/.gitignore index e361e5649..a3dfeb252 100644 --- a/.gitignore +++ b/.gitignore @@ -367,6 +367,8 @@ !/src/commands/ci/fetch-default-org-slug.test.mts !/src/commands/ci/handle-ci.mts !/src/commands/ci/handle-ci.test.mts +!/src/commands/ci/detect-ci-pull-request-context.mts +!/src/commands/ci/detect-ci-pull-request-context.test.mts !/src/commands/cli.test.mts !/src/commands/config/cmd-config-auto.mts !/src/commands/config/cmd-config-auto.test.mts diff --git a/src/commands/ci/detect-ci-pull-request-context.mts b/src/commands/ci/detect-ci-pull-request-context.mts index b2d657105..47d4cfdeb 100644 --- a/src/commands/ci/detect-ci-pull-request-context.mts +++ b/src/commands/ci/detect-ci-pull-request-context.mts @@ -6,40 +6,72 @@ export type CiPullRequestContext = { baseBranch: string | undefined baseCommitHash: string | undefined number: number - provider: 'github' | undefined + provider: 'azure' | 'github' | 'gitlab' | undefined } export async function detectCiPullRequestContext(): Promise { - const match = /^refs\/pull\/(\d+)\//.exec( - envAsString(process.env['GITHUB_REF']), + const environment = process.env + const githubMatch = /^refs\/pull\/(\d+)\//.exec( + envAsString(environment['GITHUB_REF']), ) - if (!match) { + if (githubMatch) { + let baseCommitHash = envAsString(environment['GITHUB_BASE_SHA']) || undefined + const eventPath = envAsString(environment['GITHUB_EVENT_PATH']) + if (!baseCommitHash && eventPath) { + try { + const event = JSON.parse(await readFile(eventPath, 'utf8')) + baseCommitHash = + typeof event?.pull_request?.base?.sha === 'string' + ? event.pull_request.base.sha + : undefined + } catch { + baseCommitHash = undefined + } + } + return { - baseBranch: undefined, - baseCommitHash: undefined, - number: 0, - provider: undefined, + baseBranch: envAsString(environment['GITHUB_BASE_REF']) || undefined, + baseCommitHash, + number: Number(githubMatch[1]), + provider: 'github', + } + } + + const gitlabNumber = Number(envAsString(environment['CI_MERGE_REQUEST_IID'])) + if (Number.isSafeInteger(gitlabNumber) && gitlabNumber > 0) { + return { + baseBranch: + envAsString(environment['CI_MERGE_REQUEST_TARGET_BRANCH_NAME']) || + undefined, + baseCommitHash: + envAsString(environment['CI_MERGE_REQUEST_DIFF_BASE_SHA']) || undefined, + number: gitlabNumber, + provider: 'gitlab', } } - let baseCommitHash = envAsString(process.env['GITHUB_BASE_SHA']) || undefined - const eventPath = envAsString(process.env['GITHUB_EVENT_PATH']) - if (!baseCommitHash && eventPath) { - try { - const event = JSON.parse(await readFile(eventPath, 'utf8')) - baseCommitHash = - typeof event?.pull_request?.base?.sha === 'string' - ? event.pull_request.base.sha - : undefined - } catch { - baseCommitHash = undefined + const azureNumber = Number( + envAsString(environment['SYSTEM_PULLREQUEST_PULLREQUESTNUMBER']) || + envAsString(environment['SYSTEM_PULLREQUEST_PULLREQUESTID']), + ) + if (Number.isSafeInteger(azureNumber) && azureNumber > 0) { + return { + baseBranch: + envAsString(environment['SYSTEM_PULLREQUEST_TARGETBRANCH']).replace( + /^refs\/heads\//u, + '', + ) || + undefined, + baseCommitHash: undefined, + number: azureNumber, + provider: 'azure', } } return { - baseBranch: envAsString(process.env['GITHUB_BASE_REF']) || undefined, - baseCommitHash, - number: Number(match[1]), - provider: 'github', + baseBranch: undefined, + baseCommitHash: undefined, + number: 0, + provider: undefined, } } diff --git a/src/commands/ci/detect-ci-pull-request-context.test.mts b/src/commands/ci/detect-ci-pull-request-context.test.mts new file mode 100644 index 000000000..59929cf9e --- /dev/null +++ b/src/commands/ci/detect-ci-pull-request-context.test.mts @@ -0,0 +1,54 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +import { detectCiPullRequestContext } from './detect-ci-pull-request-context.mts' + +afterEach(() => { + vi.unstubAllEnvs() +}) + +beforeEach(() => { + vi.stubEnv('GITHUB_REF', '') + vi.stubEnv('CI_MERGE_REQUEST_IID', '') + vi.stubEnv('SYSTEM_PULLREQUEST_PULLREQUESTNUMBER', '') + vi.stubEnv('SYSTEM_PULLREQUEST_PULLREQUESTID', '') +}) + +describe('detectCiPullRequestContext', () => { + it('detects GitHub pull requests', async () => { + vi.stubEnv('GITHUB_REF', 'refs/pull/42/merge') + await expect(detectCiPullRequestContext()).resolves.toMatchObject({ + number: 42, + provider: 'github', + }) + }) + + it('detects GitLab merge requests', async () => { + vi.stubEnv('CI_MERGE_REQUEST_IID', '17') + await expect(detectCiPullRequestContext()).resolves.toMatchObject({ + number: 17, + provider: 'gitlab', + }) + }) + + it.each(['refs/heads/release/v1', 'release/v1'])( + 'detects an Azure pull request targeting %s', + async target => { + vi.stubEnv('SYSTEM_PULLREQUEST_PULLREQUESTNUMBER', '9') + vi.stubEnv('SYSTEM_PULLREQUEST_TARGETBRANCH', target) + await expect(detectCiPullRequestContext()).resolves.toMatchObject({ + baseBranch: 'release/v1', + number: 9, + provider: 'azure', + }) + }, + ) + + it('ignores zero and malformed provider values', async () => { + vi.stubEnv('CI_MERGE_REQUEST_IID', '0') + vi.stubEnv('SYSTEM_PULLREQUEST_PULLREQUESTID', 'nope') + await expect(detectCiPullRequestContext()).resolves.toMatchObject({ + number: 0, + provider: undefined, + }) + }) +}) diff --git a/src/commands/ci/handle-ci.mts b/src/commands/ci/handle-ci.mts index 1c12f04bb..624668832 100644 --- a/src/commands/ci/handle-ci.mts +++ b/src/commands/ci/handle-ci.mts @@ -27,10 +27,11 @@ export async function handleCi(autoManifest: boolean): Promise { } const orgSlug = orgSlugCResult.data + const pullRequestContext = await detectCiPullRequestContext() + const { number: pullRequest } = pullRequestContext const cwd = process.cwd() const branchName = (await gitBranch(cwd)) || (await detectDefaultBranch(cwd)) const repoName = await getRepoName(cwd) - const { number: pullRequest } = await detectCiPullRequestContext() debugFn( 'notice', diff --git a/src/commands/manifest/bazel/bazel-native.e2e.test.mts b/src/commands/manifest/bazel/bazel-native.e2e.test.mts index cf88eb372..955a13a08 100644 --- a/src/commands/manifest/bazel/bazel-native.e2e.test.mts +++ b/src/commands/manifest/bazel/bazel-native.e2e.test.mts @@ -33,6 +33,9 @@ const FIXTURES = path.resolve( const EXPECTED: unknown = JSON.parse( readFileSync(path.join(FIXTURES, 'maven.expected.json'), 'utf8'), ) +// Match the packaged CLI budget for cold Maven resolution on CI runners. +const MAVEN_QUERY_TIMEOUT_MS = 120_000 + const CLI = process.env['SOCKET_BAZEL_TEST_CLI'] ?? path.resolve(import.meta.dirname, '../../../../bin/cli.js') @@ -117,6 +120,7 @@ describe.skipIf(process.env['SOCKET_BAZEL_E2E'] !== '1')( cwd: scratch, out, env: { SOCKET_BAZEL_OUTPUT_ROOT: outputParent }, + perRepoTimeoutMs: MAVEN_QUERY_TIMEOUT_MS, verbose: true, }) expect(result.status).toBe('complete') @@ -170,6 +174,7 @@ describe.skipIf(process.env['SOCKET_BAZEL_E2E'] !== '1')( out: scratch, outLayout: 'flat', env: { SOCKET_BAZEL_OUTPUT_ROOT: outputParent }, + perRepoTimeoutMs: MAVEN_QUERY_TIMEOUT_MS, verbose: true, }) expect(result.status).toBe('complete')