mheap / pin-github-action

Pin your GitHub actions to a specific hash
MIT License
74 stars 7 forks source link

Update CLI to not reformat input files #158

Open ericcornelissen opened 7 months ago

ericcornelissen commented 7 months ago

Closes #153

Adjust the implementation of the CLI to leverage the actions output list from the main library instead of the workflow output to rewrite the input file. In particular, this constructs are regular expression to find uses: directives to replace and replaces them (with a fixed format). The result is that the input file isn't reformatted, apart from the uses: 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 using workflow.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.

lucasgonze commented 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.

ericcornelissen commented 7 months ago

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:

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):

ericcornelissen commented 2 months ago

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

mheap commented 1 month ago

@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

ericcornelissen commented 1 month ago

@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?

mheap commented 1 month ago

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: @.***>

ericcornelissen commented 1 month ago

@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.