Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 52 additions & 23 deletions src/commands/ci/detect-ci-pull-request-context.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CiPullRequestContext> {
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,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Azure base branch keeps ref prefix

Medium Severity

Azure's SYSTEM_PULLREQUEST_TARGETBRANCH is a full ref such as refs/heads/main, while GitHub's GITHUB_BASE_REF and GitLab's CI_MERGE_REQUEST_TARGET_BRANCH_NAME are bare branch names. Azure baseBranch therefore does not match the other providers, so git lookups and default-branch comparisons against that value miss.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 671c420. Configure here.

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,
}
}
49 changes: 49 additions & 0 deletions src/commands/ci/detect-ci-pull-request-context.test.mts
Original file line number Diff line number Diff line change
@@ -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,
})
})
})
3 changes: 2 additions & 1 deletion src/commands/ci/handle-ci.mts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ export async function handleCi(autoManifest: boolean): Promise<void> {
}

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',
Expand Down