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

308 lines
16 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>GHCheckRunBuilder.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">GHCheckRunBuilder.java</span></div><h1>GHCheckRunBuilder.java</h1><pre class="source lang-java linenums">/*
* The MIT License
*
* Copyright 2020 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the &quot;Software&quot;), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.kohsuke.github;
import com.fasterxml.jackson.annotation.JsonInclude;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.kohsuke.github.internal.Previews;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
/**
* Drafts or updates a check run.
*
* @see GHCheckRun
* @see GHRepository#createCheckRun
* @see &lt;a href=&quot;https://developer.github.com/v3/checks/runs/#create-a-check-run&quot;&gt;documentation&lt;/a&gt;
* @see GHCheckRun#update()
* @see &lt;a href=&quot;https://developer.github.com/v3/checks/runs/#update-a-check-run&quot;&gt;documentation&lt;/a&gt;
*/
@SuppressFBWarnings(value = &quot;URF_UNREAD_FIELD&quot;, justification = &quot;Jackson serializes these even without a getter&quot;)
@Preview(Previews.ANTIOPE)
@Deprecated
public final class GHCheckRunBuilder {
protected final GHRepository repo;
protected final Requester requester;
private Output output;
private List&lt;Action&gt; actions;
<span class="fc" id="L59"> private GHCheckRunBuilder(GHRepository repo, Requester requester) {</span>
<span class="fc" id="L60"> this.repo = repo;</span>
<span class="fc" id="L61"> this.requester = requester;</span>
<span class="fc" id="L62"> }</span>
GHCheckRunBuilder(GHRepository repo, String name, String headSHA) {
<span class="fc" id="L65"> this(repo,</span>
<span class="fc" id="L66"> repo.root.createRequest()</span>
<span class="fc" id="L67"> .withPreview(Previews.ANTIOPE)</span>
<span class="fc" id="L68"> .method(&quot;POST&quot;)</span>
<span class="fc" id="L69"> .with(&quot;name&quot;, name)</span>
<span class="fc" id="L70"> .with(&quot;head_sha&quot;, headSHA)</span>
<span class="fc" id="L71"> .withUrlPath(repo.getApiTailUrl(&quot;check-runs&quot;)));</span>
<span class="fc" id="L72"> }</span>
GHCheckRunBuilder(GHRepository repo, long checkId) {
<span class="fc" id="L75"> this(repo,</span>
<span class="fc" id="L76"> repo.root.createRequest()</span>
<span class="fc" id="L77"> .withPreview(Previews.ANTIOPE)</span>
<span class="fc" id="L78"> .method(&quot;PATCH&quot;)</span>
<span class="fc" id="L79"> .withUrlPath(repo.getApiTailUrl(&quot;check-runs/&quot; + checkId)));</span>
<span class="fc" id="L80"> }</span>
public @NonNull GHCheckRunBuilder withDetailsURL(@CheckForNull String detailsURL) {
<span class="fc" id="L83"> requester.with(&quot;details_url&quot;, detailsURL);</span>
<span class="fc" id="L84"> return this;</span>
}
public @NonNull GHCheckRunBuilder withExternalID(@CheckForNull String externalID) {
<span class="fc" id="L88"> requester.with(&quot;external_id&quot;, externalID);</span>
<span class="fc" id="L89"> return this;</span>
}
public @NonNull GHCheckRunBuilder withStatus(@CheckForNull GHCheckRun.Status status) {
<span class="pc bpc" id="L93" title="1 of 2 branches missed."> if (status != null) {</span>
// Do *not* use the overload taking Enum, as that s/_/-/g which would be wrong here.
<span class="fc" id="L95"> requester.with(&quot;status&quot;, status.toString().toLowerCase(Locale.ROOT));</span>
}
<span class="fc" id="L97"> return this;</span>
}
public @NonNull GHCheckRunBuilder withConclusion(@CheckForNull GHCheckRun.Conclusion conclusion) {
<span class="pc bpc" id="L101" title="1 of 2 branches missed."> if (conclusion != null) {</span>
<span class="fc" id="L102"> requester.with(&quot;conclusion&quot;, conclusion.toString().toLowerCase(Locale.ROOT));</span>
}
<span class="fc" id="L104"> return this;</span>
}
public @NonNull GHCheckRunBuilder withStartedAt(@CheckForNull Date startedAt) {
<span class="pc bpc" id="L108" title="1 of 2 branches missed."> if (startedAt != null) {</span>
<span class="fc" id="L109"> requester.with(&quot;started_at&quot;, GitHubClient.printDate(startedAt));</span>
}
<span class="fc" id="L111"> return this;</span>
}
public @NonNull GHCheckRunBuilder withCompletedAt(@CheckForNull Date completedAt) {
<span class="pc bpc" id="L115" title="1 of 2 branches missed."> if (completedAt != null) {</span>
<span class="fc" id="L116"> requester.with(&quot;completed_at&quot;, GitHubClient.printDate(completedAt));</span>
}
<span class="fc" id="L118"> return this;</span>
}
public @NonNull GHCheckRunBuilder add(@NonNull Output output) {
<span class="pc bpc" id="L122" title="1 of 2 branches missed."> if (this.output != null) {</span>
<span class="nc" id="L123"> throw new IllegalStateException(&quot;cannot add Output twice&quot;);</span>
}
<span class="fc" id="L125"> this.output = output;</span>
<span class="fc" id="L126"> return this;</span>
}
public @NonNull GHCheckRunBuilder add(@NonNull Action action) {
<span class="pc bpc" id="L130" title="1 of 2 branches missed."> if (actions == null) {</span>
<span class="fc" id="L131"> actions = new LinkedList&lt;&gt;();</span>
}
<span class="fc" id="L133"> actions.add(action);</span>
<span class="fc" id="L134"> return this;</span>
}
private static final int MAX_ANNOTATIONS = 50;
/**
* Actually creates the check run. (If more than fifty annotations were requested, this is done in batches.)
*
* @return the resulting run
* @throws IOException
* for the usual reasons
*/
public @NonNull GHCheckRun create() throws IOException {
List&lt;Annotation&gt; extraAnnotations;
<span class="fc bfc" id="L147" title="All 6 branches covered."> if (output != null &amp;&amp; output.annotations != null &amp;&amp; output.annotations.size() &gt; MAX_ANNOTATIONS) {</span>
<span class="fc" id="L148"> extraAnnotations = output.annotations.subList(MAX_ANNOTATIONS, output.annotations.size());</span>
<span class="fc" id="L149"> output.annotations = output.annotations.subList(0, MAX_ANNOTATIONS);</span>
} else {
<span class="fc" id="L151"> extraAnnotations = Collections.emptyList();</span>
}
<span class="fc" id="L153"> GHCheckRun run = requester.with(&quot;output&quot;, output).with(&quot;actions&quot;, actions).fetch(GHCheckRun.class).wrap(repo);</span>
<span class="fc bfc" id="L154" title="All 2 branches covered."> while (!extraAnnotations.isEmpty()) {</span>
<span class="fc" id="L155"> Output output2 = new Output(output.title, output.summary).withText(output.text);</span>
<span class="fc" id="L156"> int i = Math.min(extraAnnotations.size(), MAX_ANNOTATIONS);</span>
<span class="fc" id="L157"> output2.annotations = extraAnnotations.subList(0, i);</span>
<span class="fc" id="L158"> extraAnnotations = extraAnnotations.subList(i, extraAnnotations.size());</span>
<span class="fc" id="L159"> run = repo.root.createRequest()</span>
<span class="fc" id="L160"> .withPreview(Previews.ANTIOPE)</span>
<span class="fc" id="L161"> .method(&quot;PATCH&quot;)</span>
<span class="fc" id="L162"> .with(&quot;output&quot;, output2)</span>
<span class="fc" id="L163"> .withUrlPath(repo.getApiTailUrl(&quot;check-runs/&quot; + run.getId()))</span>
<span class="fc" id="L164"> .fetch(GHCheckRun.class)</span>
<span class="fc" id="L165"> .wrap(repo);</span>
<span class="fc" id="L166"> }</span>
<span class="fc" id="L167"> return run;</span>
}
/**
* @see &lt;a href=&quot;https://developer.github.com/v3/checks/runs/#output-object&quot;&gt;documentation&lt;/a&gt;
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public static final class Output {
private final String title;
private final String summary;
private String text;
private List&lt;Annotation&gt; annotations;
private List&lt;Image&gt; images;
<span class="fc" id="L182"> public Output(@NonNull String title, @NonNull String summary) {</span>
<span class="fc" id="L183"> this.title = title;</span>
<span class="fc" id="L184"> this.summary = summary;</span>
<span class="fc" id="L185"> }</span>
public @NonNull Output withText(@CheckForNull String text) {
<span class="fc" id="L188"> this.text = text;</span>
<span class="fc" id="L189"> return this;</span>
}
public @NonNull Output add(@NonNull Annotation annotation) {
<span class="fc bfc" id="L193" title="All 2 branches covered."> if (annotations == null) {</span>
<span class="fc" id="L194"> annotations = new LinkedList&lt;&gt;();</span>
}
<span class="fc" id="L196"> annotations.add(annotation);</span>
<span class="fc" id="L197"> return this;</span>
}
public @NonNull Output add(@NonNull Image image) {
<span class="pc bpc" id="L201" title="1 of 2 branches missed."> if (images == null) {</span>
<span class="fc" id="L202"> images = new LinkedList&lt;&gt;();</span>
}
<span class="fc" id="L204"> images.add(image);</span>
<span class="fc" id="L205"> return this;</span>
}
}
/**
* @see &lt;a href=&quot;https://developer.github.com/v3/checks/runs/#annotations-object&quot;&gt;documentation&lt;/a&gt;
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public static final class Annotation {
private final String path;
private final int start_line;
private final int end_line;
private final String annotation_level;
private final String message;
private Integer start_column;
private Integer end_column;
private String title;
private String raw_details;
public Annotation(@NonNull String path,
int line,
@NonNull GHCheckRun.AnnotationLevel annotationLevel,
@NonNull String message) {
<span class="fc" id="L230"> this(path, line, line, annotationLevel, message);</span>
<span class="fc" id="L231"> }</span>
public Annotation(@NonNull String path,
int startLine,
int endLine,
@NonNull GHCheckRun.AnnotationLevel annotationLevel,
<span class="fc" id="L237"> @NonNull String message) {</span>
<span class="fc" id="L238"> this.path = path;</span>
<span class="fc" id="L239"> start_line = startLine;</span>
<span class="fc" id="L240"> end_line = endLine;</span>
<span class="fc" id="L241"> annotation_level = annotationLevel.toString().toLowerCase(Locale.ROOT);</span>
<span class="fc" id="L242"> this.message = message;</span>
<span class="fc" id="L243"> }</span>
public @NonNull Annotation withStartColumn(@CheckForNull Integer startColumn) {
<span class="nc" id="L246"> start_column = startColumn;</span>
<span class="nc" id="L247"> return this;</span>
}
public @NonNull Annotation withEndColumn(@CheckForNull Integer endColumn) {
<span class="nc" id="L251"> end_column = endColumn;</span>
<span class="nc" id="L252"> return this;</span>
}
public @NonNull Annotation withTitle(@CheckForNull String title) {
<span class="fc" id="L256"> this.title = title;</span>
<span class="fc" id="L257"> return this;</span>
}
public @NonNull Annotation withRawDetails(@CheckForNull String rawDetails) {
<span class="nc" id="L261"> raw_details = rawDetails;</span>
<span class="nc" id="L262"> return this;</span>
}
}
/**
* @see &lt;a href=&quot;https://developer.github.com/v3/checks/runs/#images-object&quot;&gt;documentation&lt;/a&gt;
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public static final class Image {
private final String alt;
private final String image_url;
private String caption;
<span class="fc" id="L277"> public Image(@NonNull String alt, @NonNull String imageURL) {</span>
<span class="fc" id="L278"> this.alt = alt;</span>
<span class="fc" id="L279"> image_url = imageURL;</span>
<span class="fc" id="L280"> }</span>
public @NonNull Image withCaption(@CheckForNull String caption) {
<span class="fc" id="L283"> this.caption = caption;</span>
<span class="fc" id="L284"> return this;</span>
}
}
/**
* @see &lt;a href=&quot;https://developer.github.com/v3/checks/runs/#actions-object&quot;&gt;documentation&lt;/a&gt;
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public static final class Action {
private final String label;
private final String description;
private final String identifier;
<span class="fc" id="L299"> public Action(@NonNull String label, @NonNull String description, @NonNull String identifier) {</span>
<span class="fc" id="L300"> this.label = label;</span>
<span class="fc" id="L301"> this.description = description;</span>
<span class="fc" id="L302"> this.identifier = identifier;</span>
<span class="fc" id="L303"> }</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>