Skip to content

Commit

Permalink
context: opt-in pull request head ref
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Jan 13, 2025
1 parent 54bdcf6 commit 042dcea
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
36 changes: 35 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,8 +44,42 @@ 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_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');
});
});

describe('provenanceBuilderID', () => {
it('returns 2188748038', async () => {
expect(Context.provenanceBuilderID()).toEqual('https://github.com/docker/actions-toolkit/actions/runs/2188748038');
});
});
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_GIT_CONTEXT_PR_HEAD_REF && process.env.DOCKER_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 042dcea

Please sign in to comment.