Skip to content
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

add get commit statuses #128

Merged
merged 3 commits into from
Apr 9, 2020
Merged
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
20 changes: 19 additions & 1 deletion src/GitLabApiClient/CommitsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ public sealed class CommitsClient
private readonly GitLabHttpFacade _httpFacade;
private readonly CommitQueryBuilder _commitQueryBuilder;
private readonly CommitRefsQueryBuilder _commitRefsQueryBuilder;
private readonly CommitStatusesQueryBuilder _commitStatusesQueryBuilder;

internal CommitsClient(GitLabHttpFacade httpFacade, CommitQueryBuilder commitQueryBuilder, CommitRefsQueryBuilder commitRefsQueryBuilder)
internal CommitsClient(GitLabHttpFacade httpFacade, CommitQueryBuilder commitQueryBuilder, CommitRefsQueryBuilder commitRefsQueryBuilder, CommitStatusesQueryBuilder commitStatusesQueryBuilder)
{
_httpFacade = httpFacade;
_commitQueryBuilder = commitQueryBuilder;
_commitRefsQueryBuilder = commitRefsQueryBuilder;
_commitStatusesQueryBuilder = commitStatusesQueryBuilder;
}

/// <summary>
Expand Down Expand Up @@ -74,5 +76,21 @@ public async Task<IList<Diff>> GetDiffsAsync(ProjectId projectId, string sha)
string url = $"projects/{projectId}/repository/commits/{sha}/diff";
return await _httpFacade.GetPagedList<Diff>(url);
}

/// <summary>
/// Retrieve a list of statuses in this commit
/// </summary>
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
/// <param name="options">Query Options <see cref="CommitStatusesQueryOptions"/>.</param>
/// <param name="sha">The commit hash</param>
/// <returns></returns>
public async Task<IList<CommitStatuses>> GetStatusesAsync(ProjectId projectId, string sha, Action<CommitStatusesQueryOptions> options = null)
{
var queryOptions = new CommitStatusesQueryOptions();
options?.Invoke(queryOptions);

string url = _commitStatusesQueryBuilder.Build($"projects/{projectId}/repository/commits/{sha}/statuses", queryOptions);
return await _httpFacade.GetPagedList<CommitStatuses>(url);
}
}
}
3 changes: 2 additions & 1 deletion src/GitLabApiClient/GitLabClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public GitLabClient(string hostUrl, string authenticationToken = "")
var tagQueryBuilder = new TagQueryBuilder();
var commitQueryBuilder = new CommitQueryBuilder();
var commitRefsQueryBuilder = new CommitRefsQueryBuilder();
var commitStatusesQueryBuilder = new CommitStatusesQueryBuilder();
var pipelineQueryBuilder = new PipelineQueryBuilder();
var treeQueryBuilder = new TreeQueryBuilder();
var jobQueryBuilder = new JobQueryBuilder();
Expand All @@ -64,7 +65,7 @@ public GitLabClient(string hostUrl, string authenticationToken = "")
Releases = new ReleaseClient(_httpFacade, releaseQueryBuilder);
Tags = new TagClient(_httpFacade, tagQueryBuilder);
Webhooks = new WebhookClient(_httpFacade);
Commits = new CommitsClient(_httpFacade, commitQueryBuilder, commitRefsQueryBuilder);
Commits = new CommitsClient(_httpFacade, commitQueryBuilder, commitRefsQueryBuilder, commitStatusesQueryBuilder);
Markdown = new MarkdownClient(_httpFacade);
Pipelines = new PipelineClient(_httpFacade, pipelineQueryBuilder, jobQueryBuilder);
Trees = new TreesClient(_httpFacade, treeQueryBuilder);
Expand Down
24 changes: 24 additions & 0 deletions src/GitLabApiClient/Internal/Queries/CommitStatusesQueryBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using GitLabApiClient.Internal.Utilities;
using GitLabApiClient.Models.Commits.Requests;

