Open ericcornelissen opened 7 months ago
Let's brainstorm on how a regex could backfire, given the prevailing wisdom on not parsing subtle stuff as strings.
Thinking in terms of test cases would help.
Fair enough @lucasgonze. First of all, if you have a simpler alternative I'm more than happy to use it. Some arguments/thoughs in favor of using regular expressions:
key: uses: foo/bar@baz
). I would argue that we can generally expect workflows/manifests not to contain such strings. To minimize this risk I intentionally included the uses:(\\s*)
prefix.uses:
is simple/regular enough to be parsed with a regular expressions. Though admittedly, GitHub Actions might be more permissive than I'm thinking.(\\s*)
is forced because there is no restriction on the amount of whitespace after the :
. The group after ${actionId}@${currentVersion}
is forced because we may need to add a comment to an action that doesn't have it. This group needs to match a comment, which might be preceded by whitespace (so the second (\\s*)
is forced too). The remaining part is for matching the line comment text, so I opted for the most general expression (anything that's not a line-ending) to match and replace it (which is the same as the existing behavior).--experimental-replace-inline
- the best way to test is on real world data.Some example cases to consider (excluding combinations of scenarios):
# with leading hyphen
- uses: foo/bar@baz
# without leading hyphen
- name: name
uses: foo/bar@baz
# one space after uses
- uses: foo/bar@baz
# with pin comment, no spacing
- uses: foo/bar@baz#pin@main
# with pin comment, spacing before #
- uses: foo/bar@baz #pin@main
# with pin comment, spacing after #
- uses: foo/bar@baz# pin@main
# with pin comment, spacing before and after #
- uses: foo/bar@baz # pin@main
# with non-pin comment
- uses: foo/bar@baz # foobar
Some cases the current expression doesn't handle (should probably be fixed, only realized while writing this response):
whitespace before colon
- uses : foo/bar@baz
no final newline:
- uses: foo/bar@baz
# No newline at end of file
Some cases the current expression doesn't handle (should probably be fixed, only realized while writing this response):
These, and one more case, are now handled with e28bbefa1651bf688ec9c2e8e3f1540224bf70e2
@ericcornelissen I think this is a good addition. For it to be accepted, it would need to be refactored in to replaceActions
(https://github.com/mheap/pin-github-action/blob/main/replaceActions.js) and tests added
@mheap, thanks! Before I do that, do you want me to replace the existing implementation or add it as an alternative strategy? And in case of the latter, any preferences for the API?
I’d be ok replacing the implementation so long as all of the tests still pass.On 17 Sep 2024, at 18:20, Eric Cornelissen @.***> wrote: @mheap, thanks! Before I do that, do you want me to replace the existing implementation or add it as an alternative strategy? And in case of the latter, any preferences for the API?
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>
@mheap I updated the PR now. Looking forward to your review :slightly_smiling_face:
I replaced the existing implementation for the new one. The test suite for replaceActions
still passed (after adjusting for the changed API). I expanded it with the cases I have mentioned in this thread and two more related to indentation.
Also, this change eliminated the need for the --yaml-line-width
and --yaml-null-str
inputs but I kept them on the CLI to avoid breaking users that expect those flags to exist - I just changed the help text to mark them as deprecated.
Closes #153
Adjust the implementation of the CLI to leverage the
actions
output list from the main library instead of theworkflow
output to rewrite the input file. In particular, this constructs are regular expression to finduses:
directives to replace and replaces them (with a fixed format). The result is that the input file isn't reformatted, apart from theuses:
directives.Currently this is implemented in for the CLI only. It might make sense to lift this into the main library instead and use this rewrite strategy for the
workflow
output instead of usingworkflow.toString()
.Also note that this duplicates the logic for the default comment from replaceActions.js. This should probably be addressed before this is merged, but I'm unsure how to resolve that within the current project structure.