-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from crazy-max/images-opts
attribute to enable/disable images
- Loading branch information
Showing
9 changed files
with
302 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import {describe, expect, test} from '@jest/globals'; | ||
import {Transform, Image} from '../src/image'; | ||
|
||
describe('transform', () => { | ||
// prettier-ignore | ||
test.each([ | ||
[ | ||
[ | ||
`name/foo` | ||
], | ||
[ | ||
{ | ||
name: `name/foo`, | ||
enable: true, | ||
} | ||
] as Image[], | ||
false | ||
], | ||
[ | ||
[ | ||
`name/foo,name/bar` | ||
], | ||
[ | ||
{ | ||
name: `name/foo`, | ||
enable: true, | ||
}, | ||
{ | ||
name: `name/bar`, | ||
enable: true, | ||
} | ||
] as Image[], | ||
false | ||
], | ||
[ | ||
[ | ||
`name/foo`, | ||
`name/bar` | ||
], | ||
[ | ||
{ | ||
name: `name/foo`, | ||
enable: true, | ||
}, | ||
{ | ||
name: `name/bar`, | ||
enable: true, | ||
} | ||
] as Image[], | ||
false | ||
], | ||
[ | ||
[ | ||
`name=name/bar`, | ||
`name/foo,enable=false`, | ||
`name=ghcr.io/name/foo,enable=true` | ||
], | ||
[ | ||
{ | ||
name: `name/bar`, | ||
enable: true, | ||
}, | ||
{ | ||
name: `name/foo`, | ||
enable: false, | ||
}, | ||
{ | ||
name: `ghcr.io/name/foo`, | ||
enable: true, | ||
}, | ||
] as Image[], | ||
false | ||
], | ||
[ | ||
[`value=name/foo`], undefined, true | ||
], | ||
[ | ||
[`name/foo,enable=bar`], undefined, true | ||
], | ||
[ | ||
[`name/foo,bar=baz`], undefined, true | ||
], | ||
[ | ||
[`name=,enable=true`], undefined, true | ||
], | ||
[ | ||
[`name/foo,name=name/bar,enable=true`], undefined, true | ||
] | ||
])('given %p', async (l: string[], expected: Image[], invalid: boolean) => { | ||
try { | ||
const images = Transform(l); | ||
expect(images).toEqual(expected); | ||
} catch (err) { | ||
if (!invalid) { | ||
console.error(err); | ||
} | ||
// eslint-disable-next-line jest/no-conditional-expect | ||
expect(true).toBe(invalid); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import {parse} from 'csv-parse/sync'; | ||
import * as core from '@actions/core'; | ||
|
||
export interface Image { | ||
name: string; | ||
enable: boolean; | ||
} | ||
|
||
export function Transform(inputs: string[]): Image[] { | ||
let images: Image[] = []; | ||
|
||
// backward compatibility with old format | ||
if (inputs.length == 1) { | ||
let newformat = false; | ||
const fields = parse(inputs[0], { | ||
relaxColumnCount: true, | ||
skipEmptyLines: true | ||
})[0]; | ||
for (const field of fields) { | ||
const parts = field | ||
.toString() | ||
.split('=') | ||
.map(item => item.trim()); | ||
if (parts.length == 1) { | ||
images.push({name: parts[0].toLowerCase(), enable: true}); | ||
} else { | ||
newformat = true; | ||
break; | ||
} | ||
} | ||
if (!newformat) { | ||
return output(images); | ||
} | ||
} | ||
|
||
images = []; | ||
for (const input of inputs) { | ||
const image: Image = {name: '', enable: true}; | ||
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.length == 1) { | ||
image.name = parts[0].toLowerCase(); | ||
} else { | ||
const key = parts[0].toLowerCase(); | ||
const value = parts[1]; | ||
switch (key) { | ||
case 'name': { | ||
image.name = value.toLowerCase(); | ||
break; | ||
} | ||
case 'enable': { | ||
if (!['true', 'false'].includes(value)) { | ||
throw new Error(`Invalid enable attribute value: ${input}`); | ||
} | ||
image.enable = /true/i.test(value); | ||
break; | ||
} | ||
default: { | ||
throw new Error(`Unknown image attribute: ${input}`); | ||
} | ||
} | ||
} | ||
} | ||
if (image.name.length == 0) { | ||
throw new Error(`Image name attribute empty: ${input}`); | ||
} | ||
images.push(image); | ||
} | ||
return output(images); | ||
} | ||
|
||
function output(images: Image[]): Image[] { | ||
core.startGroup(`Processing images input`); | ||
for (const image of images) { | ||
core.info(`name=${image.name},enable=${image.enable}`); | ||
} | ||
core.endGroup(); | ||
return images; | ||
} |
Oops, something went wrong.