-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathGiteaChecksDetails.java
More file actions
152 lines (137 loc) · 5.04 KB
/
GiteaChecksDetails.java
File metadata and controls
152 lines (137 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package io.jenkins.plugins.checks.gitea;
import io.jenkins.plugins.checks.api.ChecksConclusion;
import io.jenkins.plugins.checks.api.ChecksDetails;
import io.jenkins.plugins.checks.api.ChecksStatus;
import java.net.URI;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import org.jenkinsci.plugin.gitea.client.api.GiteaCommitState;
/**
* An adaptor which adapts the generic checks objects of {@link ChecksDetails}
* to the specific Gitea checks run.
*/
class GiteaChecksDetails {
private final ChecksDetails details;
private static final int GITEA_MAX_DESCRIPTION_SIZE = 256;
/**
* Construct with the given {@link ChecksDetails}.
*
* @param details the details of a generic check run
*/
GiteaChecksDetails(final ChecksDetails details) {
if (details.getConclusion() == ChecksConclusion.NONE) {
if (details.getStatus() == ChecksStatus.COMPLETED) {
throw new IllegalArgumentException("No conclusion has been set when status is completed.");
}
if (details.getCompletedAt().isPresent()) {
throw new IllegalArgumentException("No conclusion has been set when \"completedAt\" is provided.");
}
}
this.details = details;
}
public String getName() {
return details.getName()
.filter(StringUtils::isNotBlank)
.orElseThrow(() -> new IllegalArgumentException("The check name is blank."));
}
/**
* Returns the name of a Gitea commit status. This is displayed on a PR page on
* gitea, together with
* the description.
*
* @return the name of the check
*/
public String getContextString() {
return getName();
}
/**
* Returns the {@link GiteaCommitState} of a Gitea check run.
*
* @return the status of a check run
* @throws IllegalArgumentException if the status of the {@code details} is not
* one of {@link ChecksStatus}
*/
@SuppressWarnings({"PMD.CyclomaticComplexity","PMD.ExhaustiveSwitchHasDefault"})
public GiteaCommitState getStatus() {
switch (details.getStatus()) {
case NONE:
case QUEUED:
case IN_PROGRESS:
return GiteaCommitState.PENDING;
case COMPLETED:
switch (details.getConclusion()) {
case NEUTRAL:
case SKIPPED:
case SUCCESS:
return GiteaCommitState.SUCCESS;
case ACTION_REQUIRED:
return GiteaCommitState.WARNING;
case CANCELED:
case FAILURE:
return GiteaCommitState.FAILURE;
case TIME_OUT:
return GiteaCommitState.ERROR;
default:
throw new IllegalArgumentException("Unsupported checks status: " + details.getStatus());
}
// fallthrough - Can't happen happen
default:
throw new IllegalArgumentException("Unsupported checks status: " + details.getStatus());
}
}
/**
* Returns the URL of site which contains details of a Gitea check run.
*
* @return an URL of the site
*/
public Optional<String> getDetailsURL() {
if (details.getDetailsURL().filter(StringUtils::isBlank).isPresent()) {
return Optional.empty();
}
details.getDetailsURL().ifPresent(url -> {
if (!StringUtils.equalsAny(URI.create(url).getScheme(), "http", "https")) {
throw new IllegalArgumentException("The details url is not http or https scheme: " + url);
}
});
return details.getDetailsURL();
}
/**
* Returns the description {@link String} of a Gitea check run.
*
* @return the output of a check run
*/
public Optional<String> getDescription() {
if (details.getOutput().isPresent()) {
return details.getOutput().get().getSummary(GITEA_MAX_DESCRIPTION_SIZE);
}
return Optional.empty();
}
/**
* Returns the UTC time when the check started.
*
* @deprecated This is being deprecated since 1.0
* @return the start time of a check
*/
@Deprecated
public Optional<Date> getStartedAt() {
if (details.getStartedAt().isPresent()) {
return Optional.of(Date.from(details.getStartedAt().get().toInstant(ZoneOffset.UTC)));
}
return Optional.empty();
}
/**
* Returns the UTC time when the check completed.
*
* @deprecated This is being deprecated since 1.0
* @return the completed time of a check
*/
@Deprecated
public Optional<Date> getCompletedAt() {
if (details.getCompletedAt().isPresent()) {
return Optional.of(Date.from(details.getCompletedAt().get().toInstant(ZoneOffset.UTC)));
}
return Optional.empty();
}
}