-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathJenkinsClientJobs.cs
More file actions
279 lines (263 loc) · 10.8 KB
/
JenkinsClientJobs.cs
File metadata and controls
279 lines (263 loc) · 10.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
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
278
279
using JenkinsNET.Exceptions;
using JenkinsNET.Internal.Commands;
using JenkinsNET.Models;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace JenkinsNET
{
/// <summary>
/// A collection of methods used for interacting with Jenkins Jobs API.
/// </summary>
/// <remarks>
/// Used internally by <seealso cref="JenkinsClient"/>
/// </remarks>
public sealed class JenkinsClientJobs
{
private readonly IJenkinsContext context;
internal JenkinsClientJobs(IJenkinsContext context)
{
this.context = context;
}
/// <summary>
/// Enqueues a Job to be built.
/// </summary>
/// <param name="jobName">The name of the Job.</param>
/// <exception cref="JenkinsJobBuildException"></exception>
public JenkinsBuildResult Build(string jobName)
{
try {
var cmd = new JobBuildCommand(context, jobName);
cmd.Run();
return cmd.Result;
}
catch (Exception error) {
throw new JenkinsJobBuildException($"Failed to build Jenkins Job '{jobName}'!", error);
}
}
#if NET_ASYNC
/// <summary>
/// Enqueues a Job to be built.
/// </summary>
/// <param name="jobName">The name of the Job.</param>
/// <param name="token">An optional token for aborting the request.</param>
/// <exception cref="JenkinsJobBuildException"></exception>
public async Task<JenkinsBuildResult> BuildAsync(string jobName, CancellationToken token = default)
{
try {
var cmd = new JobBuildCommand(context, jobName);
await cmd.RunAsync(token);
return cmd.Result;
}
catch (Exception error) {
throw new JenkinsJobBuildException($"Failed to build Jenkins Job '{jobName}'!", error);
}
}
#endif
/// <summary>
/// Enqueues a Job with parameters to be built.
/// </summary>
/// <param name="jobName">The name of the Job.</param>
/// <param name="jobParameters">The collection of parameters for building the job.</param>
/// <exception cref="JenkinsJobBuildException"></exception>
public JenkinsBuildResult BuildWithParameters(string jobName, IDictionary<string, string> jobParameters)
{
try {
var cmd = new JobBuildWithParametersCommand(context, jobName, jobParameters);
cmd.Run();
return cmd.Result;
}
catch (Exception error) {
throw new JenkinsJobBuildException($"Failed to build Jenkins Job '{jobName}'!", error);
}
}
#if NET_ASYNC
/// <summary>
/// Enqueues a Job with parameters to be built.
/// </summary>
/// <param name="jobName">The name of the Job.</param>
/// <param name="jobParameters">The collection of parameters for building the job.</param>
/// <param name="token">An optional token for aborting the request.</param>
/// <exception cref="JenkinsJobBuildException"></exception>
public async Task<JenkinsBuildResult> BuildWithParametersAsync(string jobName, IDictionary<string, string> jobParameters, CancellationToken token = default)
{
try {
var cmd = new JobBuildWithParametersCommand(context, jobName, jobParameters);
await cmd.RunAsync(token);
return cmd.Result;
}
catch (Exception error) {
throw new JenkinsJobBuildException($"Failed to build Jenkins Job '{jobName}'!", error);
}
}
#endif
/// <summary>
/// Gets a Job description from Jenkins.
/// </summary>
/// <param name="jobName">The Name of the Job to retrieve.</param>
/// <exception cref="JenkinsNetException"></exception>
public T Get<T>(string jobName) where T : class, IJenkinsJob
{
try {
var cmd = new JobGetCommand<T>(context, jobName);
cmd.Run();
return cmd.Result;
}
catch (Exception error) {
throw new JenkinsNetException($"Failed to get Jenkins Job '{jobName}'!", error);
}
}
#if NET_ASYNC
/// <summary>
/// Gets a Job description from Jenkins asynchronously.
/// </summary>
/// <param name="jobName">The Name of the Job to retrieve.</param>
/// <param name="token">An optional token for aborting the request.</param>
/// <exception cref="JenkinsNetException"></exception>
public async Task<T> GetAsync<T>(string jobName, CancellationToken token = default) where T : class, IJenkinsJob
{
try {
var cmd = new JobGetCommand<T>(context, jobName);
await cmd.RunAsync(token);
return cmd.Result;
}
catch (Exception error) {
throw new JenkinsNetException($"Failed to get Jenkins Job '{jobName}'!", error);
}
}
#endif
/// <summary>
/// Gets a Job configuration from Jenkins.
/// </summary>
/// <param name="jobName">The Name of the Job to retrieve.</param>
/// <exception cref="JenkinsNetException"></exception>
public JenkinsProject GetConfiguration(string jobName)
{
try {
var cmd = new JobGetConfigCommand(context, jobName);
cmd.Run();
return cmd.Result;
}
catch (Exception error) {
throw new JenkinsNetException($"Failed to get Jenkins Job Configuration '{jobName}'!", error);
}
}
#if NET_ASYNC
/// <summary>
/// Gets a Job configuration from Jenkins asynchronously.
/// </summary>
/// <param name="jobName">The Name of the Job to retrieve.</param>
/// <param name="token">An optional token for aborting the request.</param>
/// <exception cref="JenkinsNetException"></exception>
public async Task<JenkinsProject> GetConfigurationAsync(string jobName, CancellationToken token = default)
{
try {
var cmd = new JobGetConfigCommand(context, jobName);
await cmd.RunAsync(token);
return cmd.Result;
}
catch (Exception error) {
throw new JenkinsNetException($"Failed to get Jenkins Job Configuration '{jobName}'!", error);
}
}
#endif
/// <summary>
/// Creates a new Job in Jenkins.
/// </summary>
/// <param name="jobName">The name of the Job to create.</param>
/// <param name="job">The description of the Job to create.</param>
/// <exception cref="JenkinsNetException"></exception>
public void Create(string jobName, JenkinsProject job)
{
try {
new JobCreateCommand(context, jobName, job).Run();
}
catch (Exception error) {
throw new JenkinsNetException($"Failed to create Jenkins Job '{jobName}'!", error);
}
}
#if NET_ASYNC
/// <summary>
/// Creates a new Job in Jenkins asynchronously.
/// </summary>
/// <param name="jobName">The name of the Job to create.</param>
/// <param name="job">The description of the Job to create.</param>
/// <param name="token">An optional token for aborting the request.</param>
/// <exception cref="JenkinsNetException"></exception>
public async Task CreateAsync(string jobName, JenkinsProject job, CancellationToken token = default)
{
try {
await new JobCreateCommand(context, jobName, job).RunAsync(token);
}
catch (Exception error) {
throw new JenkinsNetException($"Failed to create Jenkins Job '{jobName}'!", error);
}
}
#endif
/// <summary>
/// Updates the configuration on an existing Job in Jenkins.
/// </summary>
/// <param name="jobName">The name of the Job.</param>
/// <param name="job">The updated description of the Job.</param>
/// <exception cref="JenkinsNetException"></exception>
public void UpdateConfiguration(string jobName, JenkinsProject job)
{
try {
new JobUpdateConfigurationCommand(context, jobName, job).Run();
}
catch (Exception error) {
throw new JenkinsNetException($"Failed to update Jenkins Job configuration '{jobName}'!", error);
}
}
#if NET_ASYNC
/// <summary>
/// Updates the configuration on an existing Job in Jenkins asynchronously.
/// </summary>
/// <param name="jobName">The name of the Job.</param>
/// <param name="job">The updated description of the Job.</param>
/// <param name="token">An optional token for aborting the request.</param>
/// <exception cref="JenkinsNetException"></exception>
public async Task UpdateConfigurationAsync(string jobName, JenkinsProject job, CancellationToken token = default)
{
try {
await new JobUpdateConfigurationCommand(context, jobName, job).RunAsync(token);
}
catch (Exception error) {
throw new JenkinsNetException($"Failed to update Jenkins Job configuration '{jobName}'!", error);
}
}
#endif
/// <summary>
/// Deletes a Job from Jenkins.
/// </summary>
/// <param name="jobName">The name of the Job to delete.</param>
/// <exception cref="JenkinsJobDeleteException"></exception>
public void Delete(string jobName)
{
try {
new JobDeleteCommand(context, jobName).Run();
}
catch (Exception error) {
throw new JenkinsJobDeleteException($"Failed to delete Jenkins Job '{jobName}'!", error);
}
}
#if NET_ASYNC
/// <summary>
/// Deletes a Job from Jenkins asynchronously.
/// </summary>
/// <param name="jobName">The name of the Job to delete.</param>
/// <param name="token">An optional token for aborting the request.</param>
/// <exception cref="JenkinsJobDeleteException"></exception>
public async Task DeleteAsync(string jobName, CancellationToken token = default)
{
try {
await new JobDeleteCommand(context, jobName).RunAsync(token);
}
catch (Exception error) {
throw new JenkinsJobDeleteException($"Failed to delete Jenkins Job '{jobName}'!", error);
}
}
#endif
}
}