-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathGitHubChecksContext.java
More file actions
162 lines (134 loc) · 4.8 KB
/
GitHubChecksContext.java
File metadata and controls
162 lines (134 loc) · 4.8 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
153
154
155
156
157
158
159
160
161
162
package io.jenkins.plugins.checks.github;
import java.util.Optional;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import hudson.EnvVars;
import hudson.model.TaskListener;
import org.apache.commons.lang3.StringUtils;
import edu.hm.hafner.util.FilteredLog;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.model.Job;
import hudson.model.Run;
/**
* Base class for a context that publishes GitHub checks.
*/
public abstract class GitHubChecksContext {
private final Job<?, ?> job;
private final String url;
private final SCMFacade scmFacade;
protected GitHubChecksContext(final Job<?, ?> job, final String url, final SCMFacade scmFacade) {
this.job = job;
this.url = url;
this.scmFacade = scmFacade;
}
/**
* Returns the commit sha of the run.
*
* @return the commit sha of the run
*/
public abstract String getHeadSha();
/**
* Returns the source repository's full name of the run. The full name consists of the owner's name and the
* repository's name, e.g. jenkins-ci/jenkins
*
* @return the source repository's full name
*/
public abstract String getRepository();
/**
* Returns whether the context is valid (with all properties functional) to use.
*
* @param logger
* the filtered logger
* @return whether the context is valid to use
*/
public abstract boolean isValid(FilteredLog logger);
@CheckForNull
protected abstract String getCredentialsId();
/**
* Returns the credentials to access the remote GitHub repository.
*
* @return the credentials
*/
public StandardUsernameCredentials getCredentials() {
return getGitHubAppCredentials(StringUtils.defaultIfEmpty(getCredentialsId(), ""));
}
/**
* Returns the URL of the run's summary page, e.g. https://ci.jenkins.io/job/Core/job/jenkins/job/master/2000/.
*
* @return the URL of the summary page
*/
public String getURL() {
return url;
}
protected Job<?, ?> getJob() {
return job;
}
protected final SCMFacade getScmFacade() {
return scmFacade;
}
protected StandardUsernameCredentials getGitHubAppCredentials(final String credentialsId) {
return findGitHubAppCredentials(credentialsId).orElseThrow(() ->
new IllegalStateException("No GitHub APP credentials available for job: " + getJob().getName()));
}
protected boolean hasGitHubAppCredentials() {
return findGitHubAppCredentials(StringUtils.defaultIfEmpty(getCredentialsId(), "")).isPresent();
}
protected boolean hasCredentialsId() {
return StringUtils.isNoneBlank(getCredentialsId());
}
protected boolean hasValidCredentials(final FilteredLog logger) {
if (!hasCredentialsId()) {
logger.logError("No credentials found");
return false;
}
if (!hasGitHubAppCredentials()) {
logger.logError("No GitHub app credentials found: '%s'", getCredentialsId());
logger.logError("See: https://github.com/jenkinsci/github-branch-source-plugin/blob/master/docs/github-app.adoc");
return false;
}
return true;
}
private Optional<StandardUsernameCredentials> findGitHubAppCredentials(final String credentialsId) {
return getScmFacade().findGitHubAppCredentials(getJob(), credentialsId);
}
/**
* Returns the id of a {@link GitHubChecksAction} for this run, if any.
*
* @param name
* the name of the check
* @return the id of the check run
*/
public Optional<Long> getId(final String name) {
return getAction(name).map(GitHubChecksAction::getId);
}
protected abstract Optional<Run<?, ?>> getRun();
private Optional<GitHubChecksAction> getAction(final String name) {
if (getRun().isEmpty()) {
return Optional.empty();
}
return getRun().get().getActions(GitHubChecksAction.class)
.stream()
.filter(a -> a.getName().equals(name))
.findFirst();
}
void addActionIfMissing(final long id, final String name) {
if (getRun().isEmpty()) {
return;
}
Optional<GitHubChecksAction> action = getAction(name);
if (action.isEmpty()) {
getRun().get().addAction(new GitHubChecksAction(id, name));
}
}
EnvVars getEnvironmentVariables() {
if (getRun().isEmpty()) {
return new EnvVars();
}
try {
return getRun().get().getEnvironment(TaskListener.NULL);
}
catch (Exception e) {
// If we cannot get the environment variables, we return an empty EnvVars object
return new EnvVars();
}
}
}