Skip to content

Commit

Permalink
Feature/35 pipeline api (#73)
Browse files Browse the repository at this point in the history
* #35 Start with pipeline get implementation

* #35 Add create method

* #35 Add delete pipeline

* #35 add cancel implementation

* #35 Retry implementation

* #35 Make request properties readonly
  • Loading branch information
WebDucer authored and nmklotas committed Oct 12, 2019
1 parent a2577af commit fde4e25
Show file tree
Hide file tree
Showing 18 changed files with 496 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/GitLabApiClient/GitLabClient.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System;
using System.Threading.Tasks;
using GitLabApiClient.Internal.Http;
using GitLabApiClient.Internal.Http.Serialization;
using GitLabApiClient.Internal.Queries;
using GitLabApiClient.Internal.Utilities;
using GitLabApiClient.Models.Pipelines.Requests;
using GitLabApiClient.Models.Users.Responses;

namespace GitLabApiClient
Expand Down Expand Up @@ -46,6 +47,7 @@ public GitLabClient(string hostUrl, string authenticationToken = "")
var releaseQueryBuilder = new ReleaseQueryBuilder();
var tagQueryBuilder = new TagQueryBuilder();
var commitQueryBuilder = new CommitQueryBuilder();
var pipelineQueryBuilder = new PipelineQueryBuilder();

Issues = new IssuesClient(_httpFacade, issuesQueryBuilder, projectIssuesQueryBuilder, projectIssueNotesQueryBuilder);
Uploads = new UploadsClient(_httpFacade);
Expand All @@ -58,6 +60,7 @@ public GitLabClient(string hostUrl, string authenticationToken = "")
Tags = new TagClient(_httpFacade, tagQueryBuilder);
Commits = new CommitsClient(_httpFacade, commitQueryBuilder);
Markdown = new MarkdownClient(_httpFacade);
Pipelines = new PipelineClient(_httpFacade, pipelineQueryBuilder);
}

/// <summary>
Expand Down Expand Up @@ -115,6 +118,11 @@ public GitLabClient(string hostUrl, string authenticationToken = "")
/// </summary>
public MarkdownClient Markdown { get; }

/// <summary>
/// Acess GitLab's Pipeline API.
/// </summary>
public PipelineClient Pipelines { get; }

/// <summary>
/// Host address of GitLab instance. For example https://gitlab.example.com or https://gitlab.example.com/api/v4/.
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions src/GitLabApiClient/Models/Pipelines/PipelineStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Runtime.Serialization;

