diff --git a/src/main/java/org/openrewrite/jenkins/github/AddSecurityScanWorkflow.java b/src/main/java/org/openrewrite/jenkins/github/AddSecurityScanWorkflow.java new file mode 100644 index 0000000..3531488 --- /dev/null +++ b/src/main/java/org/openrewrite/jenkins/github/AddSecurityScanWorkflow.java @@ -0,0 +1,170 @@ +/* + * Copyright 2023 the original author or authors. + *
+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * https://www.apache.org/licenses/LICENSE-2.0 + *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.jenkins.github;
+
+import lombok.EqualsAndHashCode;
+import lombok.Value;
+import org.intellij.lang.annotations.Language;
+import org.openrewrite.*;
+import org.openrewrite.internal.lang.Nullable;
+import org.openrewrite.yaml.ChangePropertyValue;
+import org.openrewrite.yaml.JsonPathMatcher;
+import org.openrewrite.yaml.YamlIsoVisitor;
+import org.openrewrite.yaml.YamlParser;
+import org.openrewrite.yaml.tree.Yaml;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.stream.Collectors;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.Objects.requireNonNull;
+
+@Value
+@EqualsAndHashCode(callSuper = true)
+public class AddSecurityScanWorkflow extends ScanningRecipe
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.jenkins.github;
+
+import org.openrewrite.Cursor;
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.marker.Markers;
+import org.openrewrite.yaml.JsonPathMatcher;
+import org.openrewrite.yaml.YamlIsoVisitor;
+import org.openrewrite.yaml.tree.Yaml;
+
+import java.util.List;
+
+import static org.openrewrite.Tree.randomId;
+
+/**
+ * Adapted from {@link org.openrewrite.yaml.AppendToSequenceVisitor}
+ */
+class ReplaceSequenceVisitor extends YamlIsoVisitor
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# More information about the Jenkins security scan can be found at the developer docs: https://www.jenkins.io/redirect/jenkins-security-scan/
+
+name: Jenkins Security Scan
+on:
+ push:
+ branches:
+ - "master"
+ - "main"
+ pull_request:
+ types: [ opened, synchronize, reopened ]
+ workflow_dispatch:
+
+permissions:
+ security-events: write
+ contents: read
+ actions: read
+
+jobs:
+ security-scan:
+ uses: jenkins-infra/jenkins-security-scan/.github/workflows/jenkins-security-scan.yaml@v2
+ with:
+ java-cache: 'maven' # Optionally enable use of a build dependency cache. Specify 'maven' or 'gradle' as appropriate.
+ java-version: 11 # What version of Java to set up for the build.
diff --git a/src/test/java/org/openrewrite/jenkins/github/AddSecurityScanWorkflowTest.java b/src/test/java/org/openrewrite/jenkins/github/AddSecurityScanWorkflowTest.java
new file mode 100644
index 0000000..6515158
--- /dev/null
+++ b/src/test/java/org/openrewrite/jenkins/github/AddSecurityScanWorkflowTest.java
@@ -0,0 +1,266 @@
+/*
+ * Copyright 2023 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.jenkins.github;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
+import org.openrewrite.test.RewriteTest;
+
+import java.util.List;
+
+import static org.openrewrite.yaml.Assertions.yaml;
+
+class AddSecurityScanWorkflowTest implements RewriteTest {
+
+ @Test
+ void shouldNoOp() {
+ // language=yaml
+ rewriteRun(
+ s -> s.recipe(new AddSecurityScanWorkflow(null, null, null)),
+ yaml("""
+ # More information about the Jenkins security scan can be found at the developer docs: https://www.jenkins.io/redirect/jenkins-security-scan/
+
+ name: Jenkins Security Scan
+ on:
+ push:
+ branches:
+ - "master"
+ - "main"
+ pull_request:
+ types: [ opened, synchronize, reopened ]
+ workflow_dispatch:
+
+ permissions:
+ security-events: write
+ contents: read
+ actions: read
+
+ jobs:
+ security-scan:
+ uses: jenkins-infra/jenkins-security-scan/.github/workflows/jenkins-security-scan.yaml@v2
+ with:
+ java-cache: 'maven' # Optionally enable use of a build dependency cache. Specify 'maven' or 'gradle' as appropriate.
+ java-version: 11 # What version of Java to set up for the build.
+ """,
+ s -> s.path(".github/workflows/jenkins-security-scan.yml")
+ )
+ );
+ }
+
+ @Test
+ @DocumentExample
+ void shouldAddFileIfMissing() {
+ // language=yaml
+ rewriteRun(
+ s -> s.recipe(new AddSecurityScanWorkflow(null, null, null)),
+ yaml(null,
+ """
+ # More information about the Jenkins security scan can be found at the developer docs: https://www.jenkins.io/redirect/jenkins-security-scan/
+
+ name: Jenkins Security Scan
+ on:
+ push:
+ branches:
+ - "master"
+ - "main"
+ pull_request:
+ types: [ opened, synchronize, reopened ]
+ workflow_dispatch:
+
+ permissions:
+ security-events: write
+ contents: read
+ actions: read
+
+ jobs:
+ security-scan:
+ uses: jenkins-infra/jenkins-security-scan/.github/workflows/jenkins-security-scan.yaml@v2
+ with:
+ java-cache: 'maven' # Optionally enable use of a build dependency cache. Specify 'maven' or 'gradle' as appropriate.
+ java-version: 11 # What version of Java to set up for the build.
+ """,
+ s -> s.path(".github/workflows/jenkins-security-scan.yml")
+ )
+ );
+ }
+
+ @Test
+ void shouldAddFileWithDeclaredBranch() {
+ rewriteRun(
+ s -> s.recipe(new AddSecurityScanWorkflow(List.of("release/1.x"), null, null)),
+ yaml(null,
+ // language=yaml
+ """
+ # More information about the Jenkins security scan can be found at the developer docs: https://www.jenkins.io/redirect/jenkins-security-scan/
+
+ name: Jenkins Security Scan
+ on:
+ push:
+ branches:
+ - "release/1.x"
+ pull_request:
+ types: [ opened, synchronize, reopened ]
+ workflow_dispatch:
+
+ permissions:
+ security-events: write
+ contents: read
+ actions: read
+
+ jobs:
+ security-scan:
+ uses: jenkins-infra/jenkins-security-scan/.github/workflows/jenkins-security-scan.yaml@v2
+ with:
+ java-cache: 'maven' # Optionally enable use of a build dependency cache. Specify 'maven' or 'gradle' as appropriate.
+ java-version: 11 # What version of Java to set up for the build.
+ """,
+ s -> s.path(".github/workflows/jenkins-security-scan.yml")
+ )
+ );
+ }
+
+ @Test
+ void shouldAddFileWithDeclaredJavaVersion() {
+ rewriteRun(
+ s -> s.recipe(new AddSecurityScanWorkflow(null, 17, null)),
+ yaml(null,
+ // language=yaml
+ """
+ # More information about the Jenkins security scan can be found at the developer docs: https://www.jenkins.io/redirect/jenkins-security-scan/
+
+ name: Jenkins Security Scan
+ on:
+ push:
+ branches:
+ - "master"
+ - "main"
+ pull_request:
+ types: [ opened, synchronize, reopened ]
+ workflow_dispatch:
+
+ permissions:
+ security-events: write
+ contents: read
+ actions: read
+
+ jobs:
+ security-scan:
+ uses: jenkins-infra/jenkins-security-scan/.github/workflows/jenkins-security-scan.yaml@v2
+ with:
+ java-cache: 'maven' # Optionally enable use of a build dependency cache. Specify 'maven' or 'gradle' as appropriate.
+ java-version: 17 # What version of Java to set up for the build.
+ """,
+ s -> s.path(".github/workflows/jenkins-security-scan.yml")
+ )
+ );
+ }
+
+ @Test
+ void shouldAddFileWithDeclaredBuildTool() {
+ rewriteRun(
+ s -> s.recipe(new AddSecurityScanWorkflow(null, null, "gradle")),
+ yaml(null,
+ // language=yaml
+ """
+ # More information about the Jenkins security scan can be found at the developer docs: https://www.jenkins.io/redirect/jenkins-security-scan/
+
+ name: Jenkins Security Scan
+ on:
+ push:
+ branches:
+ - "master"
+ - "main"
+ pull_request:
+ types: [ opened, synchronize, reopened ]
+ workflow_dispatch:
+
+ permissions:
+ security-events: write
+ contents: read
+ actions: read
+
+ jobs:
+ security-scan:
+ uses: jenkins-infra/jenkins-security-scan/.github/workflows/jenkins-security-scan.yaml@v2
+ with:
+ java-cache: 'gradle' # Optionally enable use of a build dependency cache. Specify 'maven' or 'gradle' as appropriate.
+ java-version: 11 # What version of Java to set up for the build.
+ """,
+ s -> s.path(".github/workflows/jenkins-security-scan.yml")
+ )
+ );
+ }
+
+ @Test
+ void shouldChangeFile() {
+ rewriteRun(
+ s -> s.recipe(new AddSecurityScanWorkflow(List.of("release/1.x"), 21, "gradle")),
+ // language=yaml
+ yaml("""
+ # More information about the Jenkins security scan can be found at the developer docs: https://www.jenkins.io/redirect/jenkins-security-scan/
+
+ name: Jenkins Security Scan
+ on:
+ push:
+ branches:
+ - "master"
+ - "main"
+ pull_request:
+ types: [ opened, synchronize, reopened ]
+ workflow_dispatch:
+
+ permissions:
+ security-events: write
+ contents: read
+ actions: read
+
+ jobs:
+ security-scan:
+ uses: jenkins-infra/jenkins-security-scan/.github/workflows/jenkins-security-scan.yaml@v2
+ with:
+ java-cache: 'maven' # Optionally enable use of a build dependency cache. Specify 'maven' or 'gradle' as appropriate.
+ java-version: 17 # What version of Java to set up for the build.
+ """,
+ """
+ # More information about the Jenkins security scan can be found at the developer docs: https://www.jenkins.io/redirect/jenkins-security-scan/
+
+ name: Jenkins Security Scan
+ on:
+ push:
+ branches:
+ - "release/1.x"
+ pull_request:
+ types: [ opened, synchronize, reopened ]
+ workflow_dispatch:
+
+ permissions:
+ security-events: write
+ contents: read
+ actions: read
+
+ jobs:
+ security-scan:
+ uses: jenkins-infra/jenkins-security-scan/.github/workflows/jenkins-security-scan.yaml@v2
+ with:
+ java-cache: 'gradle' # Optionally enable use of a build dependency cache. Specify 'maven' or 'gradle' as appropriate.
+ java-version: 21 # What version of Java to set up for the build.
+ """,
+ s -> s.path(".github/workflows/jenkins-security-scan.yml")
+ )
+ );
+ }
+
+}