Ejemplo n.º 1
0
  private void processCreatedEvent(GithubEvent event) {
    JiraConnector conn = new JiraConnector(config);

    if (event.hasIssue() && event.hasComment()) {
      if (event.getIssue().isReallyAPullRequest()) {
        // Allow comments on pull requests to link to an issue
        linkPullRequestToIssue(conn, event);
      } else {
        String body = event.getComment().getBody();
        Matcher m = jiraCommentPattern.matcher(body);

        if (!m.find()) {
          // Comment originating on GH, post to JIRA

          // The JIRA issue key is in the title
          String issueTitle = event.getIssue().getTitle();
          m = jiraIssuePattern.matcher(issueTitle);
          if (m.find()) {
            postCommentToJira(conn, m.group(1), event.getComment().getUser().getLogin(), body);
          } else {
            // The issue isn't in JIRA. Check to see if we should
            // import it. This is for when the service is added to
            // a GH repo with existing issues.
            Repository repo = config.getRepoForGithubName(event.getRepository().getName());
            if (repo.importOnComment()) {
              String jiraIssueKey = processOpenedEvent(event);

              // Now we have to import comments
              GithubConnector ghConn = new GithubConnector(config);

              GetCommentsOnIssue get =
                  new GetCommentsOnIssue.Builder()
                      .withRepository(repo)
                      .withIssueNumber(event.getIssue().getNumber())
                      .build();
              try {
                List<GithubEvent.Comment> comments = ghConn.execute(get);
                for (Comment comment : comments) {
                  postCommentToJira(
                      conn, jiraIssueKey, comment.getUser().getLogin(), comment.getBody());
                }
              } catch (ExecutionException ex) {
                Logger.getLogger(GithubWebhook.class.getName()).log(Level.SEVERE, null, ex);
              }
            }
          }
        }
      }
    }
  }
Ejemplo n.º 2
0
  private void linkPullRequestToIssue(JiraConnector conn, GithubEvent event) {
    // PR created, see if it mentions a GH isse or JIRA issue
    // If it does, update in JIRA.
    String body;
    if (event.hasPullRequest()) {
      body = event.getPullRequest().getBody();
    } else // It's a pull request comment, disguised as an issue (wrapped in an Enigma)
    {
      body = event.getComment().getBody();
    }

    List<String> ghIssueNumbers = scanForGithubIssueMentions(body);
    List<String> directJiraMentions = scanForJiraIssueMentions(body);

    // For GH issues, we have to query JIRA and get back the issue
    // keys that have the issue in the github issue id field and add those
    // to the jira keys.
    List<String> ghIssueMentions = new LinkedList<>();

    // Of course, they can't make this easy. Querying custom fields
    // requires a format of cf[xxxx] rather than, you know, the field
    // name.
    Matcher m = extractCustomFieldNumber.matcher(config.getJira().getGithubIssueNumberField());
    m.find();
    String cfNumber = m.group(1);
    ServiceConfig.Repository repo = config.getRepoForGithubName(event.getRepository().getName());
    String jiraProjectKey = repo.getJiraProjectKey();

    Map<String, String> jiraKeyToGhNum = new HashMap<>();

    for (String ghIssueNum : ghIssueNumbers) {
      String jql = "project = " + jiraProjectKey + " and cf[" + cfNumber + "] = " + ghIssueNum;

      SearchIssues search = new SearchIssues.Builder().withJQL(jql).build();
      try {
        List<JiraEvent.Issue> issues = conn.execute(search);
        for (JiraEvent.Issue issue : issues) {
          ghIssueMentions.add(issue.getJiraIssueKey());
          jiraKeyToGhNum.put(issue.getJiraIssueKey(), ghIssueNum);
        }
      } catch (ExecutionException ex) {
        Logger.getLogger(GithubWebhook.class.getName()).log(Level.SEVERE, null, ex);
      }
    }

    // Now we have all the JIRA issues mentioned in this PR, either
    // directly or indirectly. Update them with the link to the PR
    List<String> jiraIssueKeys = new LinkedList<>();
    jiraIssueKeys.addAll(directJiraMentions);
    jiraIssueKeys.addAll(ghIssueMentions);
    for (String jKey : jiraIssueKeys) {
      try {
        createExternalLink(conn, jKey, event);
      } catch (ExecutionException ex) {
        Logger.getLogger(GithubWebhook.class.getName()).log(Level.SEVERE, null, ex);
      }
    }

    // For direct JIRA mentions, we want to update the PR
    // with the GH issue #. For GH Issue nums, the JIRA key. Unfortunately when
    // you add an external link to a JIRA issue it doesn't send an
    // issue update out. It seems as though editing a PR in GH doesn't
    // send out a update notice which is kinda annoying on one hand,
    // but should work well here.

    GithubConnector ghConn = new GithubConnector(config);

    for (String jKey : jiraIssueKeys) {
      try {
        if (jiraKeyToGhNum.containsKey(jKey)) {
          String ghIssueNum = jiraKeyToGhNum.get(jKey);
          body = body.replace("#" + ghIssueNum, "#" + ghIssueNum + " (" + jKey + ")");
        } else {
          // Get GH issue number from issue in JIRA
          GetIssue get = new GetIssue.Builder().withIssueKey(jKey).build();
          JiraEvent.Issue issue = conn.execute(get);
          // update this PR body with the GH issue number
          if (issue.hasGithubIssueNumber(config)) {
            body = body.replace(jKey, jKey + " (#" + issue.getGithubIssueNumber(config) + ")");
          }
        }

        if (event.hasPullRequest()) {
          UpdatePullRequest update =
              new UpdatePullRequest.Builder()
                  .withRepository(repo)
                  .withBody(body)
                  .withPullRequestNumber(event.getPullRequest().getNumber())
                  .build();

          ghConn.execute(update);

        } else // pull request comment
        {
          ModifyComment modify =
              new ModifyComment.Builder()
                  .withBody(body)
                  .withRepository(repo)
                  .withCommentId(event.getComment().getId())
                  .build();

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