From 671c4203e99e7a1d87b72adf9f9371286f9a6b2b Mon Sep 17 00:00:00 2001 From: jdalton Date: Wed, 25 Nov 2026 13:48:52 -0500 Subject: [PATCH] feat(python-cli-parity): detect GitLab and Azure PR context --- .gitignore | 2 + .../ci/detect-ci-pull-request-context.mts | 75 +++++++++++++------ .../detect-ci-pull-request-context.test.mts | 49 ++++++++++++ src/commands/ci/handle-ci.mts | 3 +- 4 files changed, 105 insertions(+), 24 deletions(-) create mode 100644 src/commands/ci/detect-ci-pull-request-context.test.mts diff --git a/.gitignore b/.gitignore index 17f4bed20..cecf2671d 100644 --- a/.gitignore +++ b/.gitignore @@ -475,6 +475,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..92a48c81c 100644 --- a/src/commands/ci/detect-ci-pull-request-context.mts +++ b/src/commands/ci/detect-ci-pull-request-context.mts @@ -6,40 +6,69 @@ 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']) || + 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..59316f57e --- /dev/null +++ b/src/commands/ci/detect-ci-pull-request-context.test.mts @@ -0,0 +1,49 @@ +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('detects Azure pull requests', async () => { + vi.stubEnv('SYSTEM_PULLREQUEST_PULLREQUESTNUMBER', '9') + await expect(detectCiPullRequestContext()).resolves.toMatchObject({ + 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 f801488ec..6e0ad3c83 100644 --- a/src/commands/ci/handle-ci.mts +++ b/src/commands/ci/handle-ci.mts @@ -26,10 +26,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',