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

167 lines
7.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>GHHooks.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">GHHooks.java</span></div><h1>GHHooks.java</h1><pre class="source lang-java linenums">package org.kohsuke.github;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* Utility class for creating and retrieving webhooks; removes duplication between GHOrganization and GHRepository
* functionality
*/
<span class="nc" id="L14">class GHHooks {</span>
static abstract class Context extends GitHubInteractiveObject {
<span class="fc" id="L17"> private Context(GitHub root) {</span>
<span class="fc" id="L18"> this.root = root;</span>
<span class="fc" id="L19"> }</span>
/**
* Gets hooks.
*
* @return the hooks
* @throws IOException
* the io exception
*/
public List&lt;GHHook&gt; getHooks() throws IOException {
<span class="fc" id="L30"> GHHook[] hookArray = root.createRequest().withUrlPath(collection()).fetch(collectionClass()); // jdk/eclipse</span>
// bug
// requires this
// to be on separate line
<span class="fc" id="L34"> List&lt;GHHook&gt; list = new ArrayList&lt;GHHook&gt;(Arrays.asList(hookArray));</span>
<span class="pc bpc" id="L35" title="1 of 2 branches missed."> for (GHHook h : list)</span>
<span class="nc" id="L36"> wrap(h);</span>
<span class="fc" id="L37"> return list;</span>
}
/**
* Gets hook.
*
* @param id
* the id
* @return the hook
* @throws IOException
* the io exception
*/
public GHHook getHook(int id) throws IOException {
<span class="fc" id="L50"> GHHook hook = root.createRequest().withUrlPath(collection() + &quot;/&quot; + id).fetch(clazz());</span>
<span class="fc" id="L51"> return wrap(hook);</span>
}
/**
* Create hook gh hook.
*
* @param name
* the name
* @param config
* the config
* @param events
* the events
* @param active
* the active
* @return the gh hook
* @throws IOException
* the io exception
*/
public GHHook createHook(String name, Map&lt;String, String&gt; config, Collection&lt;GHEvent&gt; events, boolean active)
throws IOException {
<span class="fc" id="L71"> List&lt;String&gt; ea = null;</span>
<span class="pc bpc" id="L72" title="1 of 2 branches missed."> if (events != null) {</span>
<span class="nc" id="L73"> ea = new ArrayList&lt;String&gt;();</span>
<span class="nc bnc" id="L74" title="All 2 branches missed."> for (GHEvent e : events)</span>
<span class="nc" id="L75"> ea.add(e.symbol());</span>
}
<span class="fc" id="L78"> GHHook hook = root.createRequest()</span>
<span class="fc" id="L79"> .method(&quot;POST&quot;)</span>
<span class="fc" id="L80"> .with(&quot;name&quot;, name)</span>
<span class="fc" id="L81"> .with(&quot;active&quot;, active)</span>
<span class="fc" id="L82"> .with(&quot;config&quot;, config)</span>
<span class="fc" id="L83"> .with(&quot;events&quot;, ea)</span>
<span class="fc" id="L84"> .withUrlPath(collection())</span>
<span class="fc" id="L85"> .fetch(clazz());</span>
<span class="fc" id="L87"> return wrap(hook);</span>
}
abstract String collection();
abstract Class&lt;? extends GHHook[]&gt; collectionClass();
abstract Class&lt;? extends GHHook&gt; clazz();
abstract GHHook wrap(GHHook hook);
}
private static class RepoContext extends Context {
private final GHRepository repository;
private final GHUser owner;
private RepoContext(GHRepository repository, GHUser owner) {
<span class="fc" id="L104"> super(repository.root);</span>
<span class="fc" id="L105"> this.repository = repository;</span>
<span class="fc" id="L106"> this.owner = owner;</span>
<span class="fc" id="L107"> }</span>
@Override
String collection() {
<span class="fc" id="L111"> return String.format(&quot;/repos/%s/%s/hooks&quot;, owner.getLogin(), repository.getName());</span>
}
@Override
Class&lt;? extends GHHook[]&gt; collectionClass() {
<span class="fc" id="L116"> return GHRepoHook[].class;</span>
}
@Override
Class&lt;? extends GHHook&gt; clazz() {
<span class="fc" id="L121"> return GHRepoHook.class;</span>
}
@Override
GHHook wrap(GHHook hook) {
<span class="fc" id="L126"> return ((GHRepoHook) hook).wrap(repository);</span>
}
}
private static class OrgContext extends Context {
private final GHOrganization organization;
private OrgContext(GHOrganization organization) {
<span class="fc" id="L134"> super(organization.root);</span>
<span class="fc" id="L135"> this.organization = organization;</span>
<span class="fc" id="L136"> }</span>
@Override
String collection() {
<span class="fc" id="L140"> return String.format(&quot;/orgs/%s/hooks&quot;, organization.getLogin());</span>
}
@Override
Class&lt;? extends GHHook[]&gt; collectionClass() {
<span class="nc" id="L145"> return GHOrgHook[].class;</span>
}
@Override
Class&lt;? extends GHHook&gt; clazz() {
<span class="fc" id="L150"> return GHOrgHook.class;</span>
}
@Override
GHHook wrap(GHHook hook) {
<span class="fc" id="L155"> return ((GHOrgHook) hook).wrap(organization);</span>
}
}
static Context repoContext(GHRepository repository, GHUser owner) {
<span class="fc" id="L160"> return new RepoContext(repository, owner);</span>
}
static Context orgContext(GHOrganization organization) {
<span class="fc" id="L164"> return new OrgContext(organization);</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>