/
BitbucketBuildStatusNotifications.java
214 lines (198 loc) · 9.15 KB
/
BitbucketBuildStatusNotifications.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* The MIT License
*
* Copyright (c) 2016, 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 com.cloudbees.jenkins.plugins.bitbucket;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketBuildStatus;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.listeners.RunListener;
import hudson.model.listeners.SCMListener;
import hudson.plugins.mercurial.MercurialSCMSource;
import hudson.scm.SCM;
import hudson.scm.SCMRevisionState;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.annotation.CheckForNull;
import jenkins.model.JenkinsLocationConfiguration;
import jenkins.plugins.git.AbstractGitSCMSource;
import jenkins.scm.api.SCMHeadObserver;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMRevisionAction;
import jenkins.scm.api.SCMSource;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;
/**
* This class encapsulates all Bitbucket notifications logic.
* {@link JobCompletedListener} sends a notification to Bitbucket after a build finishes.
* Only builds derived from a job that was created as part of a multi branch project will be processed by this listener.
*/
public class BitbucketBuildStatusNotifications {
private static String getRootURL(@NonNull Run<?, ?> build) {
JenkinsLocationConfiguration cfg = JenkinsLocationConfiguration.get();
if (cfg == null || cfg.getUrl() == null) {
throw new IllegalStateException("Could not determine Jenkins URL.");
}
String url = DisplayURLProvider.get().getRunURL(build);
return checkURL(url);
}
/**
* Check if the build URL is compatible with Bitbucket API.
* For example, Bitbucket API doesn't accept simple hostnames as URLs host value
* Throws an IllegalStateException if it is not valid, or return the url otherwise
*
* @param url the URL of the build to check
* @return the url if it is valid
*/
static String checkURL(@NonNull String url) {
if (url.startsWith("http://unconfigured-jenkins-location/")) {
throw new IllegalStateException("Could not determine Jenkins URL.");
}
try {
URL u = new URL(url);
if (!u.getHost().contains(".")) {
throw new IllegalStateException("Please use a fully qualified name or an IP address for Jenkins URL");
}
} catch (MalformedURLException e) {
throw new IllegalStateException("Bad Jenkins URL");
}
return url;
}
private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener listener,
@NonNull BitbucketApi bitbucket, @NonNull String hash)
throws IOException, InterruptedException {
String url;
try {
url = getRootURL(build);
} catch (IllegalStateException e) {
listener.getLogger().println("Can not determine Jenkins root URL " +
"or Jenkins URL is not a valid URL regarding Bitbucket API. " +
"Commit status notifications are disabled until a root URL is " +
"configured in Jenkins global configuration.");
return;
}
String key = build.getParent().getFullName(); // use the job full name as the key for the status
String name = build.getFullDisplayName(); // use the build number as the display name of the status
BitbucketBuildStatus status;
Result result = build.getResult();
String buildDescription = build.getDescription();
String statusDescription;
String state;
if (Result.SUCCESS.equals(result)) {
statusDescription = StringUtils.defaultIfBlank(buildDescription, "This commit looks good.");
state = "SUCCESSFUL";
} else if (Result.UNSTABLE.equals(result)) {
statusDescription = StringUtils.defaultIfBlank(buildDescription, "This commit has test failures.");
state = "FAILED";
} else if (Result.FAILURE.equals(result)) {
statusDescription = StringUtils.defaultIfBlank(buildDescription, "There was a failure building this commit.");
state = "FAILED";
} else if (result != null) { // ABORTED etc.
statusDescription = StringUtils.defaultIfBlank(buildDescription, "Something is wrong with the build of this commit.");
state = "FAILED";
} else {
statusDescription = StringUtils.defaultIfBlank(buildDescription, "The build is in progress...");
state = "INPROGRESS";
}
status = new BitbucketBuildStatus(hash, statusDescription, state, url, key, name);
new BitbucketChangesetCommentNotifier(bitbucket).buildStatus(status);
if (result != null) {
listener.getLogger().println("[Bitbucket] Build result notified");
}
}
private static void sendNotifications(Run<?, ?> build, TaskListener listener)
throws IOException, InterruptedException {
final SCMSource s = SCMSource.SourceByItem.findSource(build.getParent());
if (!(s instanceof BitbucketSCMSource)) {
return;
}
BitbucketSCMSource source = (BitbucketSCMSource) s;
if (new BitbucketSCMSourceContext(null, SCMHeadObserver.none())
.withTraits(source.getTraits())
.notificationsDisabled()) {
return;
}
SCMRevision r = SCMRevisionAction.getRevision(build); // TODO JENKINS-44648 getRevision(s, build)
String hash = getHash(r);
if (hash == null) {
return;
}
if (r instanceof PullRequestSCMRevision) {
listener.getLogger().println("[Bitbucket] Notifying pull request build result");
createStatus(build, listener, source.buildBitbucketClient((PullRequestSCMHead) r.getHead()), hash);
} else {
listener.getLogger().println("[Bitbucket] Notifying commit build result");
createStatus(build, listener, source.buildBitbucketClient(), hash);
}
}
@CheckForNull
private static String getHash(@CheckForNull SCMRevision revision) {
if (revision instanceof PullRequestSCMRevision) {
// unwrap
revision = ((PullRequestSCMRevision) revision).getPull();
}
if (revision instanceof BitbucketSCMSource.MercurialRevision) {
return ((BitbucketSCMSource.MercurialRevision) revision).getHash();
} else if (revision instanceof MercurialSCMSource.MercurialRevision) {
return ((MercurialSCMSource.MercurialRevision) revision).getHash();
} else if (revision instanceof AbstractGitSCMSource.SCMRevisionImpl) {
return ((AbstractGitSCMSource.SCMRevisionImpl) revision).getHash();
}
return null;
}
/**
* Sends notifications to Bitbucket on Checkout (for the "In Progress" Status).
*/
@Extension
public static class JobCheckOutListener extends SCMListener {
@Override
public void onCheckout(Run<?, ?> build, SCM scm, FilePath workspace, TaskListener listener, File changelogFile,
SCMRevisionState pollingBaseline) throws Exception {
try {
sendNotifications(build, listener);
} catch (IOException | InterruptedException e) {
e.printStackTrace(listener.error("Could not send notifications"));
}
}
}
/**
* Sends notifications to Bitbucket on Run completed.
*/
@Extension
public static class JobCompletedListener extends RunListener<Run<?, ?>> {
@Override
public void onCompleted(Run<?, ?> build, TaskListener listener) {
try {
sendNotifications(build, listener);
} catch (IOException | InterruptedException e) {
e.printStackTrace(listener.error("Could not send notifications"));
}
}
}
}