Skip to content

Add Jenkins view creation functionality #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Jenkins.Net/IJenkinsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public interface IJenkinsClient
/// </summary>
JenkinsClientArtifacts Artifacts {get;}

/// <summary>
/// Group of methods for interacting with Jenkins Views.
/// </summary>
JenkinsClientViews Views { get; }

/// <summary>
/// Updates the security Crumb attached to this client.
/// </summary>
Expand Down
42 changes: 42 additions & 0 deletions Jenkins.Net/Internal/Commands/ViewCreateCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using JenkinsNET.Models;
using System;

namespace JenkinsNET.Internal.Commands
{
internal class ViewCreateCommand : JenkinsHttpCommand
{
public ViewCreateCommand(IJenkinsContext context, string viewName, JenkinsProject view)
{
if (context == null)
throw new ArgumentNullException(nameof(context));

if (string.IsNullOrEmpty(viewName))
throw new ArgumentException("Value cannot be empty!", nameof(viewName));

if (view == null)
throw new ArgumentNullException(nameof(view));

Url = NetPath.Combine(context.BaseUrl, "createView")
+ NetPath.Query(new { name = viewName });

UserName = context.UserName;
Password = context.ApiToken;
Crumb = context.Crumb;

OnWrite = request =>
{
request.Method = "POST";
request.ContentType = "application/xml";
WriteXml(request, view.Node);
};

#if NET_ASYNC
OnWriteAsync = async (request, token) => {
request.Method = "POST";
request.ContentType = "application/xml";
await WriteXmlAsync(request, view.Node, token);
};
#endif
}
}
}
5 changes: 5 additions & 0 deletions Jenkins.Net/JenkinsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public string Password {
/// </summary>
public JenkinsClientArtifacts Artifacts {get;}

/// <summary>
/// Group of methods for interacting with Jenkins Views.
/// </summary>
public JenkinsClientViews Views { get; }

/// <summary>
/// Creates a new Jenkins Client.
Expand All @@ -75,6 +79,7 @@ public JenkinsClient()
Builds = new JenkinsClientBuilds(this);
Queue = new JenkinsClientQueue(this);
Artifacts = new JenkinsClientArtifacts(this);
Views = new JenkinsClientViews(this);
}

/// <summary>
Expand Down
64 changes: 64 additions & 0 deletions Jenkins.Net/JenkinsClientViews.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using JenkinsNET.Exceptions;
using JenkinsNET.Internal.Commands;
using JenkinsNET.Models;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace JenkinsNET
{
/// <summary>
/// A collection of methods used for interacting with Jenkins Views API.
/// </summary>
/// <remarks>
/// Used internally by <seealso cref="JenkinsClient"/>
/// </remarks>
public sealed class JenkinsClientViews
{
private readonly IJenkinsContext _context;

internal JenkinsClientViews(IJenkinsContext context)
{
_context = context;
}

/// <summary>
/// Creates a new View in Jenkins.
/// </summary>
/// <param name="viewName">The name of the View to create.</param>
/// <param name="view">The description of the Job to create.</param>
/// <exception cref="JenkinsNetException"></exception>
public void Create(string viewName, JenkinsProject view)
{
try
{
new ViewCreateCommand(_context, viewName, view).Run();
}
catch (Exception error)
{
throw new JenkinsNetException($"Failed to create Jenkins View '{viewName}'!", error);
}
}

#if NET_ASYNC
/// <summary>
/// Creates a new View in Jenkins asynchronously.
/// </summary>
/// <param name="viewName">The name of the View to create.</param>
/// <param name="view">The description of the View to create.</param>
/// <param name="token">An optional token for aborting the request.</param>
/// <exception cref="JenkinsNetException"></exception>
public async Task CreateAsync(string viewName, JenkinsProject view, CancellationToken token = default)
{
try
{
await new ViewCreateCommand(_context, viewName, view).RunAsync(token);
}
catch (Exception error)
{
throw new JenkinsNetException($"Failed to create Jenkins Job '{viewName}'!", error);
}
}
#endif
}
}