-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDeploygateRecorder.java
More file actions
277 lines (222 loc) · 6.92 KB
/
DeploygateRecorder.java
File metadata and controls
277 lines (222 loc) · 6.92 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package deploygate;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import hudson.util.RunList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.io.IOException;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.io.FileUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
public class DeploygateRecorder extends Recorder {
private String userName;
public String getUserName() {
return userName;
}
private String apiToken;
public String getApiToken() {
return this.apiToken;
}
private String buildNotes;
public String getBuildNotes() {
return this.buildNotes;
}
private String releaseNotes;
public String getReleaseNotes() {
return this.releaseNotes;
}
private String releaseNotesFilePath;
public String getReleaseNotesFilePath() {
return this.releaseNotesFilePath;
}
private String filePath;
public String getFilePath() {
return this.filePath;
}
private String distributionKey;
public String getDistributionKey() {
return distributionKey;
}
private String proxyHost;
public String getProxyHost() {
return proxyHost;
}
private String proxyUser;
public String getProxyUser() {
return proxyUser;
}
private String proxyPass;
public String getProxyPass() {
return proxyPass;
}
private int proxyPort;
public int getProxyPort() {
return proxyPort;
}
@DataBoundConstructor
public DeploygateRecorder(String apiToken, String userName,
String buildNotes, String releaseNotes, String releaseNotesFilePath,
String filePath, String distributionKey,
String proxyHost,
String proxyUser, String proxyPass, int proxyPort) {
this.apiToken = apiToken;
this.userName = userName;
this.buildNotes = buildNotes;
this.releaseNotes = releaseNotes;
this.releaseNotesFilePath = releaseNotesFilePath;
this.filePath = filePath;
this.distributionKey = distributionKey;
this.proxyHost = proxyHost;
this.proxyUser = proxyUser;
this.proxyPass = proxyPass;
this.proxyPort = proxyPort;
}
@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}
@Override
public boolean perform(AbstractBuild build, Launcher launcher,
final BuildListener listener) {
if (build.getResult().isWorseOrEqualTo(Result.FAILURE))
return false;
listener.getLogger().println("Uploading to deploygate.com");
try {
EnvVars vars = build.getEnvironment(listener);
boolean pathSpecified = filePath != null
&& !filePath.trim().isEmpty();
String expandPath;
if (!pathSpecified)
expandPath = "$WORKSPACE";
else
expandPath = filePath;
DeploygateUploader.UploadRequest ur = createPartialUploadRequest(
vars, expandPath);
DeploygateRemoteRecorder remoteRecorder = new DeploygateRemoteRecorder(
pathSpecified, ur, listener);
final Map parsedMap;
try {
Object result = launcher.getChannel().call(remoteRecorder);
parsedMap = (Map) result;
} catch (UploadException ue) {
listener.getLogger().println(
"Incorrect response code: " + ue.getStatusCode());
listener.getLogger().println(ue.getResponseBody());
return false;
}
DeploygateBuildAction installAction = new DeploygateBuildAction();
installAction.displayName = "Uploaded to DeployGate!";
installAction.iconFileName = "package.gif";
installAction.urlName = "https://www.deploygate.com/";
build.addAction(installAction);
} catch (Throwable e) {
listener.getLogger().println(e);
e.printStackTrace(listener.getLogger());
return false;
}
return true;
}
private DeploygateUploader.UploadRequest createPartialUploadRequest(
EnvVars vars, String expandPath) {
DeploygateUploader.UploadRequest ur = new DeploygateUploader.UploadRequest();
ur.userName = userName;
ur.filePath = vars.expand(expandPath);
ur.apiToken = vars.expand(apiToken);
ur.buildNotes = vars.expand(buildNotes);
String expandedReleaseNotes = vars.expand(releaseNotes);
String expandedReleaseNotesFilePath = vars.expand(releaseNotesFilePath);
if(expandedReleaseNotesFilePath.isEmpty()) ur.releaseNotes = expandedReleaseNotes;
else {
try {
ur.releaseNotes = expandedReleaseNotes
+ FileUtils.readFileToString(
new File(vars.get("WORKSPACE", ""), expandedReleaseNotesFilePath), StandardCharsets.UTF_8);
}
catch (IOException ex) {
ur.releaseNotes = expandedReleaseNotes;
}
}
ur.distributionKey = distributionKey;
ur.proxyHost = proxyHost;
ur.proxyPass = proxyPass;
ur.proxyPort = proxyPort;
ur.proxyUser = proxyUser;
return ur;
}
@Override
public Collection<? extends Action> getProjectActions(
AbstractProject<?, ?> project) {
ArrayList<DeploygateBuildAction> actions = new ArrayList<DeploygateBuildAction>();
RunList<? extends AbstractBuild<?, ?>> builds = project.getBuilds();
Collection predicated = CollectionUtils.select(builds, new Predicate() {
public boolean evaluate(Object o) {
return ((AbstractBuild<?, ?>) o).getResult().isBetterOrEqualTo(
Result.SUCCESS);
}
});
ArrayList<AbstractBuild<?, ?>> filteredList = new ArrayList<AbstractBuild<?, ?>>(
predicated);
Collections.reverse(filteredList);
for (AbstractBuild<?, ?> build : filteredList) {
List<DeploygateBuildAction> testflightActions = build
.getActions(DeploygateBuildAction.class);
if (testflightActions != null && testflightActions.size() > 0) {
for (DeploygateBuildAction action : testflightActions) {
actions.add(new DeploygateBuildAction(action));
}
break;
}
}
return actions;
}
@Extension
// This indicates to Jenkins that this is an implementation of an extension
// point.
public static final class DescriptorImpl extends
BuildStepDescriptor<Publisher> {
public DescriptorImpl() {
super(DeploygateRecorder.class);
load();
}
public boolean isApplicable(Class<? extends AbstractProject> aClass) {
// Indicates that this builder can be used with all kinds of project
// types
return true;
}
@Override
public boolean configure(StaplerRequest req, JSONObject json)
throws FormException {
// XXX is this now the right style?
req.bindJSON(this, json);
save();
return true;
}
/**
* This human readable name is used in the configuration screen.
*/
public String getDisplayName() {
return "Upload to DeployGate";
}
}
}