-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathJobBuildWithParametersCommand.cs
More file actions
79 lines (64 loc) · 2.63 KB
/
Copy pathJobBuildWithParametersCommand.cs
File metadata and controls
79 lines (64 loc) · 2.63 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
using JenkinsNET.Exceptions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
namespace JenkinsNET.Internal.Commands
{
internal class JobBuildWithParametersCommand : JenkinsHttpCommand
{
public JenkinsBuildResult Result {get; internal set;}
public JobBuildWithParametersCommand(JenkinsClient client, string jobName, IDictionary<string, string> jobParameters) : base(client)
{
if (string.IsNullOrEmpty(jobName))
throw new ArgumentException("'jobName' cannot be empty!");
if (jobParameters == null)
throw new ArgumentNullException(nameof(jobParameters));
var _params = new Dictionary<string, string>(jobParameters) {
["delay"] = "0sec",
};
var query = new StringWriter();
WriteJobParameters(query, _params);
Path = $"job/{jobName}/buildWithParameters?{query}";
OnWrite = request => {
request.Method = "POST";
};
OnRead = response => {
if (response.StatusCode != System.Net.HttpStatusCode.Created)
throw new JenkinsJobBuildException($"Expected HTTP status code 201 but found {(int)response.StatusCode}!");
Result = new JenkinsBuildResult {
QueueItemUrl = response.GetResponseHeader("Location"),
};
};
#if NET_ASYNC
OnWriteAsync = async (request, token) => {
request.Method = "POST";
};
OnReadAsync = async (response, token) => {
if (response.StatusCode != System.Net.HttpStatusCode.Created)
throw new JenkinsJobBuildException($"Expected HTTP status code 201 but found {(int)response.StatusCode}!");
Result = new JenkinsBuildResult {
QueueItemUrl = response.GetResponseHeader("Location"),
};
};
#endif
}
private void WriteJobParameters(TextWriter writer, IDictionary<string, string> jobParameters)
{
var isFirst = true;
foreach (var pair in jobParameters) {
if (isFirst) {
isFirst = false;
}
else {
writer.Write('&');
}
var encodedName = HttpUtility.UrlEncode(pair.Key);
var encodedValue = HttpUtility.UrlEncode(pair.Value);
writer.Write(encodedName);
writer.Write('=');
writer.Write(encodedValue);
}
}
}
}