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); } }
private void handleIssueCommentEvent(String payload) throws IOException { GitHub gitHub = createGitHub(JSONObject.fromObject(payload)); if (gitHub == null) { return; } GHEventPayload.IssueComment issueComment = gitHub.parseEventPayload(new StringReader(payload), GHEventPayload.IssueComment.class); if (LOGGER.isLoggable(Level.FINER)) { LOGGER.finer( MessageFormat.format( "Comment on {0} from {1}: {2}", issueComment.getIssue().getUrl(), issueComment.getComment().getUser(), issueComment.getComment().getBody())); } if (!"created".equals(issueComment.getAction())) { if (LOGGER.isLoggable(Level.FINER)) { LOGGER.finer("Unsupported issue_comment action: " + issueComment.getAction()); } return; } Authentication old = SecurityContextHolder.getContext().getAuthentication(); SecurityContextHolder.getContext().setAuthentication(ACL.SYSTEM); try { for (AbstractProject<?, ?> job : Jenkins.getInstance().getAllItems(AbstractProject.class)) { PullRequestBuildTrigger trigger = job.getTrigger(PullRequestBuildTrigger.class); if (trigger != null && trigger.getBuildHandler() instanceof PullRequestBuildHandler) { ((PullRequestBuildHandler) trigger.getBuildHandler()).handle(issueComment, gitHub); } } } finally { SecurityContextHolder.getContext().setAuthentication(old); } }
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())); } }