Ejemplo n.º 1
0
  private void processAssigned(GithubEvent event) {
    if (config.hasUserMappings()) {
      GithubEvent.User ghUser = event.getAssignee();
      String jiraUser = config.getJiraUser(ghUser.getLogin());

      if (jiraUser != null) {
        // Get the current issue from JIRA
        String title = event.getIssue().getTitle();
        Matcher m = jiraIssuePattern.matcher(title);
        if (m.find()) {
          JiraConnector conn = new JiraConnector(config);
          GetIssue get = new GetIssue.Builder().withIssueKey(m.group(1)).build();
          try {
            JiraEvent.Issue issue = conn.execute(get);
            String jiraCurrentAssignee = issue.getAssignee();

            if (event.getAction().equals(GITHUB_UNASSIGNED)) {
              if (jiraCurrentAssignee != null && jiraCurrentAssignee.equals(jiraUser)) {
                // Update Jira issue with no one assigned
                UpdateIssue update =
                    new UpdateIssue.Builder()
                        .withJiraIssueKey(m.group(1))
                        .withAssignee(UpdateIssue.NO_ASSIGNEE)
                        .build();

                conn.execute(update);
              }
            } else {
              if (jiraCurrentAssignee == null || !jiraCurrentAssignee.equals(jiraUser)) {
                UpdateIssue update =
                    new UpdateIssue.Builder()
                        .withJiraIssueKey(m.group(1))
                        .withAssignee(jiraUser)
                        .build();

                conn.execute(update);
              }
            }
          } catch (ExecutionException ex) {
            Logger.getLogger(GithubWebhook.class.getName()).log(Level.SEVERE, null, ex);
          }
        }
      }
    }
  }
Ejemplo n.º 2
0
  private String processOpenedEvent(GithubEvent event) {
    JiraConnector conn = new JiraConnector(config);
    String jiraIssueKey = null;

    if (event.hasIssue()) {
      String title = event.getIssue().getTitle();
      Matcher m = jiraIssuePattern.matcher(title);
      if (m.find()) {
        // Originated from JIRA, need to update JIRA with issue number and external link

        String githubIssueField = config.getJira().getGithubIssueNumberField();
        jiraIssueKey = m.group(1);

        UpdateIssue update =
            new UpdateIssue.Builder()
                .withCustomField(githubIssueField, event.getIssue().getNumber())
                .withJiraIssueKey(jiraIssueKey)
                .build();
        try {
          conn.execute(update);
          createExternalLink(conn, jiraIssueKey, event);
        } catch (ExecutionException ex) {
          Logger.getLogger(GithubWebhook.class.getName()).log(Level.SEVERE, null, ex);
        }
      } else {
        // Originated in github. Create in JIRA and add external link

        String githubIssueField = config.getJira().getGithubIssueNumberField();
        String jiraRepoField = config.getJira().getGithubRepoNameField();
        ServiceConfig.Repository repo =
            config.getRepoForGithubName(event.getRepository().getName());

        String jiraProjectKey = repo.getJiraProjectKey();
        String jiraRepoName = repo.getJiraName();
        int githubIssueNumber = event.getIssue().getNumber();

        String body =
            event.getIssue().getBody()
                + "\n\n[Created in Github by "
                + event.getIssue().getUser().getLogin()
                + " ]";

        /*
         * If we're mapping milestones to eipcs, have some
         * work to do here.
         */
        String epicJiraKey = null;
        if (repo.mapEpicsToMilestones() && event.getIssue().hasMilestone()) {
          Milestone ms = event.getIssue().getMilestone();
          String epicName = ms.getTitle();

          // Of course, they can't make this easy. Querying custom fields
          // requires a format of cf[xxxx] rather than, you know, the field
          // name.
          m = extractCustomFieldNumber.matcher(config.getJira().getEpicNameField());
          m.find();
          String cfNumber = m.group(1);
          String jql =
              "project = " + jiraProjectKey + " and cf[" + cfNumber + "] = \"" + epicName + "\"";

          SearchIssues search = new SearchIssues.Builder().withJQL(jql).build();
          try {
            List<JiraEvent.Issue> epicList = conn.execute(search);
            // Should only return one or zero.
            if (!epicList.isEmpty()) {
              JiraEvent.Issue epic = epicList.get(0);
              epicJiraKey = epic.getJiraIssueKey();
            }

          } catch (ExecutionException ex) {
            Logger.getLogger(GithubWebhook.class.getName()).log(Level.SEVERE, null, ex);
          }
        }

        CreateIssue.Builder builder =
            new CreateIssue.Builder()
                .withProjectKey(jiraProjectKey)
                .withIssuetype("Task")
                .withSummary(event.getIssue().getTitle())
                .withDescription(body)
                .withCustomField(githubIssueField, githubIssueNumber)
                .withCustomField(jiraRepoField, "value", jiraRepoName);

        // populate any custom fields from repo config
        for (ServiceConfig.Repository.JiraField field : repo.getJiraFields()) {
          if (field.getType().equals("object")) {
            builder.withCustomField(field.getName(), field.getKey(), field.getValue());
          } else {
            builder.withCustomField(field.getName(), field.getValue());
          }
        }

        if (epicJiraKey != null) {
          builder.withCustomField(config.getJira().getEpicLinkField(), epicJiraKey);
        }

        if (repo.labelVersions()) {
          List<String> fixVersions = new LinkedList<>();
          List<String> affectsVersions = new LinkedList<>();
          for (GithubEvent.Issue.Label label : event.getIssue().getLabels()) {
            m = extractFixedVersion.matcher(label.getName());
            if (m.find()) {
              fixVersions.add(m.group(1));
            } else {
              m = extractAffectsVersion.matcher(label.getName());
              if (m.find()) {
                affectsVersions.add(m.group(1));
              }
            }
          }
          builder.withAffectsVersions(affectsVersions).withFixVersions(fixVersions);
        }

        if (config.hasUserMappings() && event.getIssue().hasAssignee()) {
          String jiraAssignee = config.getJiraUser(event.getIssue().getAssignee().getLogin());

          builder.withAssignee(jiraAssignee);
        }

        try {
          jiraIssueKey = conn.execute(builder.build());
          createExternalLink(conn, jiraIssueKey, event);
        } catch (ExecutionException ex) {
          Logger.getLogger(GithubWebhook.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
    } else if (event.hasPullRequest()) {
      linkPullRequestToIssue(conn, event);
    }

    return jiraIssueKey;
  }