namespace GitLabApiClient.Models.Pipelines
{
public enum PipelineStatus
{
All,
[EnumMember(Value = "running")]
Running,
[EnumMember(Value = "pending")]
Pending,
[EnumMember(Value = "success")]
Success,
[EnumMember(Value = "failed")]
Failed,
[EnumMember(Value = "canceled")]
Canceled,
[EnumMember(Value = "skipped")]
Skipped
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using GitLabApiClient.Internal.Utilities;
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Pipelines.Requests
{
public abstract class BasePipelineRequest
{
protected BasePipelineRequest(string projectId, int pipelineId)
{
Guard.NotEmpty(projectId, nameof(projectId));
Guard.IsTrue(pipelineId > 0, nameof(pipelineId));

ProjectId = projectId;
PipelineId = pipelineId;
}

/// <summary>
/// The ID or URL-encoded path of the project owned by the authenticated user
/// </summary>
[JsonProperty("id")]
public string ProjectId { get; }

/// <summary>
/// The ID of a pipeline
/// </summary>
[JsonProperty("pipeline_id")]
public int PipelineId { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace GitLabApiClient.Models.Pipelines.Requests
{
public sealed class CancelPipelineRequest : BasePipelineRequest
{
public CancelPipelineRequest(string projectId, int pipelineId) : base(projectId, pipelineId)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Collections.Generic;
using GitLabApiClient.Internal.Utilities;
using GitLabApiClient.Models.Pipelines.Responses;
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Pipelines.Requests
{
public sealed class CreatePipelineRequest
{
public CreatePipelineRequest(string projectId, string reference, IList<PipelineVariable> variables = null)
{
Guard.NotEmpty(projectId, nameof(projectId));
Guard.NotEmpty(reference, nameof(reference));

ProjectId = projectId;
Reference = reference;

Variables = variables ?? new List<PipelineVariable>();
}

/// <summary>
/// The ID or URL-encoded path of the project owned by the authenticated user
/// </summary>
[JsonProperty("id")]
public string ProjectId { get; }

/// <summary>
/// Reference to commit
/// </summary>
[JsonProperty("ref")]
public string Reference { get; }

/// <summary>
/// An array containing the variables available in the pipeline, matching the structure [{ 'key' => 'UPLOAD_TO_S3',
/// 'variable_type' => 'file', 'value' => 'true' }]
/// </summary>
[JsonProperty("variables")]
public IList<PipelineVariable> Variables { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace GitLabApiClient.Models.Pipelines.Requests
{
public class DeletePipelineRequest : BasePipelineRequest
{
public DeletePipelineRequest(string projectId, int pipelineId) : base(projectId, pipelineId)
{
}
}
}
16 changes: 16 additions & 0 deletions src/GitLabApiClient/Models/Pipelines/Requests/PipelineOrder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Runtime.Serialization;

namespace GitLabApiClient.Models.Pipelines.Requests
{
public enum PipelineOrder
{
[EnumMember(Value = "id")]
Id,
[EnumMember(Value = "status")]
Status,
[EnumMember(Value = "ref")]
Ref,
[EnumMember(Value = "user_id")]
UserId
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using GitLabApiClient.Internal.Queries;
using GitLabApiClient.Internal.Utilities;

namespace GitLabApiClient.Models.Pipelines.Requests
{
internal sealed class PipelineQueryBuilder : QueryBuilder<PipelineQueryOptions>
{
#region Overrides of QueryBuilder<PipelineQueryOptions>

/// <inheritdoc />
protected override void BuildCore(PipelineQueryOptions options)
{
if (!options.Ref.IsNullOrEmpty())
{
Add("ref", options.Ref);
}

if (options.YamlErrors.HasValue)
{
Add("yaml_errors", options.YamlErrors.Value);
}

if (!options.Sha.IsNullOrEmpty())
{
Add("sha", options.Sha);
}

if (options.Status != PipelineStatus.All)
{
Add("status", options.Status.ToLowerCaseString());
}

if (options.Scope != PipelineScope.All)
{
Add("scope", options.Scope.ToLowerCaseString());
}

if (options.Order != PipelineOrder.Id)
{
Add("order_by", AsString(options.Order));
}

if (options.SortOrder != SortOrder.Descending)
{
Add("sort", "asc");
}
}

#endregion

private string AsString(PipelineOrder order)
{
switch (order)
{
case PipelineOrder.UserId:
return "user_id";
case PipelineOrder.Id:
case PipelineOrder.Ref:
case PipelineOrder.Status:
return order.ToLowerCaseString();
default:
throw new ArgumentException("Unknown order type", nameof(order));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace GitLabApiClient.Models.Pipelines.Requests
{
public class PipelineQueryOptions
{
internal PipelineQueryOptions() { }

/// <summary>
/// The scope of pipelines, one of: running, pending, finished, branches, tags
/// </summary>
public PipelineScope Scope { get; set; } = PipelineScope.All;

/// <summary>
/// The status of pipelines, one of: running, pending, success, failed, canceled, skipped
/// </summary>
public PipelineStatus Status { get; set; } = PipelineStatus.All;

/// <summary>
/// The ref of pipelines
/// </summary>
public string Ref { get; set; }

/// <summary>
/// The sha or pipelines
/// </summary>
public string Sha { get; set; }

/// <summary>
/// Returns pipelines with invalid configurations
/// </summary>
public bool? YamlErrors { get; set; }

/// <summary>
/// Order pipelines by id, status, ref, or user_id (default: id)
/// </summary>
public PipelineOrder Order { get; set; } = PipelineOrder.Id;

/// <summary>
/// Sort pipelines in asc or desc order (default: desc)
/// </summary>
public SortOrder SortOrder { get; set; } = SortOrder.Descending;
}
}
19 changes: 19 additions & 0 deletions src/GitLabApiClient/Models/Pipelines/Requests/PipelineScope.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Runtime.Serialization;

namespace GitLabApiClient.Models.Pipelines.Requests
{
public enum PipelineScope
{
All,
[EnumMember(Value = "running")]
Running,
[EnumMember(Value = "pending")]
Pending,
[EnumMember(Value = "finished")]
Finished,
[EnumMember(Value = "branches")]
Branches,
[EnumMember(Value = "tags")]
Tags
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace GitLabApiClient.Models.Pipelines.Requests
{
public sealed class RetryPipelineRequest : BasePipelineRequest
{
public RetryPipelineRequest(string projectId, int pipelineId) : base(projectId, pipelineId)
{
}
}
}
23 changes: 23 additions & 0 deletions src/GitLabApiClient/Models/Pipelines/Responses/Pipeline.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Pipelines.Responses
{
public class Pipeline
{
[JsonProperty("id")]
public int Id { get; set; }

[JsonProperty("status")]
public PipelineStatus Status { get; set; }

[JsonProperty("ref")]
public string Ref { get; set; }

[JsonProperty("sha")]
public string Sha { get; set; }

[JsonProperty("web_url")]
public Uri WebUrl { get; set; }
}
}
59 changes: 59 additions & 0 deletions src/GitLabApiClient/Models/Pipelines/Responses/PipelineDetail.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Text;
using GitLabApiClient.Models.Users.Responses;
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Pipelines.Responses
{
public class PipelineDetail
{
[JsonProperty("id")]
public int Id { get; set; }

[JsonProperty("sha")]
public string Sha { get; set; }

[JsonProperty("ref")]
public string Ref { get; set; }

[JsonProperty("status")]
public PipelineStatus Status { get; set; }

[JsonProperty("web_url")]
public Uri WebUrl { get; set; }

[JsonProperty("before_sha")]
public string BeforeSha { get; set; }

[JsonProperty("tag")]
public bool Tag { get; set; }

[JsonProperty("yaml_errors")]
public string YamlErrors { get; set; }

[JsonProperty("user")]
public PipelineUser User { get; set; }

[JsonProperty("created_at")]
public DateTime CreatedAt { get; set; }

[JsonProperty("updated_at")]
public DateTime? UpdatedAt { get; set; }

[JsonProperty("started_at")]
public DateTime? StartedAt { get; set; }

[JsonProperty("finished_at")]
public DateTime? FinishedAt { get; set; }

[JsonProperty("committed_at")]
public DateTime? CommittedAt { get; set; }

[JsonProperty("duration")]
public int Duration { get; set; }

[JsonProperty("coverage")]
public string Coverage { get; set; }
}
}
Loading

0 comments on commit fde4e25

Please sign in to comment.