diff --git a/.travis.yml b/.travis.yml index 527db46..de63dd7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,6 +16,7 @@ deploy: provider: releases api_key: $GITHUB_APIKEY file: "Jenkins.Net/bin/jenkinsnet.*.nupkg" + file_glob: true skip_cleanup: true on: tags: true diff --git a/Jenkins.Net/IJenkinsContext.cs b/Jenkins.Net/IJenkinsContext.cs index e5fb43d..90081a9 100644 --- a/Jenkins.Net/IJenkinsContext.cs +++ b/Jenkins.Net/IJenkinsContext.cs @@ -1,5 +1,5 @@ -using System; -using JenkinsNET.Models; +using JenkinsNET.Models; +using System; namespace JenkinsNET { diff --git a/Jenkins.Net/Internal/Commands/ArtifactGetCommand.cs b/Jenkins.Net/Internal/Commands/ArtifactGetCommand.cs index 63aeefe..50c4188 100644 --- a/Jenkins.Net/Internal/Commands/ArtifactGetCommand.cs +++ b/Jenkins.Net/Internal/Commands/ArtifactGetCommand.cs @@ -8,11 +8,8 @@ internal class ArtifactGetCommand : JenkinsHttpCommand public MemoryStream Result {get; private set;} - public ArtifactGetCommand(IJenkinsContext context, string jobName, string buildNumber, string filename) + public ArtifactGetCommand(JenkinsClient client, string jobName, string buildNumber, string filename) : base(client) { - if (context == null) - throw new ArgumentNullException(nameof(context)); - if (string.IsNullOrEmpty(jobName)) throw new ArgumentException("'jobName' cannot be empty!"); @@ -23,10 +20,7 @@ public ArtifactGetCommand(IJenkinsContext context, string jobName, string buildN throw new ArgumentException("'filename' cannot be empty!"); var urlFilename = filename.Replace('\\', '/'); - Url = NetPath.Combine(context.BaseUrl, "job", jobName, buildNumber, "artifact", urlFilename); - UserName = context.UserName; - Password = context.Password; - Crumb = context.Crumb; + Path = $"job/{jobName}/{buildNumber}/artifact/{urlFilename}"; OnWrite = request => { request.Method = "POST"; diff --git a/Jenkins.Net/Internal/Commands/BuildGetCommand.cs b/Jenkins.Net/Internal/Commands/BuildGetCommand.cs index 124b156..f3b8c6f 100644 --- a/Jenkins.Net/Internal/Commands/BuildGetCommand.cs +++ b/Jenkins.Net/Internal/Commands/BuildGetCommand.cs @@ -8,21 +8,15 @@ internal class BuildGetCommand : JenkinsHttpCommand where T : class, IJenkins public T Result {get; private set;} - public BuildGetCommand(IJenkinsContext context, string jobName, string buildNumber) + public BuildGetCommand(JenkinsClient client, string jobName, string buildNumber) : base(client) { - if (context == null) - throw new ArgumentNullException(nameof(context)); - if (string.IsNullOrEmpty(jobName)) throw new ArgumentException("'jobName' cannot be empty!"); if (string.IsNullOrEmpty(buildNumber)) throw new ArgumentException("'buildNumber' cannot be empty!"); - Url = NetPath.Combine(context.BaseUrl, "job", jobName, buildNumber, "api/xml"); - UserName = context.UserName; - Password = context.Password; - Crumb = context.Crumb; + Path = $"job/{jobName}/{buildNumber}/api/xml"; OnWrite = request => { request.Method = "POST"; diff --git a/Jenkins.Net/Internal/Commands/BuildHtmlCommand.cs b/Jenkins.Net/Internal/Commands/BuildHtmlCommand.cs index 864db48..d450062 100644 --- a/Jenkins.Net/Internal/Commands/BuildHtmlCommand.cs +++ b/Jenkins.Net/Internal/Commands/BuildHtmlCommand.cs @@ -9,21 +9,15 @@ internal class BuildHtmlCommand : JenkinsHttpCommand public string Result {get; private set;} - public BuildHtmlCommand(IJenkinsContext context, string jobName, string buildNumber) + public BuildHtmlCommand(JenkinsClient client, string jobName, string buildNumber) : base(client) { - if (context == null) - throw new ArgumentNullException(nameof(context)); - if (string.IsNullOrEmpty(jobName)) throw new ArgumentException("'jobName' cannot be empty!"); if (string.IsNullOrEmpty(buildNumber)) throw new ArgumentException("'buildNumber' cannot be empty!"); - Url = NetPath.Combine(context.BaseUrl, "job", jobName, buildNumber, "consoleFull"); - UserName = context.UserName; - Password = context.Password; - Crumb = context.Crumb; + Path = $"job/{jobName}/{buildNumber}/consoleFull"; OnRead = response => { using (var stream = response.GetResponseStream()) { diff --git a/Jenkins.Net/Internal/Commands/BuildProgressiveHtmlCommand.cs b/Jenkins.Net/Internal/Commands/BuildProgressiveHtmlCommand.cs index 1113340..a8672e7 100644 --- a/Jenkins.Net/Internal/Commands/BuildProgressiveHtmlCommand.cs +++ b/Jenkins.Net/Internal/Commands/BuildProgressiveHtmlCommand.cs @@ -10,21 +10,15 @@ internal class BuildProgressiveHtmlCommand : JenkinsHttpCommand public JenkinsProgressiveHtmlResponse Result {get; private set;} - public BuildProgressiveHtmlCommand(IJenkinsContext context, string jobName, string buildNumber, int start) + public BuildProgressiveHtmlCommand(JenkinsClient client, string jobName, string buildNumber, int start) : base(client) { - if (context == null) - throw new ArgumentNullException(nameof(context)); - if (string.IsNullOrEmpty(jobName)) throw new ArgumentException("'jobName' cannot be empty!"); if (string.IsNullOrEmpty(buildNumber)) throw new ArgumentException("'buildNumber' cannot be empty!"); - Url = NetPath.Combine(context.BaseUrl, "job", jobName, buildNumber, "logText/progressiveHtml"); - UserName = context.UserName; - Password = context.Password; - Crumb = context.Crumb; + Path = $"job/{jobName}/{buildNumber}/logText/progressiveHtml"; OnWrite = request => { request.Method = "POST"; diff --git a/Jenkins.Net/Internal/Commands/BuildProgressiveTextCommand.cs b/Jenkins.Net/Internal/Commands/BuildProgressiveTextCommand.cs index 0498523..b9ff2f8 100644 --- a/Jenkins.Net/Internal/Commands/BuildProgressiveTextCommand.cs +++ b/Jenkins.Net/Internal/Commands/BuildProgressiveTextCommand.cs @@ -10,21 +10,15 @@ internal class BuildProgressiveTextCommand : JenkinsHttpCommand public JenkinsProgressiveTextResponse Result {get; private set;} - public BuildProgressiveTextCommand(IJenkinsContext context, string jobName, string buildNumber, int start) + public BuildProgressiveTextCommand(JenkinsClient client, string jobName, string buildNumber, int start) : base(client) { - if (context == null) - throw new ArgumentNullException(nameof(context)); - if (string.IsNullOrEmpty(jobName)) throw new ArgumentException("'jobName' cannot be empty!"); if (string.IsNullOrEmpty(buildNumber)) throw new ArgumentException("'buildNumber' cannot be empty!"); - Url = NetPath.Combine(context.BaseUrl, "job", jobName, buildNumber, "logText/progressiveText"); - UserName = context.UserName; - Password = context.Password; - Crumb = context.Crumb; + Path = $"job/{jobName}/{buildNumber}/logText/progressiveText"; OnWrite = request => { request.Method = "POST"; diff --git a/Jenkins.Net/Internal/Commands/BuildTextCommand.cs b/Jenkins.Net/Internal/Commands/BuildTextCommand.cs index fdda4a4..6993c63 100644 --- a/Jenkins.Net/Internal/Commands/BuildTextCommand.cs +++ b/Jenkins.Net/Internal/Commands/BuildTextCommand.cs @@ -9,21 +9,15 @@ internal class BuildTextCommand : JenkinsHttpCommand public string Result {get; private set;} - public BuildTextCommand(IJenkinsContext context, string jobName, string buildNumber) + public BuildTextCommand(JenkinsClient client, string jobName, string buildNumber) : base(client) { - if (context == null) - throw new ArgumentNullException(nameof(context)); - if (string.IsNullOrEmpty(jobName)) throw new ArgumentException("'jobName' cannot be empty!"); if (string.IsNullOrEmpty(buildNumber)) throw new ArgumentException("'buildNumber' cannot be empty!"); - Url = NetPath.Combine(context.BaseUrl, "job", jobName, buildNumber, "consoleText"); - UserName = context.UserName; - Password = context.Password; - Crumb = context.Crumb; + Path = $"job/{jobName}/{buildNumber}/consoleText"; OnRead = response => { using (var stream = response.GetResponseStream()) { diff --git a/Jenkins.Net/Internal/Commands/CrumbGetCommand.cs b/Jenkins.Net/Internal/Commands/CrumbGetCommand.cs index 4b76334..9fe3aa2 100644 --- a/Jenkins.Net/Internal/Commands/CrumbGetCommand.cs +++ b/Jenkins.Net/Internal/Commands/CrumbGetCommand.cs @@ -9,14 +9,9 @@ internal class CrumbGetCommand : JenkinsHttpCommand public JenkinsCrumb Result {get; private set;} - public CrumbGetCommand(IJenkinsContext context) + public CrumbGetCommand(JenkinsClient client) : base(client) { - if (context == null) - throw new ArgumentNullException(nameof(context)); - - Url = NetPath.Combine(context.BaseUrl, "crumbIssuer/api/xml"); - UserName = context.UserName; - Password = context.Password; + Path = $"crumbIssuer/api/xml"; OnWrite = request => { request.Method = "GET"; diff --git a/Jenkins.Net/Internal/Commands/JenkinsGetCommand.cs b/Jenkins.Net/Internal/Commands/JenkinsGetCommand.cs index 7b67f3f..ef859b0 100644 --- a/Jenkins.Net/Internal/Commands/JenkinsGetCommand.cs +++ b/Jenkins.Net/Internal/Commands/JenkinsGetCommand.cs @@ -1,5 +1,4 @@ using JenkinsNET.Models; -using System; namespace JenkinsNET.Internal.Commands { @@ -8,16 +7,9 @@ internal class JenkinsGetCommand : JenkinsHttpCommand public Jenkins Result {get; private set;} - public JenkinsGetCommand(IJenkinsContext context) + public JenkinsGetCommand(JenkinsClient client) : base(client) { - if (context == null) - throw new ArgumentNullException(nameof(context)); - - Url = NetPath.Combine(context.BaseUrl, "api/xml"); - - UserName = context.UserName; - Password = context.Password; - Crumb = context.Crumb; + Path = "api/xml"; OnWrite = request => { request.Method = "GET"; diff --git a/Jenkins.Net/Internal/Commands/JobBuildCommand.cs b/Jenkins.Net/Internal/Commands/JobBuildCommand.cs index d922945..729e82f 100644 --- a/Jenkins.Net/Internal/Commands/JobBuildCommand.cs +++ b/Jenkins.Net/Internal/Commands/JobBuildCommand.cs @@ -7,18 +7,12 @@ internal class JobBuildCommand : JenkinsHttpCommand { public JenkinsBuildResult Result {get; internal set;} - public JobBuildCommand(IJenkinsContext context, string jobName) + public JobBuildCommand(JenkinsClient client, string jobName) : base(client) { - if (context == null) - throw new ArgumentNullException(nameof(context)); - if (string.IsNullOrEmpty(jobName)) throw new ArgumentException("'jobName' cannot be empty!"); - Url = NetPath.Combine(context.BaseUrl, "job", jobName, "build?delay=0sec"); - UserName = context.UserName; - Password = context.Password; - Crumb = context.Crumb; + Path = $"job/{jobName}/build?delay=0sec"; OnWrite = request => { request.Method = "POST"; diff --git a/Jenkins.Net/Internal/Commands/JobBuildWithParametersCommand.cs b/Jenkins.Net/Internal/Commands/JobBuildWithParametersCommand.cs index c753439..e71cde7 100644 --- a/Jenkins.Net/Internal/Commands/JobBuildWithParametersCommand.cs +++ b/Jenkins.Net/Internal/Commands/JobBuildWithParametersCommand.cs @@ -10,11 +10,8 @@ internal class JobBuildWithParametersCommand : JenkinsHttpCommand { public JenkinsBuildResult Result {get; internal set;} - public JobBuildWithParametersCommand(IJenkinsContext context, string jobName, IDictionary jobParameters) + public JobBuildWithParametersCommand(JenkinsClient client, string jobName, IDictionary jobParameters) : base(client) { - if (context == null) - throw new ArgumentNullException(nameof(context)); - if (string.IsNullOrEmpty(jobName)) throw new ArgumentException("'jobName' cannot be empty!"); @@ -28,10 +25,7 @@ public JobBuildWithParametersCommand(IJenkinsContext context, string jobName, ID var query = new StringWriter(); WriteJobParameters(query, _params); - Url = NetPath.Combine(context.BaseUrl, "job", jobName, $"buildWithParameters?{query}"); - UserName = context.UserName; - Password = context.Password; - Crumb = context.Crumb; + Path = $"job/{jobName}/buildWithParameters?{query}"; OnWrite = request => { request.Method = "POST"; diff --git a/Jenkins.Net/Internal/Commands/JobCreateCommand.cs b/Jenkins.Net/Internal/Commands/JobCreateCommand.cs index e6d73ef..1ff6caa 100644 --- a/Jenkins.Net/Internal/Commands/JobCreateCommand.cs +++ b/Jenkins.Net/Internal/Commands/JobCreateCommand.cs @@ -5,23 +5,16 @@ namespace JenkinsNET.Internal.Commands { internal class JobCreateCommand : JenkinsHttpCommand { - public JobCreateCommand(IJenkinsContext context, string jobName, JenkinsProject job) + public JobCreateCommand(JenkinsClient client, string jobName, JenkinsProject job) : base(client) { - if (context == null) - throw new ArgumentNullException(nameof(context)); - if (string.IsNullOrEmpty(jobName)) throw new ArgumentException("Value cannot be empty!", nameof(jobName)); if (job == null) throw new ArgumentNullException(nameof(job)); - Url = NetPath.Combine(context.BaseUrl, "createItem") - + NetPath.Query(new {name = jobName}); - - UserName = context.UserName; - Password = context.Password; - Crumb = context.Crumb; + var query = NetPath.Query(new {name = jobName}); + Path = $"createItem{query}"; OnWrite = request => { request.Method = "POST"; diff --git a/Jenkins.Net/Internal/Commands/JobDeleteCommand.cs b/Jenkins.Net/Internal/Commands/JobDeleteCommand.cs index 43d47af..f431bed 100644 --- a/Jenkins.Net/Internal/Commands/JobDeleteCommand.cs +++ b/Jenkins.Net/Internal/Commands/JobDeleteCommand.cs @@ -4,18 +4,12 @@ namespace JenkinsNET.Internal.Commands { internal class JobDeleteCommand : JenkinsHttpCommand { - public JobDeleteCommand(IJenkinsContext context, string jobName) + public JobDeleteCommand(JenkinsClient client, string jobName) : base(client) { - if (context == null) - throw new ArgumentNullException(nameof(context)); - if (string.IsNullOrEmpty(jobName)) throw new ArgumentException("'jobName' cannot be empty!"); - Url = NetPath.Combine(context.BaseUrl, "job", jobName, "doDelete"); - UserName = context.UserName; - Password = context.Password; - Crumb = context.Crumb; + Path = $"job/{jobName}/doDelete"; OnWrite = request => { request.Method = "POST"; diff --git a/Jenkins.Net/Internal/Commands/JobGetCommand.cs b/Jenkins.Net/Internal/Commands/JobGetCommand.cs index 3d05a14..f63fa89 100644 --- a/Jenkins.Net/Internal/Commands/JobGetCommand.cs +++ b/Jenkins.Net/Internal/Commands/JobGetCommand.cs @@ -8,19 +8,12 @@ internal class JobGetCommand : JenkinsHttpCommand where T : class, IJenkinsJo public T Result {get; private set;} - public JobGetCommand(IJenkinsContext context, string jobName) + public JobGetCommand(JenkinsClient client, string jobName) : base(client) { - if (context == null) - throw new ArgumentNullException(nameof(context)); - if (string.IsNullOrEmpty(jobName)) throw new ArgumentException("Value cannot be empty!", nameof(jobName)); - Url = NetPath.Combine(context.BaseUrl, "job", jobName, "api/xml"); - - UserName = context.UserName; - Password = context.Password; - Crumb = context.Crumb; + Path = $"job/{jobName}/api/xml"; OnWrite = request => { request.Method = "GET"; diff --git a/Jenkins.Net/Internal/Commands/JobGetConfigCommand.cs b/Jenkins.Net/Internal/Commands/JobGetConfigCommand.cs index 972f62b..0f727d7 100644 --- a/Jenkins.Net/Internal/Commands/JobGetConfigCommand.cs +++ b/Jenkins.Net/Internal/Commands/JobGetConfigCommand.cs @@ -12,19 +12,12 @@ internal class JobGetConfigCommand : JenkinsHttpCommand public JenkinsProject Result {get; private set;} - public JobGetConfigCommand(IJenkinsContext context, string jobName) + public JobGetConfigCommand(JenkinsClient client, string jobName) : base(client) { - if (context == null) - throw new ArgumentNullException(nameof(context)); - if (string.IsNullOrEmpty(jobName)) throw new ArgumentException("Value cannot be empty!", nameof(jobName)); - Url = NetPath.Combine(context.BaseUrl, "job", jobName, "config.xml"); - - UserName = context.UserName; - Password = context.Password; - Crumb = context.Crumb; + Path = $"job/{jobName}/config.xml"; OnWrite = request => { request.Method = "GET"; diff --git a/Jenkins.Net/Internal/Commands/JobUpdateConfigurationCommand.cs b/Jenkins.Net/Internal/Commands/JobUpdateConfigurationCommand.cs index 75dad03..461dcf2 100644 --- a/Jenkins.Net/Internal/Commands/JobUpdateConfigurationCommand.cs +++ b/Jenkins.Net/Internal/Commands/JobUpdateConfigurationCommand.cs @@ -5,22 +5,15 @@ namespace JenkinsNET.Internal.Commands { internal class JobUpdateConfigurationCommand : JenkinsHttpCommand { - public JobUpdateConfigurationCommand(IJenkinsContext context, string jobName, JenkinsProject job) + public JobUpdateConfigurationCommand(JenkinsClient client, string jobName, JenkinsProject job) : base(client) { - if (context == null) - throw new ArgumentNullException(nameof(context)); - if (string.IsNullOrEmpty(jobName)) throw new ArgumentException("Value cannot be empty!", nameof(jobName)); if (job == null) throw new ArgumentNullException(nameof(job)); - Url = NetPath.Combine(context.BaseUrl, "job", jobName, "config.xml"); - - UserName = context.UserName; - Password = context.Password; - Crumb = context.Crumb; + Path = $"job/{jobName}/config.xml"; OnWrite = request => { request.Method = "POST"; diff --git a/Jenkins.Net/Internal/Commands/QueueGetItemCommand.cs b/Jenkins.Net/Internal/Commands/QueueGetItemCommand.cs index f767040..38abb2f 100644 --- a/Jenkins.Net/Internal/Commands/QueueGetItemCommand.cs +++ b/Jenkins.Net/Internal/Commands/QueueGetItemCommand.cs @@ -9,15 +9,9 @@ internal class QueueGetItemCommand : JenkinsHttpCommand public JenkinsQueueItem Result {get; private set;} - public QueueGetItemCommand(IJenkinsContext context, int itemNumber) + public QueueGetItemCommand(JenkinsClient client, int itemNumber) : base(client) { - if (context == null) - throw new ArgumentNullException(nameof(context)); - - Url = NetPath.Combine(context.BaseUrl, "queue/item", itemNumber.ToString(), "api/xml"); - UserName = context.UserName; - Password = context.Password; - Crumb = context.Crumb; + Path = $"queue/item/{itemNumber}/api/xml"; OnWrite = request => { request.Method = "POST"; diff --git a/Jenkins.Net/Internal/Commands/QueueItemListCommand.cs b/Jenkins.Net/Internal/Commands/QueueItemListCommand.cs index 5a0e514..b51dd23 100644 --- a/Jenkins.Net/Internal/Commands/QueueItemListCommand.cs +++ b/Jenkins.Net/Internal/Commands/QueueItemListCommand.cs @@ -11,15 +11,9 @@ internal class QueueItemListCommand : JenkinsHttpCommand public JenkinsQueueItem[] Result {get; private set;} - public QueueItemListCommand(IJenkinsContext context) + public QueueItemListCommand(JenkinsClient client) : base(client) { - if (context == null) - throw new ArgumentNullException(nameof(context)); - - Url = NetPath.Combine(context.BaseUrl, "queue/api/xml"); - UserName = context.UserName; - Password = context.Password; - Crumb = context.Crumb; + Path = "queue/api/xml"; OnWrite = request => { request.Method = "GET"; diff --git a/Jenkins.Net/Internal/JenkinsHttpCommand.cs b/Jenkins.Net/Internal/JenkinsHttpCommand.cs index fa0e280..fc62312 100644 --- a/Jenkins.Net/Internal/JenkinsHttpCommand.cs +++ b/Jenkins.Net/Internal/JenkinsHttpCommand.cs @@ -1,5 +1,4 @@ -using JenkinsNET.Models; -using System; +using System; using System.IO; using System.Net; using System.Text; @@ -16,10 +15,9 @@ namespace JenkinsNET.Internal { internal class JenkinsHttpCommand { - public string Url {get; set;} - public string UserName {get; set;} - public string Password {get; set;} - public JenkinsCrumb Crumb {get; set;} + private readonly JenkinsClient client; + + public string Path {get; set;} public Action OnWrite {get; set;} public Action OnRead {get; set;} @@ -29,12 +27,19 @@ internal class JenkinsHttpCommand #endif + public JenkinsHttpCommand(JenkinsClient client) + { + this.client = client ?? throw new ArgumentNullException(nameof(client)); + } + public void Run() { var request = CreateRequest(); OnWrite?.Invoke(request); + client.OnBeforeRequestSend(request); + using (var response = (HttpWebResponse)request.GetResponse()) { OnRead?.Invoke(response); } @@ -58,23 +63,24 @@ public async Task RunAsync(CancellationToken token = default) private HttpWebRequest CreateRequest() { - var _url = Url; - var hasUser = !string.IsNullOrEmpty(UserName); - var hasPass = !string.IsNullOrEmpty(Password); + var _url = NetPath.Combine(client.BaseUrl, Path); + var _password = client.ApiToken ?? client.Password; + var hasUser = !string.IsNullOrEmpty(client.UserName); + var hasPass = !string.IsNullOrEmpty(_password); var request = (HttpWebRequest)WebRequest.Create(_url); request.UserAgent = "Jenkins.NET Client"; request.AllowAutoRedirect = true; request.KeepAlive = true; - if (Crumb != null) - request.Headers.Add(Crumb.CrumbRequestField, Crumb.Crumb); + if (client.Crumb != null) + request.Headers.Add(client.Crumb.CrumbRequestField, client.Crumb.Crumb); if (hasUser && hasPass) { request.PreAuthenticate = true; request.UseDefaultCredentials = false; - var data = Encoding.UTF8.GetBytes($"{UserName}:{Password}"); + var data = Encoding.UTF8.GetBytes($"{client.UserName}:{_password}"); var basicAuthToken = Convert.ToBase64String(data); request.Headers["Authorization"] = $"Basic {basicAuthToken}"; } diff --git a/Jenkins.Net/Jenkins.Net.csproj b/Jenkins.Net/Jenkins.Net.csproj index 77c290d..88db5c6 100644 --- a/Jenkins.Net/Jenkins.Net.csproj +++ b/Jenkins.Net/Jenkins.Net.csproj @@ -17,9 +17,9 @@ true false True - 1.0.4 - 1.0.4 - 1.0.4 + 1.0.5 + 1.0.5 + 1.0.5 latest diff --git a/Jenkins.Net/JenkinsClient.cs b/Jenkins.Net/JenkinsClient.cs index 86fd421..3281c60 100644 --- a/Jenkins.Net/JenkinsClient.cs +++ b/Jenkins.Net/JenkinsClient.cs @@ -2,6 +2,7 @@ using JenkinsNET.Internal.Commands; using JenkinsNET.Models; using System; +using System.Net; #if NET_ASYNC using System.Threading; @@ -10,11 +11,15 @@ namespace JenkinsNET { + public delegate void BeforeRequestSendEventHandler(WebRequest request); + /// /// HTTP-Client for interacting with Jenkins API. /// public class JenkinsClient : IJenkinsContext, IJenkinsClient { + public event BeforeRequestSendEventHandler BeforeRequestSend; + /// /// The address of the Jenkins instance. /// ie: http://localhost:8080 @@ -165,5 +170,10 @@ public async Task GetAsync(CancellationToken token = default) } } #endif + + internal void OnBeforeRequestSend(WebRequest request) + { + BeforeRequestSend?.Invoke(request); + } } } diff --git a/Jenkins.Net/JenkinsClientArtifacts.cs b/Jenkins.Net/JenkinsClientArtifacts.cs index 46670fa..e3b4d0a 100644 --- a/Jenkins.Net/JenkinsClientArtifacts.cs +++ b/Jenkins.Net/JenkinsClientArtifacts.cs @@ -2,8 +2,11 @@ using JenkinsNET.Internal.Commands; using System; using System.IO; + +#if NET_ASYNC using System.Threading; using System.Threading.Tasks; +#endif namespace JenkinsNET { @@ -15,12 +18,12 @@ namespace JenkinsNET /// public sealed class JenkinsClientArtifacts { - private readonly IJenkinsContext context; + private readonly JenkinsClient client; - internal JenkinsClientArtifacts(IJenkinsContext context) + internal JenkinsClientArtifacts(JenkinsClient client) { - this.context = context; + this.client = client; } /// @@ -34,7 +37,7 @@ internal JenkinsClientArtifacts(IJenkinsContext context) public MemoryStream Get(string jobName, string buildNumber, string filename) { try { - var cmd = new ArtifactGetCommand(context, jobName, buildNumber, filename); + var cmd = new ArtifactGetCommand(client, jobName, buildNumber, filename); cmd.Run(); return cmd.Result; } @@ -56,7 +59,7 @@ public MemoryStream Get(string jobName, string buildNumber, string filename) public async Task GetAsync(string jobName, string buildNumber, string filename, CancellationToken token = default) { try { - var cmd = new ArtifactGetCommand(context, jobName, buildNumber, filename); + var cmd = new ArtifactGetCommand(client, jobName, buildNumber, filename); await cmd.RunAsync(token); return cmd.Result; } diff --git a/Jenkins.Net/JenkinsClientBuilds.cs b/Jenkins.Net/JenkinsClientBuilds.cs index 8e8aad8..d2b223d 100644 --- a/Jenkins.Net/JenkinsClientBuilds.cs +++ b/Jenkins.Net/JenkinsClientBuilds.cs @@ -2,8 +2,11 @@ using JenkinsNET.Internal.Commands; using JenkinsNET.Models; using System; + +#if NET_ASYNC using System.Threading; using System.Threading.Tasks; +#endif namespace JenkinsNET { @@ -15,12 +18,12 @@ namespace JenkinsNET /// public sealed class JenkinsClientBuilds { - private readonly IJenkinsContext context; + private readonly JenkinsClient client; - internal JenkinsClientBuilds(IJenkinsContext context) + internal JenkinsClientBuilds(JenkinsClient client) { - this.context = context; + this.client = client; } /// @@ -32,7 +35,7 @@ internal JenkinsClientBuilds(IJenkinsContext context) public T Get(string jobName, string buildNumber) where T : class, IJenkinsBuild { try { - var cmd = new BuildGetCommand(context, jobName, buildNumber); + var cmd = new BuildGetCommand(client, jobName, buildNumber); cmd.Run(); return cmd.Result; } @@ -52,7 +55,7 @@ public T Get(string jobName, string buildNumber) where T : class, IJenkinsBui public async Task GetAsync(string jobName, string buildNumber, CancellationToken token = default) where T : class, IJenkinsBuild { try { - var cmd = new BuildGetCommand(context, jobName, buildNumber); + var cmd = new BuildGetCommand(client, jobName, buildNumber); await cmd.RunAsync(token); return cmd.Result; } @@ -71,7 +74,7 @@ public async Task GetAsync(string jobName, string buildNumber, Cancellatio public string GetConsoleText(string jobName, string buildNumber) { try { - var cmd = new BuildTextCommand(context, jobName, buildNumber); + var cmd = new BuildTextCommand(client, jobName, buildNumber); cmd.Run(); return cmd.Result; } @@ -91,7 +94,7 @@ public string GetConsoleText(string jobName, string buildNumber) public async Task GetConsoleTextAsync(string jobName, string buildNumber, CancellationToken token = default) { try { - var cmd = new BuildTextCommand(context, jobName, buildNumber); + var cmd = new BuildTextCommand(client, jobName, buildNumber); await cmd.RunAsync(token); return cmd.Result; } @@ -110,7 +113,7 @@ public async Task GetConsoleTextAsync(string jobName, string buildNumber public string GetConsoleHtml(string jobName, string buildNumber) { try { - var cmd = new BuildHtmlCommand(context, jobName, buildNumber); + var cmd = new BuildHtmlCommand(client, jobName, buildNumber); cmd.Run(); return cmd.Result; } @@ -130,7 +133,7 @@ public string GetConsoleHtml(string jobName, string buildNumber) public async Task GetConsoleHtmlAsync(string jobName, string buildNumber, CancellationToken token = default) { try { - var cmd = new BuildHtmlCommand(context, jobName, buildNumber); + var cmd = new BuildHtmlCommand(client, jobName, buildNumber); await cmd.RunAsync(token); return cmd.Result; } @@ -150,7 +153,7 @@ public async Task GetConsoleHtmlAsync(string jobName, string buildNumber public JenkinsProgressiveTextResponse GetProgressiveText(string jobName, string buildNumber, int start) { try { - var cmd = new BuildProgressiveTextCommand(context, jobName, buildNumber, start); + var cmd = new BuildProgressiveTextCommand(client, jobName, buildNumber, start); cmd.Run(); return cmd.Result; } @@ -171,7 +174,7 @@ public JenkinsProgressiveTextResponse GetProgressiveText(string jobName, string public async Task GetProgressiveTextAsync(string jobName, string buildNumber, int start, CancellationToken token = default) { try { - var cmd = new BuildProgressiveTextCommand(context, jobName, buildNumber, start); + var cmd = new BuildProgressiveTextCommand(client, jobName, buildNumber, start); await cmd.RunAsync(token); return cmd.Result; } @@ -191,7 +194,7 @@ public async Task GetProgressiveTextAsync(string public JenkinsProgressiveHtmlResponse GetProgressiveHtml(string jobName, string buildNumber, int start) { try { - var cmd = new BuildProgressiveHtmlCommand(context, jobName, buildNumber, start); + var cmd = new BuildProgressiveHtmlCommand(client, jobName, buildNumber, start); cmd.Run(); return cmd.Result; } @@ -212,7 +215,7 @@ public JenkinsProgressiveHtmlResponse GetProgressiveHtml(string jobName, string public async Task GetProgressiveHtmlAsync(string jobName, string buildNumber, int start, CancellationToken token = default) { try { - var cmd = new BuildProgressiveHtmlCommand(context, jobName, buildNumber, start); + var cmd = new BuildProgressiveHtmlCommand(client, jobName, buildNumber, start); await cmd.RunAsync(token); return cmd.Result; } diff --git a/Jenkins.Net/JenkinsClientJobs.cs b/Jenkins.Net/JenkinsClientJobs.cs index 3ca33e6..2fffdc3 100644 --- a/Jenkins.Net/JenkinsClientJobs.cs +++ b/Jenkins.Net/JenkinsClientJobs.cs @@ -3,8 +3,11 @@ using JenkinsNET.Models; using System; using System.Collections.Generic; + +#if NET_ASYNC using System.Threading; using System.Threading.Tasks; +#endif namespace JenkinsNET { @@ -16,12 +19,12 @@ namespace JenkinsNET /// public sealed class JenkinsClientJobs { - private readonly IJenkinsContext context; + private readonly JenkinsClient client; - internal JenkinsClientJobs(IJenkinsContext context) + internal JenkinsClientJobs(JenkinsClient client) { - this.context = context; + this.client = client; } /// @@ -32,7 +35,7 @@ internal JenkinsClientJobs(IJenkinsContext context) public JenkinsBuildResult Build(string jobName) { try { - var cmd = new JobBuildCommand(context, jobName); + var cmd = new JobBuildCommand(client, jobName); cmd.Run(); return cmd.Result; } @@ -51,7 +54,7 @@ public JenkinsBuildResult Build(string jobName) public async Task BuildAsync(string jobName, CancellationToken token = default) { try { - var cmd = new JobBuildCommand(context, jobName); + var cmd = new JobBuildCommand(client, jobName); await cmd.RunAsync(token); return cmd.Result; } @@ -70,7 +73,7 @@ public async Task BuildAsync(string jobName, CancellationTok public JenkinsBuildResult BuildWithParameters(string jobName, IDictionary jobParameters) { try { - var cmd = new JobBuildWithParametersCommand(context, jobName, jobParameters); + var cmd = new JobBuildWithParametersCommand(client, jobName, jobParameters); cmd.Run(); return cmd.Result; } @@ -90,7 +93,7 @@ public JenkinsBuildResult BuildWithParameters(string jobName, IDictionary BuildWithParametersAsync(string jobName, IDictionary jobParameters, CancellationToken token = default) { try { - var cmd = new JobBuildWithParametersCommand(context, jobName, jobParameters); + var cmd = new JobBuildWithParametersCommand(client, jobName, jobParameters); await cmd.RunAsync(token); return cmd.Result; } @@ -108,7 +111,7 @@ public async Task BuildWithParametersAsync(string jobName, I public T Get(string jobName) where T : class, IJenkinsJob { try { - var cmd = new JobGetCommand(context, jobName); + var cmd = new JobGetCommand(client, jobName); cmd.Run(); return cmd.Result; } @@ -127,7 +130,7 @@ public T Get(string jobName) where T : class, IJenkinsJob public async Task GetAsync(string jobName, CancellationToken token = default) where T : class, IJenkinsJob { try { - var cmd = new JobGetCommand(context, jobName); + var cmd = new JobGetCommand(client, jobName); await cmd.RunAsync(token); return cmd.Result; } @@ -145,7 +148,7 @@ public async Task GetAsync(string jobName, CancellationToken token = defau public JenkinsProject GetConfiguration(string jobName) { try { - var cmd = new JobGetConfigCommand(context, jobName); + var cmd = new JobGetConfigCommand(client, jobName); cmd.Run(); return cmd.Result; } @@ -164,7 +167,7 @@ public JenkinsProject GetConfiguration(string jobName) public async Task GetConfigurationAsync(string jobName, CancellationToken token = default) { try { - var cmd = new JobGetConfigCommand(context, jobName); + var cmd = new JobGetConfigCommand(client, jobName); await cmd.RunAsync(token); return cmd.Result; } @@ -183,7 +186,7 @@ public async Task GetConfigurationAsync(string jobName, Cancella public void Create(string jobName, JenkinsProject job) { try { - new JobCreateCommand(context, jobName, job).Run(); + new JobCreateCommand(client, jobName, job).Run(); } catch (Exception error) { throw new JenkinsNetException($"Failed to create Jenkins Job '{jobName}'!", error); @@ -201,7 +204,7 @@ public void Create(string jobName, JenkinsProject job) public async Task CreateAsync(string jobName, JenkinsProject job, CancellationToken token = default) { try { - await new JobCreateCommand(context, jobName, job).RunAsync(token); + await new JobCreateCommand(client, jobName, job).RunAsync(token); } catch (Exception error) { throw new JenkinsNetException($"Failed to create Jenkins Job '{jobName}'!", error); @@ -218,7 +221,7 @@ public async Task CreateAsync(string jobName, JenkinsProject job, CancellationTo public void UpdateConfiguration(string jobName, JenkinsProject job) { try { - new JobUpdateConfigurationCommand(context, jobName, job).Run(); + new JobUpdateConfigurationCommand(client, jobName, job).Run(); } catch (Exception error) { throw new JenkinsNetException($"Failed to update Jenkins Job configuration '{jobName}'!", error); @@ -236,7 +239,7 @@ public void UpdateConfiguration(string jobName, JenkinsProject job) public async Task UpdateConfigurationAsync(string jobName, JenkinsProject job, CancellationToken token = default) { try { - await new JobUpdateConfigurationCommand(context, jobName, job).RunAsync(token); + await new JobUpdateConfigurationCommand(client, jobName, job).RunAsync(token); } catch (Exception error) { throw new JenkinsNetException($"Failed to update Jenkins Job configuration '{jobName}'!", error); @@ -252,7 +255,7 @@ public async Task UpdateConfigurationAsync(string jobName, JenkinsProject job, C public void Delete(string jobName) { try { - new JobDeleteCommand(context, jobName).Run(); + new JobDeleteCommand(client, jobName).Run(); } catch (Exception error) { throw new JenkinsJobDeleteException($"Failed to delete Jenkins Job '{jobName}'!", error); @@ -269,7 +272,7 @@ public void Delete(string jobName) public async Task DeleteAsync(string jobName, CancellationToken token = default) { try { - await new JobDeleteCommand(context, jobName).RunAsync(token); + await new JobDeleteCommand(client, jobName).RunAsync(token); } catch (Exception error) { throw new JenkinsJobDeleteException($"Failed to delete Jenkins Job '{jobName}'!", error); diff --git a/Jenkins.Net/JenkinsClientQueue.cs b/Jenkins.Net/JenkinsClientQueue.cs index 6c44c3a..f5a5425 100644 --- a/Jenkins.Net/JenkinsClientQueue.cs +++ b/Jenkins.Net/JenkinsClientQueue.cs @@ -2,8 +2,11 @@ using JenkinsNET.Internal.Commands; using JenkinsNET.Models; using System; + +#if NET_ASYNC using System.Threading; using System.Threading.Tasks; +#endif namespace JenkinsNET { @@ -15,12 +18,12 @@ namespace JenkinsNET /// public sealed class JenkinsClientQueue { - private readonly IJenkinsContext context; + private readonly JenkinsClient client; - internal JenkinsClientQueue(IJenkinsContext context) + internal JenkinsClientQueue(JenkinsClient client) { - this.context = context; + this.client = client; } /// @@ -30,7 +33,7 @@ internal JenkinsClientQueue(IJenkinsContext context) public JenkinsQueueItem[] GetAllItems() { try { - var cmd = new QueueItemListCommand(context); + var cmd = new QueueItemListCommand(client); cmd.Run(); return cmd.Result; } @@ -48,7 +51,7 @@ public JenkinsQueueItem[] GetAllItems() public async Task GetAllItemsAsync(CancellationToken token = default) { try { - var cmd = new QueueItemListCommand(context); + var cmd = new QueueItemListCommand(client); await cmd.RunAsync(token); return cmd.Result; } @@ -66,7 +69,7 @@ public async Task GetAllItemsAsync(CancellationToken token = public JenkinsQueueItem GetItem(int itemNumber) { try { - var cmd = new QueueGetItemCommand(context, itemNumber); + var cmd = new QueueGetItemCommand(client, itemNumber); cmd.Run(); return cmd.Result; } @@ -85,7 +88,7 @@ public JenkinsQueueItem GetItem(int itemNumber) public async Task GetItemAsync(int itemNumber, CancellationToken token = default) { try { - var cmd = new QueueGetItemCommand(context, itemNumber); + var cmd = new QueueGetItemCommand(client, itemNumber); await cmd.RunAsync(token); return cmd.Result; } diff --git a/Jenkins.Net/Utilities/ProgressiveHtmlReader.cs b/Jenkins.Net/Utilities/ProgressiveHtmlReader.cs index ca5b132..731566a 100644 --- a/Jenkins.Net/Utilities/ProgressiveHtmlReader.cs +++ b/Jenkins.Net/Utilities/ProgressiveHtmlReader.cs @@ -1,6 +1,6 @@ using System; -#if !NET40 +#if NET_ASYNC using System.Threading.Tasks; #endif @@ -73,7 +73,7 @@ public void Update() IsComplete = true; } - #if !NET40 + #if NET_ASYNC /// /// Retrieves and appends any additional text returned /// by the running Jenkins Job asynchronously. diff --git a/Jenkins.Net/Utilities/ProgressiveTextReader.cs b/Jenkins.Net/Utilities/ProgressiveTextReader.cs index a29e69e..c002b1e 100644 --- a/Jenkins.Net/Utilities/ProgressiveTextReader.cs +++ b/Jenkins.Net/Utilities/ProgressiveTextReader.cs @@ -1,6 +1,6 @@ using System; -#if !NET40 +#if NET_ASYNC using System.Threading.Tasks; #endif @@ -73,7 +73,7 @@ public void Update() IsComplete = true; } - #if !NET40 + #if NET_ASYNC /// /// Retrieves and appends any additional text returned /// by the running Jenkins Job asynchronously.