Skip to content

Commit

Permalink
Merge pull request #23 from XiongKezhi/use-optional
Browse files Browse the repository at this point in the history
An experiment to use optional for parameters
  • Loading branch information
XiongKezhi authored Jul 2, 2020
2 parents 557de3d + 58d74e2 commit c89df50
Show file tree
Hide file tree
Showing 19 changed files with 659 additions and 512 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/jenkins/plugins/checks/ContextResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import jenkins.scm.api.SCMSource;

public class ContextResolver {
// TODO: Refactor to avoid returning null
@CheckForNull
public SCMSource resolveSource(final Run<?, ?> run) {
return SCMSource.SourceByItem.findSource(run.getParent());
Expand All @@ -22,8 +23,7 @@ public String resolveHeadSha(final SCMSource source, final Run<?, ?> run) {
try {
return resolveHeadSha(source.fetch(head, null));
} catch (IOException | InterruptedException e) {
throw new IllegalStateException(String.format("Could not resolve head sha, source: %s, run: %s",
source, run));
throw new IllegalStateException("Could not resolve head sha: ", e);
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/main/java/io/jenkins/plugins/checks/JobListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public class JobListener extends RunListener<Run<?, ?>> {
@Override
public void onInitialize(final Run run) {
ChecksPublisher publisher = ChecksPublisherFactory.fromRun(run);
publisher.publish(new ChecksDetailsBuilder(CHECKS_NAME, ChecksStatus.QUEUED).build());
publisher.publish(new ChecksDetailsBuilder()
.withName(CHECKS_NAME)
.withStatus(ChecksStatus.QUEUED)
.build());
}

/**
Expand All @@ -37,7 +40,10 @@ public void onInitialize(final Run run) {
@Override
public void onStarted(final Run run, final TaskListener listener) {
ChecksPublisher publisher = ChecksPublisherFactory.fromRun(run);
publisher.publish(new ChecksDetailsBuilder(CHECKS_NAME, ChecksStatus.IN_PROGRESS).build());
publisher.publish(new ChecksDetailsBuilder()
.withName(CHECKS_NAME)
.withStatus(ChecksStatus.IN_PROGRESS)
.build());
}

/**
Expand All @@ -49,7 +55,9 @@ public void onStarted(final Run run, final TaskListener listener) {
public void onCompleted(final Run run, @NonNull final TaskListener listener) {
ChecksPublisher publisher = ChecksPublisherFactory.fromRun(run);
// TODO: extract result from run
publisher.publish(new ChecksDetailsBuilder(CHECKS_NAME, ChecksStatus.COMPLETED)
publisher.publish(new ChecksDetailsBuilder()
.withName(CHECKS_NAME)
.withStatus(ChecksStatus.COMPLETED)
.withConclusion(ChecksConclusion.SUCCESS)
.build());
}
Expand Down
35 changes: 14 additions & 21 deletions src/main/java/io/jenkins/plugins/checks/api/ChecksAction.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.jenkins.plugins.checks.api;

import static java.util.Objects.*;
import java.util.Optional;

import edu.umd.cs.findbugs.annotations.Nullable;

public class ChecksAction {
private final String label;
Expand All @@ -22,31 +24,22 @@ public class ChecksAction {
* @param identifier
* a reference for the action on the integrator's system
*/
public ChecksAction(final String label, final String description, final String identifier) {
this.label = requireNonNull(label);
this.description = requireNonNull(description);
this.identifier = requireNonNull(identifier);
}

/**
* Copy constructor of the {@link ChecksOutput}.
*
* @param that
* the source to copy from
*/
public ChecksAction(final ChecksAction that) {
this(that.getLabel(), that.getDescription(), that.getIdentifier());
public ChecksAction(@Nullable final String label, @Nullable final String description,
@Nullable final String identifier) {
this.label = label;
this.description = description;
this.identifier = identifier;
}

public String getLabel() {
return label;
public Optional<String> getLabel() {
return Optional.ofNullable(label);
}

public String getDescription() {
return description;
public Optional<String> getDescription() {
return Optional.ofNullable(description);
}

public String getIdentifier() {
return identifier;
public Optional<String> getIdentifier() {
return Optional.ofNullable(identifier);
}
}
172 changes: 102 additions & 70 deletions src/main/java/io/jenkins/plugins/checks/api/ChecksAnnotation.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package io.jenkins.plugins.checks.api;

import java.util.Optional;

import static java.util.Objects.*;

/**
* The annotation for specific lines of code.
*/
public class ChecksAnnotation {
private final String path;
private final int startLine;
private final int endLine;
private final Integer startLine;
private final Integer endLine;
private final ChecksAnnotationLevel annotationLevel;
private final String message;
private final Integer startColumn;
Expand All @@ -17,7 +19,7 @@ public class ChecksAnnotation {
private final String rawDetails;

private ChecksAnnotation(final String path,
final int startLine, final int endLine,
final Integer startLine, final Integer endLine,
final ChecksAnnotationLevel annotationLevel,
final String message,
final Integer startColumn, final Integer endColumn,
Expand All @@ -41,47 +43,50 @@ private ChecksAnnotation(final String path,
* the source
*/
public ChecksAnnotation(final ChecksAnnotation that) {
this(that.getPath(), that.getStartLine(), that.getEndLine(), that.getAnnotationLevel(), that.getMessage(),
that.getStartColumn(), that.getEndColumn(), that.getTitle(), that.getRawDetails());
this(that.getPath().orElse(null), that.getStartLine().orElse(null), that.getEndLine().orElse(null),
that.getAnnotationLevel(), that.getMessage().orElse(null), that.getStartColumn().orElse(null),
that.getEndColumn().orElse(null), that.getTitle().orElse(null),
that.getRawDetails().orElse(null));
}

public String getPath() {
return path;
public Optional<String> getPath() {
return Optional.ofNullable(path);
}

public int getStartLine() {
return startLine;
public Optional<Integer> getStartLine() {
return Optional.ofNullable(startLine);
}

public int getEndLine() {
return endLine;
public Optional<Integer> getEndLine() {
return Optional.ofNullable(endLine);
}

public ChecksAnnotationLevel getAnnotationLevel() {
return annotationLevel;
}

public String getMessage() {
return message;
public Optional<String> getMessage() {
return Optional.ofNullable(message);
}

public Integer getStartColumn() {
return startColumn;
public Optional<Integer> getStartColumn() {
return Optional.ofNullable(startColumn);
}

public Integer getEndColumn() {
return endColumn;
public Optional<Integer> getEndColumn() {
return Optional.ofNullable(endColumn);
}

public String getTitle() {
return title;
public Optional<String> getTitle() {
return Optional.ofNullable(title);
}

public String getRawDetails() {
return rawDetails;
public Optional<String> getRawDetails() {
return Optional.ofNullable(rawDetails);
}

public enum ChecksAnnotationLevel {
NONE,
NOTICE,
WARNING,
FAILURE
Expand All @@ -91,76 +96,107 @@ public enum ChecksAnnotationLevel {
* Builder for {@link ChecksAnnotation}.
*/
public static class ChecksAnnotationBuilder {
private final String path;
private final int startLine;
private final int endLine;
private final ChecksAnnotationLevel annotationLevel;
private final String message;
private String path;
private Integer startLine;
private Integer endLine;
private ChecksAnnotationLevel annotationLevel;
private String message;
private Integer startColumn;
private Integer endColumn;
private String title;
private String rawDetails;

/**
* Constructs a builder with required parameters for a {@link ChecksAnnotation}.
* Constructs a builder for {@link ChecksAnnotation}.
*/
public ChecksAnnotationBuilder() {
this.annotationLevel = ChecksAnnotationLevel.NONE;
}

/**
* Sets the path of the file to annotate.
*
* @param path
* the relative path of the file to annotation, e.g. assets/css/main.css
* the relative path of the file to annotation,
* e.g. src/main/java/io/jenkins/plugins/checks/api/ChecksAnnotation.java
* @return this builder
*/
public ChecksAnnotationBuilder withPath(final String path) {
this.path = requireNonNull(path);
return this;
}

/**
* Sets the line of the single line annotation.
*
* @param line
* the line of code to annotate
* @return this builder
*/
public ChecksAnnotationBuilder withLine(final int line) {
withStartLine(line);
withEndLine(line);
return this;
}

/**
* Sets the start line of annotation
*
* @param startLine
* the start line of the annotation
* the start line of code to annotate
* @return this builder
*/
public ChecksAnnotationBuilder withStartLine(final Integer startLine) {
this.startLine = requireNonNull(startLine);
return this;
}

/**
* Sets the end line of annotation.
*
* @param endLine
* the end line of the annotation
* @param annotationLevel
* the level of the annotation, can be one of {@link ChecksAnnotationLevel#NOTICE},
* {@link ChecksAnnotationLevel#WARNING}, {@link ChecksAnnotationLevel#FAILURE}
* @param message
* a short description of the feedback for the annotated code
* the end line of code to annotate
* @return this builder
*/
public ChecksAnnotationBuilder(final String path, final int startLine, final int endLine,
final ChecksAnnotationLevel annotationLevel, final String message) {
this.path = requireNonNull(path);
this.startLine = startLine;
this.endLine = endLine;
this.annotationLevel = requireNonNull(annotationLevel);
this.message = requireNonNull(message);
public ChecksAnnotationBuilder withEndLine(final Integer endLine) {
this.endLine = requireNonNull(endLine);
return this;
}

/**
* Constructs a builder with required parameters for a {@link ChecksAnnotation}.
* Sets the annotation level, one of {@code NOTICE}, {@code WARNING}, or {@code FAILURE}.
* The default is {@code WARNING}.
*
* <p>
* Note that for a GitHub check run annotation, the {@code message} must not exceed 64 KB.
* </p>
* @param level
* the annotation level
* @return this builder
*/
public ChecksAnnotationBuilder withAnnotationLevel(final ChecksAnnotationLevel level) {
this.annotationLevel = requireNonNull(level);
return this;
}

/**
* Sets a short description of the feedback for the annotation.
*
* @param path
* the relative path of the file to annotation, e.g. assets/css/main.css
* @param line
* the line of the code to annotate
* @param annotationLevel
* the level of the annotation, can be one of {@link ChecksAnnotationLevel#NOTICE},
* {@link ChecksAnnotationLevel#WARNING}, {@link ChecksAnnotationLevel#FAILURE}
* @param message
* a short description of the feedback for the annotated code
* a short description
* @return this builder
*/
public ChecksAnnotationBuilder(final String path, final int line, final ChecksAnnotationLevel annotationLevel,
final String message) {
this(path, line, line, annotationLevel, message);
public ChecksAnnotationBuilder withMessage(final String message) {
this.message = requireNonNull(message);
return this;
}

/**
* Adds start column of the annotation.
* TODO: determine how GitHub behaves when the start and end column are not provided at the same time
*
* @param startColumn
* the start column of the annotation
* @return this builder
*/
public ChecksAnnotationBuilder withStartColumn(final int startColumn) {
if (startLine != endLine) {
throw new IllegalArgumentException(String.format("startLine and endLine attributes must be the same "
+ "when adding column, start line: %d, end line: %d", startLine, endLine));
}
this.startColumn = startColumn;
public ChecksAnnotationBuilder withStartColumn(final Integer startColumn) {
this.startColumn = requireNonNull(startColumn);
return this;
}

Expand All @@ -171,12 +207,8 @@ public ChecksAnnotationBuilder withStartColumn(final int startColumn) {
* the end column of the annotation
* @return this builder
*/
public ChecksAnnotationBuilder withEndColumn(final int endColumn) {
if (startLine != endLine) {
throw new IllegalArgumentException(String.format("startLine and endLine attributes must be the same "
+ "when adding column, start line: %d, end line: %d", startLine, endLine));
}
this.endColumn = endColumn;
public ChecksAnnotationBuilder withEndColumn(final Integer endColumn) {
this.endColumn = requireNonNull(endColumn);
return this;
}

Expand Down
Loading

0 comments on commit c89df50

Please sign in to comment.