Skip to content
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

Introduce AddSecurityScanWorkflow Recipe #36

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

sghill
Copy link
Collaborator

@sghill sghill commented Aug 19, 2023

What's changed?

Introduced a new recipe to add a GitHub Actions workflow.

What's your motivation?

Closes #22.

Anything in particular you'd like reviewers to focus on?

Are there better patterns for taking an example file and layering changes on top of it? One problem with this implementation is it's possible to get out of sync with the archetype.

The recipe allows specifying branch name, but I think the intent of the workflow is to target the default branch. Is the default branch name (or indexed branch) available in the LST? The implementation would be more useful out of the box if we could read the correct target branch from the LST.

Anyone you would like to review specifically?

@timtebeek

Have you considered any alternatives or workarounds?

ReplaceSequenceVisitor was adapted from AddToSequenceVisitor in rewrite-yaml. I think it makes more sense to include it there long-term, but it may be faster to prove out its utility as part of this module. As preparation for an eventual move, it is not public.

Rather than specifying the build tool or java version, I thought about inferring both from the environment. Since there are so few Gradle implementations and we already have logic around supported Java versions, I didn't think that was required for a first pass.

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've added the license header to any new files through ./gradlew licenseFormat
  • I've used the IntelliJ auto-formatter on affected files
  • I've updated the documentation (if applicable)

@timtebeek timtebeek self-requested a review August 19, 2023 09:46
@timtebeek timtebeek added the recipe Requests for new automated code changes label Aug 19, 2023
@timtebeek
Copy link
Contributor

Are there better patterns for taking an example file and layering changes on top of it? One problem with this implementation is it's possible to get out of sync with the archetype.

As for getting out of sync with the archetype you might be interested in this recipe that updates the Gradle wrapper, which downloads rather than bundles the corresponding files it needs. For layering changes on top I think indeed manipulating the template with recipes is the way to go.

The recipe allows specifying branch name, but I think the intent of the workflow is to target the default branch. Is the default branch name (or indexed branch) available in the LST? The implementation would be more useful out of the box if we could read the correct target branch from the LST.

We have the GitProvenance marker available on every source file. We for instance use that to link SQL statements to the correct GitHub commit hash, but you can similarly extract the ingested branch, which is almost always be the main branch.

Comment on lines +68 to +74
@Option(displayName = "Build Tool",
description = "Set up dependency cache.",
example = "maven",
valid = {"maven", "gradle"},
required = false)
@Nullable
String buildTool;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the GitProvenance marker, we also have markers for the BuildTool and LstProvenance. Both of those contain a Type, such that you likely don't have to pass in an argument buildTool here, as that is already determined.

Comment on lines +61 to +66
@Option(displayName = "Java Version",
description = "Version of Java to set for build.",
example = "11",
required = false)
@Nullable
Integer javaVersion;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what you might have come to expect by now: we also have a JavaVersion marker that you can use to determine the Java version. You might want to account for projects that use a different version for src/main versus src/test, but other than that it should be convenient not to have to pass this parameter I think.

At least I think; I see this with Kotlin, but not with Java.
Comment on lines +157 to +164
boolean licenseHeaderDone = false;
for (String line : lines) {
if (licenseHeaderDone) {
wanted.add(line);
} else if (line.isEmpty()) {
licenseHeaderDone = true;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to configure the license plugin to the workflow file with something like this?

license {
    exclude "**/*.properties"
    excludes(["**/*.txt", "**/*.conf"])
}

Unless we go for downloading the file on the fly of course, in which case this wouldn't be necessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks! Didn't know this was an option

Comment on lines +30 to +32
* Adapted from {@link org.openrewrite.yaml.AppendToSequenceVisitor}
*/
class ReplaceSequenceVisitor extends YamlIsoVisitor<ExecutionContext> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad to see you were able to make this work; I'm wondering if it would make sense to pull this up, or change either AppendToSequence or ChangePropertyValue to be able to set a new list of values. Just a thought for now, not something I'd ask of you, but curious how you'd see that. Fine to keep this here for now, and know that it's here if we get similar requests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think that would make sense. I actually checked both those recipes to see if they already did this.

I think it might make sense for AppendToSequence to have a strategy called REPLACE, but it definitely seems like ChangePropertyValue could take a list of values. I attempted to use ChangePropertyValue with a yaml list [ a, b, c ], but the formatting didn't quite work.

Copy link
Contributor

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to see this addition! Some suggestions for alternatives on my end, but you're welcome to resolve those however you see fit.

On a general note I'm not sure if you knew we also have a rewrite-github-actions. I don't think that's applicable for the changes you're doing here, but thought to point it out in case there's more changes you want to do to GitHub Actions files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
recipe Requests for new automated code changes
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

Enable Jenkins Security Scan if needed
2 participants