@CheckForNull public URL getGithubUrl(@Nullable InputComponent inputComponent, @Nullable Integer issueLine) { if (inputComponent instanceof InputPath) { String path = getPath((InputPath) inputComponent); URL url1 = ghRepo.getHtmlUrl(); try { return new URI(url1.getProtocol(), null, url1.getHost(), url1.getPort(), url1.getFile() + "/blob/" + pr.getHead().getSha() + "/" + path, null, issueLine != null ? ("L" + issueLine) : "").toURL(); } catch (MalformedURLException | URISyntaxException e) { LOG.error("Invalid URL", e); } } return null; }
/** * Head and Ref are part of the PullRequest object * * @return the sha for the head. */ public String getSource() { try { return getPullRequest().getHead().getRef(); } catch (IOException e) { return "UNKNOWN"; } }
public static GHCommitStatus getLastCommitStatus(GHRepository repository, GHPullRequest pullRequest) throws IOException { String commitSha = pullRequest.getHead().getRef(); return repository.getLastCommitStatus(commitSha); }
public static GHCommitStatus getLastCommitStatus(GHRepository repository, GHPullRequest pullRequest) throws IOException { String commitSha = pullRequest.getHead().getRef(); return repository.getLastCommitStatus(commitSha); }
public String getPullRequestHeadBranchName(int requestNumber) throws IOException { return ghRepo.getPullRequest(requestNumber).getHead().getRef(); }
private void deleteBranch(Run<?, ?> build, Launcher launcher, final TaskListener listener) { if (!getDeleteOnMerge()) { return; } String branchName = pr.getHead().getRef(); try { GHRepository repo = pr.getRepository(); GHRef ref = repo.getRef("heads/" + branchName); ref.delete(); listener.getLogger().println("Deleted branch " + branchName); } catch (IOException e) { listener.getLogger().println("Unable to delete branch " + branchName); e.printStackTrace(listener.getLogger()); } }
public void createOrUpdateReviewComment(InputFile inputFile, Integer line, String body) { String fullpath = getPath(inputFile); Integer lineInPatch = patchPositionMappingByFile.get(fullpath).get(line); try { if (existingReviewCommentsByLocationByFile.containsKey(fullpath) && existingReviewCommentsByLocationByFile.get(fullpath).containsKey(lineInPatch)) { GHPullRequestReviewComment existingReview = existingReviewCommentsByLocationByFile.get(fullpath).get(lineInPatch); if (!existingReview.getBody().equals(body)) { existingReview.update(body); } reviewCommentToBeDeletedById.remove(existingReview.getId()); } else { pr.createReviewComment(body, pr.getHead().getSha(), fullpath, lineInPatch); } } catch (IOException e) { throw new IllegalStateException("Unable to create or update review comment in file " + fullpath + " at line " + line, e); } }
@Override public String fetchHeadSha(GHRepository remoteRepo) throws IOException { GHPullRequest pullRequest = remoteRepo.getPullRequest(prNumber); if (pullRequest == null) { throw new IOException("No PR " + prNumber + " in " + remoteRepo.getFullName()); } return pullRequest.getHead().getSha(); }
public void mergePr(GHPullRequest pullRequest, String message) throws IOException { //match merge method to enum, case insensitive GHPullRequest.MergeMethod gitMergeMethod = Arrays.stream(GHPullRequest.MergeMethod.values()) .filter(e -> e.name().equalsIgnoreCase(mergeMethod)).findAny().orElse(GHPullRequest.MergeMethod.MERGE); pullRequest.merge(message,null,gitMergeMethod); if(deleteMergedBranches){ GitHubHelpers.deleteUpdateBotBranch(pullRequest.getRepository(),pullRequest.getHead().getRef()); } }
public String getAuthorRepoGitUrl() { GHCommitPointer prHead = pr.getHead(); String authorRepoGitUrl = ""; if (prHead != null && prHead.getRepository() != null) { authorRepoGitUrl = prHead.getRepository().gitHttpTransportUrl(); } return authorRepoGitUrl; }
private boolean checkCommit(GHPullRequest pr) { GHCommitPointer head = pr.getHead(); GHCommitPointer base = pr.getBase(); String headSha = head.getSha(); String baseSha = base.getSha(); if (StringUtils.equals(headSha, this.head) && StringUtils.equals(baseSha, this.base)) { return false; } LOGGER.log(Level.FINE, "New commit. Sha: Head[{0} => {1}] Base[{2} => {3}]", new Object[] {this.head, headSha, this.base, baseSha}); setHead(headSha); setBase(baseSha); if (accepted) { shouldRun = true; } return true; }
@CheckForNull GHCommitStatus getCommitStatusForContext(GHPullRequest pr, String context) { List<GHCommitStatus> statuses; try { statuses = pr.getRepository().listCommitStatuses(pr.getHead().getSha()).asList(); } catch (IOException e) { throw new IllegalStateException("Unable to retrieve commit statuses.", e); } for (GHCommitStatus status : statuses) { if (context.equals(status.getContext())) { return status; } } return null; } }
@Override public PullRequestStatus apply(GHPullRequest input) { int prID = GHUtils.prIdFrom(input.getDiffUrl().toString()); try { GHUser user = input.getUser(); return new PullRequestStatus(prID, input.getHead().getSha(), mergedSHA, input.getHead().getLabel(), input.getBase().getLabel(), input.getHtmlUrl().toString(), user.getName(), user.getEmail(), input.getBody(), input.getTitle()); } catch (IOException e) { throw new RuntimeException(e); } } };
private GHPullRequest getPullRequestWithPullReqIdentifier(GHRepository parent) throws IOException { List<GHPullRequest> pullRequests; GHUser myself; try { pullRequests = parent.getPullRequests(GHIssueState.OPEN); myself = gitHubUtil.getMyself(); } catch (IOException e) { log.warn("Error occurred while retrieving pull requests for {}", parent.getFullName()); return null; } for (GHPullRequest pullRequest : pullRequests) { GHUser user = pullRequest.getHead().getUser(); if (myself.equals(user) && pullRequest.getBody().equals(Constants.PULL_REQ_ID)) { return pullRequest; } } return null; } }
private GHPullRequest getPullRequestWithPullReqIdentifier(GHRepository parent) throws IOException { List<GHPullRequest> pullRequests; GHUser myself; try { pullRequests = parent.getPullRequests(GHIssueState.OPEN); myself = gitHubUtil.getMyself(); } catch (IOException e) { log.warn("Error occurred while retrieving pull requests for {}", parent.getFullName()); return null; } for (GHPullRequest pullRequest : pullRequests) { GHUser user = pullRequest.getHead().getUser(); if (myself.equals(user) && pullRequest.getBody().equals(Constants.PULL_REQ_ID)) { return pullRequest; } } return null; } }
/** * {@inheritDoc} */ @Override public boolean isExcluded(@NonNull SCMSourceRequest request, @NonNull SCMHead head) { if (head instanceof BranchSCMHead && request instanceof GitHubSCMSourceRequest) { for (GHPullRequest p : ((GitHubSCMSourceRequest) request).getPullRequests()) { GHRepository headRepo = p.getHead().getRepository(); if (headRepo != null // head repo can be null if the PR is from a repo that has been deleted && p.getBase().getRepository().getFullName().equalsIgnoreCase(headRepo.getFullName()) && p.getHead().getRef().equals(head.getName())) { return true; } } } return false; } }
/** * {@inheritDoc} */ @Override public boolean isExcluded(@NonNull SCMSourceRequest request, @NonNull SCMHead head) { if (head instanceof BranchSCMHead && request instanceof GitHubSCMSourceRequest) { for (GHPullRequest p : ((GitHubSCMSourceRequest) request).getPullRequests()) { GHRepository headRepo = p.getHead().getRepository(); if (headRepo != null // head repo can be null if the PR is from a repo that has been deleted && p.getBase().getRepository().getFullName().equalsIgnoreCase(headRepo.getFullName()) && p.getHead().getRef().equals(head.getName())) { return false; } } return true; } return false; } }
/** * Resolve remote repository branch at runtime * * @param context * @return remote branch name */ protected String resolveRemoteBranch(CommandContext context) { // Let's try to find an existing pull request branch if we use single pull requests for repository if(isUseSinglePullRequest(context)) { try { GHPullRequest pullRequest = findOpenGHPullRequest(context); if (pullRequest != null ) return pullRequest.getHead().getRef(); } catch (IOException e) { throw new RuntimeException(e); } } return context.getRepository().resolveRemoteBranch(); }
PullRequestSCMHead(GHPullRequest pr, String name, boolean merge) { super(name); // the merge flag is encoded into the name, so safe to store here this.merge = merge; this.number = pr.getNumber(); this.target = new BranchSCMHead(pr.getBase().getRef()); // the source stuff is immutable for a pull request on github, so safe to store here GHRepository repository = pr.getHead().getRepository(); // may be null for deleted forks JENKINS-41246 this.sourceOwner = repository == null ? null : repository.getOwnerName(); this.sourceRepo = repository == null ? null : repository.getName(); this.sourceBranch = pr.getHead().getRef(); this.origin = pr.getRepository().getOwnerName().equalsIgnoreCase(sourceOwner) ? SCMHeadOrigin.DEFAULT : new SCMHeadOrigin.Fork(this.sourceOwner); }
public org.kohsuke.github.GHCommit getHeadCommitForBranch(final String branch) throws IOException { String sha; final GitBranch gitBranch = new GitBranch(branch); if (gitBranch.isPullRequest()) { try { sha = getGithubRepository().getPullRequest(gitBranch.pullRequestNumber()).getHead().getSha(); } catch (final IOException e) { throw new RuntimeException(e); } } else { try { final GHRef ref = getRef("heads/" + gitBranch); sha = ref.getObject().getSha(); } catch (final IOException e) { sha = gitBranch.toString(); } } return getGithubRepository().getCommit(sha); }