Skip to content

Commit 6f13050

Browse files
authored
add support for retrieving the diffs from a commit (#119)
1 parent 8d8e88c commit 6f13050

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

src/GitLabApiClient/CommitsClient.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,17 @@ public async Task<IList<CommitRef>> GetRefsAsync(ProjectId projectId, string sha
6262
string url = _commitRefsQueryBuilder.Build($"projects/{projectId}/repository/commits/{sha}/refs", queryOptions);
6363
return await _httpFacade.GetPagedList<CommitRef>(url);
6464
}
65+
66+
/// <summary>
67+
/// Retrieve a list of differences in this commit
68+
/// </summary>
69+
/// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param>
70+
/// <param name="sha">The commit hash or name of a repository branch or tag</param>
71+
/// <returns></returns>
72+
public async Task<IList<Diff>> GetDiffsAsync(ProjectId projectId, string sha)
73+
{
74+
string url = $"projects/{projectId}/repository/commits/{sha}/diff";
75+
return await _httpFacade.GetPagedList<Diff>(url);
76+
}
6577
}
6678
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using GitLabApiClient.Models.Releases.Responses;
5+
using Newtonsoft.Json;
6+
7+
namespace GitLabApiClient.Models.Commits.Responses
8+
{
9+
public sealed class Diff
10+
{
11+
[JsonProperty("diff")]
12+
public string DiffText { get; set; }
13+
14+
[JsonProperty("new_path")]
15+
public string NewPath { get; set; }
16+
17+
[JsonProperty("old_path")]
18+
public string OldPath { get; set; }
19+
20+
[JsonProperty("a_mode")]
21+
public string AMode { get; set; }
22+
23+
[JsonProperty("b_mode")]
24+
public string BMode { get; set; }
25+
26+
[JsonProperty("new_file")]
27+
public bool IsNewFile { get; set; }
28+
29+
[JsonProperty("renamed_file")]
30+
public bool IsRenamedFile { get; set; }
31+
32+
[JsonProperty("deleted_file")]
33+
public bool IsDeletedFile { get; set; }
34+
}
35+
}

test/GitLabApiClient.Test/CommitsClientTest.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,44 @@ public async void GetCommitsByRefName()
6060
}
6161

6262
}
63+
64+
[Fact]
65+
public async void GetDiffsForCommit()
66+
{
67+
string gitlabServer = "http://fake-gitlab.com/";
68+
string projectId = "id";
69+
string sha = "6104942438c14ec7bd21c6cd5bd995272b3faff6";
70+
string url = $"/projects/id/repository/commits/{sha}/diff?per_page=100&page=1";
71+
72+
var handler = A.Fake<MockHandler>(opt => opt.CallsBaseMethods());
73+
A.CallTo(() => handler.SendAsync(HttpMethod.Get, url))
74+
.ReturnsLazily(() => HttpResponseMessageProducer.Success(
75+
$"[ {{ \"diff\": \"diff1\", \"new_path\": \"new_path1\", \"old_path\": \"old_path1\", \"a_mode\": \"a_mode1\", \"b_mode\": \"b_mode1\", \"new_file\": \"true\", \"renamed_file\": \"false\", \"deleted_file\": \"false\" }},\n {{\"diff\": \"diff2\", \"new_path\": \"new_path2\", \"old_path\": \"old_path2\", \"a_mode\": \"a_mode2\", \"b_mode\": \"b_mode2\", \"new_file\": \"false\", \"renamed_file\": \"true\", \"deleted_file\": \"true\"}}]"));
76+
using (var client = new HttpClient(handler) { BaseAddress = new Uri(gitlabServer) })
77+
{
78+
var gitlabHttpFacade = new GitLabHttpFacade(new RequestsJsonSerializer(), client);
79+
var commitsClient = new CommitsClient(gitlabHttpFacade, new CommitQueryBuilder(), new CommitRefsQueryBuilder());
80+
81+
var diffsFromClient = await commitsClient.GetDiffsAsync(projectId, sha);
82+
diffsFromClient[0].DiffText.Should().BeEquivalentTo("diff1");
83+
diffsFromClient[0].NewPath.Should().BeEquivalentTo("new_path1");
84+
diffsFromClient[0].OldPath.Should().BeEquivalentTo("old_path1");
85+
diffsFromClient[0].AMode.Should().BeEquivalentTo("a_mode1");
86+
diffsFromClient[0].BMode.Should().BeEquivalentTo("b_mode1");
87+
diffsFromClient[0].IsNewFile.Should().BeTrue();
88+
diffsFromClient[0].IsRenamedFile.Should().BeFalse();
89+
diffsFromClient[0].IsDeletedFile.Should().BeFalse();
90+
91+
diffsFromClient[1].DiffText.Should().BeEquivalentTo("diff2");
92+
diffsFromClient[1].NewPath.Should().BeEquivalentTo("new_path2");
93+
diffsFromClient[1].OldPath.Should().BeEquivalentTo("old_path2");
94+
diffsFromClient[1].AMode.Should().BeEquivalentTo("a_mode2");
95+
diffsFromClient[1].BMode.Should().BeEquivalentTo("b_mode2");
96+
diffsFromClient[1].IsNewFile.Should().BeFalse();
97+
diffsFromClient[1].IsRenamedFile.Should().BeTrue();
98+
diffsFromClient[1].IsDeletedFile.Should().BeTrue();
99+
100+
}
101+
}
63102
}
64103
}

0 commit comments

Comments
 (0)