mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-15 08:21:23 +00:00
590 lines
20 KiB
HTML
590 lines
20 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>GHCommit.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> > <a href="index.source.html" class="el_package">org.kohsuke.github</a> > <span class="el_source">GHCommit.java</span></div><h1>GHCommit.java</h1><pre class="source lang-java linenums">package org.kohsuke.github;
|
|
|
|
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
|
|
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
|
|
|
import java.io.IOException;
|
|
import java.net.URL;
|
|
import java.util.AbstractList;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
import static org.kohsuke.github.internal.Previews.ANTIOPE;
|
|
import static org.kohsuke.github.internal.Previews.GROOT;
|
|
|
|
/**
|
|
* A commit in a repository.
|
|
*
|
|
* @author Kohsuke Kawaguchi
|
|
* @see GHRepository#getCommit(String) GHRepository#getCommit(String)
|
|
* @see GHCommitComment#getCommit() GHCommitComment#getCommit()
|
|
*/
|
|
@SuppressFBWarnings(value = { "NP_UNWRITTEN_FIELD", "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
|
|
<span class="fc" id="L25">public class GHCommit {</span>
|
|
private GHRepository owner;
|
|
|
|
private ShortInfo commit;
|
|
|
|
/**
|
|
* Short summary of this commit.
|
|
*/
|
|
@SuppressFBWarnings(
|
|
value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD",
|
|
"UWF_UNWRITTEN_FIELD" },
|
|
justification = "JSON API")
|
|
<span class="fc" id="L37"> public static class ShortInfo {</span>
|
|
private GHAuthor author;
|
|
private GHAuthor committer;
|
|
|
|
private String message;
|
|
|
|
private int comment_count;
|
|
|
|
private GHVerification verification;
|
|
|
|
<span class="fc" id="L47"> static class Tree {</span>
|
|
String sha;
|
|
}
|
|
|
|
private Tree tree;
|
|
|
|
/**
|
|
* Gets author.
|
|
*
|
|
* @return the author
|
|
*/
|
|
@WithBridgeMethods(value = GHAuthor.class, castRequired = true)
|
|
public GitUser getAuthor() {
|
|
<span class="fc" id="L60"> return author;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets authored date.
|
|
*
|
|
* @return the authored date
|
|
*/
|
|
public Date getAuthoredDate() {
|
|
<span class="fc" id="L69"> return author.getDate();</span>
|
|
}
|
|
|
|
/**
|
|
* Gets committer.
|
|
*
|
|
* @return the committer
|
|
*/
|
|
@WithBridgeMethods(value = GHAuthor.class, castRequired = true)
|
|
public GitUser getCommitter() {
|
|
<span class="fc" id="L79"> return committer;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets commit date.
|
|
*
|
|
* @return the commit date
|
|
*/
|
|
public Date getCommitDate() {
|
|
<span class="fc" id="L88"> return committer.getDate();</span>
|
|
}
|
|
|
|
/**
|
|
* Gets message.
|
|
*
|
|
* @return Commit message.
|
|
*/
|
|
public String getMessage() {
|
|
<span class="fc" id="L97"> return message;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets comment count.
|
|
*
|
|
* @return the comment count
|
|
*/
|
|
public int getCommentCount() {
|
|
<span class="nc" id="L106"> return comment_count;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets Verification Status.
|
|
*
|
|
* @return the Verification status
|
|
*/
|
|
public GHVerification getVerification() {
|
|
<span class="fc" id="L115"> return verification;</span>
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The type GHAuthor.
|
|
*
|
|
* @deprecated Use {@link GitUser} instead.
|
|
*/
|
|
<span class="fc" id="L124"> public static class GHAuthor extends GitUser {</span>
|
|
}
|
|
|
|
/**
|
|
* The type Stats.
|
|
*/
|
|
<span class="fc" id="L130"> public static class Stats {</span>
|
|
int total, additions, deletions;
|
|
}
|
|
|
|
/**
|
|
* A file that was modified.
|
|
*/
|
|
@SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", justification = "It's being initilized by JSON deserialization")
|
|
<span class="fc" id="L138"> public static class File {</span>
|
|
String status;
|
|
int changes, additions, deletions;
|
|
String raw_url, blob_url, sha, patch;
|
|
String filename, previous_filename;
|
|
|
|
/**
|
|
* Gets lines changed.
|
|
*
|
|
* @return Number of lines added + removed.
|
|
*/
|
|
public int getLinesChanged() {
|
|
<span class="fc" id="L150"> return changes;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets lines added.
|
|
*
|
|
* @return Number of lines added.
|
|
*/
|
|
public int getLinesAdded() {
|
|
<span class="fc" id="L159"> return additions;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets lines deleted.
|
|
*
|
|
* @return Number of lines removed.
|
|
*/
|
|
public int getLinesDeleted() {
|
|
<span class="fc" id="L168"> return deletions;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets status.
|
|
*
|
|
* @return "modified", "added", or "removed"
|
|
*/
|
|
public String getStatus() {
|
|
<span class="fc" id="L177"> return status;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets file name.
|
|
*
|
|
* @return Full path in the repository.
|
|
*/
|
|
@SuppressFBWarnings(value = "NM_CONFUSING",
|
|
justification = "It's a part of the library's API and cannot be renamed")
|
|
public String getFileName() {
|
|
<span class="fc" id="L188"> return filename;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets previous filename.
|
|
*
|
|
* @return Previous path, in case file has moved.
|
|
*/
|
|
public String getPreviousFilename() {
|
|
<span class="fc" id="L197"> return previous_filename;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets patch.
|
|
*
|
|
* @return The actual change.
|
|
*/
|
|
public String getPatch() {
|
|
<span class="fc" id="L206"> return patch;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets raw url.
|
|
*
|
|
* @return URL like
|
|
* 'https://raw.github.com/jenkinsci/jenkins/4eb17c197dfdcf8ef7ff87eb160f24f6a20b7f0e/core/pom.xml' that
|
|
* resolves to the actual content of the file.
|
|
*/
|
|
public URL getRawUrl() {
|
|
<span class="fc" id="L217"> return GitHubClient.parseURL(raw_url);</span>
|
|
}
|
|
|
|
/**
|
|
* Gets blob url.
|
|
*
|
|
* @return URL like
|
|
* 'https://github.com/jenkinsci/jenkins/blob/1182e2ebb1734d0653142bd422ad33c21437f7cf/core/pom.xml'
|
|
* that resolves to the HTML page that describes this file.
|
|
*/
|
|
public URL getBlobUrl() {
|
|
<span class="fc" id="L228"> return GitHubClient.parseURL(blob_url);</span>
|
|
}
|
|
|
|
/**
|
|
* Gets sha.
|
|
*
|
|
* @return [0 -9a-f]{40} SHA1 checksum.
|
|
*/
|
|
public String getSha() {
|
|
<span class="fc" id="L237"> return sha;</span>
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The type Parent.
|
|
*/
|
|
<span class="fc" id="L244"> public static class Parent {</span>
|
|
@SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "We don't provide it in API now")
|
|
String url;
|
|
String sha;
|
|
}
|
|
|
|
<span class="fc" id="L250"> static class User {</span>
|
|
// TODO: what if someone who doesn't have an account on GitHub makes a commit?
|
|
@SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "We don't provide it in API now")
|
|
String url, avatar_url, gravatar_id;
|
|
@SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "We don't provide it in API now")
|
|
int id;
|
|
|
|
String login;
|
|
}
|
|
|
|
String url, html_url, sha;
|
|
List<File> files;
|
|
Stats stats;
|
|
List<Parent> parents;
|
|
User author, committer;
|
|
|
|
/**
|
|
* Gets commit short info.
|
|
*
|
|
* @return the commit short info
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
public ShortInfo getCommitShortInfo() throws IOException {
|
|
<span class="fc bfc" id="L274" title="All 2 branches covered."> if (commit == null)</span>
|
|
<span class="fc" id="L275"> populate();</span>
|
|
<span class="fc" id="L276"> return commit;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets owner.
|
|
*
|
|
* @return the repository that contains the commit.
|
|
*/
|
|
public GHRepository getOwner() {
|
|
<span class="nc" id="L285"> return owner;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets lines changed.
|
|
*
|
|
* @return the number of lines added + removed.
|
|
* @throws IOException
|
|
* if the field was not populated and refresh fails
|
|
*/
|
|
public int getLinesChanged() throws IOException {
|
|
<span class="nc" id="L296"> populate();</span>
|
|
<span class="nc" id="L297"> return stats.total;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets lines added.
|
|
*
|
|
* @return Number of lines added.
|
|
* @throws IOException
|
|
* if the field was not populated and refresh fails
|
|
*/
|
|
public int getLinesAdded() throws IOException {
|
|
<span class="nc" id="L308"> populate();</span>
|
|
<span class="nc" id="L309"> return stats.additions;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets lines deleted.
|
|
*
|
|
* @return Number of lines removed.
|
|
* @throws IOException
|
|
* if the field was not populated and refresh fails
|
|
*/
|
|
public int getLinesDeleted() throws IOException {
|
|
<span class="nc" id="L320"> populate();</span>
|
|
<span class="nc" id="L321"> return stats.deletions;</span>
|
|
}
|
|
|
|
/**
|
|
* Use this method to walk the tree.
|
|
*
|
|
* @return a GHTree to walk
|
|
* @throws IOException
|
|
* on error
|
|
*/
|
|
public GHTree getTree() throws IOException {
|
|
<span class="fc" id="L332"> return owner.getTree(getCommitShortInfo().tree.sha);</span>
|
|
}
|
|
|
|
/**
|
|
* Gets html url.
|
|
*
|
|
* @return URL of this commit like
|
|
* "https://github.com/kohsuke/sandbox-ant/commit/8ae38db0ea5837313ab5f39d43a6f73de3bd9000"
|
|
*/
|
|
public URL getHtmlUrl() {
|
|
<span class="fc" id="L342"> return GitHubClient.parseURL(html_url);</span>
|
|
}
|
|
|
|
/**
|
|
* Gets sha 1.
|
|
*
|
|
* @return [0 -9a-f]{40} SHA1 checksum.
|
|
*/
|
|
public String getSHA1() {
|
|
<span class="fc" id="L351"> return sha;</span>
|
|
}
|
|
|
|
/**
|
|
* List of files changed/added/removed in this commit.
|
|
*
|
|
* @return Can be empty but never null.
|
|
* @throws IOException
|
|
* on error
|
|
*/
|
|
public List<File> getFiles() throws IOException {
|
|
<span class="fc" id="L362"> populate();</span>
|
|
<span class="pc bpc" id="L363" title="1 of 2 branches missed."> return files != null ? Collections.unmodifiableList(files) : Collections.<File>emptyList();</span>
|
|
}
|
|
|
|
/**
|
|
* Gets parent sha 1 s.
|
|
*
|
|
* @return SHA1 of parent commit objects.
|
|
*/
|
|
public List<String> getParentSHA1s() {
|
|
<span class="pc bpc" id="L372" title="1 of 2 branches missed."> if (parents == null)</span>
|
|
<span class="nc" id="L373"> return Collections.emptyList();</span>
|
|
<span class="fc" id="L374"> return new AbstractList<String>() {</span>
|
|
@Override
|
|
public String get(int index) {
|
|
<span class="fc" id="L377"> return parents.get(index).sha;</span>
|
|
}
|
|
|
|
@Override
|
|
public int size() {
|
|
<span class="fc" id="L382"> return parents.size();</span>
|
|
}
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Resolves the parent commit objects and return them.
|
|
*
|
|
* @return parent commit objects
|
|
* @throws IOException
|
|
* on error
|
|
*/
|
|
public List<GHCommit> getParents() throws IOException {
|
|
<span class="fc" id="L395"> List<GHCommit> r = new ArrayList<GHCommit>();</span>
|
|
<span class="fc bfc" id="L396" title="All 2 branches covered."> for (String sha1 : getParentSHA1s())</span>
|
|
<span class="fc" id="L397"> r.add(owner.getCommit(sha1));</span>
|
|
<span class="fc" id="L398"> return r;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets author.
|
|
*
|
|
* @return the author
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
public GHUser getAuthor() throws IOException {
|
|
<span class="nc" id="L409"> return resolveUser(author);</span>
|
|
}
|
|
|
|
/**
|
|
* Gets the date the change was authored on.
|
|
*
|
|
* @return the date the change was authored on.
|
|
* @throws IOException
|
|
* if the information was not already fetched and an attempt at fetching the information failed.
|
|
*/
|
|
public Date getAuthoredDate() throws IOException {
|
|
<span class="nc" id="L420"> return getCommitShortInfo().getAuthoredDate();</span>
|
|
}
|
|
|
|
/**
|
|
* Gets committer.
|
|
*
|
|
* @return the committer
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
public GHUser getCommitter() throws IOException {
|
|
<span class="nc" id="L431"> return resolveUser(committer);</span>
|
|
}
|
|
|
|
/**
|
|
* Gets the date the change was committed on.
|
|
*
|
|
* @return the date the change was committed on.
|
|
* @throws IOException
|
|
* if the information was not already fetched and an attempt at fetching the information failed.
|
|
*/
|
|
public Date getCommitDate() throws IOException {
|
|
<span class="nc" id="L442"> return getCommitShortInfo().getCommitDate();</span>
|
|
}
|
|
|
|
private GHUser resolveUser(User author) throws IOException {
|
|
<span class="nc bnc" id="L446" title="All 4 branches missed."> if (author == null || author.login == null)</span>
|
|
<span class="nc" id="L447"> return null;</span>
|
|
<span class="nc" id="L448"> return owner.root.getUser(author.login);</span>
|
|
}
|
|
|
|
/**
|
|
* Retrieves a list of pull requests which contain this commit.
|
|
*
|
|
* @return {@link PagedIterable} with the pull requests which contain this commit
|
|
*/
|
|
@Preview(GROOT)
|
|
@Deprecated
|
|
public PagedIterable<GHPullRequest> listPullRequests() {
|
|
<span class="fc" id="L459"> return owner.root.createRequest()</span>
|
|
<span class="fc" id="L460"> .withPreview(GROOT)</span>
|
|
<span class="fc" id="L461"> .withUrlPath(String.format("/repos/%s/%s/commits/%s/pulls", owner.getOwnerName(), owner.getName(), sha))</span>
|
|
<span class="fc" id="L462"> .toIterable(GHPullRequest[].class, item -> item.wrapUp(owner));</span>
|
|
}
|
|
|
|
/**
|
|
* Retrieves a list of branches where this commit is the head commit.
|
|
*
|
|
* @return {@link PagedIterable} with the branches where the commit is the head commit
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
@Preview(GROOT)
|
|
@Deprecated
|
|
public PagedIterable<GHBranch> listBranchesWhereHead() throws IOException {
|
|
<span class="fc" id="L475"> return owner.root.createRequest()</span>
|
|
<span class="fc" id="L476"> .withPreview(GROOT)</span>
|
|
<span class="fc" id="L477"> .withUrlPath(String.format("/repos/%s/%s/commits/%s/branches-where-head",</span>
|
|
<span class="fc" id="L478"> owner.getOwnerName(),</span>
|
|
<span class="fc" id="L479"> owner.getName(),</span>
|
|
sha))
|
|
<span class="fc" id="L481"> .toIterable(GHBranch[].class, item -> item.wrap(owner));</span>
|
|
}
|
|
|
|
/**
|
|
* List comments paged iterable.
|
|
*
|
|
* @return {@link PagedIterable} with all the commit comments in this repository.
|
|
*/
|
|
public PagedIterable<GHCommitComment> listComments() {
|
|
<span class="nc" id="L490"> return owner.root.createRequest()</span>
|
|
<span class="nc" id="L491"> .withUrlPath(</span>
|
|
<span class="nc" id="L492"> String.format("/repos/%s/%s/commits/%s/comments", owner.getOwnerName(), owner.getName(), sha))</span>
|
|
<span class="nc" id="L493"> .toIterable(GHCommitComment[].class, item -> item.wrap(owner));</span>
|
|
}
|
|
|
|
/**
|
|
* Creates a commit comment.
|
|
* <p>
|
|
* I'm not sure how path/line/position parameters interact with each other.
|
|
*
|
|
* @param body
|
|
* body of the comment
|
|
* @param path
|
|
* path of file being commented on
|
|
* @param line
|
|
* target line for comment
|
|
* @param position
|
|
* position on line
|
|
* @return created GHCommitComment
|
|
* @throws IOException
|
|
* if comment is not created
|
|
*/
|
|
public GHCommitComment createComment(String body, String path, Integer line, Integer position) throws IOException {
|
|
<span class="fc" id="L514"> GHCommitComment r = owner.root.createRequest()</span>
|
|
<span class="fc" id="L515"> .method("POST")</span>
|
|
<span class="fc" id="L516"> .with("body", body)</span>
|
|
<span class="fc" id="L517"> .with("path", path)</span>
|
|
<span class="fc" id="L518"> .with("line", line)</span>
|
|
<span class="fc" id="L519"> .with("position", position)</span>
|
|
<span class="fc" id="L520"> .withUrlPath(</span>
|
|
<span class="fc" id="L521"> String.format("/repos/%s/%s/commits/%s/comments", owner.getOwnerName(), owner.getName(), sha))</span>
|
|
<span class="fc" id="L522"> .fetch(GHCommitComment.class);</span>
|
|
<span class="fc" id="L523"> return r.wrap(owner);</span>
|
|
}
|
|
|
|
/**
|
|
* Create comment gh commit comment.
|
|
*
|
|
* @param body
|
|
* the body
|
|
* @return the gh commit comment
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
public GHCommitComment createComment(String body) throws IOException {
|
|
<span class="fc" id="L536"> return createComment(body, null, null, null);</span>
|
|
}
|
|
|
|
/**
|
|
* List statuses paged iterable.
|
|
*
|
|
* @return status of this commit, newer ones first.
|
|
* @throws IOException
|
|
* if statuses cannot be read
|
|
*/
|
|
public PagedIterable<GHCommitStatus> listStatuses() throws IOException {
|
|
<span class="nc" id="L547"> return owner.listCommitStatuses(sha);</span>
|
|
}
|
|
|
|
/**
|
|
* Gets last status.
|
|
*
|
|
* @return the last status of this commit, which is what gets shown in the UI.
|
|
* @throws IOException
|
|
* on error
|
|
*/
|
|
public GHCommitStatus getLastStatus() throws IOException {
|
|
<span class="fc" id="L558"> return owner.getLastCommitStatus(sha);</span>
|
|
}
|
|
|
|
/**
|
|
* Gets check-runs for given sha.
|
|
*
|
|
* @return check runs for given sha.
|
|
* @throws IOException
|
|
* on error
|
|
*/
|
|
@Preview(ANTIOPE)
|
|
@Deprecated
|
|
public PagedIterable<GHCheckRun> getCheckRuns() throws IOException {
|
|
<span class="nc" id="L571"> return owner.getCheckRuns(sha);</span>
|
|
}
|
|
|
|
/**
|
|
* Some of the fields are not always filled in when this object is retrieved as a part of another API call.
|
|
*
|
|
* @throws IOException
|
|
* on error
|
|
*/
|
|
void populate() throws IOException {
|
|
<span class="pc bpc" id="L581" title="1 of 4 branches missed."> if (files == null && stats == null)</span>
|
|
<span class="fc" id="L582"> owner.root.createRequest().withUrlPath(owner.getApiTailUrl("commits/" + sha)).fetchInto(this);</span>
|
|
<span class="fc" id="L583"> }</span>
|
|
|
|
GHCommit wrapUp(GHRepository owner) {
|
|
<span class="fc" id="L586"> this.owner = owner;</span>
|
|
<span class="fc" id="L587"> return this;</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> |