Skip to content

Commit 8d8e88c

Browse files
jadentomjetersen
andauthored
Add runners and job models and some queries (#93)
Co-authored-by: Joseph Petersen <josephp90@gmail.com>
1 parent 7a5188e commit 8d8e88c

File tree

13 files changed

+299
-5
lines changed

13 files changed

+299
-5
lines changed

src/GitLabApiClient/GitLabClient.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using GitLabApiClient.Internal.Http.Serialization;
55
using GitLabApiClient.Internal.Queries;
66
using GitLabApiClient.Internal.Utilities;
7+
using GitLabApiClient.Models.Job.Requests;
78
using GitLabApiClient.Models.Oauth.Requests;
89
using GitLabApiClient.Models.Oauth.Responses;
910
using GitLabApiClient.Models.Pipelines.Requests;
@@ -51,11 +52,12 @@ public GitLabClient(string hostUrl, string authenticationToken = "")
5152
var commitRefsQueryBuilder = new CommitRefsQueryBuilder();
5253
var pipelineQueryBuilder = new PipelineQueryBuilder();
5354
var treeQueryBuilder = new TreeQueryBuilder();
55+
var jobQueryBuilder = new JobQueryBuilder();
5456

5557
Issues = new IssuesClient(_httpFacade, issuesQueryBuilder, projectIssueNotesQueryBuilder);
5658
Uploads = new UploadsClient(_httpFacade);
5759
MergeRequests = new MergeRequestsClient(_httpFacade, mergeRequestsQueryBuilder, projectMergeRequestsQueryBuilder);
58-
Projects = new ProjectsClient(_httpFacade, projectQueryBuilder, projectMilestonesQueryBuilder);
60+
Projects = new ProjectsClient(_httpFacade, projectQueryBuilder, projectMilestonesQueryBuilder, jobQueryBuilder);
5961
Users = new UsersClient(_httpFacade);
6062
Groups = new GroupsClient(_httpFacade, groupsQueryBuilder, projectsGroupsQueryBuilder, projectMilestonesQueryBuilder, groupLabelsQueryBuilder);
6163
Branches = new BranchClient(_httpFacade, branchQueryBuilder);
@@ -64,9 +66,10 @@ public GitLabClient(string hostUrl, string authenticationToken = "")
6466
Webhooks = new WebhookClient(_httpFacade);
6567
Commits = new CommitsClient(_httpFacade, commitQueryBuilder, commitRefsQueryBuilder);
6668
Markdown = new MarkdownClient(_httpFacade);
67-
Pipelines = new PipelineClient(_httpFacade, pipelineQueryBuilder);
69+
Pipelines = new PipelineClient(_httpFacade, pipelineQueryBuilder, jobQueryBuilder);
6870
Trees = new TreesClient(_httpFacade, treeQueryBuilder);
6971
Files = new FilesClient(_httpFacade);
72+
Runners = new RunnersClient(_httpFacade);
7073
}
7174

7275
/// <summary>
@@ -144,6 +147,11 @@ public GitLabClient(string hostUrl, string authenticationToken = "")
144147
/// </summary>
145148
public PipelineClient Pipelines { get; }
146149

