Skip to content

Commit 5292140

Browse files
authored
feat(python-cli-parity): detect GitLab and Azure PR context (#1772)
1 parent 247a9cf commit 5292140

5 files changed

Lines changed: 118 additions & 24 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@
367367
!/src/commands/ci/fetch-default-org-slug.test.mts
368368
!/src/commands/ci/handle-ci.mts
369369
!/src/commands/ci/handle-ci.test.mts
370+
!/src/commands/ci/detect-ci-pull-request-context.mts
371+
!/src/commands/ci/detect-ci-pull-request-context.test.mts
370372
!/src/commands/cli.test.mts
371373
!/src/commands/config/cmd-config-auto.mts
372374
!/src/commands/config/cmd-config-auto.test.mts

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

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,72 @@ 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']).replace(
61+
/^refs\/heads\//u,
62+
'',
63+
) ||
64+
undefined,
65+
baseCommitHash: undefined,
66+
number: azureNumber,
67+
provider: 'azure',
3668
}
3769
}
3870

3971
return {
40-
baseBranch: envAsString(process.env['GITHUB_BASE_REF']) || undefined,
41-
baseCommitHash,
42-
number: Number(match[1]),
43-
provider: 'github',
72+
baseBranch: undefined,
73+
baseCommitHash: undefined,
74+
number: 0,
75+
provider: undefined,
4476
}
4577
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.each(['refs/heads/release/v1', 'release/v1'])(
34+
'detects an Azure pull request targeting %s',
35+
async target => {
36+
vi.stubEnv('SYSTEM_PULLREQUEST_PULLREQUESTNUMBER', '9')
37+
vi.stubEnv('SYSTEM_PULLREQUEST_TARGETBRANCH', target)
38+
await expect(detectCiPullRequestContext()).resolves.toMatchObject({
39+
baseBranch: 'release/v1',
40+
number: 9,
41+
provider: 'azure',
42+
})
43+
},
44+
)
45+
46+
it('ignores zero and malformed provider values', async () => {
47+
vi.stubEnv('CI_MERGE_REQUEST_IID', '0')
48+
vi.stubEnv('SYSTEM_PULLREQUEST_PULLREQUESTID', 'nope')
49+
await expect(detectCiPullRequestContext()).resolves.toMatchObject({
50+
number: 0,
51+
provider: undefined,
52+
})
53+
})
54+
})

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

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

2929
const orgSlug = orgSlugCResult.data
30+
const pullRequestContext = await detectCiPullRequestContext()
31+
const { number: pullRequest } = pullRequestContext
3032
const cwd = process.cwd()
3133
const branchName = (await gitBranch(cwd)) || (await detectDefaultBranch(cwd))
3234
const repoName = await getRepoName(cwd)
33-
const { number: pullRequest } = await detectCiPullRequestContext()
3435

3536
debugFn(
3637
'notice',

‎src/commands/manifest/bazel/bazel-native.e2e.test.mts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ const FIXTURES = path.resolve(
3333
const EXPECTED: unknown = JSON.parse(
3434
readFileSync(path.join(FIXTURES, 'maven.expected.json'), 'utf8'),
3535
)
36+
// Match the packaged CLI budget for cold Maven resolution on CI runners.
37+
const MAVEN_QUERY_TIMEOUT_MS = 120_000
38+
3639
const CLI =
3740
process.env['SOCKET_BAZEL_TEST_CLI'] ??
3841
path.resolve(import.meta.dirname, '../../../../bin/cli.js')
@@ -117,6 +120,7 @@ describe.skipIf(process.env['SOCKET_BAZEL_E2E'] !== '1')(
117120
cwd: scratch,
118121
out,
119122
env: { SOCKET_BAZEL_OUTPUT_ROOT: outputParent },
123+
perRepoTimeoutMs: MAVEN_QUERY_TIMEOUT_MS,
120124
verbose: true,
121125
})
122126
expect(result.status).toBe('complete')
@@ -170,6 +174,7 @@ describe.skipIf(process.env['SOCKET_BAZEL_E2E'] !== '1')(
170174
out: scratch,
171175
outLayout: 'flat',
172176
env: { SOCKET_BAZEL_OUTPUT_ROOT: outputParent },
177+
perRepoTimeoutMs: MAVEN_QUERY_TIMEOUT_MS,
173178
verbose: true,
174179
})
175180
expect(result.status).toBe('complete')

0 commit comments

Comments
 (0)