mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-21 00:11:23 +00:00
369 lines
15 KiB
HTML
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> > <a href="index.source.html" class="el_package">org.kohsuke.github</a> > <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 = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD",
|
|
"URF_UNREAD_FIELD" },
|
|
justification = "JSON API")
|
|
public class GHBranchProtectionBuilder {
|
|
private final GHBranch branch;
|
|
|
|
private boolean enforceAdmins;
|
|
private Map<String, Object> 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<String> 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("dismiss_stale_reviews", 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("PUT")</span>
|
|
<span class="fc" id="L92"> .withNullable("required_status_checks", statusChecks)</span>
|
|
<span class="fc" id="L93"> .withNullable("required_pull_request_reviews", prReviews)</span>
|
|
<span class="fc" id="L94"> .withNullable("restrictions", restrictions)</span>
|
|
<span class="fc" id="L95"> .withNullable("enforce_admins", 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("required_approving_review_count", 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("require_code_owner_reviews", 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("dismissal_restrictions")) {</span>
|
|
<span class="nc" id="L195"> prReviews.put("dismissal_restrictions", 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<GHTeam> 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<GHTeam> 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<GHUser> 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<GHUser> 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("dismissal_restrictions");</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<String, Object> getPrReviews() {
|
|
<span class="fc bfc" id="L335" title="All 2 branches covered."> if (prReviews == null) {</span>
|
|
<span class="fc" id="L336"> prReviews = new HashMap<String, Object>();</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<String> teams = new HashSet<String>();</span>
|
|
<span class="nc" id="L361"> private Set<String> users = new HashSet<String>();</span>
|
|
}
|
|
|
|
<span class="fc" id="L364"> private static class StatusChecks {</span>
|
|
<span class="fc" id="L365"> final List<String> contexts = new ArrayList<String>();</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> |