mirror of
https://github.com/docker/actions-toolkit.git
synced 2024-11-23 03:16:09 +08:00
Merge pull request #350 from crazy-max/gha-rest
buildx(build): resolveCacheToAttrs func
This commit is contained in:
commit
33d4b448ac
@ -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([
|
||||||
|
@ -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');
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user