コード例 #1
0
  public void doIndex(StaplerRequest req, StaplerResponse resp) {
    String event = req.getHeader("X-GitHub-Event");
    String payload = req.getParameter("payload");
    if (payload == null) {
      logger.log(Level.SEVERE, "Request doesn't contain payload.");
      return;
    }

    GhprbGitHub gh = GhprbTrigger.getDscp().getGitHub();

    logger.log(Level.INFO, "Got payload event: {0}", event);
    try {
      if ("issue_comment".equals(event)) {
        GHEventPayload.IssueComment issueComment =
            gh.get()
                .parseEventPayload(new StringReader(payload), GHEventPayload.IssueComment.class);
        for (GhprbRepository repo : getRepos(issueComment.getRepository())) {
          repo.onIssueCommentHook(issueComment);
        }
      } else if ("pull_request".equals(event)) {
        GHEventPayload.PullRequest pr =
            gh.get().parseEventPayload(new StringReader(payload), GHEventPayload.PullRequest.class);
        for (GhprbRepository repo : getRepos(pr.getPullRequest().getRepository())) {
          repo.onPullRequestHook(pr);
        }
      } else {
        logger.log(Level.WARNING, "Request not known");
      }
    } catch (IOException ex) {
      logger.log(Level.SEVERE, "Failed to parse github hook payload.", ex);
    }
  }
コード例 #2
0
  void handle(GHEventPayload.IssueComment issueComment, GitHub gitHub) throws IOException {
    // check the trigger phrase
    PullRequestBuildTrigger trigger = project.getTrigger(PullRequestBuildTrigger.class);
    if (StringUtils.isBlank(trigger.getTriggerPhrase())) {
      if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine("No trigger phrase configured for project: " + project.getDisplayName());
      }
      return;
    }

    String issueUrl = issueComment.getIssue().getHtmlUrl().toString();
    if (!issueUrl.startsWith(gitHubRepositoryUrl)) {
      LOGGER.finest(
          MessageFormat.format(
              "GitHub issue {0} is not related to project {1}. "
                  + "GitHub project URL configured for project {1}: {2}",
              issueUrl, project.getFullName(), gitHubRepositoryUrl));

      return;
    }

    String commentBody = issueComment.getComment().getBody();
    if (!triggerPhrasePattern.matcher(commentBody).find()) {
      if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine("No trigger phrase matching on comment: " + commentBody);
      }
      return;
    }

    GHUser buildRequester = issueComment.getComment().getUser();
    if (!isWhitelisted(buildRequester, gitHub)) {
      LOGGER.info(
          MessageFormat.format(
              "GitHub user {0} is not in the whitelist of project {1}",
              buildRequester.getLogin(), project.getFullName()));

      return;
    }

    final int prNumber = issueComment.getIssue().getNumber();

    GHPullRequest pullRequest = issueComment.getRepository().getPullRequest(prNumber);
    // Updating PR to force the cache (if present) to refresh:
    pullRequest.setTitle(pullRequest.getTitle());

    pullRequest = issueComment.getRepository().getPullRequest(prNumber);

    if (LOGGER.isLoggable(Level.FINE)) {
      LOGGER.fine("ghPullRequest = " + getPullRequestAsString(pullRequest));
    }

    if (pullRequest.getState() == GHIssueState.OPEN) {
      PullRequestData pullRequestData =
          PullRequestManager.getInstance().addPullRequestData(pullRequest, project);

      cancelBuilds(pullRequestData);
      build(pullRequest, buildRequester, new TriggerCause(pullRequest, buildRequester));
    } else if (LOGGER.isLoggable(Level.FINE)) {
      LOGGER.fine(
          MessageFormat.format(
              "Pull request {0} is not opened, no build is triggered",
              pullRequest.getHtmlUrl().toString()));
    }
  }