Skip to content
This repository has been archived by the owner on May 9, 2022. It is now read-only.

Commit

Permalink
refactor + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nmklotas committed Sep 15, 2017
1 parent 7c3a36d commit 0a81d6d
Show file tree
Hide file tree
Showing 44 changed files with 359 additions and 65 deletions.
8 changes: 7 additions & 1 deletion GitLabApiClient.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitLabApiClient", "GitLabApiClient\GitLabApiClient.csproj", "{92D1EE01-7A8D-4EFC-B856-54C0E0645454}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GitLabApiClient", "src\GitLabApiClient\GitLabApiClient.csproj", "{92D1EE01-7A8D-4EFC-B856-54C0E0645454}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitLabApiClient.Test", "test\GitLabApiClient.Test\GitLabApiClient.Test.csproj", "{11F0436A-3BAC-4C3F-9E59-2F271730A5C8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +17,10 @@ Global
{92D1EE01-7A8D-4EFC-B856-54C0E0645454}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92D1EE01-7A8D-4EFC-B856-54C0E0645454}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92D1EE01-7A8D-4EFC-B856-54C0E0645454}.Release|Any CPU.Build.0 = Release|Any CPU
{11F0436A-3BAC-4C3F-9E59-2F271730A5C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{11F0436A-3BAC-4C3F-9E59-2F271730A5C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{11F0436A-3BAC-4C3F-9E59-2F271730A5C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{11F0436A-3BAC-4C3F-9E59-2F271730A5C8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
30 changes: 0 additions & 30 deletions GitLabApiClient/ProjectsClient.cs

This file was deleted.

3 changes: 3 additions & 0 deletions src/GitLabApiClient/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly:InternalsVisibleTo("GitLabApiClient.Test")]
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using GitLabApiClient.Http;
using GitLabApiClient.Models;
using GitLabApiClient.Models.Users;

namespace GitLabApiClient
{
public class GitLabClient
{
private readonly GitlabHttpFacade _httpFacade;
private readonly GitLabHttpFacade _httpFacade;

public GitLabClient(string hostUrl, string authenticationToken = "")
{
_httpFacade = new GitlabHttpFacade(hostUrl, authenticationToken);
_httpFacade = new GitLabHttpFacade(
hostUrl ?? throw new ArgumentNullException(nameof(hostUrl)),
authenticationToken ?? throw new ArgumentNullException(nameof(authenticationToken)));

Issues = new IssuesClient(_httpFacade);
MergeRequests = new MergeRequestsClient(_httpFacade);
Projects = new ProjectsClient(_httpFacade);
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using GitLabApiClient.Models;
using GitLabApiClient.Models.Users;

namespace GitLabApiClient.Http
{
internal class GitlabHttpFacade
internal class GitLabHttpFacade
{
private const string PrivateToken = "PRIVATE-TOKEN";

private readonly object _locker = new object();
private readonly HttpClient _httpClient;
private readonly HttpRequestor _requestor;

public GitlabHttpFacade(string hostUrl, string authenticationToken = "")
public GitLabHttpFacade(string hostUrl, string authenticationToken = "")
{
_httpClient = new HttpClient
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace GitLabApiClient
namespace GitLabApiClient.Http
{
internal class HttpRequestor
{
Expand Down Expand Up @@ -111,13 +111,13 @@ private static StringContent SerializeToString(object data, bool ignoreNullValue

private static string GetAPIUrl(string url)
{
if (!tailAPIUrl.StartsWith("/", StringComparison.OrdinalIgnoreCase))
tailAPIUrl = "/" + tailAPIUrl;
if (!url.StartsWith("/", StringComparison.OrdinalIgnoreCase))
url = "/" + url;

if (!tailAPIUrl.StartsWith("/api/v4", StringComparison.OrdinalIgnoreCase))
tailAPIUrl = "/api/v4" + tailAPIUrl;
if (!url.StartsWith("/api/v4", StringComparison.OrdinalIgnoreCase))
url = "/api/v4" + url;

return tailAPIUrl;
return url;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ namespace GitLabApiClient
{
public class IssuesClient
{
private readonly GitlabHttpFacade _httpFacade;
private readonly GitLabHttpFacade _httpFacade;

internal IssuesClient(GitlabHttpFacade httpFacade) =>
internal IssuesClient(GitLabHttpFacade httpFacade) =>
_httpFacade = httpFacade;

public async Task<IList<Issue>> GetAsync(int projectId) =>
await _httpFacade.GetAll<Issue>($"/projects/{projectId}/issues");

public async Task<Issue> GetAsync(int projectId, int issueId) =>
public async Task<Issue> GetAsync(int projectId, long issueId) =>
await _httpFacade.Get<Issue>($"/projects/{projectId}/issues/{issueId}");

public async Task<IList<Issue>> GetOwnedAsync() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace GitLabApiClient
{
public class MergeRequestsClient
{
private readonly GitlabHttpFacade _httpFacade;
private readonly GitLabHttpFacade _httpFacade;

internal MergeRequestsClient(GitlabHttpFacade httpFacade) =>
internal MergeRequestsClient(GitLabHttpFacade httpFacade) =>
_httpFacade = httpFacade;

public async Task<IList<MergeRequest>> GetAsync(int projectId) =>
Expand All @@ -25,14 +25,18 @@ public async Task<MergeRequest> CreateAsync(CreateMergeRequest request) =>
public async Task<MergeRequest> UpdateAsync(EditMergeRequest request) =>
await _httpFacade.Put<MergeRequest>($"/projects/{request.ProjectId}/merge_requests/{request.MergeRequestId}", request);

public async Task<MergeRequest> AcceptAsync(int projectId, int mergeRequestId, string message) =>
await _httpFacade.Put<MergeRequest>($"/projects/{projectId}/merge_requests/{mergeRequestId}/merge",
new MergeCommitMessage
{
Message = message
});
public async Task<MergeRequest> AcceptAsync(int projectId, int mergeRequestId, string message)
{
var commitMessage = new MergeCommitMessage
{
Message = message
};

return await _httpFacade.Put<MergeRequest>(
$"/projects/{projectId}/merge_requests/{mergeRequestId}/merge", commitMessage);
}

public async Task DeleteAsync(int projectId, int mergeRequestId) =>
public async Task DeleteAsync(int projectId, long mergeRequestId) =>
await _httpFacade.Delete($"/projects/{projectId}/merge_requests/{mergeRequestId}");

private class MergeCommitMessage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using GitLabApiClient.Models.Users;
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Issues
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using GitLabApiClient.Models.Users;
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Merges
Expand Down Expand Up @@ -45,7 +46,7 @@ public class MergeRequest : ModifiableObject
public long ProjectId { get; set; }

[JsonProperty("should_remove_source_branch")]
public bool ShouldRemoveSourceBranch { get; set; }
public bool? ShouldRemoveSourceBranch { get; set; }

[JsonProperty("target_project_id")]
public long TargetProjectId { get; set; }
Expand Down
43 changes: 43 additions & 0 deletions src/GitLabApiClient/Models/Projects/Group.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Projects
{
public class Group
{
[JsonProperty("full_path")]
public string FullPath { get; set; }

[JsonProperty("parent_id")]
public object ParentId { get; set; }

[JsonProperty("description")]
public string Description { get; set; }

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

[JsonProperty("full_name")]
public string FullName { get; set; }

[JsonProperty("lfs_enabled")]
public bool LfsEnabled { get; set; }

[JsonProperty("id")]
public long Id { get; set; }

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

[JsonProperty("request_access_enabled")]
public bool RequestAccessEnabled { get; set; }

[JsonProperty("path")]
public string Path { get; set; }

[JsonProperty("visibility")]
public string Visibility { get; set; }

[JsonProperty("web_url")]
public string WebUrl { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class Project
public Owner Owner { get; set; }

[JsonProperty("shared_with_groups")]
public List<string> SharedWithGroups { get; } = new List<string>();
public List<Group> SharedWithGroups { get; } = new List<Group>();

[JsonProperty("namespace")]
public Namespace Namespace { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Newtonsoft.Json;

namespace GitLabApiClient.Models
namespace GitLabApiClient.Models.Users
{
public class Assignee
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace GitLabApiClient.Models
namespace GitLabApiClient.Models.Users
{
public class Session
{
Expand Down Expand Up @@ -37,7 +37,7 @@ public class Session
public string Skype { get; set; }

[JsonProperty("identities")]
public List<string> Identities { get; } = new List<string>();
public List<Identity> Identities { get; } = new List<Identity>();

[JsonProperty("id")]
public long Id { get; set; }
Expand Down
34 changes: 34 additions & 0 deletions src/GitLabApiClient/ProjectsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using GitLabApiClient.Http;
using GitLabApiClient.Models.Projects;
using GitLabApiClient.Models.Users;

namespace GitLabApiClient
{
public class ProjectsClient
{
private readonly GitLabHttpFacade _httpFacade;

internal ProjectsClient(GitLabHttpFacade httpFacade) =>
_httpFacade = httpFacade;

public async Task<IList<Project>> GetAsync() =>
await _httpFacade.GetAll<Project>("/projects");

public async Task<Project> GetAsync(int projectId) =>
await _httpFacade.Get<Project>($"/projects/{projectId}");

public async Task<IList<Project>> GetByUserIdAsync(int userId) =>
await _httpFacade.GetAll<Project>($"/users/{userId}/projects");

public async Task<IList<User>> GetUsers(int projectId) =>
await _httpFacade.GetAll<User>($"/projects/{projectId}/users");

public async Task<Project> CreateAsync(CreateProjectRequest request) =>
await _httpFacade.Post<Project>("/projects", request);

public async Task DeleteAsync(int id) =>
await _httpFacade.Delete($"/projects/{id}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
using System.Linq;
using System.Threading.Tasks;
using GitLabApiClient.Http;
using GitLabApiClient.Models;
using GitLabApiClient.Models.Users;

namespace GitLabApiClient
{
public class UsersClient
{
private readonly GitlabHttpFacade _httpFacade;
private readonly GitLabHttpFacade _httpFacade;

internal UsersClient(GitlabHttpFacade httpFacade) =>
internal UsersClient(GitLabHttpFacade httpFacade) =>
_httpFacade = httpFacade;

public async Task<IEnumerable<User>> GetAsync() =>
public async Task<IList<User>> GetAsync() =>
await _httpFacade.GetAll<User>("/users");

public async Task<User> GetAsync(string name) =>
Expand Down
20 changes: 20 additions & 0 deletions test/GitLabApiClient.Test/GitLabApiClient.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="4.19.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\GitLabApiClient\GitLabApiClient.csproj" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions test/GitLabApiClient.Test/GitLabApiHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using GitLabApiClient.Http;

namespace GitLabApiClient.Test
{
internal static class GitLabApiHelper
{
public static GitLabHttpFacade GetFacade()
{
var facade = new GitLabHttpFacade(
"https://gitlab.com/api/v3", "yYZSLFnrKyFsG4uD1Wa6");

return facade;
}

public static int TestProjectId { get; set; } = 4006344;
}
}
Loading

0 comments on commit 0a81d6d

Please sign in to comment.