void onIssueCommentHook(IssueComment issueComment) throws IOException { if (helper.isProjectDisabled()) { logger.log(Level.FINE, "Not checking comments since build is disabled"); return; } int id = issueComment.getIssue().getNumber(); logger.log( Level.FINER, "Comment on issue #{0} from {1}: {2}", new Object[] { id, issueComment.getComment().getUser(), issueComment.getComment().getBody() }); if (!"created".equals(issueComment.getAction())) { return; } ConcurrentMap<Integer, GhprbPullRequest> pulls = helper.getTrigger().getPulls(); GhprbPullRequest pull = pulls.get(id); if (pull == null) { pull = new GhprbPullRequest(getGitHubRepo().getPullRequest(id), helper, this); pulls.put(id, pull); } pull.check(issueComment.getComment()); GhprbTrigger.getDscp().save(); }
void onPullRequestHook(PullRequest pr) throws IOException { ConcurrentMap<Integer, GhprbPullRequest> pulls = helper.getTrigger().getPulls(); if ("closed".equals(pr.getAction())) { pulls.remove(pr.getNumber()); } else if (helper.isProjectDisabled()) { logger.log(Level.FINE, "Not processing Pull request since the build is disabled"); } else if ("opened".equals(pr.getAction()) || "reopened".equals(pr.getAction())) { GhprbPullRequest pull = pulls.get(pr.getNumber()); if (pull == null) { pulls.putIfAbsent(pr.getNumber(), new GhprbPullRequest(pr.getPullRequest(), helper, this)); pull = pulls.get(pr.getNumber()); } pull.check(pr.getPullRequest()); } else if ("synchronize".equals(pr.getAction())) { GhprbPullRequest pull = pulls.get(pr.getNumber()); if (pull == null) { pulls.putIfAbsent(pr.getNumber(), new GhprbPullRequest(pr.getPullRequest(), helper, this)); pull = pulls.get(pr.getNumber()); } if (pull == null) { logger.log(Level.SEVERE, "Pull Request #{0} doesn''t exist", pr.getNumber()); return; } pull.check(pr.getPullRequest()); } else { logger.log(Level.WARNING, "Unknown Pull Request hook action: {0}", pr.getAction()); } GhprbTrigger.getDscp().save(); }
private String getHookUrl() { String baseUrl = helper.getTrigger().getGitHubApiAuth().getJenkinsUrl(); if (baseUrl == null) { baseUrl = Jenkins.getInstance().getRootUrl(); } return baseUrl + GhprbRootAction.URL + "/"; }
public void commentOnFailure( AbstractBuild<?, ?> build, TaskListener listener, GhprbCommitStatusException ex) { PrintStream stream = null; if (listener != null) { stream = listener.getLogger(); } GHCommitState state = ex.getState(); Exception baseException = ex.getException(); String newMessage; if (baseException instanceof FileNotFoundException) { newMessage = "FileNotFoundException means that the credentials Jenkins is using is probably wrong. Or the user account does not have write access to the repo."; } else { newMessage = "Could not update commit status of the Pull Request on GitHub."; } if (stream != null) { stream.println(newMessage); baseException.printStackTrace(stream); } else { logger.log(Level.INFO, newMessage, baseException); } if (GhprbTrigger.getDscp().getUseComments()) { StringBuilder msg = new StringBuilder(ex.getMessage()); if (build != null) { msg.append("\n"); GhprbTrigger trigger = Ghprb.extractTrigger(build); for (GhprbExtension ext : Ghprb.matchesAll(trigger.getExtensions(), GhprbBuildStatus.class)) { if (ext instanceof GhprbCommentAppender) { msg.append(((GhprbCommentAppender) ext).postBuildComment(build, null)); } } } if (GhprbTrigger.getDscp().getUseDetailedComments() || (state == GHCommitState.SUCCESS || state == GHCommitState.FAILURE)) { logger.log(Level.INFO, "Trying to send comment.", baseException); addComment(ex.getId(), msg.toString()); } } else { logger.log(Level.SEVERE, "Could not update commit status of the Pull Request on GitHub."); } }
public void check() { if (!initGhRepository()) { return; } if (helper.isProjectDisabled()) { logger.log(Level.FINE, "Project is disabled, not checking github state"); return; } List<GHPullRequest> openPulls; try { openPulls = ghRepository.getPullRequests(GHIssueState.OPEN); } catch (IOException ex) { logger.log(Level.SEVERE, "Could not retrieve open pull requests.", ex); return; } ConcurrentMap<Integer, GhprbPullRequest> pulls = helper.getTrigger().getPulls(); Set<Integer> closedPulls = new HashSet<Integer>(pulls.keySet()); for (GHPullRequest pr : openPulls) { if (pr.getHead() == null) { try { pr = ghRepository.getPullRequest(pr.getNumber()); } catch (IOException ex) { logger.log(Level.SEVERE, "Could not retrieve pr " + pr.getNumber(), ex); return; } } try { check(pr); } catch (IOException ex) { logger.log(Level.SEVERE, "Could not retrieve pr " + pr.getNumber(), ex); return; } closedPulls.remove(pr.getNumber()); } // remove closed pulls so we don't check them again for (Integer id : closedPulls) { pulls.remove(id); } }
private void check(GHPullRequest pr) throws IOException { ConcurrentMap<Integer, GhprbPullRequest> pulls = helper.getTrigger().getPulls(); final Integer id = pr.getNumber(); GhprbPullRequest pull; if (pulls.containsKey(id)) { pull = pulls.get(id); } else { pulls.putIfAbsent(id, new GhprbPullRequest(pr, helper, this)); pull = pulls.get(id); } pull.check(pr); }
public void init() { // make the initial check call to populate our data structures if (!initGhRepository()) { // We could have hit the rate limit while initializing. If we // continue, then we will loop back around and attempt to re-init. return; } for (Entry<Integer, GhprbPullRequest> next : helper.getTrigger().getPulls().entrySet()) { GhprbPullRequest pull = next.getValue(); try { pull.init(helper, this); } catch (IOException e) { logger.log( Level.SEVERE, "Unable to initialize pull request #{0} for repo {1}, job {2}", new Object[] { next.getKey(), reponame, helper.getTrigger().getActualProject().getFullName() }); e.printStackTrace(); } } }
private boolean initGhRepository() { GitHub gitHub = null; try { GhprbGitHub repo = helper.getGitHub(); if (repo == null) { return false; } gitHub = repo.get(); if (gitHub == null) { logger.log(Level.SEVERE, "No connection returned to GitHub server!"); return false; } if (gitHub.getRateLimit().remaining == 0) { return false; } } catch (FileNotFoundException ex) { logger.log(Level.INFO, "Rate limit API not found."); } catch (IOException ex) { logger.log(Level.SEVERE, "Error while accessing rate limit API", ex); return false; } if (ghRepository == null) { try { ghRepository = gitHub.getRepository(reponame); } catch (IOException ex) { logger.log( Level.SEVERE, "Could not retrieve GitHub repository named " + reponame + " (Do you have properly set 'GitHub project' field in job configuration?)", ex); return false; } } return true; }
private String getSecret() { return helper.getTrigger().getGitHubApiAuth().getSecret(); }