Skip to content

Commit 6158a3b

Browse files
authored
Merge pull request #1519 from jonesbusy/feature/auto-merge-workflows
Add auto-merge workflows recipes
2 parents 08ee372 + f05c231 commit 6158a3b

File tree

7 files changed

+172
-1
lines changed

7 files changed

+172
-1
lines changed

plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/extractor/ArchetypeCommonFile.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ public enum ArchetypeCommonFile {
3030
*/
3131
WORKFLOW_SECURITY(".github/workflows/jenkins-security-scan.yml", ".github/workflows/jenkins-security-scan.yaml"),
3232

33+
/**
34+
* Auto-merge safe dependencies workflow
35+
*/
36+
WORKFLOW_AUTO_MERGE_SAFE_DEPS(".github/workflows/auto-merge-safe-deps.yml"),
37+
38+
/**
39+
* Close bom PR if passing CI
40+
*/
41+
WORKFLOW_CLOSE_BOM_IF_PASSING(".github/workflows/close-bom-if-passing.yml"),
42+
3343
/**
3444
* Release drafter file
3545
*/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@import io.jenkins.tools.pluginmodernizer.core.model.Plugin
2+
@import io.jenkins.tools.pluginmodernizer.core.model.Recipe
3+
@param Plugin plugin
4+
@param Recipe recipe
5+
Hello `${plugin.getName()}` developers! :wave:
6+
7+
This is an automated pull request created by the [Jenkins Plugin Modernizer](https://github.com/jenkins-infra/plugin-modernizer-tool) tool. The tool has applied the following recipes to modernize the plugin:
8+
<details aria-label="Recipe details for ${recipe.getDisplayName()}">
9+
<summary>${recipe.getDisplayName()}</summary>
10+
<p><em>${recipe.getName()}</em></p>
11+
<blockquote>${recipe.getDescription()}</blockquote>
12+
</details>
13+
14+
## Setup auto-merge workflows
15+
16+
Setup the auto-merge workflows from Jenkins plugin archetype for safe dependencies updates (like parent pom or extensions).
17+
18+
Workflows either merge or close the PRs depending on the dependency.
19+
20+
## Why is this important?
21+
22+
Reduce maintenance burden and noise in automatic PRs by auto-merging safe dependency updates.
23+
24+
Closing BOM update PRs will ensure that the plugin depends on minimum plugins version for the given Jenkins baseline.
25+
26+
Bumping the BOM version is only required when the plugin needs a newer version of a plugin dependency.
27+
28+
See [Archetype](https://github.com/jenkinsci/archetypes/tree/master/common-files/.github/workflows) for more details.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import io.jenkins.tools.pluginmodernizer.core.model.Plugin
2+
@import io.jenkins.tools.pluginmodernizer.core.model.Recipe
3+
@param Plugin plugin
4+
@param Recipe recipe
5+
chore(workflows): Setup auto-merge workflows for safe dependencies updates and BOM updates.

plugin-modernizer-core/src/main/resources/META-INF/rewrite/recipes.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,3 +779,38 @@ recipeList:
779779
oldPackageName: javax.annotation
780780
newPackageName: edu.umd.cs.findbugs.annotations
781781
recursive: false
782+
---
783+
type: specs.openrewrite.org/v1beta/recipe
784+
name: io.jenkins.tools.pluginmodernizer.AutoMergeWorkflows
785+
displayName: Setup auto-merge workflows
786+
description: Setup auto-merge workflows for safe dependency updates and bom.
787+
tags: ['chore', 'skip-verification']
788+
recipeList:
789+
- org.openrewrite.text.CreateTextFile:
790+
relativeFileName: .github/workflows/auto-merge-safe-deps.yml
791+
overwriteExisting: false
792+
fileContents: |
793+
name: Automatically approve and merge safe dependency updates
794+
on:
795+
- pull_request_target
796+
permissions:
797+
contents: write
798+
pull-requests: write
799+
jobs:
800+
auto-merge-safe-deps:
801+
uses: jenkins-infra/github-reusable-workflows/.github/workflows/auto-merge-safe-deps.yml@v1
802+
- org.openrewrite.text.CreateTextFile:
803+
relativeFileName: .github/workflows/close-bom-if-passing.yml
804+
overwriteExisting: false
805+
fileContents: |
806+
name: Close BOM update PR if passing
807+
on:
808+
check_run:
809+
types:
810+
- completed
811+
permissions:
812+
contents: read
813+
pull-requests: write
814+
jobs:
815+
close-bom-if-passing:
816+
uses: jenkins-infra/github-reusable-workflows/.github/workflows/close-bom-if-passing.yml@v1

plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/DeclarativeRecipesTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4107,6 +4107,55 @@ void shouldNotAddGitIgnoreIfAlreadyPresent() {
41074107
}));
41084108
}
41094109

