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

369 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>GHBranchProtectionBuilder.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">GHBranchProtectionBuilder.java</span></div><h1>GHBranchProtectionBuilder.java</h1><pre class="source lang-java linenums">package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.kohsuke.github.internal.Previews.LUKE_CAGE;
/**
* Builder to configure the branch protection settings.
*
* @see GHBranch#enableProtection() GHBranch#enableProtection()
*/
@SuppressFBWarnings(
value = { &quot;UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD&quot;, &quot;UWF_UNWRITTEN_FIELD&quot;, &quot;NP_UNWRITTEN_FIELD&quot;,
&quot;URF_UNREAD_FIELD&quot; },
justification = &quot;JSON API&quot;)
public class GHBranchProtectionBuilder {
private final GHBranch branch;
private boolean enforceAdmins;
private Map&lt;String, Object&gt; prReviews;
private Restrictions restrictions;
private StatusChecks statusChecks;
<span class="fc" id="L34"> GHBranchProtectionBuilder(GHBranch branch) {</span>
<span class="fc" id="L35"> this.branch = branch;</span>
<span class="fc" id="L36"> }</span>
/**
* Add required checks gh branch protection builder.
*
* @param checks
* the checks
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder addRequiredChecks(Collection&lt;String&gt; checks) {
<span class="fc" id="L46"> getStatusChecks().contexts.addAll(checks);</span>
<span class="fc" id="L47"> return this;</span>
}
/**
* Add required checks gh branch protection builder.
*
* @param checks
* the checks
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder addRequiredChecks(String... checks) {
<span class="fc" id="L58"> addRequiredChecks(Arrays.asList(checks));</span>
<span class="fc" id="L59"> return this;</span>
}
/**
* Dismiss stale reviews gh branch protection builder.
*
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder dismissStaleReviews() {
<span class="fc" id="L68"> return dismissStaleReviews(true);</span>
}
/**
* Dismiss stale reviews gh branch protection builder.
*
* @param v
* the v
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder dismissStaleReviews(boolean v) {
<span class="fc" id="L79"> getPrReviews().put(&quot;dismiss_stale_reviews&quot;, v);</span>
<span class="fc" id="L80"> return this;</span>
}
/**
* Enable gh branch protection.
*
* @return the gh branch protection
* @throws IOException
* the io exception
*/
public GHBranchProtection enable() throws IOException {
<span class="fc" id="L91"> return requester().method(&quot;PUT&quot;)</span>
<span class="fc" id="L92"> .withNullable(&quot;required_status_checks&quot;, statusChecks)</span>
<span class="fc" id="L93"> .withNullable(&quot;required_pull_request_reviews&quot;, prReviews)</span>
<span class="fc" id="L94"> .withNullable(&quot;restrictions&quot;, restrictions)</span>
<span class="fc" id="L95"> .withNullable(&quot;enforce_admins&quot;, enforceAdmins)</span>
<span class="fc" id="L96"> .withUrlPath(branch.getProtectionUrl().toString())</span>
<span class="fc" id="L97"> .fetch(GHBranchProtection.class)</span>
<span class="fc" id="L98"> .wrap(branch);</span>
}
/**
* Include admins gh branch protection builder.
*
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder includeAdmins() {
<span class="fc" id="L107"> return includeAdmins(true);</span>
}
/**
* Include admins gh branch protection builder.
*
* @param v
* the v
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder includeAdmins(boolean v) {
<span class="fc" id="L118"> enforceAdmins = v;</span>
<span class="fc" id="L119"> return this;</span>
}
/**
* Required reviewers gh branch protection builder.
*
* @param v
* the v
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder requiredReviewers(int v) {
<span class="fc" id="L130"> getPrReviews().put(&quot;required_approving_review_count&quot;, v);</span>
<span class="fc" id="L131"> return this;</span>
}
/**
* Require branch is up to date gh branch protection builder.
*
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder requireBranchIsUpToDate() {
<span class="fc" id="L140"> return requireBranchIsUpToDate(true);</span>
}
/**
* Require branch is up to date gh branch protection builder.
*
* @param v
* the v
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder requireBranchIsUpToDate(boolean v) {
<span class="fc" id="L151"> getStatusChecks().strict = v;</span>
<span class="fc" id="L152"> return this;</span>
}
/**
* Require code own reviews gh branch protection builder.
*
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder requireCodeOwnReviews() {
<span class="fc" id="L161"> return requireCodeOwnReviews(true);</span>
}
/**
* Require code own reviews gh branch protection builder.
*
* @param v
* the v
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder requireCodeOwnReviews(boolean v) {
<span class="fc" id="L172"> getPrReviews().put(&quot;require_code_owner_reviews&quot;, v);</span>
<span class="fc" id="L173"> return this;</span>
}
/**
* Require reviews gh branch protection builder.
*
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder requireReviews() {
<span class="fc" id="L182"> getPrReviews();</span>
<span class="fc" id="L183"> return this;</span>
}
/**
* Restrict review dismissals gh branch protection builder.
*
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder restrictReviewDismissals() {
<span class="nc" id="L192"> getPrReviews();</span>
<span class="nc bnc" id="L194" title="All 2 branches missed."> if (!prReviews.containsKey(&quot;dismissal_restrictions&quot;)) {</span>
<span class="nc" id="L195"> prReviews.put(&quot;dismissal_restrictions&quot;, new Restrictions());</span>
}
<span class="nc" id="L198"> return this;</span>
}
/**
* Restrict push access gh branch protection builder.
*
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder restrictPushAccess() {
<span class="nc" id="L207"> getRestrictions();</span>
<span class="nc" id="L208"> return this;</span>
}
/**
* Team push access gh branch protection builder.
*
* @param teams
* the teams
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder teamPushAccess(Collection&lt;GHTeam&gt; teams) {
<span class="nc bnc" id="L219" title="All 2 branches missed."> for (GHTeam team : teams) {</span>
<span class="nc" id="L220"> teamPushAccess(team);</span>
<span class="nc" id="L221"> }</span>
<span class="nc" id="L222"> return this;</span>
}
/**
* Team push access gh branch protection builder.
*
* @param teams
* the teams
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder teamPushAccess(GHTeam... teams) {
<span class="nc bnc" id="L233" title="All 2 branches missed."> for (GHTeam team : teams) {</span>
<span class="nc" id="L234"> getRestrictions().teams.add(team.getSlug());</span>
}
<span class="nc" id="L236"> return this;</span>
}
/**
* Team review dismissals gh branch protection builder.
*
* @param teams
* the teams
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder teamReviewDismissals(Collection&lt;GHTeam&gt; teams) {
<span class="nc bnc" id="L247" title="All 2 branches missed."> for (GHTeam team : teams) {</span>
<span class="nc" id="L248"> teamReviewDismissals(team);</span>
<span class="nc" id="L249"> }</span>
<span class="nc" id="L250"> return this;</span>
}
/**
* Team review dismissals gh branch protection builder.
*
* @param teams
* the teams
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder teamReviewDismissals(GHTeam... teams) {
<span class="nc bnc" id="L261" title="All 2 branches missed."> for (GHTeam team : teams) {</span>
<span class="nc" id="L262"> addReviewRestriction(team.getSlug(), true);</span>
}
<span class="nc" id="L264"> return this;</span>
}
/**
* User push access gh branch protection builder.
*
* @param users
* the users
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder userPushAccess(Collection&lt;GHUser&gt; users) {
<span class="nc bnc" id="L275" title="All 2 branches missed."> for (GHUser user : users) {</span>
<span class="nc" id="L276"> userPushAccess(user);</span>
<span class="nc" id="L277"> }</span>
<span class="nc" id="L278"> return this;</span>
}
/**
* User push access gh branch protection builder.
*
* @param users
* the users
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder userPushAccess(GHUser... users) {
<span class="nc bnc" id="L289" title="All 2 branches missed."> for (GHUser user : users) {</span>
<span class="nc" id="L290"> getRestrictions().users.add(user.getLogin());</span>
}
<span class="nc" id="L292"> return this;</span>
}
/**
* User review dismissals gh branch protection builder.
*
* @param users
* the users
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder userReviewDismissals(Collection&lt;GHUser&gt; users) {
<span class="nc bnc" id="L303" title="All 2 branches missed."> for (GHUser team : users) {</span>
<span class="nc" id="L304"> userReviewDismissals(team);</span>
<span class="nc" id="L305"> }</span>
<span class="nc" id="L306"> return this;</span>
}
/**
* User review dismissals gh branch protection builder.
*
* @param users
* the users
* @return the gh branch protection builder
*/
public GHBranchProtectionBuilder userReviewDismissals(GHUser... users) {
<span class="nc bnc" id="L317" title="All 2 branches missed."> for (GHUser user : users) {</span>
<span class="nc" id="L318"> addReviewRestriction(user.getLogin(), false);</span>
}
<span class="nc" id="L320"> return this;</span>
}
private void addReviewRestriction(String restriction, boolean isTeam) {
<span class="nc" id="L324"> restrictReviewDismissals();</span>
<span class="nc" id="L325"> Restrictions restrictions = (Restrictions) prReviews.get(&quot;dismissal_restrictions&quot;);</span>
<span class="nc bnc" id="L327" title="All 2 branches missed."> if (isTeam) {</span>
<span class="nc" id="L328"> restrictions.teams.add(restriction);</span>
} else {
<span class="nc" id="L330"> restrictions.users.add(restriction);</span>
}
<span class="nc" id="L332"> }</span>
private Map&lt;String, Object&gt; getPrReviews() {
<span class="fc bfc" id="L335" title="All 2 branches covered."> if (prReviews == null) {</span>
<span class="fc" id="L336"> prReviews = new HashMap&lt;String, Object&gt;();</span>
}
<span class="fc" id="L338"> return prReviews;</span>
}
private Restrictions getRestrictions() {
<span class="nc bnc" id="L342" title="All 2 branches missed."> if (restrictions == null) {</span>
<span class="nc" id="L343"> restrictions = new Restrictions();</span>
}
<span class="nc" id="L345"> return restrictions;</span>
}
private StatusChecks getStatusChecks() {
<span class="fc bfc" id="L349" title="All 2 branches covered."> if (statusChecks == null) {</span>
<span class="fc" id="L350"> statusChecks = new StatusChecks();</span>
}
<span class="fc" id="L352"> return statusChecks;</span>
}
private Requester requester() {
<span class="fc" id="L356"> return branch.getRoot().createRequest().withPreview(LUKE_CAGE);</span>
}
<span class="nc" id="L359"> private static class Restrictions {</span>
<span class="nc" id="L360"> private Set&lt;String&gt; teams = new HashSet&lt;String&gt;();</span>
<span class="nc" id="L361"> private Set&lt;String&gt; users = new HashSet&lt;String&gt;();</span>
}
<span class="fc" id="L364"> private static class StatusChecks {</span>
<span class="fc" id="L365"> final List&lt;String&gt; contexts = new ArrayList&lt;String&gt;();</span>
boolean strict;
}
}
</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>