New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
attribute to enable/disable images #193
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think splitting by
=
makes something like this invalidnot sure if you ever meant for it to be supported and I guess I could always have the same condition with other operators like
startsWith
without using=
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
${{ github.event_name == 'pull_request' }}
is interpreted upstream by the GitHub Runner logic so it would producename=lucacome/test-foo2,enable=<true|false>
before being passed to the action.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah interesting, I thought that was the cause for
Error: Image name attribute empty: enable=true
that I'm seeing, but then I'm not sure I understand what this error means 😄I have this and I assumed it wasn't coming from the first one, as it would get the default
enable=true
, and that it was the second line causing the error.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sounds like a bug then, will take a look, thanks for your feedback
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lucacome Hum I was not able to repro, do you have a link to your repo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@crazy-max I have this repo where I test workflows https://github.com/lucacome/workflows/runs/6178399824?check_suite_focus=true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok was able to repro: https://github.com/docker/metadata-action/runs/6195927044?check_suite_focus=true#step:3:19
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lucacome Should be fixed, let me know if it looks good to you. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! 🚀