Skip to content

Commit 1fdca30

Browse files
committed
feat(python-cli-parity): detect GitLab and Azure PR context
1 parent 025e1e9 commit 1fdca30

4 files changed

Lines changed: 87 additions & 6 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,8 @@
472472
!/src/commands/ci/fetch-default-org-slug.test.mts
473473
!/src/commands/ci/handle-ci.mts
474474
!/src/commands/ci/handle-ci.test.mts
475+
!/src/commands/ci/detect-ci-pull-request-context.mts
476+
!/src/commands/ci/detect-ci-pull-request-context.test.mts
475477
!/src/commands/cli.test.mts
476478
!/src/commands/config/cmd-config-auto.mts
477479
!/src/commands/config/cmd-config-auto.test.mts
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export type CiPullRequestContext = {
2+
number: number
3+
provider: 'azure' | 'github' | 'gitlab' | undefined
4+
}
5+
6+
export function detectCiPullRequestContext(): CiPullRequestContext {
7+
const environment = process.env
8+
const githubRef = environment['GITHUB_REF'] ?? ''
9+
const githubMatch = /^refs\/pull\/(\d+)\//.exec(githubRef)
10+
if (githubMatch) {
11+
return { number: Number(githubMatch[1]), provider: 'github' }
12+
}
13+
14+
const gitlabNumber = Number(environment['CI_MERGE_REQUEST_IID'] ?? '')
15+
if (Number.isSafeInteger(gitlabNumber) && gitlabNumber > 0) {
16+
return { number: gitlabNumber, provider: 'gitlab' }
17+
}
18+
19+
if (environment['SYSTEM_PULLREQUEST_PULLREQUESTNUMBER']) {
20+
const azureNumber = Number(
21+
environment['SYSTEM_PULLREQUEST_PULLREQUESTNUMBER'],
22+
)
23+
if (Number.isSafeInteger(azureNumber) && azureNumber > 0) {
24+
return { number: azureNumber, provider: 'azure' }
25+
}
26+
}
27+
28+
const azureId = Number(environment['SYSTEM_PULLREQUEST_PULLREQUESTID'] ?? '')
29+
return Number.isSafeInteger(azureId) && azureId > 0
30+
? { number: azureId, provider: 'azure' }
31+
: { number: 0, provider: undefined }
32+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2+
3+
import { detectCiPullRequestContext } from './detect-ci-pull-request-context.mts'
4+
5+
afterEach(() => {
6+
vi.unstubAllEnvs()
7+
})
8+
9+
beforeEach(() => {
10+
vi.stubEnv('GITHUB_REF', '')
11+
vi.stubEnv('CI_MERGE_REQUEST_IID', '')
12+
vi.stubEnv('SYSTEM_PULLREQUEST_PULLREQUESTNUMBER', '')
13+
vi.stubEnv('SYSTEM_PULLREQUEST_PULLREQUESTID', '')
14+
})
15+
16+
describe('detectCiPullRequestContext', () => {
17+
it('detects GitHub pull requests', () => {
18+
vi.stubEnv('GITHUB_REF', 'refs/pull/42/merge')
19+
expect(detectCiPullRequestContext()).toEqual({
20+
number: 42,
21+
provider: 'github',
22+
})
23+
})
24+
25+
it('detects GitLab merge requests', () => {
26+
vi.stubEnv('CI_MERGE_REQUEST_IID', '17')
27+
expect(detectCiPullRequestContext()).toEqual({
28+
number: 17,
29+
provider: 'gitlab',
30+
})
31+
})
32+
33+
it('detects Azure pull requests', () => {
34+
vi.stubEnv('SYSTEM_PULLREQUEST_PULLREQUESTNUMBER', '9')
35+
expect(detectCiPullRequestContext()).toEqual({
36+
number: 9,
37+
provider: 'azure',
38+
})
39+
})
40+
41+
it('ignores zero and malformed provider values', () => {
42+
vi.stubEnv('CI_MERGE_REQUEST_IID', '0')
43+
vi.stubEnv('SYSTEM_PULLREQUEST_PULLREQUESTID', 'nope')
44+
expect(detectCiPullRequestContext()).toEqual({
45+
number: 0,
46+
provider: undefined,
47+
})
48+
})
49+
})

‎src/commands/ci/handle-ci.mts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { detectCiPullRequestContext } from './detect-ci-pull-request-context.mts'
12
import { getDefaultOrgSlug } from './fetch-default-org-slug.mts'
23
import constants from '../../constants.mts'
34
import { debugDir, debugFn } from '../../utils/debug.mts'
4-
import { envAsString } from '../../utils/env.mts'
55
import {
66
detectDefaultBranch,
77
getRepoName,
@@ -18,10 +18,7 @@ import { handleCreateNewScan } from '../scan/handle-create-new-scan.mts'
1818
* `pull_request` for falsy values).
1919
*/
2020
export function detectCiPullRequestNumber(): number {
21-
const match = /^refs\/pull\/(\d+)\//.exec(
22-
envAsString(process.env['GITHUB_REF']),
23-
)
24-
return match ? Number(match[1]) : 0
21+
return detectCiPullRequestContext().number
2522
}
2623

2724
export async function handleCi(autoManifest: boolean): Promise<void> {
@@ -39,6 +36,7 @@ export async function handleCi(autoManifest: boolean): Promise<void> {
3936
}
4037

4138
const orgSlug = orgSlugCResult.data
39+
const { number: pullRequest } = detectCiPullRequestContext()
4240
const cwd = process.cwd()
4341
const branchName = (await gitBranch(cwd)) || (await detectDefaultBranch(cwd))
4442
const repoName = await getRepoName(cwd)
@@ -62,7 +60,7 @@ export async function handleCi(autoManifest: boolean): Promise<void> {
6260
outputKind: 'json',
6361
// When 'pendingHead' is true, it requires 'branchName' set and 'tmp' false.
6462
pendingHead: true,
65-
pullRequest: detectCiPullRequestNumber(),
63+
pullRequest,
6664
reach: {
6765
dynamicSbomInference: undefined,
6866
excludePaths: [],

0 commit comments

Comments
 (0)