npm-groovy-lint icon indicating copy to clipboard operation
npm-groovy-lint copied to clipboard

embedded shell scripts

Open simzacks opened this issue 1 year ago • 13 comments

A lot of the bugs that we end up with in our groovy scripts (Jenkins pipelines) are embedded shell scripts that should be caught by a shell linter. Is there a way from within npm-groovy-lint to pass the script from the sh function to a bash linter (such as shellcheck) and return that as part of the results?

As an example, what I'm testing right now is something like this (which still doesn't work with multi line, and I'm sure my regex can be fine-tuned):

grep -Po "(?<=sh\(\"|sh\(script: \").*(?=\")" . -r | sed 's/[${}]//g'  | while read -r line; do shellcheck -s bash <(echo "$line"); done

simzacks avatar Jul 25 '22 08:07 simzacks

That would be a hug evolution for npm-groovy-lint... but it would require some workforce !

  • Parse Jenkinsfile to extract shell commands
  • Call shellcheck (that should be shipped with npm-groovy-lint)
  • Get shellcheck results and convert them into npm-groovy-lint results

I don't have enough available time to make it happen before a while, but if you or someone else would like to make a PR I'll be glad to assist by providing support :)

nvuillam avatar Jul 25 '22 19:07 nvuillam

I'll start working on this. I wanted to make sure it was an acceptable feature before putting time in. Though it will probably take me some time as well :-)

simzacks avatar Jul 26 '22 08:07 simzacks

Such feature would probably make npm-groovy-lint the best Jenkinsfile linter in the universe, sure it's acceptable 😃

nvuillam avatar Jul 26 '22 09:07 nvuillam

@simzacks I'm about to release npm-groovy-lint v10.0.0, if you make such enhancement I'll release a 11.0.0 :)

nvuillam avatar Aug 12 '22 22:08 nvuillam

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

If you think this issue should stay open, please remove the O: stale 🤖 label or comment on the issue.

github-actions[bot] avatar Sep 12 '22 02:09 github-actions[bot]

@simzacks do you still plan to implement this great evolution? :)

nvuillam avatar Sep 12 '22 05:09 nvuillam

I wrote it in python (mother tongue). https://github.com/simzacks/groovy_shell_lint Now I have to figure out how to migrate this to javascript and convert them into npm-groovy-lint results

simzacks avatar Sep 12 '22 06:09 simzacks

That's great :) About shellcheck, actionlint already embedded it to analyze shell scripts, if it can help

https://github.com/rhysd/actionlint/blob/main/rule_shellcheck.go

Can let you convert in Js then I'll assist you to plug it within npm-groovy-lint ?

nvuillam avatar Sep 12 '22 06:09 nvuillam

Can let you convert in Js then I'll assist you to plug it within npm-groovy-lint ? Nothing like a little motivation :-) I'll try to have that done by the end of the week

simzacks avatar Sep 12 '22 06:09 simzacks

I'm having some problem with js regex. Still working on it :-(

simzacks avatar Sep 18 '22 13:09 simzacks

Show me the issues you have, I may help :)

nvuillam avatar Sep 18 '22 21:09 nvuillam

I figured out a way around it, and should have the rest of the script finished today or tomorrow. The problems I was facing were: The ultimate goal: I couldn't figure out a regex that would find a whole word sh and then (ignoring the optional parenthesis) a valid quotation mark delimiter (either 3 double/single quotes or single double/single quote) and an identical end delimiter and capture everything in between .

Being that I couldn't figure that out, I found the whole word sh that doesn't have a "." before it, then I search for the quotation delimiters. In python, I was able to then search for the same delimiter that it found (if it found a single quote, it should search for a single quote, if it found 3 double quotes, it should search for 3 double quotes). When I tried setting a regex for the value found, I got an error that indicated the quotes weren't escaped (which is probably a language bug). i.e.

endq = "(?<!\\)" + start_quote[0];
p_endq = new RegExp(endq, "g");
                 ^

SyntaxError: Invalid regular expression: /(?<!\)"""/: Unterminated group

I slept on it and the solution was to loop search for the delimiter using exec until a match was found that was identical to the first match it found.

simzacks avatar Sep 19 '22 07:09 simzacks

Glad you unblocked yourself :)

nvuillam avatar Sep 19 '22 07:09 nvuillam

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

If you think this issue should stay open, please remove the O: stale 🤖 label or comment on the issue.

github-actions[bot] avatar Oct 20 '22 02:10 github-actions[bot]

not closed, still hoping to see a PR about it someday :)

nvuillam avatar Nov 04 '22 06:11 nvuillam

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

If you think this issue should stay open, please remove the O: stale 🤖 label or comment on the issue.

github-actions[bot] avatar Dec 05 '22 01:12 github-actions[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

If you think this issue should stay open, please remove the O: stale 🤖 label or comment on the issue.

github-actions[bot] avatar Feb 04 '23 01:02 github-actions[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

If you think this issue should stay open, please remove the O: stale 🤖 label or comment on the issue.

github-actions[bot] avatar Mar 07 '23 01:03 github-actions[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

If you think this issue should stay open, please remove the O: stale 🤖 label or comment on the issue.

github-actions[bot] avatar Apr 07 '23 01:04 github-actions[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

If you think this issue should stay open, please remove the O: stale 🤖 label or comment on the issue.

github-actions[bot] avatar May 08 '23 01:05 github-actions[bot]