150+
/// <summary>
151+
/// Access GitLab's Runners API.
152+
/// </summary>
153+
public RunnersClient Runners { get; }
154+
147155
/// <summary>
148156
/// Host address of GitLab instance. For example https://gitlab.example.com or https://gitlab.example.com/api/v4/.
149157
/// </summary>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using GitLabApiClient.Internal.Queries;
3+
using GitLabApiClient.Internal.Utilities;
4+
5+
namespace GitLabApiClient.Models.Job.Requests
6+
{
7+
internal sealed class JobQueryBuilder : QueryBuilder<JobQueryOptions>
8+
{
9+
#region Overrides of QueryBuilder<PipelineQueryOptions>
10+
11+
/// <inheritdoc />
12+
protected override void BuildCore(JobQueryOptions options)
13+
{
14+
if (options.Scope != JobScope.All)
15+
{
16+
Add("scope", options.Scope.ToLowerCaseString());
17+
}
18+
}
19+
20+
#endregion
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace GitLabApiClient.Models.Job.Requests
2+
{
3+
public class JobQueryOptions
4+
{
5+
internal JobQueryOptions() { }
6+
7+
/// <summary>
8+
/// The scope of jobs, one of: running, pending, finished, branches, tags
9+
/// </summary>
10+
public JobScope Scope { get; set; } = JobScope.All;
11+
}
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace GitLabApiClient.Models.Job.Requests
4+
{
5+
public enum JobScope
6+
{
7+
All,
8+
[EnumMember(Value = "created")]
9+
Created,
10+
[EnumMember(Value = "pending")]
11+
Pending,
12+
[EnumMember(Value = "running")]
13+
Running,
14+
[EnumMember(Value = "failed")]
15+
Failed,
16+
[EnumMember(Value = "success")]
17+
Success,
18+
[EnumMember(Value = "canceled")]
19+
Canceled,
20+
[EnumMember(Value = "skipped")]
21+
Skipped,
22+
[EnumMember(Value = "manual")]
23+
Manual,
24+
}
25+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using GitLabApiClient.Models.Commits.Responses;
3+
using GitLabApiClient.Models.Pipelines.Responses;
4+
using GitLabApiClient.Models.Runners.Responses;
5+
using GitLabApiClient.Models.Users.Responses;
6+
using Newtonsoft.Json;
7+
8+
namespace GitLabApiClient.Models.Job.Responses
9+
{
10+
public sealed class Job
11+
{
12+
[JsonProperty("allow_failure")]
13+
public bool AllowFailure { get; set; }
14+
15+
[JsonProperty("artifacts_expire_at")]
16+
public DateTime ArtifactsExpireAt { get; set; }
17+
18+
[JsonProperty("commit")]
19+
public Commit Commit { get; set; }
20+
21+
[JsonProperty("created_at")]
22+
public DateTime CreatedAt { get; set; }
23+
24+
[JsonProperty("duration")]
25+
public double Duration { get; set; }
26+
27+
[JsonProperty("finished_at")]
28+
public DateTime? FinishedAt { get; set; }
29+
30+
[JsonProperty("id")]
31+
public int Id { get; set; }
32+
33+
[JsonProperty("name")]
34+
public string Name { get; set; }
35+
36+
[JsonProperty("pipeline")]
37+
public Pipeline Pipeline { get; set; }
38+
39+
[JsonProperty("ref")]
40+
public string Ref { get; set; }
41+
42+
[JsonProperty("runner")]
43+
public Runner Runner { get; set; }
44+
45+
[JsonProperty("stage")]
46+
public string Stage { get; set; }
47+
48+
[JsonProperty("started_at")]
49+
public DateTime? StartedAt { get; set; }
50+
51+
[JsonProperty("status")]
52+
public string Status { get; set; }
53+
54+
[JsonProperty("user")]
55+
public User User { get; set; }
56+
57+
[JsonProperty("web_url")]
58+
public string WebUrl { get; set; }
59+
}
60+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace GitLabApiClient.Models.Runners.Requests
2+
{
3+
public class RunnerQueryOptions
4+
{
5+
internal RunnerQueryOptions() { }
6+
}
7+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace GitLabApiClient.Models.Runners.Requests
4+
{
5+
public enum RunnerStatus
6+
{
7+
All,
8+
[EnumMember(Value = "active")]
9+
Active,
10+
[EnumMember(Value = "paused")]
11+
Paused,
12+
[EnumMember(Value = "online")]
13+
Online,
14+
[EnumMember(Value = "offline")]
15+
Offline,
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace GitLabApiClient.Models.Runners.Requests
4+
{
5+
public enum RunnerType
6+
{
7+
All,
8+
[EnumMember(Value = "instance_type")]
9+
InstanceType,
10+
[EnumMember(Value = "group_type")]
11+
GroupType,
12+
[EnumMember(Value = "project_type")]
13+
ProjectType,
14+
}
15+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Newtonsoft.Json;
2+
3+
namespace GitLabApiClient.Models.Runners.Responses
4+
{
5+
public sealed class Runner
6+
{
7+
[JsonProperty("active")]
8+
public bool Active { get; set; }
9+
10+
[JsonProperty("description")]
11+
public string Description { get; set; }
12+
13+
[JsonProperty("id")]
14+
public int Id { get; set; }
15+
16+
[JsonProperty("is_shared")]
17+
public bool IsShared { get; set; }
18+
19+
[JsonProperty("ip_address")]
20+
public string IpAddresses { get; set; }
21+
22+
[JsonProperty("name")]
23+
public string Name { get; set; }
24+
25+
[JsonProperty("online")]
26+
public bool Online { get; set; }
27+
28+
[JsonProperty("status")]
29+
public string Status { get; set; }
30+
}
31+
}

src/GitLabApiClient/PipelineClient.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Threading.Tasks;
44
using GitLabApiClient.Internal.Http;
55
using GitLabApiClient.Internal.Paths;
6+
using GitLabApiClient.Models.Job.Requests;
7+
using GitLabApiClient.Models.Job.Responses;
68
using GitLabApiClient.Models.Pipelines.Requests;
79
using GitLabApiClient.Models.Pipelines.Responses;
810

@@ -12,11 +14,13 @@ public sealed class PipelineClient
1214
{
1315
private readonly GitLabHttpFacade _httpFacade;
1416
private readonly PipelineQueryBuilder _queryBuilder;
17+
private readonly JobQueryBuilder _jobQueryBuilder;
1518

16-
internal PipelineClient(GitLabHttpFacade httpFacade, PipelineQueryBuilder queryBuilder)
19+
internal PipelineClient(GitLabHttpFacade httpFacade, PipelineQueryBuilder queryBuilder, JobQueryBuilder jobQueryBuilder)
1720
{
1821
_httpFacade = httpFacade;
1922
_queryBuilder = queryBuilder;
23+
_jobQueryBuilder = jobQueryBuilder;
2024
}
2125

2226
public async Task<PipelineDetail> GetAsync(ProjectId projectId, int pipelineId) =>
@@ -34,6 +38,15 @@ public async Task<IList<Pipeline>> GetAsync(ProjectId projectId, Action<Pipeline
3438
public async Task<IList<PipelineVariable>> GetVariablesAsync(ProjectId projectId, int pipelineId) =>
3539
await _httpFacade.Get<IList<PipelineVariable>>($"projects/{projectId}/pipelines/{pipelineId}/variables");
3640

41+
public async Task<IList<Job>> GetJobsAsync(ProjectId projectId, int pipelineId, Action<JobQueryOptions> options = null)
42+
{
43+
var queryOptions = new JobQueryOptions();
44+
options?.Invoke(queryOptions);
45+
46+
var url = _jobQueryBuilder.Build($"projects/{projectId}/pipelines/{pipelineId}/jobs", queryOptions);
47+
return await _httpFacade.GetPagedList<Job>(url);
48+
}
49+
3750
public async Task<PipelineDetail> CreateAsync(ProjectId projectId, CreatePipelineRequest request) =>
3851
await _httpFacade.Post<PipelineDetail>($"projects/{projectId}/pipeline", request);
3952

0 commit comments

Comments
 (0)