mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-11 00:11:25 +00:00
409 lines
15 KiB
HTML
409 lines
15 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>GHContent.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">GHContent.java</span></div><h1>GHContent.java</h1><pre class="source lang-java linenums">package org.kohsuke.github;
|
|
|
|
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.Base64;
|
|
|
|
/**
|
|
* A Content of a repository.
|
|
*
|
|
* @author Alexandre COLLIGNON
|
|
* @see GHRepository#getFileContent(String) GHRepository#getFileContent(String)
|
|
*/
|
|
@SuppressWarnings({ "UnusedDeclaration" })
|
|
<span class="fc" id="L18">public class GHContent extends GitHubInteractiveObject implements Refreshable {</span>
|
|
/*
|
|
* In normal use of this class, repository field is set via wrap(), but in the code search API, there's a nested
|
|
* 'repository' field that gets populated from JSON.
|
|
*/
|
|
private GHRepository repository;
|
|
|
|
private String type;
|
|
private String encoding;
|
|
private long size;
|
|
private String sha;
|
|
private String name;
|
|
private String path;
|
|
private String target;
|
|
private String content;
|
|
private String url; // this is the API url
|
|
private String git_url; // this is the Blob url
|
|
private String html_url; // this is the UI
|
|
private String download_url;
|
|
|
|
/**
|
|
* Gets owner.
|
|
*
|
|
* @return the owner
|
|
*/
|
|
public GHRepository getOwner() {
|
|
<span class="fc" id="L44"> return repository;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets type.
|
|
*
|
|
* @return the type
|
|
*/
|
|
public String getType() {
|
|
<span class="fc" id="L53"> return type;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets encoding.
|
|
*
|
|
* @return the encoding
|
|
*/
|
|
public String getEncoding() {
|
|
<span class="fc" id="L62"> return encoding;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets size.
|
|
*
|
|
* @return the size
|
|
*/
|
|
public long getSize() {
|
|
<span class="fc" id="L71"> return size;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets sha.
|
|
*
|
|
* @return the sha
|
|
*/
|
|
public String getSha() {
|
|
<span class="fc" id="L80"> return sha;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets name.
|
|
*
|
|
* @return the name
|
|
*/
|
|
public String getName() {
|
|
<span class="fc" id="L89"> return name;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets path.
|
|
*
|
|
* @return the path
|
|
*/
|
|
public String getPath() {
|
|
<span class="fc" id="L98"> return path;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets target of a symlink. This will only be set if {@code "symlink".equals(getType())}
|
|
*
|
|
* @return the target
|
|
*/
|
|
public String getTarget() {
|
|
<span class="fc" id="L107"> return target;</span>
|
|
}
|
|
|
|
/**
|
|
* Retrieve the decoded content that is stored at this location.
|
|
*
|
|
* <p>
|
|
* Due to the nature of GitHub's API, you're not guaranteed that the content will already be populated, so this may
|
|
* trigger network activity, and can throw an IOException.
|
|
*
|
|
* @return the content
|
|
* @throws IOException
|
|
* the io exception
|
|
* @deprecated Use {@link #read()}
|
|
*/
|
|
@SuppressFBWarnings("DM_DEFAULT_ENCODING")
|
|
public String getContent() throws IOException {
|
|
<span class="fc" id="L124"> return new String(Base64.getMimeDecoder().decode(getEncodedContent()));</span>
|
|
}
|
|
|
|
/**
|
|
* Retrieve the base64-encoded content that is stored at this location.
|
|
*
|
|
* <p>
|
|
* Due to the nature of GitHub's API, you're not guaranteed that the content will already be populated, so this may
|
|
* trigger network activity, and can throw an IOException.
|
|
*
|
|
* @return the encoded content
|
|
* @throws IOException
|
|
* the io exception
|
|
* @deprecated Use {@link #read()}
|
|
*/
|
|
public String getEncodedContent() throws IOException {
|
|
<span class="fc" id="L140"> refresh(content);</span>
|
|
<span class="fc" id="L141"> return content;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets url.
|
|
*
|
|
* @return the url
|
|
*/
|
|
public String getUrl() {
|
|
<span class="fc" id="L150"> return url;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets git url.
|
|
*
|
|
* @return the git url
|
|
*/
|
|
public String getGitUrl() {
|
|
<span class="nc" id="L159"> return git_url;</span>
|
|
}
|
|
|
|
/**
|
|
* Gets html url.
|
|
*
|
|
* @return the html url
|
|
*/
|
|
public String getHtmlUrl() {
|
|
<span class="nc" id="L168"> return html_url;</span>
|
|
}
|
|
|
|
/**
|
|
* Retrieves the actual content stored here.
|
|
*/
|
|
/**
|
|
* Retrieves the actual bytes of the blob.
|
|
*
|
|
* @return the input stream
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
public InputStream read() throws IOException {
|
|
<span class="fc" id="L182"> refresh(content);</span>
|
|
<span class="pc bpc" id="L183" title="1 of 2 branches missed."> if (encoding.equals("base64")) {</span>
|
|
try {
|
|
<span class="fc" id="L185"> Base64.Decoder decoder = Base64.getMimeDecoder();</span>
|
|
<span class="fc" id="L186"> return new ByteArrayInputStream(decoder.decode(content.getBytes(StandardCharsets.US_ASCII)));</span>
|
|
<span class="nc" id="L187"> } catch (IllegalArgumentException e) {</span>
|
|
<span class="nc" id="L188"> throw new AssertionError(e); // US-ASCII is mandatory</span>
|
|
}
|
|
}
|
|
|
|
<span class="nc" id="L192"> throw new UnsupportedOperationException("Unrecognized encoding: " + encoding);</span>
|
|
}
|
|
|
|
/**
|
|
* URL to retrieve the raw content of the file. Null if this is a directory.
|
|
*
|
|
* @return the download url
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
public String getDownloadUrl() throws IOException {
|
|
<span class="fc" id="L203"> refresh(download_url);</span>
|
|
<span class="fc" id="L204"> return download_url;</span>
|
|
}
|
|
|
|
/**
|
|
* Is file boolean.
|
|
*
|
|
* @return the boolean
|
|
*/
|
|
public boolean isFile() {
|
|
<span class="fc" id="L213"> return "file".equals(type);</span>
|
|
}
|
|
|
|
/**
|
|
* Is directory boolean.
|
|
*
|
|
* @return the boolean
|
|
*/
|
|
public boolean isDirectory() {
|
|
<span class="fc" id="L222"> return "dir".equals(type);</span>
|
|
}
|
|
|
|
/**
|
|
* Fully populate the data by retrieving missing data.
|
|
* <p>
|
|
* Depending on the original API call where this object is created, it may not contain everything.
|
|
*
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
protected synchronized void populate() throws IOException {
|
|
<span class="nc" id="L234"> root.createRequest().withUrlPath(url).fetchInto(this);</span>
|
|
<span class="nc" id="L235"> }</span>
|
|
|
|
/**
|
|
* List immediate children of this directory.
|
|
*
|
|
* @return the paged iterable
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
public PagedIterable<GHContent> listDirectoryContent() throws IOException {
|
|
<span class="pc bpc" id="L245" title="1 of 2 branches missed."> if (!isDirectory())</span>
|
|
<span class="nc" id="L246"> throw new IllegalStateException(path + " is not a directory");</span>
|
|
|
|
<span class="fc" id="L248"> return root.createRequest().setRawUrlPath(url).toIterable(GHContent[].class, item -> item.wrap(repository));</span>
|
|
}
|
|
|
|
/**
|
|
* Update gh content update response.
|
|
*
|
|
* @param newContent
|
|
* the new content
|
|
* @param commitMessage
|
|
* the commit message
|
|
* @return the gh content update response
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
@SuppressFBWarnings("DM_DEFAULT_ENCODING")
|
|
public GHContentUpdateResponse update(String newContent, String commitMessage) throws IOException {
|
|
<span class="fc" id="L264"> return update(newContent.getBytes(), commitMessage, null);</span>
|
|
}
|
|
|
|
/**
|
|
* Update gh content update response.
|
|
*
|
|
* @param newContent
|
|
* the new content
|
|
* @param commitMessage
|
|
* the commit message
|
|
* @param branch
|
|
* the branch
|
|
* @return the gh content update response
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
@SuppressFBWarnings("DM_DEFAULT_ENCODING")
|
|
public GHContentUpdateResponse update(String newContent, String commitMessage, String branch) throws IOException {
|
|
<span class="nc" id="L282"> return update(newContent.getBytes(), commitMessage, branch);</span>
|
|
}
|
|
|
|
/**
|
|
* Update gh content update response.
|
|
*
|
|
* @param newContentBytes
|
|
* the new content bytes
|
|
* @param commitMessage
|
|
* the commit message
|
|
* @return the gh content update response
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
public GHContentUpdateResponse update(byte[] newContentBytes, String commitMessage) throws IOException {
|
|
<span class="nc" id="L297"> return update(newContentBytes, commitMessage, null);</span>
|
|
}
|
|
|
|
/**
|
|
* Update gh content update response.
|
|
*
|
|
* @param newContentBytes
|
|
* the new content bytes
|
|
* @param commitMessage
|
|
* the commit message
|
|
* @param branch
|
|
* the branch
|
|
* @return the gh content update response
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
public GHContentUpdateResponse update(byte[] newContentBytes, String commitMessage, String branch)
|
|
throws IOException {
|
|
<span class="fc" id="L315"> String encodedContent = Base64.getEncoder().encodeToString(newContentBytes);</span>
|
|
|
|
<span class="fc" id="L317"> Requester requester = root.createRequest()</span>
|
|
<span class="fc" id="L318"> .method("POST")</span>
|
|
<span class="fc" id="L319"> .with("path", path)</span>
|
|
<span class="fc" id="L320"> .with("message", commitMessage)</span>
|
|
<span class="fc" id="L321"> .with("sha", sha)</span>
|
|
<span class="fc" id="L322"> .with("content", encodedContent)</span>
|
|
<span class="fc" id="L323"> .method("PUT");</span>
|
|
|
|
<span class="pc bpc" id="L325" title="1 of 2 branches missed."> if (branch != null) {</span>
|
|
<span class="nc" id="L326"> requester.with("branch", branch);</span>
|
|
}
|
|
|
|
<span class="fc" id="L329"> GHContentUpdateResponse response = requester.withUrlPath(getApiRoute(repository, path))</span>
|
|
<span class="fc" id="L330"> .fetch(GHContentUpdateResponse.class);</span>
|
|
|
|
<span class="fc" id="L332"> response.getContent().wrap(repository);</span>
|
|
<span class="fc" id="L333"> response.getCommit().wrapUp(repository);</span>
|
|
|
|
<span class="fc" id="L335"> this.content = encodedContent;</span>
|
|
<span class="fc" id="L336"> return response;</span>
|
|
}
|
|
|
|
/**
|
|
* Delete gh content update response.
|
|
*
|
|
* @param message
|
|
* the message
|
|
* @return the gh content update response
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
public GHContentUpdateResponse delete(String message) throws IOException {
|
|
<span class="fc" id="L349"> return delete(message, null);</span>
|
|
}
|
|
|
|
/**
|
|
* Delete gh content update response.
|
|
*
|
|
* @param commitMessage
|
|
* the commit message
|
|
* @param branch
|
|
* the branch
|
|
* @return the gh content update response
|
|
* @throws IOException
|
|
* the io exception
|
|
*/
|
|
public GHContentUpdateResponse delete(String commitMessage, String branch) throws IOException {
|
|
<span class="fc" id="L364"> Requester requester = root.createRequest()</span>
|
|
<span class="fc" id="L365"> .method("POST")</span>
|
|
<span class="fc" id="L366"> .with("path", path)</span>
|
|
<span class="fc" id="L367"> .with("message", commitMessage)</span>
|
|
<span class="fc" id="L368"> .with("sha", sha)</span>
|
|
<span class="fc" id="L369"> .method("DELETE");</span>
|
|
|
|
<span class="pc bpc" id="L371" title="1 of 2 branches missed."> if (branch != null) {</span>
|
|
<span class="nc" id="L372"> requester.with("branch", branch);</span>
|
|
}
|
|
|
|
<span class="fc" id="L375"> GHContentUpdateResponse response = requester.withUrlPath(getApiRoute(repository, path))</span>
|
|
<span class="fc" id="L376"> .fetch(GHContentUpdateResponse.class);</span>
|
|
|
|
<span class="fc" id="L378"> response.getCommit().wrapUp(repository);</span>
|
|
<span class="fc" id="L379"> return response;</span>
|
|
}
|
|
|
|
static String getApiRoute(GHRepository repository, String path) {
|
|
<span class="fc" id="L383"> return repository.getApiTailUrl("contents/" + path);</span>
|
|
}
|
|
|
|
GHContent wrap(GHRepository owner) {
|
|
<span class="fc" id="L387"> this.repository = owner;</span>
|
|
<span class="fc" id="L388"> this.root = owner.root;</span>
|
|
<span class="fc" id="L389"> return this;</span>
|
|
}
|
|
|
|
GHContent wrap(GitHub root) {
|
|
<span class="fc" id="L393"> this.root = root;</span>
|
|
<span class="pc bpc" id="L394" title="1 of 2 branches missed."> if (repository != null)</span>
|
|
<span class="fc" id="L395"> repository.wrap(root);</span>
|
|
<span class="fc" id="L396"> return this;</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.
|
|
*/
|
|
@Override
|
|
public synchronized void refresh() throws IOException {
|
|
<span class="fc" id="L406"> root.createRequest().setRawUrlPath(url).fetchInto(this);</span>
|
|
<span class="fc" id="L407"> }</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> |