build: git context query format support
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import * as core from '@actions/core';
|
||||
import * as github from '@actions/github';
|
||||
import {parse} from 'csv-parse/sync';
|
||||
|
||||
import {Buildx} from './buildx.js';
|
||||
@@ -24,7 +25,7 @@ import {Context} from '../context.js';
|
||||
import {GitHub} from '../github/github.js';
|
||||
import {Util} from '../util.js';
|
||||
|
||||
import {BuildMetadata} from '../types/buildx/build.js';
|
||||
import {BuildMetadata, GitContextFormat} from '../types/buildx/build.js';
|
||||
import {VertexWarning} from '../types/buildkit/client.js';
|
||||
import {ProvenancePredicate} from '../types/intoto/slsa_provenance/v0.2/provenance.js';
|
||||
|
||||
@@ -48,6 +49,33 @@ export class Build {
|
||||
this.metadataFilename = `build-metadata-${Util.generateRandomString()}.json`;
|
||||
}
|
||||
|
||||
public async gitContext(ref?: string, sha?: string, format?: GitContextFormat): Promise<string> {
|
||||
const setPullRequestHeadRef = Util.parseBoolOrDefault(process.env.DOCKER_DEFAULT_GIT_CONTEXT_PR_HEAD_REF);
|
||||
ref = ref || github.context.ref;
|
||||
sha = sha || github.context.sha;
|
||||
if (!ref.startsWith('refs/')) {
|
||||
ref = `refs/heads/${ref}`;
|
||||
} else if (ref.startsWith(`refs/pull/`) && setPullRequestHeadRef) {
|
||||
ref = ref.replace(/\/merge$/g, '/head');
|
||||
}
|
||||
const baseURL = `${GitHub.serverURL}/${github.context.repo.owner}/${github.context.repo.repo}.git`;
|
||||
if (!format) {
|
||||
const sendGitQueryAsInput = Util.parseBoolOrDefault(process.env.BUILDX_SEND_GIT_QUERY_AS_INPUT);
|
||||
if (sendGitQueryAsInput && (await this.buildx.versionSatisfies('>=0.29.0'))) {
|
||||
format = 'query';
|
||||
} else {
|
||||
format = 'fragment';
|
||||
}
|
||||
}
|
||||
if (format === 'query') {
|
||||
return `${baseURL}?ref=${ref}${sha ? `&checksum=${sha}` : ''}`;
|
||||
}
|
||||
if (sha && !ref.startsWith(`refs/pull/`)) {
|
||||
return `${baseURL}#${sha}`;
|
||||
}
|
||||
return `${baseURL}#${ref}`;
|
||||
}
|
||||
|
||||
public getImageIDFilePath(): string {
|
||||
return path.join(Context.tmpDir(), this.iidFilename);
|
||||
}
|
||||
|
||||
@@ -18,9 +18,6 @@ import fs from 'fs';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import * as tmp from 'tmp';
|
||||
import * as github from '@actions/github';
|
||||
|
||||
import {GitHub} from './github/github.js';
|
||||
|
||||
export class Context {
|
||||
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 {
|
||||
public static tmpName(options?: tmp.TmpNameOptions): string {
|
||||
return tmp.tmpNameSync(options);
|
||||
}
|
||||
|
||||
public static gitRef(): string {
|
||||
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 (sha && !ref.startsWith(`refs/pull/`)) {
|
||||
ref = sha;
|
||||
} else if (ref.startsWith(`refs/pull/`) && setPullRequestHeadRef) {
|
||||
ref = ref.replace(/\/merge$/g, '/head');
|
||||
}
|
||||
return ref;
|
||||
}
|
||||
|
||||
public static gitContext(): string {
|
||||
return `${GitHub.serverURL}/${github.context.repo.owner}/${github.context.repo.repo}.git#${Context.gitRef()}`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export type GitContextFormat = 'fragment' | 'query';
|
||||
|
||||
export type BuildMetadata = {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
[key: string]: any;
|
||||
|
||||
13
src/util.ts
13
src/util.ts
@@ -157,7 +157,10 @@ export class Util {
|
||||
}
|
||||
|
||||
// https://github.com/golang/go/blob/f6b93a4c358b28b350dd8fe1780c1f78e520c09c/src/strconv/atob.go#L7-L18
|
||||
public static parseBool(str: string): boolean {
|
||||
public static parseBool(str: string | undefined): boolean {
|
||||
if (str === undefined) {
|
||||
return false;
|
||||
}
|
||||
switch (str) {
|
||||
case '1':
|
||||
case 't':
|
||||
@@ -178,6 +181,14 @@ export class Util {
|
||||
}
|
||||
}
|
||||
|
||||
public static parseBoolOrDefault(str: string | undefined, defaultValue = false): boolean {
|
||||
try {
|
||||
return this.parseBool(str);
|
||||
} catch {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public static formatFileSize(bytes: number): string {
|
||||
if (bytes === 0) return '0 Bytes';
|
||||
const k = 1024;
|
||||
|
||||
Reference in New Issue
Block a user