Skip to content

Commit fde4e25

Browse files
WebDucernmklotas
authored andcommitted
Feature/35 pipeline api (#73)
* #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
1 parent a2577af commit fde4e25

18 files changed

+496
-1
lines changed

src/GitLabApiClient/GitLabClient.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using System;
1+
using System;
22
using System.Threading.Tasks;
33
using GitLabApiClient.Internal.Http;
44
using GitLabApiClient.Internal.Http.Serialization;
55
using GitLabApiClient.Internal.Queries;
66
using GitLabApiClient.Internal.Utilities;
7+
using GitLabApiClient.Models.Pipelines.Requests;
78
using GitLabApiClient.Models.Users.Responses;
89

910
namespace GitLabApiClient
@@ -46,6 +47,7 @@ public GitLabClient(string hostUrl, string authenticationToken = "")
4647
var releaseQueryBuilder = new ReleaseQueryBuilder();
4748
var tagQueryBuilder = new TagQueryBuilder();
4849
var commitQueryBuilder = new CommitQueryBuilder();
50+
var pipelineQueryBuilder = new PipelineQueryBuilder();
4951

5052
Issues = new IssuesClient(_httpFacade, issuesQueryBuilder, projectIssuesQueryBuilder, projectIssueNotesQueryBuilder);
5153
Uploads = new UploadsClient(_httpFacade);
@@ -58,6 +60,7 @@ public GitLabClient(string hostUrl, string authenticationToken = "")
5860
Tags = new TagClient(_httpFacade, tagQueryBuilder);
5961
Commits = new CommitsClient(_httpFacade, commitQueryBuilder);
6062
Markdown = new MarkdownClient(_httpFacade);
63+
Pipelines = new PipelineClient(_httpFacade, pipelineQueryBuilder);
6164
}
6265

6366
/// <summary>
@@ -115,6 +118,11 @@ public GitLabClient(string hostUrl, string authenticationToken = "")
115118
/// </summary>
116119
public MarkdownClient Markdown { get; }
117120

