Skip to content

Commit

Permalink
Merge pull request #350 from crazy-max/gha-rest
Browse files Browse the repository at this point in the history
buildx(build): resolveCacheToAttrs func
  • Loading branch information
crazy-max authored Jun 10, 2024
2 parents ee91773 + 15788e8 commit 33d4b44
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
48 changes: 48 additions & 0 deletions __tests__/buildx/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
// prettier-ignore
test.each([
Expand Down
8 changes: 7 additions & 1 deletion __tests__/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jest.spyOn(GitHub.prototype, 'repoData').mockImplementation((): Promise<GitHubRe
});

describe('repoData', () => {
it('returns GitHub repository', async () => {
it('returns GitHub repo data', async () => {
const github = new GitHub();
expect((await github.repoData()).name).toEqual('Hello-World');
});
Expand Down Expand Up @@ -89,6 +89,12 @@ describe('apiURL', () => {
});
});

describe('repository', () => {
it('returns GitHub repository', async () => {
expect(GitHub.repository).toEqual('docker/actions-toolkit');
});
});

describe('workflowRunURL', () => {
it('returns 2188748038', async () => {
expect(GitHub.workflowRunURL).toEqual('https://github.com/docker/actions-toolkit/actions/runs/2188748038/attempts/2');
Expand Down
39 changes: 39 additions & 0 deletions src/buildx/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,45 @@ export class Build {
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 {
return Build.hasExporterType('local', exporters);
}
Expand Down
6 changes: 5 additions & 1 deletion src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ export class GitHub {
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 {
const runID = process.env.GITHUB_RUN_ID || github.context.runId;
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 {
Expand Down

0 comments on commit 33d4b44

Please sign in to comment.