Skip to content

Commit

Permalink
feat(PR): Customize pr body (#595)
Browse files Browse the repository at this point in the history
* Implement a way to customize the pull request body

Fixes #75

Add support for customizing the pull request body using `jte` template files.

* Add a new method `hasBodyTemplate` in `TemplateUtils.java` to check for specific `jte` templates for the pull request body.
* Update the `renderPullRequestBody` method in `TemplateUtils.java` to use specific `jte` templates if available.
* Create a new `jte` template `pr-body-SetupJenkinsfile.jte` for customizing the pull request body for the `SetupJenkinsfile` recipe, including a detailed explanation of why this recipe is important.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/gounthar/plugin-modernizer-tool/issues/75?shareId=XXXX-XXXX-XXXX-XXXX).

* Update pr-body-SetupJenkinsfile.jte

* fix(jte):  Adding aria-label for better accessibility.

* fix(jte): Extracting common template name generation logic.

* fix(jte): Extracting common template name generation logic.

* fix(jte): Spotless.

* fix(jte): Adds a custom title for the SetupJenkinsfile recipe.

* fix(jte): Reverts the custom title "optimization"

* fix(jte): Reverts the custom title "optimization"

* fix(jte): Fix the double "jte" suffix.
  • Loading branch information
gounthar authored Jan 8, 2025
1 parent 165dcf2 commit fb4388e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,29 @@ public class TemplateUtils {
*/
private TemplateUtils() {}

private static String getTemplateNameForRecipe(String prefix, Recipe recipe) {
String shortName = recipe.getName().replaceAll(Settings.RECIPE_FQDN_PREFIX + ".", "");
return "%s-%s.jte".formatted(prefix, shortName);
}

/**
* Render the pull request body
*
* @param plugin Plugin to modernize
* @param recipe Recipe to apply
* @return The rendered pull request body
*/
public static String renderPullRequestBody(Plugin plugin, Recipe recipe) {
if (hasBodyTemplate(recipe)) {
return renderTemplate(
getTemplateNameForRecipe("pr-body", recipe), Map.of("plugin", plugin, "recipe", recipe));
}
return renderTemplate("pr-body.jte", Map.of("plugin", plugin, "recipe", recipe));
}

/**
* Render the commit message
*
* @param plugin Plugin to modernize
* @param recipe Recipe to apply
* @return The rendered commit message
Expand All @@ -46,22 +57,24 @@ public static String renderCommitMessage(Plugin plugin, Recipe recipe) {

/**
* Render the pull request title
*
* @param plugin Plugin to modernize
* @param recipe Recipe to apply
* @return The rendered pull request title
*/
public static String renderPullRequestTitle(Plugin plugin, Recipe recipe) {
if (hasTitleTemplate(recipe)) {
String shortName = recipe.getName().replaceAll(Settings.RECIPE_FQDN_PREFIX + ".", "");
return renderTemplate("pr-title-%s.jte".formatted(shortName), Map.of("plugin", plugin, "recipe", recipe));
return renderTemplate(
getTemplateNameForRecipe("pr-title", recipe), Map.of("plugin", plugin, "recipe", recipe));
}
return renderTemplate("pr-title.jte", Map.of("plugin", plugin, "recipe", recipe));
}

/**
* Render a generic template
*
* @param templateName Name of the template
* @param params Parameters to pass to the template
* @param params Parameters to pass to the template
* @return The rendered template
*/
private static String renderTemplate(String templateName, Map<String, Object> params) {
Expand All @@ -78,6 +91,7 @@ private static String renderTemplate(String templateName, Map<String, Object> pa

/**
* Check if a title template exists for one recipe
*
* @param recipe The recipe to check
* @return True if a title template exists
*/
Expand All @@ -86,4 +100,15 @@ private static boolean hasTitleTemplate(Recipe recipe) {
TemplateEngine templateEngine = TemplateEngine.createPrecompiled(ContentType.Html);
return templateEngine.hasTemplate("pr-title-%s.jte".formatted(shortName));
}

/**
* Check if a body template exists for one recipe
*
* @param recipe The recipe to check
* @return True if a body template exists
*/
private static boolean hasBodyTemplate(Recipe recipe) {
TemplateEngine templateEngine = TemplateEngine.createPrecompiled(ContentType.Html);
return templateEngine.hasTemplate(getTemplateNameForRecipe("pr-body", recipe));
}
}
25 changes: 25 additions & 0 deletions plugin-modernizer-core/src/main/jte/pr-body-SetupJenkinsfile.jte
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@import io.jenkins.tools.pluginmodernizer.core.model.Plugin
@import io.jenkins.tools.pluginmodernizer.core.model.Recipe
@param Plugin plugin
@param Recipe recipe
Hello `${plugin.getName()}` developers! :wave:

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:
<details aria-label="Recipe details for ${recipe.getDisplayName()}">
<summary>${recipe.getDisplayName()}</summary>
<p><em>${recipe.getName()}</em></p>
<blockquote>${recipe.getDescription()}</blockquote>
</details>

## Why is this important?

Starting with the Jenkins 2.463 weekly release, Jenkins now requires Java 17 or newer.
The first Long-Term Support (LTS) release requiring Java 17 or newer (version 2.479.x) was released at the end of October 2024.

The Jenkins core team strongly recommends that all users adopt either Java 17 or Java 21.
The adoption of Java 17 has almost surpassed that of Java 11, and the usage of Java 21 is rapidly increasing.

There will come a time when we no longer support plugins built with JDK 8 or 11.

While this PR does not automatically make your plugin compatible with Java 17 or 21, it represents the first step towards a new era. Your plugin will be built and tested within the Jenkins infrastructure using Java 17 and 21.
After this PR is merged, we will submit additional automated PRs to enable your plugin to build with Java 17 and 21.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import io.jenkins.tools.pluginmodernizer.core.model.Plugin
@import io.jenkins.tools.pluginmodernizer.core.model.Recipe
@param Plugin plugin
@param Recipe recipe
feat(ci): Builds on the Jenkins Infrastructure

0 comments on commit fb4388e

Please sign in to comment.