-
-
Notifications
You must be signed in to change notification settings - Fork 469
/
Job.java
188 lines (161 loc) · 6.11 KB
/
Job.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
/*
* Copyright (c) 2013 Cosmin Stejerean, Karl Heinz Marbaise, and contributors.
*
* Distributed under the MIT license: http://opensource.org/licenses/MIT
*/
package com.offbytwo.jenkins.model;
import com.offbytwo.jenkins.client.util.EncodingUtils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Job extends BaseModel {
private String name;
private String url;
private String fullName;
public Job() {
}
public Job(String name, String url) {
this();
this.name = name;
this.url = url;
this.fullName = null;
}
public Job(String name, String url, String fullName) {
this();
this.name = name;
this.url = url;
this.fullName = fullName;
}
public String getName() {
return name;
}
public String getUrl() {
return url;
}
public String getFullName() {
return fullName;
}
public JobWithDetails details() throws IOException {
return client.get(url, JobWithDetails.class);
}
/**
* Get a file from workspace.
*
* @param fileName The name of the file to download from workspace. You can
* also access files which are in sub folders of the workspace.
* @return The string which contains the content of the file.
* @throws IOException in case of an error.
*/
public String getFileFromWorkspace(String fileName) throws IOException {
InputStream is = client.getFile(URI.create(url + "/ws/" + fileName));
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toString("UTF-8");
}
/**
* Trigger a build without parameters
*
* @return {@link QueueReference} for further analysis of the queued build.
* @throws IOException in case of an error.
*/
public QueueReference build() throws IOException {
ExtractHeader location = client.post(url + "build", null, ExtractHeader.class, false);
return new QueueReference(location.getLocation());
}
/**
* Trigger a build with crumbFlag.
*
* @param crumbFlag true or false.
* @return {@link QueueReference} for further analysis of the queued build.
* @throws IOException in case of an error.
*/
public QueueReference build(boolean crumbFlag) throws IOException {
ExtractHeader location = client.post(url + "build", null, ExtractHeader.class, crumbFlag);
return new QueueReference(location.getLocation());
}
/**
* Trigger a parameterized build with string parameters only
*
* @param params the job parameters
* @return {@link QueueReference} for further analysis of the queued build.
* @throws IOException in case of an error.
*/
public QueueReference build(Map<String, String> params) throws IOException {
return build(params, null,false);
}
/**
* Trigger a parameterized build with string parameters only
*
* @param params the job parameters
* @param crumbFlag true or false.
* @return {@link QueueReference} for further analysis of the queued build.
* @throws IOException in case of an error.
*/
public QueueReference build(Map<String, String> params, boolean crumbFlag) throws IOException {
return build(params,null,crumbFlag);
}
/**
* Trigger a parameterized build with file parameters
*
* @param params the job parameters
* @param fileParams the job file parameters
* @return {@link QueueReference} for further analysis of the queued build.
* @throws IOException in case of an error.
*/
public QueueReference build(Map<String, String> params, Map<String, File> fileParams) throws IOException {
return build(params,fileParams,false);
}
/**
* Trigger a parameterized build with file parameters and crumbFlag
*
* @param params the job parameters
* @param fileParams the job file parameters
* @param crumbFlag determines whether crumb flag is used
* @return {@link QueueReference} for further analysis of the queued build.
* @throws IOException in case of an error.
*/
public QueueReference build(Map<String, String> params, Map<String, File> fileParams, boolean crumbFlag) throws IOException {
String qs = params.entrySet().stream()
.map(s -> s.getKey() + "=" + s.getValue())
.collect(Collectors.joining("&"));
// String qs = join(Collections2.transform(params.entrySet(), new MapEntryToQueryStringPair()), "&");
ExtractHeader location = client.post(url + "buildWithParameters?" + qs,null, ExtractHeader.class, fileParams, crumbFlag);
return new QueueReference(location.getLocation());
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Job job = (Job) o;
if (name != null ? !name.equals(job.name) : job.name != null)
return false;
if (url != null ? !url.equals(job.url) : job.url != null)
return false;
if (fullName != null ? !fullName.equals(job.fullName) : job.fullName != null)
return false;
return true;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (url != null ? url.hashCode() : 0) + (fullName != null ? fullName.hashCode() : 0);
return result;
}
private static class MapEntryToQueryStringPair implements Function<Map.Entry<String, String>, String> {
@Override
public String apply(Map.Entry<String, String> entry) {
return EncodingUtils.formParameter(entry.getKey()) + "=" + EncodingUtils.formParameter(entry.getValue());
}
}
}