121+
/// <summary>
122+
/// Acess GitLab's Pipeline API.
123+
/// </summary>
124+
public PipelineClient Pipelines { get; }
125+
118126
/// <summary>
119127
/// Host address of GitLab instance. For example https://gitlab.example.com or https://gitlab.example.com/api/v4/.
120128
/// </summary>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace GitLabApiClient.Models.Pipelines
4+
{
5+
public enum PipelineStatus
6+
{
7+
All,
8+
[EnumMember(Value = "running")]
9+
Running,
10+
[EnumMember(Value = "pending")]
11+
Pending,
12+
[EnumMember(Value = "success")]
13+
Success,
14+
[EnumMember(Value = "failed")]
15+
Failed,
16+
[EnumMember(Value = "canceled")]
17+
Canceled,
18+
[EnumMember(Value = "skipped")]
19+
Skipped
20+
}
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using GitLabApiClient.Internal.Utilities;
2+
using Newtonsoft.Json;
3+
4+
namespace GitLabApiClient.Models.Pipelines.Requests
5+
{
6+
public abstract class BasePipelineRequest
7+
{
8+
protected BasePipelineRequest(string projectId, int pipelineId)
9+
{
10+
Guard.NotEmpty(projectId, nameof(projectId));
11+
Guard.IsTrue(pipelineId > 0, nameof(pipelineId));
12+
13+
ProjectId = projectId;
14+
PipelineId = pipelineId;
15+
}
16+
17+
/// <summary>
18+
/// The ID or URL-encoded path of the project owned by the authenticated user
19+
/// </summary>
20+
[JsonProperty("id")]
21+
public string ProjectId { get; }
22+
23+
/// <summary>
24+
/// The ID of a pipeline
25+
/// </summary>
26+
[JsonProperty("pipeline_id")]
27+
public int PipelineId { get; }
28+
}
29+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace GitLabApiClient.Models.Pipelines.Requests
2+
{
3+
public sealed class CancelPipelineRequest : BasePipelineRequest
4+
{
5+
public CancelPipelineRequest(string projectId, int pipelineId) : base(projectId, pipelineId)
6+
{
7+
}
8+
}
9+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Collections.Generic;
2+
using GitLabApiClient.Internal.Utilities;
3+
using GitLabApiClient.Models.Pipelines.Responses;
4+
using Newtonsoft.Json;
5+
6+
namespace GitLabApiClient.Models.Pipelines.Requests
7+
{
8+
public sealed class CreatePipelineRequest
9+
{
10+
public CreatePipelineRequest(string projectId, string reference, IList<PipelineVariable> variables = null)
11+
{
12+
Guard.NotEmpty(projectId, nameof(projectId));
13+
Guard.NotEmpty(reference, nameof(reference));
14+
15+
ProjectId = projectId;
16+
Reference = reference;
17+
18+
Variables = variables ?? new List<PipelineVariable>();
19+
}
20+
21+
/// <summary>
22+
/// The ID or URL-encoded path of the project owned by the authenticated user
23+
/// </summary>
24+
[JsonProperty("id")]
25+
public string ProjectId { get; }
26+
27+
/// <summary>
28+
/// Reference to commit
29+
/// </summary>
30+
[JsonProperty("ref")]
31+
public string Reference { get; }
32+
33+
/// <summary>
34+
/// An array containing the variables available in the pipeline, matching the structure [{ 'key' => 'UPLOAD_TO_S3',
35+
/// 'variable_type' => 'file', 'value' => 'true' }]
36+
/// </summary>
37+
[JsonProperty("variables")]
38+
public IList<PipelineVariable> Variables { get; }
39+
}
40+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace GitLabApiClient.Models.Pipelines.Requests
2+
{
3+
public class DeletePipelineRequest : BasePipelineRequest
4+
{
5+
public DeletePipelineRequest(string projectId, int pipelineId) : base(projectId, pipelineId)
6+
{
7+
}
8+
}
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace GitLabApiClient.Models.Pipelines.Requests
4+
{
5+
public enum PipelineOrder
6+
{
7+
[EnumMember(Value = "id")]
8+
Id,
9+
[EnumMember(Value = "status")]
10+
Status,
11+
[EnumMember(Value = "ref")]
12+
Ref,
13+
[EnumMember(Value = "user_id")]
14+
UserId
15+
}
16+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using GitLabApiClient.Internal.Queries;
3+
using GitLabApiClient.Internal.Utilities;
4+
5+
namespace GitLabApiClient.Models.Pipelines.Requests
6+
{
7+
internal sealed class PipelineQueryBuilder : QueryBuilder<PipelineQueryOptions>
8+
{
9+
#region Overrides of QueryBuilder<PipelineQueryOptions>
10+
11+
/// <inheritdoc />
12+
protected override void BuildCore(PipelineQueryOptions options)
13+
{
14+
if (!options.Ref.IsNullOrEmpty())
15+
{
16+
Add("ref", options.Ref);
17+
}
18+
19+
if (options.YamlErrors.HasValue)
20+
{
21+
Add("yaml_errors", options.YamlErrors.Value);
22+
}
23+
24+
if (!options.Sha.IsNullOrEmpty())
25+
{
26+
Add("sha", options.Sha);
27+
}
28+
29+
if (options.Status != PipelineStatus.All)
30+
{
31+
Add("status", options.Status.ToLowerCaseString());
32+
}
33+
34+
if (options.Scope != PipelineScope.All)
35+
{
36+
Add("scope", options.Scope.ToLowerCaseString());
37+
}
38+
39+
if (options.Order != PipelineOrder.Id)
40+
{
41+
Add("order_by", AsString(options.Order));
42+
}
43+
44+
if (options.SortOrder != SortOrder.Descending)
45+
{
46+
Add("sort", "asc");
47+
}
48+
}
49+
50+
#endregion
51+
52+
private string AsString(PipelineOrder order)
53+
{
54+
switch (order)
55+
{
56+
case PipelineOrder.UserId:
57+
return "user_id";
58+
case PipelineOrder.Id:
59+
case PipelineOrder.Ref:
60+
case PipelineOrder.Status:
61+
return order.ToLowerCaseString();
62+
default:
63+
throw new ArgumentException("Unknown order type", nameof(order));
64+
}
65+
}
66+
}
67+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace GitLabApiClient.Models.Pipelines.Requests
2+
{
3+
public class PipelineQueryOptions
4+
{
5+
internal PipelineQueryOptions() { }
6+
7+
/// <summary>
8+
/// The scope of pipelines, one of: running, pending, finished, branches, tags
9+
/// </summary>
10+
public PipelineScope Scope { get; set; } = PipelineScope.All;
11+
12+
/// <summary>
13+
/// The status of pipelines, one of: running, pending, success, failed, canceled, skipped
14+
/// </summary>
15+
public PipelineStatus Status { get; set; } = PipelineStatus.All;
16+
17+
/// <summary>
18+
/// The ref of pipelines
19+
/// </summary>
20+
public string Ref { get; set; }
21+
22+
/// <summary>
23+
/// The sha or pipelines
24+
/// </summary>
25+
public string Sha { get; set; }
26+
27+
/// <summary>
28+
/// Returns pipelines with invalid configurations
29+
/// </summary>
30+
public bool? YamlErrors { get; set; }
31+
32+
/// <summary>
33+
/// Order pipelines by id, status, ref, or user_id (default: id)
34+
/// </summary>
35+
public PipelineOrder Order { get; set; } = PipelineOrder.Id;
36+
37+
/// <summary>
38+
/// Sort pipelines in asc or desc order (default: desc)
39+
/// </summary>
40+
public SortOrder SortOrder { get; set; } = SortOrder.Descending;
41+
}
42+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace GitLabApiClient.Models.Pipelines.Requests
4+
{
5+
public enum PipelineScope
6+
{
7+
All,
8+
[EnumMember(Value = "running")]
9+
Running,
10+
[EnumMember(Value = "pending")]
11+
Pending,
12+
[EnumMember(Value = "finished")]
13+
Finished,
14+
[EnumMember(Value = "branches")]
15+
Branches,
16+
[EnumMember(Value = "tags")]
17+
Tags
18+
}
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace GitLabApiClient.Models.Pipelines.Requests
2+
{
3+
public sealed class RetryPipelineRequest : BasePipelineRequest
4+
{
5+
public RetryPipelineRequest(string projectId, int pipelineId) : base(projectId, pipelineId)
6+
{
7+
}
8+
}
9+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
4+
namespace GitLabApiClient.Models.Pipelines.Responses
5+
{
6+
public class Pipeline
7+
{
8+
[JsonProperty("id")]
9+
public int Id { get; set; }
10+
11+
[JsonProperty("status")]
12+
public PipelineStatus Status { get; set; }
13+
14+
[JsonProperty("ref")]
15+
public string Ref { get; set; }
16+
17+
[JsonProperty("sha")]
18+
public string Sha { get; set; }
19+
20+
[JsonProperty("web_url")]
21+
public Uri WebUrl { get; set; }
22+
}
23+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using GitLabApiClient.Models.Users.Responses;
5+
using Newtonsoft.Json;
6+
7+
namespace GitLabApiClient.Models.Pipelines.Responses
8+
{
9+
public class PipelineDetail
10+
{
11+
[JsonProperty("id")]
12+
public int Id { get; set; }
13+
14+
[JsonProperty("sha")]
15+
public string Sha { get; set; }
16+
17+
[JsonProperty("ref")]
18+
public string Ref { get; set; }
19+
20+
[JsonProperty("status")]
21+
public PipelineStatus Status { get; set; }
22+
23+
[JsonProperty("web_url")]
24+
public Uri WebUrl { get; set; }
25+
26+
[JsonProperty("before_sha")]
27+
public string BeforeSha { get; set; }
28+
29+
[JsonProperty("tag")]
30+
public bool Tag { get; set; }
31+
32+
[JsonProperty("yaml_errors")]
33+
public string YamlErrors { get; set; }
34+
35+
[JsonProperty("user")]
36+
public PipelineUser User { get; set; }
37+
38+
[JsonProperty("created_at")]
39+
public DateTime CreatedAt { get; set; }
40+
41+
[JsonProperty("updated_at")]
42+
public DateTime? UpdatedAt { get; set; }
43+
44+
[JsonProperty("started_at")]
45+
public DateTime? StartedAt { get; set; }
46+
47+
[JsonProperty("finished_at")]
48+
public DateTime? FinishedAt { get; set; }
49+
50+
[JsonProperty("committed_at")]
51+
public DateTime? CommittedAt { get; set; }
52+
53+
[JsonProperty("duration")]
54+
public int Duration { get; set; }
55+
56+
[JsonProperty("coverage")]
57+
public string Coverage { get; set; }
58+
}
59+
}

0 commit comments

Comments
 (0)