Skip to content

Commit

Permalink
Merge pull request #551 from crazy-max/git-pr-head-ref-optin
Browse files Browse the repository at this point in the history
context: opt-in pull request head ref
  • Loading branch information
crazy-max authored Jan 15, 2025
2 parents 9881e80 + e02b7d7 commit e46ec80
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
30 changes: 29 additions & 1 deletion __tests__/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {describe, expect, jest, it, afterEach} from '@jest/globals';
import {describe, expect, jest, it, afterEach, beforeEach, test} from '@jest/globals';
import fs from 'fs';
import os from 'os';
import path from 'path';
Expand Down Expand Up @@ -44,6 +44,34 @@ describe('gitRef', () => {
});
});

describe('parseGitRef', () => {
const originalEnv = process.env;
beforeEach(() => {
jest.resetModules();
process.env = {
...originalEnv,
DOCKER_GIT_CONTEXT_PR_HEAD_REF: ''
};
});
afterEach(() => {
process.env = originalEnv;
});
// prettier-ignore
test.each([
['refs/heads/master', '860c1904a1ce19322e91ac35af1ab07466440c37', false, '860c1904a1ce19322e91ac35af1ab07466440c37'],
['master', '860c1904a1ce19322e91ac35af1ab07466440c37', false, '860c1904a1ce19322e91ac35af1ab07466440c37'],
['refs/pull/15/merge', '860c1904a1ce19322e91ac35af1ab07466440c37', false, 'refs/pull/15/merge'],
['refs/heads/master', '', false, 'refs/heads/master'],
['master', '', false, 'master'],
['refs/tags/v1.0.0', '', false, 'refs/tags/v1.0.0'],
['refs/pull/15/merge', '', false, 'refs/pull/15/merge'],
['refs/pull/15/merge', '', true, 'refs/pull/15/head'],
])('given %p and %p, should return %p', async (ref: string, sha: string, prHeadRef: boolean, expected: string) => {
process.env.DOCKER_DEFAULT_GIT_CONTEXT_PR_HEAD_REF = prHeadRef ? 'true' : '';
expect(Context.parseGitRef(ref, sha)).toEqual(expected);
});
});

describe('gitContext', () => {
it('returns refs/heads/master', async () => {
expect(Context.gitContext()).toEqual('https://github.com/docker/actions-toolkit.git#refs/heads/master');
Expand Down
20 changes: 12 additions & 8 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,20 @@ export class Context {
}

public static gitRef(): string {
let gitRef = github.context.ref;
if (github.context.sha && gitRef && !gitRef.startsWith('refs/')) {
gitRef = `refs/heads/${github.context.ref}`;
return Context.parseGitRef(github.context.ref, github.context.sha);
}

public static parseGitRef(ref: string, sha: string): string {
const setPullRequestHeadRef: boolean = !!(process.env.DOCKER_DEFAULT_GIT_CONTEXT_PR_HEAD_REF && process.env.DOCKER_DEFAULT_GIT_CONTEXT_PR_HEAD_REF === 'true');
if (sha && ref && !ref.startsWith('refs/')) {
ref = `refs/heads/${ref}`;
}
if (github.context.sha && !gitRef.startsWith(`refs/pull/`)) {
gitRef = github.context.sha;
} else if (gitRef.startsWith(`refs/pull/`)) {
gitRef = gitRef.replace(/\/merge$/g, '/head');
if (sha && !ref.startsWith(`refs/pull/`)) {
ref = sha;
} else if (ref.startsWith(`refs/pull/`) && setPullRequestHeadRef) {
ref = ref.replace(/\/merge$/g, '/head');
}
return gitRef;
return ref;
}

public static gitContext(): string {
Expand Down

0 comments on commit e46ec80

Please sign in to comment.