namespace GitLabApiClient.Internal.Queries
{
internal class CommitStatusesQueryBuilder : QueryBuilder<CommitStatusesQueryOptions>
{
protected override void BuildCore(CommitStatusesQueryOptions options)
{
if (!string.IsNullOrEmpty(options.Ref))
Add("ref", options.Ref);

if (options.Name.IsNotNullOrEmpty())
Add("name", options.Name);

if (options.Stage.IsNotNullOrEmpty())
Add("stage", options.Stage);

if (options.All.HasValue)
Add("all", options.All.Value);

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace GitLabApiClient.Models.Commits.Requests
{
public sealed class CommitStatusesQueryOptions
{
public string Ref { get; set; }

public string Stage { get; set; }

public string Name { get; set; }

public bool? All { get; set; }

internal CommitStatusesQueryOptions()
{
}
}
}
39 changes: 39 additions & 0 deletions src/GitLabApiClient/Models/Commits/Responses/CommitStatuses.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Text;
using GitLabApiClient.Models.Releases.Responses;
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Commits.Responses
{
public sealed class CommitStatuses
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("sha")]
public string Sha { get; set; }
[JsonProperty("ref")]
public string Ref { get; set; }
[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("target_url")]
public string Target_url { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("created_at")]
public DateTime Created_at { get; set; }
[JsonProperty("started_at")]
public DateTime Started_at { get; set; }
[JsonProperty("finished_at")]
public DateTime Finished_at { get; set; }
[JsonProperty("allow_failure")]
public bool Allow_failure { get; set; }
[JsonProperty("coverage")]
public float Coverage { get; set; }
[JsonProperty("author")]
public Author Author { get; set; }

}
}
29 changes: 29 additions & 0 deletions src/GitLabApiClient/Models/Commits/Responses/author.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using Newtonsoft.Json;

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

[JsonProperty("state")]
public string State { get; set; }

[JsonProperty("avatar_url")]
public string AvatarUrl { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

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

[JsonProperty("username")]
public string Username { get; set; }

[JsonProperty("web_url")]
public string WebUrl { get; set; }
}
}
38 changes: 35 additions & 3 deletions test/GitLabApiClient.Test/CommitsClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async void GetCommitBySha()
using (var client = new HttpClient(handler) { BaseAddress = new Uri(gitlabServer) })
{
var gitlabHttpFacade = new GitLabHttpFacade(new RequestsJsonSerializer(), client);
var commitsClient = new CommitsClient(gitlabHttpFacade, new CommitQueryBuilder(), new CommitRefsQueryBuilder());
var commitsClient = new CommitsClient(gitlabHttpFacade, new CommitQueryBuilder(), new CommitRefsQueryBuilder(), new CommitStatusesQueryBuilder());

var commitFromClient = await commitsClient.GetAsync(projectId, sha);
commitFromClient.Id.Should().BeEquivalentTo(sha);
Expand All @@ -52,7 +52,7 @@ public async void GetCommitsByRefName()
using (var client = new HttpClient(handler) { BaseAddress = new Uri(gitlabServer) })
{
var gitlabHttpFacade = new GitLabHttpFacade(new RequestsJsonSerializer(), client);
var commitsClient = new CommitsClient(gitlabHttpFacade, new CommitQueryBuilder(), new CommitRefsQueryBuilder());
var commitsClient = new CommitsClient(gitlabHttpFacade, new CommitQueryBuilder(), new CommitRefsQueryBuilder(), new CommitStatusesQueryBuilder());

var commitsFromClient = await commitsClient.GetAsync(projectId, o => o.RefName = refName);
commitsFromClient[0].Id.Should().BeEquivalentTo("id1");
Expand All @@ -76,7 +76,7 @@ public async void GetDiffsForCommit()
using (var client = new HttpClient(handler) { BaseAddress = new Uri(gitlabServer) })
{
var gitlabHttpFacade = new GitLabHttpFacade(new RequestsJsonSerializer(), client);
var commitsClient = new CommitsClient(gitlabHttpFacade, new CommitQueryBuilder(), new CommitRefsQueryBuilder());
var commitsClient = new CommitsClient(gitlabHttpFacade, new CommitQueryBuilder(), new CommitRefsQueryBuilder(), new CommitStatusesQueryBuilder());

var diffsFromClient = await commitsClient.GetDiffsAsync(projectId, sha);
diffsFromClient[0].DiffText.Should().BeEquivalentTo("diff1");
Expand All @@ -99,5 +99,37 @@ public async void GetDiffsForCommit()

}
}

[Fact]
public async void GetStatusesForCommit()
{
string gitlabServer = "http://fake-gitlab.com/";
string projectId = "id";
string sha = "6104942438c14ec7bd21c6cd5bd995272b3faff6";
string Name = "name1";
string url = $"/projects/id/repository/commits/{sha}/statuses?name={Name}&per_page=100&page=1";

var handler = A.Fake<MockHandler>(opt => opt.CallsBaseMethods());
A.CallTo(() => handler.SendAsync(HttpMethod.Get, url))
.ReturnsLazily(() => HttpResponseMessageProducer.Success(
$"[ {{\"id\":1,\"sha\":\"{sha}\",\"ref \":\"\",\"status\":\"success\",\"name\":\"name1\",\"target_url\":\"target_url1\",\"description\":\"success\",\"created_at\":\"2020-04-08T11:57:49.136+05:30\",\"started_at\":\"2020-04-08T11:58:00.362+05:30\",\"finished_at\":\"2020-04-08T11:58:06.121+05:30\",\"allow_failure\":false,\"coverage\":null,\"author\":{{\"id\":1,\"name\":\"name\",\"username\":\"username\",\"state\":\"active\",\"avatar_url\":\"avatar_url1\",\"web_url\":\"web_url1\"}} }},{{\"id\":2,\"sha\":\"{sha}\",\"ref \":\"\",\"status\":\"success\",\"name\":\"name2\",\"target_url\":\"target_url2\",\"description\":\"success\",\"created_at\":\"2020-04-08T11:57:49.136+05:30\",\"started_at\":\"2020-04-08T11:58:00.362+05:30\",\"finished_at\":\"2020-04-08T11:58:06.121+05:30\",\"allow_failure\":false,\"coverage\":null,\"author\":{{\"id\":2,\"name\":\"name2\",\"username\":\"username2\",\"state\":\"activ2\",\"avatar_url2\":\"avatar_url2\",\"web_url\":\"web_url2\"}} }}]"));
using (var client = new HttpClient(handler) { BaseAddress = new Uri(gitlabServer) })
{
var gitlabHttpFacade = new GitLabHttpFacade(new RequestsJsonSerializer(), client);
var commitsClient = new CommitsClient(gitlabHttpFacade, new CommitQueryBuilder(), new CommitRefsQueryBuilder(), new CommitStatusesQueryBuilder());

var statusesFromClient = await commitsClient.GetStatusesAsync(projectId, sha, o => o.Name = Name);
statusesFromClient[0].Status.Should().BeEquivalentTo("success");
statusesFromClient[0].Name.Should().BeEquivalentTo("name1");
statusesFromClient[0].Target_url.Should().BeEquivalentTo("target_url1");
statusesFromClient[0].Id.Should().BeEquivalentTo("1");

statusesFromClient[1].Status.Should().BeEquivalentTo("success");
statusesFromClient[1].Name.Should().BeEquivalentTo("name2");
statusesFromClient[1].Target_url.Should().BeEquivalentTo("target_url2");
statusesFromClient[1].Id.Should().BeEquivalentTo("2");

}
}
}
}