Skip to content

Commit e46ec80

Browse files
authored
Merge pull request #551 from crazy-max/git-pr-head-ref-optin
context: opt-in pull request head ref
2 parents 9881e80 + e02b7d7 commit e46ec80

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

__tests__/context.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {describe, expect, jest, it, afterEach} from '@jest/globals';
17+
import {describe, expect, jest, it, afterEach, beforeEach, test} from '@jest/globals';
1818
import fs from 'fs';
1919
import os from 'os';
2020
import path from 'path';
@@ -44,6 +44,34 @@ describe('gitRef', () => {
4444
});
4545
});
4646

47+
describe('parseGitRef', () => {
48+
const originalEnv = process.env;
49+
beforeEach(() => {
50+
jest.resetModules();
51+
process.env = {
52+
...originalEnv,
53+
DOCKER_GIT_CONTEXT_PR_HEAD_REF: ''
54+
};
55+
});
56+
afterEach(() => {
57+
process.env = originalEnv;
58+
});
59+
// prettier-ignore
60+
test.each([
61+
['refs/heads/master', '860c1904a1ce19322e91ac35af1ab07466440c37', false, '860c1904a1ce19322e91ac35af1ab07466440c37'],
62+
['master', '860c1904a1ce19322e91ac35af1ab07466440c37', false, '860c1904a1ce19322e91ac35af1ab07466440c37'],
63+
['refs/pull/15/merge', '860c1904a1ce19322e91ac35af1ab07466440c37', false, 'refs/pull/15/merge'],
64+
['refs/heads/master', '', false, 'refs/heads/master'],
65+
['master', '', false, 'master'],
66+
['refs/tags/v1.0.0', '', false, 'refs/tags/v1.0.0'],
67+
['refs/pull/15/merge', '', false, 'refs/pull/15/merge'],
68+
['refs/pull/15/merge', '', true, 'refs/pull/15/head'],
69+
])('given %p and %p, should return %p', async (ref: string, sha: string, prHeadRef: boolean, expected: string) => {
70+
process.env.DOCKER_DEFAULT_GIT_CONTEXT_PR_HEAD_REF = prHeadRef ? 'true' : '';
71+
expect(Context.parseGitRef(ref, sha)).toEqual(expected);
72+
});
73+
});
74+
4775
describe('gitContext', () => {
4876
it('returns refs/heads/master', async () => {
4977
expect(Context.gitContext()).toEqual('https://github.com/docker/actions-toolkit.git#refs/heads/master');

src/context.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,20 @@ export class Context {
3939
}
4040

4141
public static gitRef(): string {
42-
let gitRef = github.context.ref;
43-
if (github.context.sha && gitRef && !gitRef.startsWith('refs/')) {
44-
gitRef = `refs/heads/${github.context.ref}`;
42+
return Context.parseGitRef(github.context.ref, github.context.sha);
43+
}
44+
45+
public static parseGitRef(ref: string, sha: string): string {
46+
const setPullRequestHeadRef: boolean = !!(process.env.DOCKER_DEFAULT_GIT_CONTEXT_PR_HEAD_REF && process.env.DOCKER_DEFAULT_GIT_CONTEXT_PR_HEAD_REF === 'true');
47+
if (sha && ref && !ref.startsWith('refs/')) {
48+
ref = `refs/heads/${ref}`;
4549
}
46-
if (github.context.sha && !gitRef.startsWith(`refs/pull/`)) {
47-
gitRef = github.context.sha;
48-
} else if (gitRef.startsWith(`refs/pull/`)) {
49-
gitRef = gitRef.replace(/\/merge$/g, '/head');
50+
if (sha && !ref.startsWith(`refs/pull/`)) {
51+
ref = sha;
52+
} else if (ref.startsWith(`refs/pull/`) && setPullRequestHeadRef) {
53+
ref = ref.replace(/\/merge$/g, '/head');
5054
}
51-
return gitRef;
55+
return ref;
5256
}
5357

5458
public static gitContext(): string {

0 commit comments

Comments
 (0)