-
Notifications
You must be signed in to change notification settings - Fork 36
/
GitSCMChecksContext.java
142 lines (115 loc) · 4.43 KB
/
GitSCMChecksContext.java
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
package io.jenkins.plugins.checks.github;
import edu.hm.hafner.util.FilteredLog;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.Revision;
import hudson.plugins.git.UserRemoteConfig;
import hudson.plugins.git.util.BuildData;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.util.Optional;
/**
* Provides a {@link GitHubChecksContext} for a Jenkins job that uses a supported {@link GitSCM}.
*/
class GitSCMChecksContext extends GitHubChecksContext {
private static final String GIT_PROTOCOL = "git@github.com:";
private static final String HTTPS_PROTOCOL = "https://github.com/";
private final Run<?, ?> run;
/**
* Creates a {@link GitSCMChecksContext} according to the run. All attributes are computed during this period.
*
* @param run a run of a GitHub Branch Source project
* @param runURL the URL to the Jenkins run
*/
GitSCMChecksContext(final Run<?, ?> run, final String runURL) {
this(run, runURL, new SCMFacade());
}
GitSCMChecksContext(final Run<?, ?> run, final String runURL, final SCMFacade scmFacade) {
super(run.getParent(), runURL, scmFacade);
this.run = run;
}
@Override
public String getHeadSha() {
try {
String head = getGitCommitEnvironment();
if (StringUtils.isNotBlank(head)) {
return head;
}
return getLastBuiltRevisionFromBuildData();
}
catch (IOException | InterruptedException e) {
// ignore and return a default
}
return StringUtils.EMPTY;
}
public String getGitCommitEnvironment() throws IOException, InterruptedException {
return StringUtils.defaultString(run.getEnvironment(TaskListener.NULL).get("GIT_COMMIT"));
}
private String getLastBuiltRevisionFromBuildData() {
BuildData gitBuildData = run.getAction(BuildData.class);
if (gitBuildData != null) {
Revision lastBuiltRevision = gitBuildData.getLastBuiltRevision();
if (lastBuiltRevision != null) {
return lastBuiltRevision.getSha1().getName();
}
}
return StringUtils.EMPTY;
}
// TODO: check which other kind of repository strings are valid
@Override
public String getRepository() {
return StringUtils.removeEnd(removeProtocol(getRemoteUrl()), ".git");
}
@CheckForNull
private String getRemoteUrl() {
return getUserRemoteConfig().getUrl();
}
private String removeProtocol(final String url) {
return StringUtils.removeStart(StringUtils.removeStart(url, GIT_PROTOCOL), HTTPS_PROTOCOL);
}
@Override
@CheckForNull
protected String getCredentialsId() {
return getUserRemoteConfig().getCredentialsId();
}
private UserRemoteConfig getUserRemoteConfig() {
return getScmFacade().getUserRemoteConfig(resolveGitSCM());
}
private GitSCM resolveGitSCM() {
Optional<GitSCM> gitSCM = getScmFacade().findGitSCM(run);
if (gitSCM.isPresent()) {
return gitSCM.get();
}
throw new IllegalStateException(
"Skipped publishing GitHub checks: no Git SCM source available for job: " + getJob().getName());
}
@Override
public boolean isValid(final FilteredLog logger) {
logger.logError("Trying to resolve checks parameters from Git SCM...");
if (!getScmFacade().findGitSCM(run).isPresent()) {
logger.logError("Job does not use Git SCM");
return false;
}
String remoteUrl = getRemoteUrl();
if (!isValidUrl(remoteUrl)) {
logger.logError("No supported GitSCM repository URL: " + remoteUrl);
return false;
}
if (!hasValidCredentials(logger)) {
return false;
}
String repository = getRepository();
if (getHeadSha().isEmpty()) {
logger.logError("No HEAD SHA found for '%s'", repository);
return false;
}
logger.logInfo("Using GitSCM repository '%s' for GitHub checks", repository);
return true;
}
private boolean isValidUrl(@CheckForNull final String remoteUrl) {
return StringUtils.startsWith(remoteUrl, GIT_PROTOCOL)
|| StringUtils.startsWith(remoteUrl, HTTPS_PROTOCOL);
}
}