Skip to content

Commit 671c420

Browse files
committed
feat(python-cli-parity): detect GitLab and Azure PR context
1 parent d811f62 commit 671c420

4 files changed

Lines changed: 105 additions & 24 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@
475475
!/src/commands/ci/fetch-default-org-slug.test.mts
476476
!/src/commands/ci/handle-ci.mts
477477
!/src/commands/ci/handle-ci.test.mts
478+
!/src/commands/ci/detect-ci-pull-request-context.mts
479+
!/src/commands/ci/detect-ci-pull-request-context.test.mts
478480
!/src/commands/cli.test.mts
479481
!/src/commands/config/cmd-config-auto.mts
480482
!/src/commands/config/cmd-config-auto.test.mts

‎src/commands/ci/detect-ci-pull-request-context.mts‎

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,69 @@ export type CiPullRequestContext = {
66
baseBranch: string | undefined
77
baseCommitHash: string | undefined
88
number: number
9-
provider: 'github' | undefined
9+
provider: 'azure' | 'github' | 'gitlab' | undefined
1010
}
1111

1212
export async function detectCiPullRequestContext(): Promise<CiPullRequestContext> {
13-
const match = /^refs\/pull\/(\d+)\//.exec(
14-
envAsString(process.env['GITHUB_REF']),
13+
const environment = process.env
14+
const githubMatch = /^refs\/pull\/(\d+)\//.exec(
15+
envAsString(environment['GITHUB_REF']),
1516
)
16-
if (!match) {
17+
if (githubMatch) {
18+
let baseCommitHash = envAsString(environment['GITHUB_BASE_SHA']) || undefined
19+
const eventPath = envAsString(environment['GITHUB_EVENT_PATH'])
20+
if (!baseCommitHash && eventPath) {
21+
try {
22+
const event = JSON.parse(await readFile(eventPath, 'utf8'))
23+
baseCommitHash =
24+
typeof event?.pull_request?.base?.sha === 'string'
25+
? event.pull_request.base.sha
26+
: undefined
27+
} catch {
28+
baseCommitHash = undefined
29+
}
30+
}
31+
1732
return {
18-
baseBranch: undefined,
19-
baseCommitHash: undefined,
20-
number: 0,
21-
provider: undefined,
33+
baseBranch: envAsString(environment['GITHUB_BASE_REF']) || undefined,
34+
baseCommitHash,
35+
number: Number(githubMatch[1]),
36+
provider: 'github',
37+
}
38+
}
39+
40+
const gitlabNumber = Number(envAsString(environment['CI_MERGE_REQUEST_IID']))
41+
if (Number.isSafeInteger(gitlabNumber) && gitlabNumber > 0) {
42+
return {
43+
baseBranch:
44+
envAsString(environment['CI_MERGE_REQUEST_TARGET_BRANCH_NAME']) ||
45+
undefined,
46+
baseCommitHash:
47+
envAsString(environment['CI_MERGE_REQUEST_DIFF_BASE_SHA']) || undefined,
48+
number: gitlabNumber,
49+
provider: 'gitlab',
2250
}
2351
}
2452

25-
let baseCommitHash = envAsString(process.env['GITHUB_BASE_SHA']) || undefined
26-
const eventPath = envAsString(process.env['GITHUB_EVENT_PATH'])
27-
if (!baseCommitHash && eventPath) {
28-
try {
29-
const event = JSON.parse(await readFile(eventPath, 'utf8'))
30-
baseCommitHash =
31-
typeof event?.pull_request?.base?.sha === 'string'
32-
? event.pull_request.base.sha
33-
: undefined
34-
} catch {
35-
baseCommitHash = undefined
53+
const azureNumber = Number(
54+
envAsString(environment['SYSTEM_PULLREQUEST_PULLREQUESTNUMBER']) ||
55+
envAsString(environment['SYSTEM_PULLREQUEST_PULLREQUESTID']),
56+
)
57+
if (Number.isSafeInteger(azureNumber) && azureNumber > 0) {
58+
return {
59+
baseBranch:
60+
envAsString(environment['SYSTEM_PULLREQUEST_TARGETBRANCH']) ||
61+
undefined,
62+
baseCommitHash: undefined,
63+
number: azureNumber,
64+
provider: 'azure',
3665
}
3766
}
3867

3968
return {
40-
baseBranch: envAsString(process.env['GITHUB_BASE_REF']) || undefined,
41-
baseCommitHash,
42-
number: Number(match[1]),
43-
provider: 'github',
69+
baseBranch: undefined,
70+
baseCommitHash: undefined,
71+
number: 0,
72+
provider: undefined,
4473
}
4574
}
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', async () => {
18+
vi.stubEnv('GITHUB_REF', 'refs/pull/42/merge')
19+
await expect(detectCiPullRequestContext()).resolves.toMatchObject({
20+
number: 42,
21+
provider: 'github',
22+
})
23+
})
24+
25+
it('detects GitLab merge requests', async () => {
26+
vi.stubEnv('CI_MERGE_REQUEST_IID', '17')
27+
await expect(detectCiPullRequestContext()).resolves.toMatchObject({
28+
number: 17,
29+
provider: 'gitlab',
30+
})
31+
})
32+
33+
it('detects Azure pull requests', async () => {
34+
vi.stubEnv('SYSTEM_PULLREQUEST_PULLREQUESTNUMBER', '9')
35+
await expect(detectCiPullRequestContext()).resolves.toMatchObject({
36+
number: 9,
37+
provider: 'azure',
38+
})
39+
})
40+
41+
it('ignores zero and malformed provider values', async () => {
42+
vi.stubEnv('CI_MERGE_REQUEST_IID', '0')
43+
vi.stubEnv('SYSTEM_PULLREQUEST_PULLREQUESTID', 'nope')
44+
await expect(detectCiPullRequestContext()).resolves.toMatchObject({
45+
number: 0,
46+
provider: undefined,
47+
})
48+
})
49+
})

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ export async function handleCi(autoManifest: boolean): Promise<void> {
2626
}
2727

2828
const orgSlug = orgSlugCResult.data
29+
const pullRequestContext = await detectCiPullRequestContext()
30+
const { number: pullRequest } = pullRequestContext
2931
const cwd = process.cwd()
3032
const branchName = (await gitBranch(cwd)) || (await detectDefaultBranch(cwd))
3133
const repoName = await getRepoName(cwd)
32-
const { number: pullRequest } = await detectCiPullRequestContext()
3334

3435
debugFn(
3536
'notice',

0 commit comments

Comments
 (0)