-
Notifications
You must be signed in to change notification settings - Fork 772
GHRepository.createCheckRun #753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
7d5a39e
GHRepository.createCheckRun
jglick 36ab2a8
Redesigned using a fluent builder idiom
jglick b715e0c
Reformatted
jglick 572ff9d
I guess nullability annotations can be omitted from internal members
jglick c7fb390
Introduced enums
jglick cd7963b
Acc to https://github.com/github-api/github-api/blob/7a650132c52d40c5…
jglick e8b4de0
WireMock coverage
jglick 7ae9638
Merge branch 'master' of github.com:github-api/github-api into create…
jglick 6c41f22
Coverage of DraftImage
jglick 03de12c
SpotBugs
jglick 26b8082
Handle >50 annotations
jglick b5dc3c4
Added some Javadoc.
jglick d8274ac
Merge branch 'javadoc' into createCheckRun
jglick fd3c36a
IMHO treating Javadoc warnings as fatal makes sources less legible an…
jglick b4c4a05
Moving enums inside GHCheckRun
jglick b2c513e
Merge branch 'master' into createCheckRun
bitwiseman c01f3f5
NPE when there are no annotations
jglick c499c73
Merge branch 'master' of github.com:github-api/github-api into create…
jglick 8b51a44
Merge branch 'createCheckRun' of github.com:jglick/github-api into cr…
jglick 30d792d
Remove ‘Draft’ from nested data class names
jglick 89a6664
More links to GH docs from Javadoc
jglick 883c8cc
Merge branch 'master' of github.com:github-api/github-api into create…
jglick 6214b6a
Regenerated WireMock metadata
jglick 7c9397f
Switched from fluent style with .done() to accessible constructors
jglick 82b9c05
Strengthened test a bit
jglick 58f1fe0
createPendingCheckRun
jglick 3ab9381
createCheckRunErrMissingConclusion
jglick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
291 changes: 291 additions & 0 deletions
291
src/main/java/org/kohsuke/github/GHCheckRunBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,291 @@ | ||
| /* | ||
| * 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 "Software"), 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 "AS IS", 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 java.io.IOException; | ||
| import java.util.Collections; | ||
| import java.util.Date; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
|
|
||
| /** | ||
| * Drafts a check run. | ||
| * | ||
| * @see GHCheckRun | ||
| * @see GHRepository#createCheckRun | ||
| * @see <a href="https://developer.github.com/v3/checks/runs/#create-a-check-run">documentation</a> | ||
| */ | ||
| @SuppressFBWarnings(value = "URF_UNREAD_FIELD", justification = "Jackson serializes these even without a getter") | ||
| @Preview | ||
| @Deprecated | ||
| public final class GHCheckRunBuilder { | ||
|
|
||
| private final GHRepository repo; | ||
| private final Requester requester; | ||
| private Output output; | ||
| private List<Action> actions; | ||
|
|
||
| GHCheckRunBuilder(GHRepository repo, String name, String headSHA) { | ||
| this.repo = repo; | ||
| requester = repo.root.createRequest() | ||
| .withPreview(Previews.ANTIOPE) | ||
| .method("POST") | ||
| .with("name", name) | ||
| .with("head_sha", headSHA) | ||
| .withUrlPath(repo.getApiTailUrl("check-runs")); | ||
| } | ||
|
|
||
| public @NonNull GHCheckRunBuilder withDetailsURL(@CheckForNull String detailsURL) { | ||
| requester.with("details_url", detailsURL); | ||
| return this; | ||
| } | ||
|
|
||
| public @NonNull GHCheckRunBuilder withExternalID(@CheckForNull String externalID) { | ||
| requester.with("external_id", externalID); | ||
| return this; | ||
| } | ||
|
|
||
| public @NonNull GHCheckRunBuilder withStatus(@CheckForNull GHCheckRun.Status status) { | ||
| if (status != null) { | ||
| // Do *not* use the overload taking Enum, as that s/_/-/g which would be wrong here. | ||
| requester.with("status", status.toString().toLowerCase(Locale.ROOT)); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| public @NonNull GHCheckRunBuilder withConclusion(@CheckForNull GHCheckRun.Conclusion conclusion) { | ||
| if (conclusion != null) { | ||
| requester.with("conclusion", conclusion.toString().toLowerCase(Locale.ROOT)); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| public @NonNull GHCheckRunBuilder withStartedAt(@CheckForNull Date startedAt) { | ||
| if (startedAt != null) { | ||
| requester.with("started_at", GitHubClient.printDate(startedAt)); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| public @NonNull GHCheckRunBuilder withCompletedAt(@CheckForNull Date completedAt) { | ||
| if (completedAt != null) { | ||
| requester.with("completed_at", GitHubClient.printDate(completedAt)); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| public @NonNull GHCheckRunBuilder add(@NonNull Output output) { | ||
| if (this.output != null) { | ||
| throw new IllegalStateException("cannot add Output twice"); | ||
| } | ||
| this.output = output; | ||
| return this; | ||
| } | ||
|
|
||
| public @NonNull GHCheckRunBuilder add(@NonNull Action action) { | ||
| if (actions == null) { | ||
| actions = new LinkedList<>(); | ||
| } | ||
| actions.add(action); | ||
| return this; | ||
| } | ||
|
|
||
| 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<Annotation> extraAnnotations; | ||
| if (output != null && output.annotations != null && output.annotations.size() > MAX_ANNOTATIONS) { | ||
| extraAnnotations = output.annotations.subList(MAX_ANNOTATIONS, output.annotations.size()); | ||
| output.annotations = output.annotations.subList(0, MAX_ANNOTATIONS); | ||
| } else { | ||
| extraAnnotations = Collections.emptyList(); | ||
| } | ||
| GHCheckRun run = requester.with("output", output).with("actions", actions).fetch(GHCheckRun.class).wrap(repo); | ||
| while (!extraAnnotations.isEmpty()) { | ||
| Output output2 = new Output(output.title, output.summary); | ||
| int i = Math.min(extraAnnotations.size(), MAX_ANNOTATIONS); | ||
| output2.annotations = extraAnnotations.subList(0, i); | ||
| extraAnnotations = extraAnnotations.subList(i, extraAnnotations.size()); | ||
| run = repo.root.createRequest() | ||
| .withPreview(Previews.ANTIOPE) | ||
| .method("PATCH") | ||
| .with("output", output2) | ||
| .withUrlPath(repo.getApiTailUrl("check-runs/" + run.id)) | ||
| .fetch(GHCheckRun.class) | ||
| .wrap(repo); | ||
| } | ||
| return run; | ||
| } | ||
|
|
||
| /** | ||
| * @see <a href="https://developer.github.com/v3/checks/runs/#output-object">documentation</a> | ||
| */ | ||
| @JsonInclude(JsonInclude.Include.NON_NULL) | ||
| public static final class Output { | ||
|
|
||
| private final String title; | ||
| private final String summary; | ||
| private String text; | ||
| private List<Annotation> annotations; | ||
| private List<Image> images; | ||
|
|
||
| public Output(@NonNull String title, @NonNull String summary) { | ||
| this.title = title; | ||
| this.summary = summary; | ||
| } | ||
|
|
||
| public @NonNull Output withText(@CheckForNull String text) { | ||
| this.text = text; | ||
| return this; | ||
| } | ||
|
|
||
| public @NonNull Output add(@NonNull Annotation annotation) { | ||
| if (annotations == null) { | ||
| annotations = new LinkedList<>(); | ||
| } | ||
| annotations.add(annotation); | ||
| return this; | ||
| } | ||
|
|
||
| public @NonNull Output add(@NonNull Image image) { | ||
| if (images == null) { | ||
| images = new LinkedList<>(); | ||
| } | ||
| images.add(image); | ||
| return this; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * @see <a href="https://developer.github.com/v3/checks/runs/#annotations-object">documentation</a> | ||
| */ | ||
| @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) { | ||
| this(path, line, line, annotationLevel, message); | ||
| } | ||
|
|
||
| public Annotation(@NonNull String path, | ||
| int startLine, | ||
| int endLine, | ||
| @NonNull GHCheckRun.AnnotationLevel annotationLevel, | ||
| @NonNull String message) { | ||
| this.path = path; | ||
| start_line = startLine; | ||
| end_line = endLine; | ||
| annotation_level = annotationLevel.toString().toLowerCase(Locale.ROOT); | ||
| this.message = message; | ||
| } | ||
|
|
||
| public @NonNull Annotation withStartColumn(@CheckForNull Integer startColumn) { | ||
| start_column = startColumn; | ||
| return this; | ||
| } | ||
|
|
||
| public @NonNull Annotation withEndColumn(@CheckForNull Integer endColumn) { | ||
| end_column = endColumn; | ||
| return this; | ||
| } | ||
|
|
||
| public @NonNull Annotation withTitle(@CheckForNull String title) { | ||
| this.title = title; | ||
| return this; | ||
| } | ||
|
|
||
| public @NonNull Annotation withRawDetails(@CheckForNull String rawDetails) { | ||
| raw_details = rawDetails; | ||
| return this; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * @see <a href="https://developer.github.com/v3/checks/runs/#images-object">documentation</a> | ||
| */ | ||
| @JsonInclude(JsonInclude.Include.NON_NULL) | ||
| public static final class Image { | ||
|
|
||
| private final String alt; | ||
| private final String image_url; | ||
| private String caption; | ||
|
|
||
| public Image(@NonNull String alt, @NonNull String imageURL) { | ||
| this.alt = alt; | ||
| image_url = imageURL; | ||
| } | ||
|
|
||
| public @NonNull Image withCaption(@CheckForNull String caption) { | ||
| this.caption = caption; | ||
| return this; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * @see <a href="https://developer.github.com/v3/checks/runs/#actions-object">documentation</a> | ||
| */ | ||
| @JsonInclude(JsonInclude.Include.NON_NULL) | ||
| public static final class Action { | ||
jglick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private final String label; | ||
| private final String description; | ||
| private final String identifier; | ||
|
|
||
| public Action(@NonNull String label, @NonNull String description, @NonNull String identifier) { | ||
| this.label = label; | ||
| this.description = description; | ||
| this.identifier = identifier; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.