/**
  * Fill method for the Issue Priorities select box
  *
  * @param siteName the {@link JIRASite} name to get the priorities from
  * @return the possible priorities
  * @throws FormException in case of errors
  */
 public ListBoxModel doFillIssuePriorityItems(
     @QueryParameter(PARAMETER_PREFIX + "siteName") String siteName) throws FormException {
   ListBoxModel model = new ListBoxModel();
   if (StringUtils.isBlank(siteName)) {
     return model;
   }
   JIRAClient client = null;
   try {
     client = JIRASite.getSite(siteName).createClient();
     for (Map.Entry<String, String> priority : client.getPriorities().entrySet()) {
       model.add(priority.getValue(), priority.getKey());
     }
   } catch (RemoteException e) {
     LOGGER.log(Level.SEVERE, "Failed to get the JIRA Projects", e);
     throw new FormException(
         "Failed to get JIRA Project Keys", e, PARAMETER_PREFIX + "projectKey");
   } catch (MalformedURLException e) {
     throw new FormException("Invalid JIRA URL provided", e, PARAMETER_PREFIX + "projectKey");
   } catch (ServiceException e) {
     LOGGER.log(Level.SEVERE, "Failed to get the JIRA Projects", e);
     throw new FormException(
         "Failed to get JIRA Project Keys", e, PARAMETER_PREFIX + "projectKey");
   } finally {
     if (client != null) {
       client.logout();
     }
   }
   return model;
 }
 /**
  * Fill method for the IssueType field list box
  *
  * @param siteName the name of the selected {@link JIRASite}
  * @param projectKey the key of the JIRA Project
  * @return the possible values for the list box in a {@link ListBoxModel}
  * @throws FormException in case of communication issues with JIRA
  */
 public ListBoxModel doFillIssueTypeItems(
     @QueryParameter(PARAMETER_PREFIX + "siteName") String siteName,
     @QueryParameter(PARAMETER_PREFIX + "projectKey") String projectKey)
     throws FormException {
   ListBoxModel model = new ListBoxModel();
   if (StringUtils.isBlank(siteName) || StringUtils.isBlank(projectKey)) {
     return model;
   }
   JIRAClient client = null;
   try {
     client = JIRASite.getSite(siteName).createClient();
     for (RemoteIssueType issueType : client.getIssueTypesForProject(projectKey)) {
       model.add(issueType.getName(), issueType.getId());
     }
   } catch (RemoteException e) {
     LOGGER.log(Level.SEVERE, "Failed to get the JIRA Projects", e);
     throw new FormException(
         "Failed to get JIRA Project IssueTypes", e, PARAMETER_PREFIX + "projectKey");
   } catch (MalformedURLException e) {
     throw new FormException("Invalid JIRA URL provided", e, PARAMETER_PREFIX + "projectKey");
   } catch (ServiceException e) {
     LOGGER.log(Level.SEVERE, "Failed to get the JIRA Projects", e);
     throw new FormException(
         "Failed to get JIRA Project IssueTypes", e, PARAMETER_PREFIX + "projectKey");
   } finally {
     if (client != null) {
       client.logout();
     }
   }
   return model;
 }
 /** {@inheritDoc} */
 @Override
 public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
     throws InterruptedException, IOException {
   JIRAClient client = null;
   // Get the previous JIRA Build Action, if any.
   Run<?, ?> prevBuild = build.getPreviousBuild();
   JIRABuildResultReportAction buildAction = null;
   if (prevBuild != null) {
     buildAction = prevBuild.getAction(JIRABuildResultReportAction.class);
   }
   try {
     client = getSite().createClient();
     if (build.getResult().isWorseThan(Result.SUCCESS)) {
       String issueKey = null;
       if (buildAction != null
           && StringUtils.isNotBlank(buildAction.raisedIssueKey)
           && !buildAction.resolved) {
         // Oke the previous build also failed and has an Issue linked to it.
         // So relink that issue also to this build
         issueKey = client.updateIssue(build, buildAction.raisedIssueKey);
       } else {
         issueKey =
             client.createIssue(build, projectKey, issueType, issuePriority, assignToBuildBreaker);
       }
       build.addAction(new JIRABuildResultReportAction(build, issueKey, false));
       listener.getLogger().println("JBRR: Raised build failure issue: " + issueKey);
     } else if (autoClose && build.getResult().isBetterOrEqualTo(Result.SUCCESS)) {
       // Auto close the previously raised issues, if any
       if (buildAction != null) {
         RemoteIssue issue = client.getIssue(buildAction);
         if (issue == null || issue.getKey() == null) {
           listener
               .getLogger()
               .println(
                   "WARN: Failed to automatically close issue: Unable to locate issue "
                       + buildAction.raisedIssueKey
                       + " in JIRA site "
                       + getSite().name);
         } else if (client.canCloseIssue(issue)) {
           if (client.closeIssue(issue, build)) {
             build.addAction(new JIRABuildResultReportAction(build, issue.getKey(), true));
             listener
                 .getLogger()
                 .println(
                     "INFO: Closed issue "
                         + issue.getKey()
                         + " using action: "
                         + getSite().getCloseActionName());
           } else {
             listener
                 .getLogger()
                 .println("WARN: Failed to automatically close issue: " + issue.getKey());
           }
         }
       }
     }
   } catch (AxisFault e) {
     listener.error("JBRR: " + e.getFaultString());
   } catch (ServiceException e) {
     listener.error("JBRR: " + e.getMessage());
   } catch (MalformedURLException e) {
     listener.error("JBRR: Invalid JIRA URL configured");
   } finally {
     if (client != null) {
       client.logout();
     }
   }
   return true;
 }