Skip to content

Commit 33d4b44

Browse files
authored
Merge pull request #350 from crazy-max/gha-rest
buildx(build): resolveCacheToAttrs func
2 parents ee91773 + 15788e8 commit 33d4b44

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

__tests__/buildx/build.test.ts

+48
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,54 @@ describe('resolveSecret', () => {
202202
});
203203
});
204204

205+
describe('resolveCacheToAttrs', () => {
206+
// prettier-ignore
207+
test.each([
208+
[
209+
'',
210+
undefined,
211+
''
212+
],
213+
[
214+
'user/app:cache',
215+
undefined,
216+
'user/app:cache'
217+
],
218+
[
219+
'type=inline',
220+
undefined,
221+
'type=inline'
222+
],
223+
[
224+
'type=gha',
225+
undefined,
226+
'type=gha,repository=docker/actions-toolkit',
227+
],
228+
[
229+
'type=gha,mode=max',
230+
undefined,
231+
'type=gha,mode=max,repository=docker/actions-toolkit',
232+
],
233+
[
234+
'type=gha,mode=max',
235+
'abcd1234',
236+
'type=gha,mode=max,repository=docker/actions-toolkit,ghtoken=abcd1234',
237+
],
238+
[
239+
'type=gha,repository=foo/bar,mode=max',
240+
undefined,
241+
'type=gha,repository=foo/bar,mode=max',
242+
],
243+
[
244+
'type=gha,repository=foo/bar,mode=max',
245+
'abcd1234',
246+
'type=gha,repository=foo/bar,mode=max,ghtoken=abcd1234',
247+
],
248+
])('given %p', async (input: string, githubToken: string | undefined, expected: string) => {
249+
expect(Build.resolveCacheToAttrs(input, githubToken)).toEqual(expected);
250+
});
251+
});
252+
205253
describe('hasLocalExporter', () => {
206254
// prettier-ignore
207255
test.each([

__tests__/github.test.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jest.spyOn(GitHub.prototype, 'repoData').mockImplementation((): Promise<GitHubRe
3232
});
3333

3434
describe('repoData', () => {
35-
it('returns GitHub repository', async () => {
35+
it('returns GitHub repo data', async () => {
3636
const github = new GitHub();
3737
expect((await github.repoData()).name).toEqual('Hello-World');
3838
});
@@ -89,6 +89,12 @@ describe('apiURL', () => {
8989
});
9090
});
9191

92+
describe('repository', () => {
93+
it('returns GitHub repository', async () => {
94+
expect(GitHub.repository).toEqual('docker/actions-toolkit');
95+
});
96+
});
97+
9298
describe('workflowRunURL', () => {
9399
it('returns 2188748038', async () => {
94100
expect(GitHub.workflowRunURL).toEqual('https://github.com/docker/actions-toolkit/actions/runs/2188748038/attempts/2');

src/buildx/build.ts

+39
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,45 @@ export class Build {
161161
return `${input},builder-id=${GitHub.workflowRunURL}`;
162162
}
163163

164+
public static resolveCacheToAttrs(input: string, githubToken?: string): string {
165+
if (!input) {
166+
return input;
167+
}
168+
169+
let cacheType = 'registry';
170+
let ghaCacheRepository = '';
171+
let ghaCacheGHToken = '';
172+
173+
const fields = parse(input, {
174+
relaxColumnCount: true,
175+
skipEmptyLines: true
176+
})[0];
177+
for (const field of fields) {
178+
const parts = field
179+
.toString()
180+
.split(/(?<=^[^=]+?)=/)
181+
.map(item => item.trim());
182+
if (parts[0] === 'type') {
183+
cacheType = parts[1];
184+
} else if (parts[0] === 'repository') {
185+
ghaCacheRepository = parts[1];
186+
} else if (parts[0] === 'ghtoken') {
187+
ghaCacheGHToken = parts[1];
188+
}
189+
}
190+
191+
if (cacheType === 'gha') {
192+
if (!ghaCacheRepository) {
193+
input = `${input},repository=${GitHub.repository}`;
194+
}
195+
if (!ghaCacheGHToken && githubToken) {
196+
input = `${input},ghtoken=${githubToken}`;
197+
}
198+
}
199+
200+
return input;
201+
}
202+
164203
public static hasLocalExporter(exporters: string[]): boolean {
165204
return Build.hasExporterType('local', exporters);
166205
}

src/github.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,14 @@ export class GitHub {
6464
return process.env.GITHUB_API_URL || 'https://api.github.com';
6565
}
6666

67+
static get repository(): string {
68+
return `${github.context.repo.owner}/${github.context.repo.repo}`;
69+
}
70+
6771
static get workflowRunURL(): string {
6872
const runID = process.env.GITHUB_RUN_ID || github.context.runId;
6973
const runAttempt = process.env.GITHUB_RUN_ATTEMPT || 1;
70-
return `${GitHub.serverURL}/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${runID}/attempts/${runAttempt}`;
74+
return `${GitHub.serverURL}/${GitHub.repository}/actions/runs/${runID}/attempts/${runAttempt}`;
7175
}
7276

7377
static get actionsRuntimeToken(): GitHubActionsRuntimeToken | undefined {

0 commit comments

Comments
 (0)