Skip to content

Commit

Permalink
[JENKINS-39663] Allow issue template to be customised per-job
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel15 committed Nov 13, 2016
1 parent 072ef1e commit 026dfff
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
Expand Up @@ -34,9 +34,36 @@
* passing again.
*/
public class GitHubIssueNotifier extends Notifier implements SimpleBuildStep {
private final boolean useCustomTemplate;
private final String customTitle;
private final String customBody;
private final String customLabel;

@DataBoundConstructor
public GitHubIssueNotifier() {
public GitHubIssueNotifier(boolean useCustomTemplate, String customTitle, String customBody, String customLabel) {
this.useCustomTemplate = useCustomTemplate;
if (useCustomTemplate) {
this.customTitle = customTitle;
this.customBody = customBody;
this.customLabel = customLabel;
} else {
this.customTitle = null;
this.customBody = null;
this.customLabel = null;
}
}

public String getCustomTitle() {
return customTitle;
}
public String getCustomBody() {
return customBody;
}
public String getCustomLabel() {
return customLabel;
}
public boolean getUseCustomTemplate() {
return useCustomTemplate;
}

@Override
Expand Down Expand Up @@ -119,7 +146,7 @@ public void perform(
}

if (result == Result.FAILURE || result == Result.UNSTABLE) {
GHIssue issue = IssueCreator.createIssue(run, getDescriptor(), repo, listener, workspace);
GHIssue issue = IssueCreator.createIssue(run, this, repo, listener, workspace);
logger.format("GitHub Issue Notifier: Build has started failing, filed GitHub issue #%s%n", issue.getNumber());
property.setIssueNumber(issue.getNumber());
job.save();
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/org/jenkinsci/plugins/githubissues/IssueCreator.java
Expand Up @@ -11,6 +11,7 @@
import hudson.FilePath;
import hudson.model.Run;
import hudson.model.TaskListener;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHIssueBuilder;
Expand Down Expand Up @@ -57,26 +58,30 @@ public static String formatText(
/**
* Creates a GitHub issue for a failing build
* @param run Build that failed
* @param descriptor Descriptor for GitHubIssueNotifier
* @param notifier The instance of GitHubIssueNotifier
* @param repo Repository to create the issue in
* @param listener Build listener
* @param workspace Build workspace
* @return The issue that was created
* @throws IOException
* @throws IOException If creating the issue fails
*/
public static GHIssue createIssue(
Run<?, ?> run,
GitHubIssueNotifier.DescriptorImpl descriptor,
GitHubIssueNotifier notifier,
GHRepository repo,
TaskListener listener,
FilePath workspace
) throws IOException {
GHIssueBuilder issue = repo.createIssue(formatText(descriptor.getIssueTitle(), run, listener, workspace))
.body(formatText(descriptor.getIssueBody(), run, listener, workspace));
GitHubIssueNotifier.DescriptorImpl descriptor = notifier.getDescriptor();
String title = StringUtils.defaultIfBlank(notifier.getCustomTitle(), descriptor.getIssueTitle());
String body = StringUtils.defaultIfBlank(notifier.getCustomBody(), descriptor.getIssueBody());
String label = StringUtils.defaultIfBlank(notifier.getCustomLabel(), descriptor.getIssueLabel());

GHIssueBuilder issue = repo.createIssue(formatText(title, run, listener, workspace))
.body(formatText(body, run, listener, workspace));

String issueLabel = descriptor.getIssueLabel();
if (issueLabel != null && !issueLabel.isEmpty()) {
issue = issue.label(issueLabel);
if (label != null && !label.isEmpty()) {
issue = issue.label(label);
}
return issue.create();
}
Expand Down
@@ -0,0 +1,16 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:block>
<f:optionalBlock name="useCustomTemplate" inline="true" title="Customize issue template" checked="${instance.useCustomTemplate}">
<f:entry title="Issue Title Template" field="customTitle">
<f:textbox default="${descriptor.issueTitle}" />
</f:entry>
<f:entry title="Issue Body Template" field="customBody">
<f:textarea default="${descriptor.issueBody}" />
</f:entry>
<f:entry title="Issue Label" field="customLabel">
<f:textbox default="${descriptor.issueLabel}" />
</f:entry>
</f:optionalBlock>
</f:block>
</j:jelly>

0 comments on commit 026dfff

Please sign in to comment.