-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathJenkinsClient.cs
175 lines (156 loc) · 5.49 KB
/
JenkinsClient.cs
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
using JenkinsNET.Exceptions;
using JenkinsNET.Internal.Commands;
using JenkinsNET.Models;
using System;
#if NET_ASYNC
using System.Threading;
using System.Threading.Tasks;
#endif
namespace JenkinsNET
{
/// <summary>
/// HTTP-Client for interacting with Jenkins API.
/// </summary>
public class JenkinsClient : IJenkinsContext, IJenkinsClient
{
/// <summary>
/// The address of the Jenkins instance.
/// ie: http://localhost:8080
/// </summary>
public string BaseUrl {get; set;}
/// <summary>
/// [optional] Jenkins Username.
/// </summary>
public string UserName {get; set;}
/// <summary>
/// [optional] Jenkins ApiToken for the <see cref="UserName"/>.
/// </summary>
public string ApiToken {get; set;}
/// <summary>
/// Gets or sets the security Crumb to use on API requests.
/// </summary>
public JenkinsCrumb Crumb {get; set;}
/// <summary>
/// [optional] Jenkins Password.
/// </summary>
[Obsolete("This property will be removed in future versions; please use 'JenkinsClient.ApiToken' instead.")]
public string Password {
get => ApiToken;
set => ApiToken = value;
}
/// <summary>
/// Group of methods for interacting with Jenkins Jobs.
/// </summary>
public JenkinsClientJobs Jobs {get;}
/// <summary>
/// Group of methods for interacting with Jenkins Builds.
/// </summary>
public JenkinsClientBuilds Builds {get;}
/// <summary>
/// Group of methods for interacting with the Jenkins Job-Queue.
/// </summary>
public JenkinsClientQueue Queue {get;}
/// <summary>
/// Group of methods for interacting with Jenkins Artifacts.
/// </summary>
public JenkinsClientArtifacts Artifacts {get;}
/// <summary>
/// Group of methods for interacting with Jenkins Executors.
/// </summary>
public JenkinsClientNodes Nodes { get; }
/// <summary>
/// Creates a new Jenkins Client.
/// </summary>
public JenkinsClient()
{
Jobs = new JenkinsClientJobs(this);
Builds = new JenkinsClientBuilds(this);
Queue = new JenkinsClientQueue(this);
Artifacts = new JenkinsClientArtifacts(this);
Nodes = new JenkinsClientNodes(this);
}
/// <summary>
/// Creates a new Jenkins Client using the provided BaseUrl.
/// </summary>
public JenkinsClient(string baseUrl) : this()
{
this.BaseUrl = baseUrl;
}
/// <summary>
/// Creates a new Jenkins Client using the provided <see cref="IJenkinsContext"/>.
/// </summary>
public JenkinsClient(IJenkinsContext context) : this(context.BaseUrl)
{
this.UserName = context.UserName;
this.ApiToken = context.ApiToken;
this.Password = context.Password;
this.Crumb = context.Crumb;
}
/// <summary>
/// Updates the security Crumb attached to this client.
/// </summary>
/// <exception cref="JenkinsNetException"></exception>
public void UpdateSecurityCrumb()
{
try {
var cmd = new CrumbGetCommand(this);
cmd.Run();
Crumb = cmd.Result;
}
catch (Exception error) {
throw new JenkinsNetException("Failed to retrieve crumb!", error);
}
}
#if NET_ASYNC
/// <summary>
/// Updates the security Crumb attached to this client asynchronously.
/// </summary>
/// <param name="token">An optional token for aborting the request.</param>
/// <exception cref="JenkinsNetException"></exception>
public async Task UpdateSecurityCrumbAsync(CancellationToken token = default)
{
try {
var cmd = new CrumbGetCommand(this);
await cmd.RunAsync(token);
Crumb = cmd.Result;
}
catch (Exception error) {
throw new JenkinsNetException("Failed to retrieve crumb!", error);
}
}
#endif
/// <summary>
/// Gets the root description of the Jenkins node.
/// </summary>
/// <exception cref="JenkinsNetException"></exception>
public Jenkins Get()
{
try {
var cmd = new JenkinsGetCommand(this);
cmd.Run();
return cmd.Result;
}
catch (Exception error) {
throw new JenkinsNetException("Failed to retrieve Jenkins description!", error);
}
}
#if NET_ASYNC
/// <summary>
/// Gets the root description of the Jenkins node asynchronously.
/// </summary>
/// <param name="token">An optional token for aborting the request.</param>
/// <exception cref="JenkinsNetException"></exception>
public async Task<Jenkins> GetAsync(CancellationToken token = default)
{
try {
var cmd = new JenkinsGetCommand(this);
await cmd.RunAsync(token);
return cmd.Result;
}
catch (Exception error) {
throw new JenkinsNetException("Failed to retrieve Jenkins description!", error);
}
}
#endif
}
}