4110+
@Test
4111+
void shouldAddAutoMergeWorkflows() {
4112+
rewriteRun(
4113+
spec -> spec.recipeFromResource(
4114+
"/META-INF/rewrite/recipes.yml", "io.jenkins.tools.pluginmodernizer.AutoMergeWorkflows"),
4115+
text(""), // Need one minimum file to trigger the recipe
4116+
text(null, """
4117+
name: Close BOM update PR if passing
4118+
on:
4119+
check_run:
4120+
types:
4121+
- completed
4122+
permissions:
4123+
contents: read
4124+
pull-requests: write
4125+
jobs:
4126+
close-bom-if-passing:
4127+
uses: jenkins-infra/github-reusable-workflows/.github/workflows/close-bom-if-passing.yml@v1
4128+
""", sourceSpecs -> {
4129+
sourceSpecs.path(ArchetypeCommonFile.WORKFLOW_CLOSE_BOM_IF_PASSING.getPath());
4130+
}),
4131+
text(null, """
4132+
name: Automatically approve and merge safe dependency updates
4133+
on:
4134+
- pull_request_target
4135+
permissions:
4136+
contents: write
4137+
pull-requests: write
4138+
jobs:
4139+
auto-merge-safe-deps:
4140+
uses: jenkins-infra/github-reusable-workflows/.github/workflows/auto-merge-safe-deps.yml@v1
4141+
""", sourceSpecs -> {
4142+
sourceSpecs.path(ArchetypeCommonFile.WORKFLOW_AUTO_MERGE_SAFE_DEPS.getPath());
4143+
}));
4144+
}
4145+
4146+
@Test
4147+
void shouldNotAddAutoMergeWorkflowsIfAlreadyPresent() {
4148+
rewriteRun(
4149+
spec -> spec.recipeFromResource(
4150+
"/META-INF/rewrite/recipes.yml", "io.jenkins.tools.pluginmodernizer.AutoMergeWorkflows"),
4151+
text("name: Close BOM update PR if passing", sourceSpecs -> {
4152+
sourceSpecs.path(ArchetypeCommonFile.WORKFLOW_CLOSE_BOM_IF_PASSING.getPath());
4153+
}),
4154+
text("name: Automatically approve and merge safe dependency updates", sourceSpecs -> {
4155+
sourceSpecs.path(ArchetypeCommonFile.WORKFLOW_AUTO_MERGE_SAFE_DEPS.getPath());
4156+
}));
4157+
}
4158+
41104159
@Test
41114160
void shouldAddGitIgnore() {
41124161
rewriteRun(

plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/utils/TemplateUtilsTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,26 @@ public void testFriendlyPrTitleSwitchToRenovate() {
406406
assertEquals("chore(dependencies): Switch to Renovate for automated dependency updates", result);
407407
}
408408

409+
@Test
410+
public void testFriendlyPrTitleAutoMergeWorkflows() {
411+
// Mocks
412+
Plugin plugin = mock(Plugin.class);
413+
PluginMetadata metadata = mock(PluginMetadata.class);
414+
Recipe recipe = mock(Recipe.class);
415+
416+
doReturn(metadata).when(plugin).getMetadata();
417+
doReturn("io.jenkins.tools.pluginmodernizer.AutoMergeWorkflows")
418+
.when(recipe)
419+
.getName();
420+
421+
// Test
422+
String result = TemplateUtils.renderPullRequestTitle(plugin, recipe);
423+
424+
// Assert
425+
assertEquals(
426+
"chore(workflows): Setup auto-merge workflows for safe dependencies updates and BOM updates.", result);
427+
}
428+
409429
@Test
410430
public void testFriendlyPrTitleSetupJenkinsfile() {
411431

@@ -541,6 +561,29 @@ public void testFriendlyPrBodyRemoveReleaseDrafter() {
541561
"Missing or invalid link");
542562
}
543563

564+
@Test
565+
public void testFriendlyPrBodyAutoMergeWorkflows() {
566+
567+
// Mocks
568+
Plugin plugin = mock(Plugin.class);
569+
PluginMetadata metadata = mock(PluginMetadata.class);
570+
Recipe recipe = mock(Recipe.class);
571+
572+
doReturn(metadata).when(plugin).getMetadata();
573+
doReturn("io.jenkins.tools.pluginmodernizer.AutoMergeWorkflows")
574+
.when(recipe)
575+
.getName();
576+
577+
// Test
578+
String result = TemplateUtils.renderPullRequestBody(plugin, recipe);
579+
580+
// Assert
581+
assertTrue(result.contains("Why is this important?"), "Missing 'Why is this important?' section");
582+
assertTrue(
583+
result.contains("https://github.com/jenkinsci/archetypes/tree/master/common-files/.github/workflows"),
584+
"Missing or invalid link");
585+
}
586+
544587
@Test
545588
public void testFriendlyPrTitleEnsureRelativePath() {
546589

scripts/validate_metadata.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@
9797
"io.jenkins.tools.pluginmodernizer.RemoveOldJavaVersionForModernJenkins",
9898
"io.jenkins.tools.pluginmodernizer.SwitchToRenovate",
9999
"io.jenkins.tools.pluginmodernizer.JavaxAnnotationsToSpotbugs",
100-
"io.jenkins.tools.pluginmodernizer.AddIncrementals"
100+
"io.jenkins.tools.pluginmodernizer.AddIncrementals",
101+
"io.jenkins.tools.pluginmodernizer.AutoMergeWorkflows"
101102
]
102103

103104
def validate_metadata(file_path):

0 commit comments

Comments
 (0)