Merge pull request #350 from crazy-max/gha-rest

buildx(build): resolveCacheToAttrs func
This commit is contained in:
CrazyMax 2024-06-10 13:39:47 +02:00 committed by GitHub
commit 33d4b448ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 99 additions and 2 deletions

View File

@ -202,6 +202,54 @@ describe('resolveSecret', () => {
}); });
}); });
describe('resolveCacheToAttrs', () => {
// prettier-ignore
test.each([
[
'',
undefined,
''
],
[
'user/app:cache',
undefined,
'user/app:cache'
],
[
'type=inline',
undefined,
'type=inline'
],
[
'type=gha',
undefined,
'type=gha,repository=docker/actions-toolkit',
],
[
'type=gha,mode=max',
undefined,
'type=gha,mode=max,repository=docker/actions-toolkit',
],
[
'type=gha,mode=max',
'abcd1234',
'type=gha,mode=max,repository=docker/actions-toolkit,ghtoken=abcd1234',
],
[
'type=gha,repository=foo/bar,mode=max',
undefined,
'type=gha,repository=foo/bar,mode=max',
],
[
'type=gha,repository=foo/bar,mode=max',
'abcd1234',
'type=gha,repository=foo/bar,mode=max,ghtoken=abcd1234',
],
])('given %p', async (input: string, githubToken: string | undefined, expected: string) => {
expect(Build.resolveCacheToAttrs(input, githubToken)).toEqual(expected);
});
});
describe('hasLocalExporter', () => { describe('hasLocalExporter', () => {
// prettier-ignore // prettier-ignore
test.each([ test.each([

View File

@ -32,7 +32,7 @@ jest.spyOn(GitHub.prototype, 'repoData').mockImplementation((): Promise<GitHubRe
}); });
describe('repoData', () => { describe('repoData', () => {
it('returns GitHub repository', async () => { it('returns GitHub repo data', async () => {
const github = new GitHub(); const github = new GitHub();
expect((await github.repoData()).name).toEqual('Hello-World'); expect((await github.repoData()).name).toEqual('Hello-World');
}); });
@ -89,6 +89,12 @@ describe('apiURL', () => {
}); });
}); });
describe('repository', () => {
it('returns GitHub repository', async () => {
expect(GitHub.repository).toEqual('docker/actions-toolkit');
});
});
describe('workflowRunURL', () => { describe('workflowRunURL', () => {
it('returns 2188748038', async () => { it('returns 2188748038', async () => {
expect(GitHub.workflowRunURL).toEqual('https://github.com/docker/actions-toolkit/actions/runs/2188748038/attempts/2'); expect(GitHub.workflowRunURL).toEqual('https://github.com/docker/actions-toolkit/actions/runs/2188748038/attempts/2');

View File

@ -161,6 +161,45 @@ export class Build {
return `${input},builder-id=${GitHub.workflowRunURL}`; return `${input},builder-id=${GitHub.workflowRunURL}`;
} }
public static resolveCacheToAttrs(input: string, githubToken?: string): string {
if (!input) {
return input;
}
let cacheType = 'registry';
let ghaCacheRepository = '';
let ghaCacheGHToken = '';
const fields = parse(input, {
relaxColumnCount: true,
skipEmptyLines: true
})[0];
for (const field of fields) {
const parts = field
.toString()
.split(/(?<=^[^=]+?)=/)
.map(item => item.trim());
if (parts[0] === 'type') {
cacheType = parts[1];
} else if (parts[0] === 'repository') {
ghaCacheRepository = parts[1];
} else if (parts[0] === 'ghtoken') {
ghaCacheGHToken = parts[1];
}
}
if (cacheType === 'gha') {
if (!ghaCacheRepository) {
input = `${input},repository=${GitHub.repository}`;
}
if (!ghaCacheGHToken && githubToken) {
input = `${input},ghtoken=${githubToken}`;
}
}
return input;
}
public static hasLocalExporter(exporters: string[]): boolean { public static hasLocalExporter(exporters: string[]): boolean {
return Build.hasExporterType('local', exporters); return Build.hasExporterType('local', exporters);
} }

View File

@ -64,10 +64,14 @@ export class GitHub {
return process.env.GITHUB_API_URL || 'https://api.github.com'; return process.env.GITHUB_API_URL || 'https://api.github.com';
} }
static get repository(): string {
return `${github.context.repo.owner}/${github.context.repo.repo}`;
}
static get workflowRunURL(): string { static get workflowRunURL(): string {
const runID = process.env.GITHUB_RUN_ID || github.context.runId; const runID = process.env.GITHUB_RUN_ID || github.context.runId;
const runAttempt = process.env.GITHUB_RUN_ATTEMPT || 1; const runAttempt = process.env.GITHUB_RUN_ATTEMPT || 1;
return `${GitHub.serverURL}/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${runID}/attempts/${runAttempt}`; return `${GitHub.serverURL}/${GitHub.repository}/actions/runs/${runID}/attempts/${runAttempt}`;
} }
static get actionsRuntimeToken(): GitHubActionsRuntimeToken | undefined { static get actionsRuntimeToken(): GitHubActionsRuntimeToken | undefined {