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

202 lines
9.5 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>GHRef.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">GHRef.java</span></div><h1>GHRef.java</h1><pre class="source lang-java linenums">package org.kohsuke.github;
import com.fasterxml.jackson.databind.JsonMappingException;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.net.URL;
/**
* Provides information on a Git ref from GitHub.
*
* @author Michael Clarke
*/
<span class="fc" id="L14">public class GHRef extends GitHubInteractiveObject {</span>
private String ref, url;
private GHObject object;
/**
* Name of the ref, such as &quot;refs/tags/abc&quot;
*
* @return the ref
*/
public String getRef() {
<span class="fc" id="L24"> return ref;</span>
}
/**
* The API URL of this tag, such as https://api.github.com/repos/jenkinsci/jenkins/git/refs/tags/1.312
*
* @return the url
*/
public URL getUrl() {
<span class="fc" id="L33"> return GitHubClient.parseURL(url);</span>
}
/**
* The object that this ref points to.
*
* @return the object
*/
public GHObject getObject() {
<span class="fc" id="L42"> return object;</span>
}
/**
* Updates this ref to the specified commit.
*
* @param sha
* The SHA1 value to set this reference to
* @throws IOException
* the io exception
*/
public void updateTo(String sha) throws IOException {
<span class="fc" id="L54"> updateTo(sha, false);</span>
<span class="fc" id="L55"> }</span>
/**
* Updates this ref to the specified commit.
*
* @param sha
* The SHA1 value to set this reference to
* @param force
* Whether or not to force this ref update.
* @throws IOException
* the io exception
*/
public void updateTo(String sha, Boolean force) throws IOException {
<span class="fc" id="L68"> root.createRequest()</span>
<span class="fc" id="L69"> .method(&quot;PATCH&quot;)</span>
<span class="fc" id="L70"> .with(&quot;sha&quot;, sha)</span>
<span class="fc" id="L71"> .with(&quot;force&quot;, force)</span>
<span class="fc" id="L72"> .withUrlPath(url)</span>
<span class="fc" id="L73"> .fetch(GHRef.class)</span>
<span class="fc" id="L74"> .wrap(root);</span>
<span class="fc" id="L75"> }</span>
/**
* Deletes this ref from the repository using the GitHub API.
*
* @throws IOException
* the io exception
*/
public void delete() throws IOException {
<span class="fc" id="L84"> root.createRequest().method(&quot;DELETE&quot;).withUrlPath(url).send();</span>
<span class="fc" id="L85"> }</span>
GHRef wrap(GitHub root) {
<span class="fc" id="L88"> this.root = root;</span>
<span class="fc" id="L89"> return this;</span>
}
/**
* Retrive a ref of the given type for the current GitHub repository.
*
* @param repository
* the repository to read from
* @param refName
* eg: heads/branch
* @return refs matching the request type
* @throws IOException
* on failure communicating with GitHub, potentially due to an invalid ref type being requested
*/
static GHRef read(GHRepository repository, String refName) throws IOException {
// Also accept e.g. &quot;refs/heads/branch&quot; for consistency with createRef().
<span class="fc bfc" id="L105" title="All 2 branches covered."> if (refName.startsWith(&quot;refs/&quot;)) {</span>
<span class="fc" id="L106"> refName = refName.replaceFirst(&quot;refs/&quot;, &quot;&quot;);</span>
}
// We would expect this to use `git/ref/%s` but some versions of GHE seem to not support it
// Instead use `git/refs/%s` and check the result actually matches the ref
<span class="fc" id="L111"> GHRef result = null;</span>
try {
<span class="fc" id="L113"> result = repository.root.createRequest()</span>
<span class="fc" id="L114"> .withUrlPath(repository.getApiTailUrl(String.format(&quot;git/refs/%s&quot;, refName)))</span>
<span class="fc" id="L115"> .fetch(GHRef.class)</span>
<span class="fc" id="L116"> .wrap(repository.root);</span>
<span class="fc" id="L117"> } catch (IOException e) {</span>
// If the parse exception is due to the above returning an array instead of a single ref
// that means the individual ref did not exist. Handled by result check below.
// Otherwise, rethrow.
<span class="fc bfc" id="L121" title="All 2 branches covered."> if (!(e.getCause() instanceof JsonMappingException)) {</span>
<span class="fc" id="L122"> throw e;</span>
}
<span class="fc" id="L124"> }</span>
// Verify that the ref returned is the one requested
// Used .endsWith(refName) instead of .equals(&quot;refs/&quot; + refName) to workaround a GitBucket
// issue where the &quot;ref&quot; field omits the &quot;refs/&quot; prefix. &quot;endsWith()&quot; is functionally
// the same for this scenario - the server refs matching is prefix-based, so
// a ref that ends with the correct string will always be the correct one.
<span class="pc bpc" id="L131" title="1 of 4 branches missed."> if (result == null || !result.getRef().endsWith(refName)) {</span>
<span class="fc" id="L132"> throw new GHFileNotFoundException(String.format(&quot;git/refs/%s&quot;, refName)</span>
+ &quot; {\&quot;message\&quot;:\&quot;Not Found\&quot;,\&quot;documentation_url\&quot;:\&quot;https://developer.github.com/v3/git/refs/#get-a-reference\&quot;}&quot;);
}
<span class="fc" id="L135"> return result;</span>
}
/**
* Retrieves all refs of the given type for the current GitHub repository.
*
* @param repository
* the repository to read from
* @param refType
* the type of reg to search for e.g. &lt;code&gt;tags&lt;/code&gt; or &lt;code&gt;commits&lt;/code&gt;
* @return paged iterable of all refs of the specified type
* @throws IOException
* on failure communicating with GitHub, potentially due to an invalid ref type being requested
*/
static PagedIterable&lt;GHRef&gt; readMatching(GHRepository repository, String refType) throws IOException {
<span class="fc bfc" id="L150" title="All 2 branches covered."> if (refType.startsWith(&quot;refs/&quot;)) {</span>
<span class="fc" id="L151"> refType = refType.replaceFirst(&quot;refs/&quot;, &quot;&quot;);</span>
}
<span class="fc" id="L154"> String url = repository.getApiTailUrl(String.format(&quot;git/refs/%s&quot;, refType));</span>
// if no types, do not end with slash just to be safe.
<span class="fc bfc" id="L156" title="All 2 branches covered."> if (refType.equals(&quot;&quot;)) {</span>
<span class="fc" id="L157"> url = url.substring(0, url.length() - 1);</span>
}
<span class="fc" id="L159"> return repository.root.createRequest()</span>
<span class="fc" id="L160"> .withUrlPath(url)</span>
<span class="fc" id="L161"> .toIterable(GHRef[].class, item -&gt; item.wrap(repository.root));</span>
}
/**
* The type GHObject.
*/
@SuppressFBWarnings(
value = { &quot;UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD&quot;, &quot;UWF_UNWRITTEN_FIELD&quot;, &quot;NP_UNWRITTEN_FIELD&quot; },
justification = &quot;JSON API&quot;)
<span class="fc" id="L170"> public static class GHObject {</span>
private String type, sha, url;
/**
* Type of the object, such as &quot;commit&quot;
*
* @return the type
*/
public String getType() {
<span class="nc" id="L179"> return type;</span>
}
/**
* SHA1 of this object.
*
* @return the sha
*/
public String getSha() {
<span class="fc" id="L188"> return sha;</span>
}
/**
* API URL to this Git data, such as
* https://api.github.com/repos/jenkinsci/jenkins/git/commits/b72322675eb0114363a9a86e9ad5a170d1d07ac0
*
* @return the url
*/
public URL getUrl() {
<span class="nc" id="L198"> return GitHubClient.parseURL(url);</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>