Skip to content

fix: use raw ref for PR branch to avoid confusion bw upstream and fork #1132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 8 additions & 10 deletions src/ci_providers/provider_githubactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,12 @@ async function _getBuildURL(inputs: UploaderInputs): Promise<string> {

function _getBranch(inputs: UploaderInputs): string {
const { args, envs } = inputs
const branchRegex = /refs\/heads\/(.*)/
const branchMatches = branchRegex.exec(envs.GITHUB_REF || '')
let branch
if (branchMatches) {
branch = branchMatches[1]
const triggerName = envs.GITHUB_EVENT_NAME
if (triggerName == "pull_request" || triggerName == "pull_request_target") {
return args.branch || envs.GITHUB_REF || ''
} else {
return args.branch || envs.GITHUB_REF_NAME || ''
}

if (envs.GITHUB_HEAD_REF && envs.GITHUB_HEAD_REF !== '') {
branch = envs.GITHUB_HEAD_REF
}
return args.branch || branch || ''
}

function _getJob(envs: UploaderEnvs): string {
Expand Down Expand Up @@ -138,6 +133,7 @@ export async function getServiceParams(inputs: UploaderInputs): Promise<IService
}
}

// https://docs.github.com/en/actions/learn-github-actions/variables
export function getEnvVarNames(): string[] {
return [
'GITHUB_ACTION',
Expand All @@ -148,5 +144,7 @@ export function getEnvVarNames(): string[] {
'GITHUB_SERVER_URL',
'GITHUB_SHA',
'GITHUB_WORKFLOW',
'GITHUB_EVENT_NAME',
'GITHUB_REF_NAME',
]
}
33 changes: 24 additions & 9 deletions test/providers/provider_githubactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('GitHub Actions Params', () => {
args: { ...createEmptyArgs() },
envs: {
GITHUB_ACTIONS: 'true',
GITHUB_REF: 'refs/heads/master',
GITHUB_REF_NAME: 'master',
GITHUB_REPOSITORY: 'testOrg/testRepo',
GITHUB_RUN_ID: '2',
GITHUB_SERVER_URL: 'https://github.com',
Expand All @@ -76,7 +76,7 @@ describe('GitHub Actions Params', () => {
})

it('gets correct params for a PR', async () => {
const inputs: UploaderInputs = {
let inputs: UploaderInputs = {
args: { ...createEmptyArgs() },
envs: {
GITHUB_ACTIONS: 'true',
Expand All @@ -87,10 +87,11 @@ describe('GitHub Actions Params', () => {
GITHUB_SERVER_URL: 'https://github.com',
GITHUB_SHA: 'testingsha',
GITHUB_WORKFLOW: 'testWorkflow',
GITHUB_EVENT_NAME: 'pull_request',
},
}
const expected: IServiceParams = {
branch: 'branch',
branch: 'refs/pull/1/merge',
build: '2',
buildURL: 'https://github.com/testOrg/testRepo/actions/runs/2',
commit: 'testingsha',
Expand All @@ -100,13 +101,23 @@ describe('GitHub Actions Params', () => {
slug: 'testOrg/testRepo',
}

const spawnSync = td.replace(childProcess, 'spawnSync')
let spawnSync = td.replace(childProcess, 'spawnSync')
td.when(
spawnSync('git', ['show', '--no-patch', '--format=%P'], { maxBuffer: SPAWNPROCESSBUFFERSIZE }),
).thenReturn({
stdout: Buffer.from('testingsha'),
})
const params = await providerGitHubactions.getServiceParams(inputs)
let params = await providerGitHubactions.getServiceParams(inputs)
expect(params).toMatchObject(expected)

inputs["envs"]["GITHUB_EVENT_NAME"] = 'pull_request_target'
spawnSync = td.replace(childProcess, 'spawnSync')
td.when(
spawnSync('git', ['show', '--no-patch', '--format=%P'], { maxBuffer: SPAWNPROCESSBUFFERSIZE }),
).thenReturn({
stdout: 'testingsha',
})
params = await providerGitHubactions.getServiceParams(inputs)
expect(params).toMatchObject(expected)
})

Expand All @@ -123,10 +134,11 @@ describe('GitHub Actions Params', () => {
GITHUB_SERVER_URL: 'https://github.com',
GITHUB_SHA: 'testingsha',
GITHUB_WORKFLOW: 'testWorkflow',
GITHUB_EVENT_NAME: 'pull_request',
},
}
const expected: IServiceParams = {
branch: 'branch',
branch: 'refs/pull/1/merge',
build: '2',
buildURL: 'https://github.com/testOrg/testRepo/actions/runs/2',
commit: 'testingsha',
Expand Down Expand Up @@ -182,10 +194,11 @@ describe('GitHub Actions Params', () => {
GITHUB_SERVER_URL: 'https://github.com',
GITHUB_SHA: 'testingsha',
GITHUB_WORKFLOW: 'testWorkflow',
GITHUB_EVENT_NAME: 'pull_request',
},
}
const expected: IServiceParams = {
branch: 'branch',
branch: 'refs/pull/1/merge',
build: '2',
buildURL: 'https://github.com/testOrg/testRepo/actions/runs/2/jobs/2',
commit: 'testingsha',
Expand Down Expand Up @@ -240,10 +253,11 @@ describe('GitHub Actions Params', () => {
GITHUB_SERVER_URL: 'https://github.com',
GITHUB_SHA: 'testingmergecommitsha',
GITHUB_WORKFLOW: 'testWorkflow',
GITHUB_EVENT_NAME: 'pull_request',
},
}
const expected: IServiceParams = {
branch: 'branch',
branch: 'refs/pull/1/merge',
build: '2',
buildURL: 'https://github.com/testOrg/testRepo/actions/runs/2',
commit: 'testingmergecommitsha2345678901234567890',
Expand Down Expand Up @@ -312,10 +326,11 @@ describe('GitHub Actions Params', () => {
GITHUB_SERVER_URL: 'https://github.com',
GITHUB_SHA: 'testingsha',
GITHUB_WORKFLOW: 'testWorkflow',
GITHUB_EVENT_NAME: 'pull_request',
},
}
const expected: IServiceParams = {
branch: 'branch',
branch: 'refs/pull/1/merge',
build: '2',
buildURL: 'https://github.com/testOrg/testRepo/actions/runs/2',
commit: 'testingsha',
Expand Down