Files
github-api/jacoco/org.kohsuke.github/GHPullRequest.java.html
2021-06-02 11:09:28 -07:00

652 lines
25 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>GHPullRequest.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">GitHub API for Java</a> &gt; <a href="index.source.html" class="el_package">org.kohsuke.github</a> &gt; <span class="el_source">GHPullRequest.java</span></div><h1>GHPullRequest.java</h1><pre class="source lang-java linenums">/*
* The MIT License
*
* Copyright (c) 2010, Kohsuke Kawaguchi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the &quot;Software&quot;), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.kohsuke.github;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import javax.annotation.CheckForNull;
import static org.kohsuke.github.internal.Previews.LYDIAN;
import static org.kohsuke.github.internal.Previews.SHADOW_CAT;
/**
* A pull request.
*
* @author Kohsuke Kawaguchi
* @see GHRepository#getPullRequest(int) GHRepository#getPullRequest(int)
*/
@SuppressWarnings({ &quot;UnusedDeclaration&quot; })
<span class="fc" id="L49">public class GHPullRequest extends GHIssue implements Refreshable {</span>
private static final String COMMENTS_ACTION = &quot;/comments&quot;;
private static final String REQUEST_REVIEWERS = &quot;/requested_reviewers&quot;;
private String patch_url, diff_url, issue_url;
private GHCommitPointer base;
private String merged_at;
private GHCommitPointer head;
// details that are only available when obtained from ID
private GHUser merged_by;
private int review_comments, additions, commits;
private boolean merged, maintainer_can_modify;
// making these package private to all for testing
boolean draft;
private Boolean mergeable;
private int deletions;
private String mergeable_state;
private int changed_files;
private String merge_commit_sha;
// pull request reviewers
private GHUser[] requested_reviewers;
private GHTeam[] requested_teams;
GHPullRequest wrapUp(GHRepository owner) {
<span class="fc" id="L76"> this.wrap(owner);</span>
<span class="fc" id="L77"> return wrapUp(owner.root);</span>
}
GHPullRequest wrapUp(GitHub root) {
<span class="fc bfc" id="L81" title="All 2 branches covered."> if (owner != null)</span>
<span class="fc" id="L82"> owner.wrap(root);</span>
<span class="pc bpc" id="L83" title="1 of 2 branches missed."> if (base != null)</span>
<span class="fc" id="L84"> base.wrapUp(root);</span>
<span class="pc bpc" id="L85" title="1 of 2 branches missed."> if (head != null)</span>
<span class="fc" id="L86"> head.wrapUp(root);</span>
<span class="fc bfc" id="L87" title="All 2 branches covered."> if (merged_by != null)</span>
<span class="fc" id="L88"> merged_by.wrapUp(root);</span>
<span class="fc bfc" id="L89" title="All 2 branches covered."> if (requested_reviewers != null)</span>
<span class="fc" id="L90"> GHUser.wrap(requested_reviewers, root);</span>
<span class="fc bfc" id="L91" title="All 2 branches covered."> if (requested_teams != null)</span>
<span class="fc" id="L92"> GHTeam.wrapUp(requested_teams, this);</span>
<span class="fc" id="L93"> return this;</span>
}
@Override
protected String getApiRoute() {
<span class="pc bpc" id="L98" title="1 of 2 branches missed."> if (owner == null) {</span>
// Issues returned from search to do not have an owner. Attempt to use url.
<span class="nc" id="L100"> final URL url = Objects.requireNonNull(getUrl(), &quot;Missing instance URL!&quot;);</span>
<span class="nc" id="L101"> return StringUtils.prependIfMissing(url.toString().replace(root.getApiUrl(), &quot;&quot;), &quot;/&quot;);</span>
}
<span class="fc" id="L104"> return &quot;/repos/&quot; + owner.getOwnerName() + &quot;/&quot; + owner.getName() + &quot;/pulls/&quot; + number;</span>
}
/**
* The URL of the patch file. like https://github.com/jenkinsci/jenkins/pull/100.patch
*
* @return the patch url
*/
public URL getPatchUrl() {
<span class="nc" id="L113"> return GitHubClient.parseURL(patch_url);</span>
}
/**
* The URL of the patch file. like https://github.com/jenkinsci/jenkins/pull/100.patch
*
* @return the issue url
*/
public URL getIssueUrl() {
<span class="nc" id="L122"> return GitHubClient.parseURL(issue_url);</span>
}
/**
* This points to where the change should be pulled into, but I'm not really sure what exactly it means.
*
* @return the base
*/
public GHCommitPointer getBase() {
<span class="fc" id="L131"> return base;</span>
}
/**
* The change that should be pulled. The tip of the commits to merge.
*
* @return the head
*/
public GHCommitPointer getHead() {
<span class="fc" id="L140"> return head;</span>
}
/**
* Gets issue updated at.
*
* @return the issue updated at
* @throws IOException
* the io exception
*/
@Deprecated
public Date getIssueUpdatedAt() throws IOException {
<span class="nc" id="L152"> return super.getUpdatedAt();</span>
}
/**
* The diff file, like https://github.com/jenkinsci/jenkins/pull/100.diff
*
* @return the diff url
*/
public URL getDiffUrl() {
<span class="nc" id="L161"> return GitHubClient.parseURL(diff_url);</span>
}
/**
* Gets merged at.
*
* @return the merged at
*/
public Date getMergedAt() {
<span class="nc" id="L170"> return GitHubClient.parseDate(merged_at);</span>
}
@Override
public GHUser getClosedBy() {
<span class="nc" id="L175"> return null;</span>
}
@Override
public PullRequest getPullRequest() {
<span class="nc" id="L180"> return null;</span>
}
//
// details that are only available via get with ID
//
/**
* Gets merged by.
*
* @return the merged by
* @throws IOException
* the io exception
*/
public GHUser getMergedBy() throws IOException {
<span class="fc" id="L195"> populate();</span>
<span class="fc" id="L196"> return merged_by;</span>
}
/**
* Gets review comments.
*
* @return the review comments
* @throws IOException
* the io exception
*/
public int getReviewComments() throws IOException {
<span class="fc" id="L207"> populate();</span>
<span class="fc" id="L208"> return review_comments;</span>
}
/**
* Gets additions.
*
* @return the additions
* @throws IOException
* the io exception
*/
public int getAdditions() throws IOException {
<span class="fc" id="L219"> populate();</span>
<span class="fc" id="L220"> return additions;</span>
}
/**
* Gets commits.
*
* @return the commits
* @throws IOException
* the io exception
*/
public int getCommits() throws IOException {
<span class="nc" id="L231"> populate();</span>
<span class="nc" id="L232"> return commits;</span>
}
/**
* Is merged boolean.
*
* @return the boolean
* @throws IOException
* the io exception
*/
public boolean isMerged() throws IOException {
<span class="fc" id="L243"> populate();</span>
<span class="fc" id="L244"> return merged;</span>
}
/**
* Can maintainer modify boolean.
*
* @return the boolean
* @throws IOException
* the io exception
*/
public boolean canMaintainerModify() throws IOException {
<span class="fc" id="L255"> populate();</span>
<span class="fc" id="L256"> return maintainer_can_modify;</span>
}
/**
* Is draft boolean.
*
* @return the boolean
* @throws IOException
* the io exception
*/
public boolean isDraft() throws IOException {
<span class="fc" id="L267"> populate();</span>
<span class="fc" id="L268"> return draft;</span>
}
/**
* Is this PR mergeable?
*
* @return null if the state has not been determined yet, for example when a PR is newly created. If this method is
* called on an instance whose mergeable state is not yet known, API call is made to retrieve the latest
* state.
* @throws IOException
* the io exception
*/
public Boolean getMergeable() throws IOException {
<span class="fc" id="L281"> refresh(mergeable);</span>
<span class="fc" id="L282"> return mergeable;</span>
}
/**
* for test purposes only
*/
@Deprecated
Boolean getMergeableNoRefresh() throws IOException {
<span class="fc" id="L290"> return mergeable;</span>
}
/**
* Gets deletions.
*
* @return the deletions
* @throws IOException
* the io exception
*/
public int getDeletions() throws IOException {
<span class="fc" id="L301"> populate();</span>
<span class="fc" id="L302"> return deletions;</span>
}
/**
* Gets mergeable state.
*
* @return the mergeable state
* @throws IOException
* the io exception
*/
public String getMergeableState() throws IOException {
<span class="fc" id="L313"> populate();</span>
<span class="fc" id="L314"> return mergeable_state;</span>
}
/**
* Gets changed files.
*
* @return the changed files
* @throws IOException
* the io exception
*/
public int getChangedFiles() throws IOException {
<span class="fc" id="L325"> populate();</span>
<span class="fc" id="L326"> return changed_files;</span>
}
/**
* See &lt;a href=&quot;https://developer.github.com/changes/2013-04-25-deprecating-merge-commit-sha&quot;&gt;GitHub blog post&lt;/a&gt;
*
* @return the merge commit sha
* @throws IOException
* the io exception
*/
public String getMergeCommitSha() throws IOException {
<span class="fc" id="L337"> populate();</span>
<span class="fc" id="L338"> return merge_commit_sha;</span>
}
/**
* Gets requested reviewers.
*
* @return the requested reviewers
* @throws IOException
* the io exception
*/
public List&lt;GHUser&gt; getRequestedReviewers() throws IOException {
<span class="fc" id="L349"> refresh(requested_reviewers);</span>
<span class="fc" id="L350"> return Collections.unmodifiableList(Arrays.asList(requested_reviewers));</span>
}
/**
* Gets requested teams.
*
* @return the requested teams
* @throws IOException
* the io exception
*/
public List&lt;GHTeam&gt; getRequestedTeams() throws IOException {
<span class="fc" id="L361"> refresh(requested_teams);</span>
<span class="fc" id="L362"> return Collections.unmodifiableList(Arrays.asList(requested_teams));</span>
}
/**
* Fully populate the data by retrieving missing data.
*
* Depending on the original API call where this object is created, it may not contain everything.
*/
private void populate() throws IOException {
<span class="fc bfc" id="L371" title="All 2 branches covered."> if (mergeable_state != null)</span>
<span class="fc" id="L372"> return; // already populated</span>
<span class="fc" id="L373"> refresh();</span>
<span class="fc" id="L374"> }</span>
/**
* Repopulates this object.
*/
public void refresh() throws IOException {
<span class="pc bpc" id="L380" title="1 of 4 branches missed."> if (root == null || root.isOffline()) {</span>
<span class="fc" id="L381"> return; // cannot populate, will have to live with what we have</span>
}
<span class="fc" id="L384"> URL url = getUrl();</span>
<span class="pc bpc" id="L385" title="1 of 2 branches missed."> if (url != null) {</span>
<span class="fc" id="L386"> root.createRequest().withPreview(SHADOW_CAT).setRawUrlPath(url.toString()).fetchInto(this).wrapUp(owner);</span>
}
<span class="fc" id="L388"> }</span>
/**
* Retrieves all the files associated to this pull request.
*
* @return the paged iterable
*/
public PagedIterable&lt;GHPullRequestFileDetail&gt; listFiles() {
<span class="nc" id="L396"> return root.createRequest()</span>
<span class="nc" id="L397"> .withUrlPath(String.format(&quot;%s/files&quot;, getApiRoute()))</span>
<span class="nc" id="L398"> .toIterable(GHPullRequestFileDetail[].class, null);</span>
}
/**
* Retrieves all the reviews associated to this pull request.
*
* @return the paged iterable
*/
public PagedIterable&lt;GHPullRequestReview&gt; listReviews() {
<span class="fc" id="L407"> return root.createRequest()</span>
<span class="fc" id="L408"> .withUrlPath(String.format(&quot;%s/reviews&quot;, getApiRoute()))</span>
<span class="fc" id="L409"> .toIterable(GHPullRequestReview[].class, item -&gt; item.wrapUp(this));</span>
}
/**
* Obtains all the review comments associated with this pull request.
*
* @return the paged iterable
* @throws IOException
* the io exception
*/
public PagedIterable&lt;GHPullRequestReviewComment&gt; listReviewComments() throws IOException {
<span class="fc" id="L420"> return root.createRequest()</span>
<span class="fc" id="L421"> .withUrlPath(getApiRoute() + COMMENTS_ACTION)</span>
<span class="fc" id="L422"> .toIterable(GHPullRequestReviewComment[].class, item -&gt; item.wrapUp(this));</span>
}
/**
* Retrieves all the commits associated to this pull request.
*
* @return the paged iterable
*/
public PagedIterable&lt;GHPullRequestCommitDetail&gt; listCommits() {
<span class="nc" id="L431"> return root.createRequest()</span>
<span class="nc" id="L432"> .withUrlPath(String.format(&quot;%s/commits&quot;, getApiRoute()))</span>
<span class="nc" id="L433"> .toIterable(GHPullRequestCommitDetail[].class, item -&gt; item.wrapUp(this));</span>
}
/**
* Create review gh pull request review.
*
* @param body
* the body
* @param event
* the event
* @param comments
* the comments
* @return the gh pull request review
* @throws IOException
* the io exception
* @deprecated Use {@link #createReview()}
*/
@Deprecated
public GHPullRequestReview createReview(String body,
@CheckForNull GHPullRequestReviewState event,
GHPullRequestReviewComment... comments) throws IOException {
<span class="nc" id="L454"> return createReview(body, event, Arrays.asList(comments));</span>
}
/**
* Create review gh pull request review.
*
* @param body
* the body
* @param event
* the event
* @param comments
* the comments
* @return the gh pull request review
* @throws IOException
* the io exception
* @deprecated Use {@link #createReview()}
*/
@Deprecated
public GHPullRequestReview createReview(String body,
@CheckForNull GHPullRequestReviewState event,
List&lt;GHPullRequestReviewComment&gt; comments) throws IOException {
<span class="nc" id="L475"> GHPullRequestReviewBuilder b = createReview().body(body);</span>
<span class="nc bnc" id="L476" title="All 2 branches missed."> for (GHPullRequestReviewComment c : comments) {</span>
<span class="nc" id="L477"> b.comment(c.getBody(), c.getPath(), c.getPosition());</span>
<span class="nc" id="L478"> }</span>
<span class="nc" id="L479"> return b.create();</span>
}
/**
* Create review gh pull request review builder.
*
* @return the gh pull request review builder
*/
public GHPullRequestReviewBuilder createReview() {
<span class="fc" id="L488"> return new GHPullRequestReviewBuilder(this);</span>
}
/**
* Create review comment gh pull request review comment.
*
* @param body
* the body
* @param sha
* the sha
* @param path
* the path
* @param position
* the position
* @return the gh pull request review comment
* @throws IOException
* the io exception
*/
public GHPullRequestReviewComment createReviewComment(String body, String sha, String path, int position)
throws IOException {
<span class="fc" id="L508"> return root.createRequest()</span>
<span class="fc" id="L509"> .method(&quot;POST&quot;)</span>
<span class="fc" id="L510"> .with(&quot;body&quot;, body)</span>
<span class="fc" id="L511"> .with(&quot;commit_id&quot;, sha)</span>
<span class="fc" id="L512"> .with(&quot;path&quot;, path)</span>
<span class="fc" id="L513"> .with(&quot;position&quot;, position)</span>
<span class="fc" id="L514"> .withUrlPath(getApiRoute() + COMMENTS_ACTION)</span>
<span class="fc" id="L515"> .fetch(GHPullRequestReviewComment.class)</span>
<span class="fc" id="L516"> .wrapUp(this);</span>
}
/**
* Request reviewers.
*
* @param reviewers
* the reviewers
* @throws IOException
* the io exception
*/
public void requestReviewers(List&lt;GHUser&gt; reviewers) throws IOException {
<span class="fc" id="L528"> root.createRequest()</span>
<span class="fc" id="L529"> .method(&quot;POST&quot;)</span>
<span class="fc" id="L530"> .with(&quot;reviewers&quot;, getLogins(reviewers))</span>
<span class="fc" id="L531"> .withUrlPath(getApiRoute() + REQUEST_REVIEWERS)</span>
<span class="fc" id="L532"> .send();</span>
<span class="fc" id="L533"> }</span>
/**
* Request team reviewers.
*
* @param teams
* the teams
* @throws IOException
* the io exception
*/
public void requestTeamReviewers(List&lt;GHTeam&gt; teams) throws IOException {
<span class="fc" id="L544"> List&lt;String&gt; teamReviewers = new ArrayList&lt;String&gt;(teams.size());</span>
<span class="fc bfc" id="L545" title="All 2 branches covered."> for (GHTeam team : teams) {</span>
<span class="fc" id="L546"> teamReviewers.add(team.getSlug());</span>
<span class="fc" id="L547"> }</span>
<span class="fc" id="L548"> root.createRequest()</span>
<span class="fc" id="L549"> .method(&quot;POST&quot;)</span>
<span class="fc" id="L550"> .with(&quot;team_reviewers&quot;, teamReviewers)</span>
<span class="fc" id="L551"> .withUrlPath(getApiRoute() + REQUEST_REVIEWERS)</span>
<span class="fc" id="L552"> .send();</span>
<span class="fc" id="L553"> }</span>
/**
* Set the base branch on the pull request
*
* @param newBaseBranch
* the name of the new base branch
* @throws IOException
* the io exception
* @return the updated pull request
*/
public GHPullRequest setBaseBranch(String newBaseBranch) throws IOException {
<span class="fc" id="L565"> return root.createRequest()</span>
<span class="fc" id="L566"> .method(&quot;PATCH&quot;)</span>
<span class="fc" id="L567"> .with(&quot;base&quot;, newBaseBranch)</span>
<span class="fc" id="L568"> .withUrlPath(getApiRoute())</span>
<span class="fc" id="L569"> .fetch(GHPullRequest.class)</span>
<span class="fc" id="L570"> .wrapUp(root);</span>
}
/**
* Updates the branch. The same as pressing the button in the web GUI.
*
* @throws IOException
* the io exception
*/
@Preview(LYDIAN)
@Deprecated
public void updateBranch() throws IOException {
<span class="fc" id="L582"> root.createRequest()</span>
<span class="fc" id="L583"> .withPreview(LYDIAN)</span>
<span class="fc" id="L584"> .method(&quot;PUT&quot;)</span>
<span class="fc" id="L585"> .with(&quot;expected_head_sha&quot;, head.getSha())</span>
<span class="fc" id="L586"> .withUrlPath(getApiRoute() + &quot;/update-branch&quot;)</span>
<span class="fc" id="L587"> .send();</span>
<span class="fc" id="L588"> }</span>
/**
* Merge this pull request.
* &lt;p&gt;
* The equivalent of the big green &quot;Merge pull request&quot; button.
*
* @param msg
* Commit message. If null, the default one will be used.
* @throws IOException
* the io exception
*/
public void merge(String msg) throws IOException {
<span class="nc" id="L601"> merge(msg, null);</span>
<span class="nc" id="L602"> }</span>
/**
* Merge this pull request.
* &lt;p&gt;
* The equivalent of the big green &quot;Merge pull request&quot; button.
*
* @param msg
* Commit message. If null, the default one will be used.
* @param sha
* SHA that pull request head must match to allow merge.
* @throws IOException
* the io exception
*/
public void merge(String msg, String sha) throws IOException {
<span class="nc" id="L617"> merge(msg, sha, null);</span>
<span class="nc" id="L618"> }</span>
/**
* Merge this pull request, using the specified merge method.
* &lt;p&gt;
* The equivalent of the big green &quot;Merge pull request&quot; button.
*
* @param msg
* Commit message. If null, the default one will be used.
* @param sha
* the sha
* @param method
* SHA that pull request head must match to allow merge.
* @throws IOException
* the io exception
*/
public void merge(String msg, String sha, MergeMethod method) throws IOException {
<span class="fc" id="L635"> root.createRequest()</span>
<span class="fc" id="L636"> .method(&quot;PUT&quot;)</span>
<span class="fc" id="L637"> .with(&quot;commit_message&quot;, msg)</span>
<span class="fc" id="L638"> .with(&quot;sha&quot;, sha)</span>
<span class="fc" id="L639"> .with(&quot;merge_method&quot;, method)</span>
<span class="fc" id="L640"> .withUrlPath(getApiRoute() + &quot;/merge&quot;)</span>
<span class="fc" id="L641"> .send();</span>
<span class="fc" id="L642"> }</span>
/**
* The enum MergeMethod.
*/
<span class="fc" id="L647"> public enum MergeMethod {</span>
<span class="fc" id="L648"> MERGE, SQUASH, REBASE</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>