Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/main/java/org/jenkinsci/plugins/jiraext/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public static final class PluginDescriptor
private String username;
private String password;
private String pattern;
private String includeStatuses;
private String excludeStatuses;
private boolean verboseLogging;
private Integer timeout;

Expand Down Expand Up @@ -127,6 +129,24 @@ public void setPattern(String pattern)
this.pattern = pattern;
}

public String getIncludeStatuses() {
return includeStatuses;
}

public void setIncludeStatuses(String statusList)
{
this.includeStatuses = statusList;
}

public String getExcludeStatuses() {
return excludeStatuses;
}

public void setExcludeStatuses(String statusList)
{
this.excludeStatuses = statusList;
}

public void setVerboseLogging(boolean verboseLogging)
{
this.verboseLogging = verboseLogging;
Expand All @@ -153,6 +173,8 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc
setUsername(formData.getString("username"));
setPassword(formData.getString("password"));
setPattern(formData.getString("pattern"));
setIncludeStatuses(formData.getString("includeStatuses"));
setExcludeStatuses(formData.getString("excludeStatuses"));
setVerboseLogging(formData.getBoolean("verboseLogging"));
setTimeout(formData.getInt("timeout"));
save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.Validate;
import org.jenkinsci.plugins.jiraext.Config;
import org.jenkinsci.plugins.jiraext.svc.JiraClientFactory;
import org.jenkinsci.plugins.jiraext.svc.JiraClientSvc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -75,7 +77,15 @@ public void addCommentToTicket(String jiraIssueKey, String comment) throws JiraE
{
logger.fine("Add comment to ticket: " + jiraIssueKey + " comment: " + comment);
JiraClient client = newJiraClient();
client.getIssue(jiraIssueKey).addComment(comment);
Issue issue = client.getIssue(jiraIssueKey);
Validate.notNull(issue);

//check if issue is the correct status
if (!issueStatusIsValid(issue)){
return;
}

issue.addComment(comment);
}

@Override
Expand All @@ -85,6 +95,13 @@ public void addLabelToTicket(String jiraIssueKey, String labelsToAdd) throws Jir

JiraClient client = newJiraClient();
Issue issue = client.getIssue(jiraIssueKey);
Validate.notNull(issue);

//check if issue is the correct status
if (!issueStatusIsValid(issue)){
return;
}

for (String labelToAdd : labelsToAdd.split(" "))
{
if (issue.getLabels().contains(labelToAdd))
Expand All @@ -105,6 +122,13 @@ public void changeWorkflowOfTicket(String jiraIssueKey, String transitionName) t
logger.fine("Transition ticket: " + jiraIssueKey + " transition name: " + transitionName);
JiraClient jiraClient = newJiraClient();
Issue issue = jiraClient.getIssue(jiraIssueKey);
Validate.notNull(issue);

//check if issue is the correct status
if (!issueStatusIsValid(issue)){
return;
}

issue.transition().execute(transitionName);
}

Expand All @@ -118,6 +142,12 @@ public void updateStringField(String jiraIssueKey, String jiraFieldName,
JiraClient client = newJiraClient();
Issue issue = client.getIssue(jiraIssueKey);
Validate.notNull(issue);

//check if issue is the correct status
if (!issueStatusIsValid(issue)){
return;
}

issue.update().field(jiraFieldName, content).execute();
}

Expand All @@ -130,6 +160,12 @@ public void updateMultiSelectField(String jiraIssueKey, String jiraFieldName, St
JiraClient client = newJiraClient();
Issue issue = client.getIssue(jiraIssueKey);
Validate.notNull(issue);

//check if issue is the correct status
if (!issueStatusIsValid(issue)){
return;
}

RestClient restClient = client.getRestClient();
try
{
Expand Down Expand Up @@ -162,6 +198,12 @@ public void addFixVersion(String jiraIssueKey, String newFixVersion)
JiraClient client = newJiraClient();
Issue issue = client.getIssue(jiraIssueKey);
Validate.notNull(issue);

//check if issue is the correct status
if (!issueStatusIsValid(issue)){
return;
}

List<Version> existingVersions = issue.getFixVersions();
for (Version version : existingVersions)
{
Expand Down Expand Up @@ -241,4 +283,42 @@ private Version getVersion(List<Version> projectVersions, String name)
}
throw new JiraException("Unable to find version with name: " + name + ", only found versions named: " + foundVersionNames);
}


/**
* Checks that an issue has a valid status
*
* i.e. the issue is in the list of included statuses or not in the list of
* excluded statuses
*
* @param issue
* @return true if the issue should be updated, false if it should be skipped
*/
private boolean issueStatusIsValid(Issue issue) {
Config.PluginDescriptor config = Config.getGlobalConfig();

List<String> includeStatuses = new ArrayList<>();
if (config.getIncludeStatuses().length() > 0) {
includeStatuses = Arrays.asList(config.getIncludeStatuses().split(","));
}

List<String> excludeStatuses = new ArrayList<>();
if (config.getExcludeStatuses().length() > 0) {
excludeStatuses = Arrays.asList(config.getExcludeStatuses().split(","));
}

//if 'Include status' is specified, don't update the issue if it's status is not in the
//list of included statuses.
if (!includeStatuses.isEmpty() && !includeStatuses.contains(issue.getStatus().getName())) {
return false;
}

//if 'Exclude status' is specified, don't update the issue if it's status is in the list
//of excluded statuses.
if (excludeStatuses.contains(issue.getStatus().getName())) {
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
description="Comma-separate string of jira prefixes">
<f:textbox default="FOO-,BAR-" />
</f:entry>
<f:entry title="Include Status" field="includeStatuses"
description="Comma-separate string of jira issue status's to filter on">
<f:textbox default="" />
</f:entry>
<f:entry title="Exclude Status" field="excludeStatuses"
description="Comma-separate string of jira issue status's to filter out">
<f:textbox default="" />
</f:entry>
<f:entry title="Verbose Logging" field="verboseLogging"
description="Whether to log a lot of information, ie API http debugging, in jobs">
<f:checkbox />
Expand Down