Skip to content

Commit 264a0ee

Browse files
authored
Merge pull request #287 from crazy-max/buildx-hasAttestationType
buildx: hasAttestationType and resolveAttestationAttrs funcs
2 parents de2888a + 545f7cd commit 264a0ee

4 files changed

Lines changed: 127 additions & 0 deletions

File tree

__tests__/buildx/inputs.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,41 @@ describe('hasDockerExporter', () => {
244244
});
245245
});
246246

247+
describe('hasAttestationType', () => {
248+
// prettier-ignore
249+
test.each([
250+
['type=provenance,mode=min', 'provenance', true],
251+
['type=sbom,true', 'sbom', true],
252+
['type=foo,bar', 'provenance', false],
253+
])('given %p for %p returns %p', async (attrs: string, name: string, expected: boolean) => {
254+
expect(Inputs.hasAttestationType(name, attrs)).toEqual(expected);
255+
});
256+
});
257+
258+
describe('resolveAttestationAttrs', () => {
259+
// prettier-ignore
260+
test.each([
261+
[
262+
'type=provenance,mode=min',
263+
'type=provenance,mode=min'
264+
],
265+
[
266+
'type=provenance,true',
267+
'type=provenance,disabled=false'
268+
],
269+
[
270+
'type=provenance,false',
271+
'type=provenance,disabled=true'
272+
],
273+
[
274+
'',
275+
''
276+
],
277+
])('given %p', async (input: string, expected: string) => {
278+
expect(Inputs.resolveAttestationAttrs(input)).toEqual(expected);
279+
});
280+
});
281+
247282
describe('hasGitAuthTokenSecret', () => {
248283
// prettier-ignore
249284
test.each([

__tests__/util.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,36 @@ describe('hash', () => {
279279
});
280280
});
281281

282+
// https://github.com/golang/go/blob/f6b93a4c358b28b350dd8fe1780c1f78e520c09c/src/strconv/atob_test.go#L36-L58
283+
describe('parseBool', () => {
284+
[
285+
{input: '', expected: false, throwsError: true},
286+
{input: 'asdf', expected: false, throwsError: true},
287+
{input: '0', expected: false, throwsError: false},
288+
{input: 'f', expected: false, throwsError: false},
289+
{input: 'F', expected: false, throwsError: false},
290+
{input: 'FALSE', expected: false, throwsError: false},
291+
{input: 'false', expected: false, throwsError: false},
292+
{input: 'False', expected: false, throwsError: false},
293+
{input: '1', expected: true, throwsError: false},
294+
{input: 't', expected: true, throwsError: false},
295+
{input: 'T', expected: true, throwsError: false},
296+
{input: 'TRUE', expected: true, throwsError: false},
297+
{input: 'true', expected: true, throwsError: false},
298+
{input: 'True', expected: true, throwsError: false}
299+
].forEach(({input, expected, throwsError}) => {
300+
test(`parseBool("${input}")`, () => {
301+
if (throwsError) {
302+
// eslint-disable-next-line jest/no-conditional-expect
303+
expect(() => Util.parseBool(input)).toThrow();
304+
} else {
305+
// eslint-disable-next-line jest/no-conditional-expect
306+
expect(Util.parseBool(input)).toBe(expected);
307+
}
308+
});
309+
});
310+
});
311+
282312
// See: https://github.com/actions/toolkit/blob/a1b068ec31a042ff1e10a522d8fdf0b8869d53ca/packages/core/src/core.ts#L89
283313
function getInputName(name: string): string {
284314
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;

src/buildx/inputs.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {parse} from 'csv-parse/sync';
2121

2222
import {Context} from '../context';
2323
import {GitHub} from '../github';
24+
import {Util} from '../util';
2425

2526
const parseKvp = (kvp: string): [string, string] => {
2627
const delimiterIndex = kvp.indexOf('=');
@@ -176,6 +177,45 @@ export class Inputs {
176177
return false;
177178
}
178179

180+
public static hasAttestationType(name: string, attrs: string): boolean {
181+
const records = parse(attrs, {
182+
delimiter: ',',
183+
trim: true,
184+
columns: false,
185+
relaxColumnCount: true
186+
});
187+
for (const record of records) {
188+
for (const [key, value] of record.map((chunk: string) => chunk.split('=').map(item => item.trim()))) {
189+
if (key == 'type' && value == name) {
190+
return true;
191+
}
192+
}
193+
}
194+
return false;
195+
}
196+
197+
public static resolveAttestationAttrs(attrs: string): string {
198+
const records = parse(attrs, {
199+
delimiter: ',',
200+
trim: true,
201+
columns: false,
202+
relaxColumnCount: true
203+
});
204+
const res: Array<string> = [];
205+
for (const record of records) {
206+
for (const attr of record) {
207+
try {
208+
// https://github.com/docker/buildx/blob/8abef5908705e49f7ba88ef8c957e1127b597a2a/util/buildflags/attests.go#L13-L21
209+
const v = Util.parseBool(attr);
210+
res.push(`disabled=${!v}`);
211+
} catch (err) {
212+
res.push(attr);
213+
}
214+
}
215+
}
216+
return res.join(',');
217+
}
218+
179219
public static hasGitAuthTokenSecret(secrets: string[]): boolean {
180220
for (const secret of secrets) {
181221
if (secret.startsWith('GIT_AUTH_TOKEN=')) {

src/util.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,26 @@ export class Util {
144144
public static hash(input: string): string {
145145
return crypto.createHash('sha256').update(input).digest('hex');
146146
}
147+
148+
// https://github.com/golang/go/blob/f6b93a4c358b28b350dd8fe1780c1f78e520c09c/src/strconv/atob.go#L7-L18
149+
public static parseBool(str: string): boolean {
150+
switch (str) {
151+
case '1':
152+
case 't':
153+
case 'T':
154+
case 'true':
155+
case 'TRUE':
156+
case 'True':
157+
return true;
158+
case '0':
159+
case 'f':
160+
case 'F':
161+
case 'false':
162+
case 'FALSE':
163+
case 'False':
164+
return false;
165+
default:
166+
throw new Error(`parseBool syntax error: ${str}`);
167+
}
168+
}
147169
}

0 commit comments

Comments
 (0)