Skip to content

Commit f38a018

Browse files
committed
build: git context query format support
Signed-off-by: CrazyMax <[email protected]>
1 parent 08239d0 commit f38a018

File tree

5 files changed

+71
-104
lines changed

5 files changed

+71
-104
lines changed

__tests__/buildx/build.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import * as rimraf from 'rimraf';
2323
import {Context} from '../../src/context';
2424
import {Build} from '../../src/buildx/build';
2525

26+
import {GitContextFormat} from '../../src/types/buildx/build';
27+
2628
const fixturesDir = path.join(__dirname, '..', '.fixtures');
2729
const tmpDir = fs.mkdtempSync(path.join(process.env.TEMP || os.tmpdir(), 'buildx-build-'));
2830
const tmpName = path.join(tmpDir, '.tmpname-jest');
@@ -41,6 +43,45 @@ afterEach(() => {
4143
rimraf.sync(tmpDir);
4244
});
4345

46+
describe('gitContext', () => {
47+
const originalEnv = process.env;
48+
beforeEach(() => {
49+
jest.resetModules();
50+
process.env = {
51+
...originalEnv,
52+
DOCKER_GIT_CONTEXT_PR_HEAD_REF: ''
53+
};
54+
});
55+
afterEach(() => {
56+
process.env = originalEnv;
57+
});
58+
// prettier-ignore
59+
test.each([
60+
// no format set
61+
['refs/heads/master', undefined, false, 'https://github.com/docker/actions-toolkit.git?ref=refs/heads/master&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
62+
['master', undefined, false, 'https://github.com/docker/actions-toolkit.git?ref=refs/heads/master&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
63+
['refs/pull/15/merge', undefined, false, 'https://github.com/docker/actions-toolkit.git?ref=refs/pull/15/merge&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
64+
['refs/tags/v1.0.0', undefined, false, 'https://github.com/docker/actions-toolkit.git?ref=refs/tags/v1.0.0&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
65+
['refs/pull/15/merge', undefined, true, 'https://github.com/docker/actions-toolkit.git?ref=refs/pull/15/head&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
66+
// query format
67+
['refs/heads/master', 'query', false, 'https://github.com/docker/actions-toolkit.git?ref=refs/heads/master&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
68+
['master', 'query', false, 'https://github.com/docker/actions-toolkit.git?ref=refs/heads/master&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
69+
['refs/pull/15/merge', 'query', false, 'https://github.com/docker/actions-toolkit.git?ref=refs/pull/15/merge&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
70+
['refs/tags/v1.0.0', 'query', false, 'https://github.com/docker/actions-toolkit.git?ref=refs/tags/v1.0.0&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
71+
['refs/pull/15/merge', 'query', true, 'https://github.com/docker/actions-toolkit.git?ref=refs/pull/15/head&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
72+
// fragment format
73+
['refs/heads/master', 'fragment', false, 'https://github.com/docker/actions-toolkit.git#860c1904a1ce19322e91ac35af1ab07466440c37'],
74+
['master', 'fragment', false, 'https://github.com/docker/actions-toolkit.git#860c1904a1ce19322e91ac35af1ab07466440c37'],
75+
['refs/pull/15/merge', 'fragment', false, 'https://github.com/docker/actions-toolkit.git#refs/pull/15/merge'],
76+
['refs/tags/v1.0.0', 'fragment', false, 'https://github.com/docker/actions-toolkit.git#860c1904a1ce19322e91ac35af1ab07466440c37'],
77+
['refs/pull/15/merge', 'fragment', true, 'https://github.com/docker/actions-toolkit.git#refs/pull/15/head'],
78+
])('given %p and %p, should return %p', async (ref: string, format?: string, prHeadRef: boolean, expected: string) => {
79+
process.env.DOCKER_DEFAULT_GIT_CONTEXT_PR_HEAD_REF = prHeadRef ? 'true' : '';
80+
const build = new Build();
81+
expect(await build.gitContext(ref, '860c1904a1ce19322e91ac35af1ab07466440c37', format as GitContextFormat)).toEqual(expected);
82+
});
83+
});
84+
4485
describe('resolveImageID', () => {
4586
it('matches', async () => {
4687
const imageID = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9';

__tests__/context.test.ts

Lines changed: 0 additions & 79 deletions
This file was deleted.

src/buildx/build.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
import fs from 'fs';
1818
import path from 'path';
1919
import * as core from '@actions/core';
20+
import * as github from '@actions/github';
2021
import {parse} from 'csv-parse/sync';
2122

2223
import {Buildx} from './buildx';
2324
import {Context} from '../context';
2425
import {GitHub} from '../github';
2526
import {Util} from '../util';
2627

27-
import {BuildMetadata} from '../types/buildx/build';
28+
import {BuildMetadata, GitContextFormat} from '../types/buildx/build';
2829
import {VertexWarning} from '../types/buildkit/client';
2930
import {ProvenancePredicate} from '../types/intoto/slsa_provenance/v0.2/provenance';
3031

@@ -48,6 +49,32 @@ export class Build {
4849
this.metadataFilename = `build-metadata-${Util.generateRandomString()}.json`;
4950
}
5051

52+
public async gitContext(ref?: string, sha?: string, format?: GitContextFormat): Promise<string> {
53+
const setPullRequestHeadRef: boolean = !!(process.env.DOCKER_DEFAULT_GIT_CONTEXT_PR_HEAD_REF && process.env.DOCKER_DEFAULT_GIT_CONTEXT_PR_HEAD_REF === 'true');
54+
ref = ref || github.context.ref;
55+
sha = sha || github.context.sha;
56+
if (!ref.startsWith('refs/')) {
57+
ref = `refs/heads/${ref}`;
58+
} else if (ref.startsWith(`refs/pull/`) && setPullRequestHeadRef) {
59+
ref = ref.replace(/\/merge$/g, '/head');
60+
}
61+
const baseURL = `${GitHub.serverURL}/${github.context.repo.owner}/${github.context.repo.repo}.git`;
62+
if (!format) {
63+
if (await this.buildx.versionSatisfies('>=0.29.0')) {
64+
format = 'query';
65+
} else {
66+
format = 'fragment';
67+
}
68+
}
69+
if (format === 'query') {
70+
return `${baseURL}?ref=${ref}${sha !== '' ? `&checksum=${sha}` : ''}`;
71+
}
72+
if (sha && !ref.startsWith(`refs/pull/`)) {
73+
return `${baseURL}#${sha}`;
74+
}
75+
return `${baseURL}#${ref}`;
76+
}
77+
5178
public getImageIDFilePath(): string {
5279
return path.join(Context.tmpDir(), this.iidFilename);
5380
}

src/context.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ import fs from 'fs';
1818
import os from 'os';
1919
import path from 'path';
2020
import * as tmp from 'tmp';
21-
import * as github from '@actions/github';
22-
23-
import {GitHub} from './github';
2421

2522
export class Context {
2623
private static readonly _tmpDir = fs.mkdtempSync(path.join(Context.ensureDirExists(process.env.RUNNER_TEMP || os.tmpdir()), 'docker-actions-toolkit-'));
@@ -37,25 +34,4 @@ export class Context {
3734
public static tmpName(options?: tmp.TmpNameOptions): string {
3835
return tmp.tmpNameSync(options);
3936
}
40-
41-
public static gitRef(): string {
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}`;
49-
}
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');
54-
}
55-
return ref;
56-
}
57-
58-
public static gitContext(): string {
59-
return `${GitHub.serverURL}/${github.context.repo.owner}/${github.context.repo.repo}.git#${Context.gitRef()}`;
60-
}
6137
}

src/types/buildx/build.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
export type GitContextFormat = 'fragment' | 'query';
18+
1719
export type BuildMetadata = {
1820
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1921
[key: string]: any;

0 commit comments

Comments
 (0)