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 Group Variables CRUD methods #146

Merged
merged 4 commits into from
May 30, 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
39 changes: 39 additions & 0 deletions src/GitLabApiClient/GroupsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,5 +336,44 @@ public async Task<GroupLabel> UpdateLabelAsync(GroupId groupId, UpdateGroupLabel
/// <param name="name">Name of the label.</param>
public async Task DeleteLabelAsync(GroupId groupId, string name) =>
await _httpFacade.Delete($"groups/{groupId}/labels?name={name}");

/// <summary>
/// Retrieves group variables by its id.
/// </summary>
/// <param name="groupId">Id of the group.</param>
public async Task<IList<Variable>> GetVariablesAsync(GroupId groupId) =>
await _httpFacade.GetPagedList<Variable>($"groups/{groupId}/variables");

/// <summary>
/// Creates new project variable.
/// </summary>
/// <param name="groupId">The ID, path or <see cref="Group"/> of the group.</param>
/// <param name="request">Create variable request.</param>
/// <returns>Newly created variable.</returns>
public async Task<Variable> CreateVariableAsync(GroupId groupId, CreateGroupVariableRequest request)
{
Guard.NotNull(request, nameof(request));
return await _httpFacade.Post<Variable>($"groups/{groupId}/variables", request);
}

/// <summary>
/// Updates an existing group variable.
/// </summary>
/// <param name="groupId">The ID, path or <see cref="Group"/> of the group.</param>
/// <param name="request">Update variable request.</param>
/// <returns>Newly modified variable.</returns>
public async Task<Variable> UpdateVariableAsync(GroupId groupId, UpdateGroupVariableRequest request)
{
Guard.NotNull(request, nameof(request));
return await _httpFacade.Put<Variable>($"groups/{groupId}/variables/{request.Key}", request);
}

/// <summary>
/// Deletes group variable
/// </summary>
/// <param name="groupId">The ID, path or <see cref="Group"/> of the group.</param>
/// <param name="key">The Key ID of the variable.</param>
public async Task DeleteVariableAsync(GroupId groupId, string key) =>
await _httpFacade.Delete($"groups/{groupId}/variables/{key}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Groups.Requests
{
public class CreateGroupVariableRequest
{
/// <summary>
/// The type of a variable.
/// Available types are: env_var (default) and file
/// </summary>
[JsonProperty("variable_type")]
public string VariableType { get; set; }

/// <summary>
/// The key of a variable
/// </summary>
[JsonProperty("key")]
public string Key { get; set; }

/// <summary>
/// The value of a variable
/// </summary>
[JsonProperty("value")]
public string Value { get; set; }

/// <summary>
/// Whether the variable is protected
/// </summary>
[JsonProperty("protected")]
public bool? Protected { get; set; }

/// <summary>
/// Whether the variable is masked
/// </summary>
[JsonProperty("masked")]
public bool? Masked { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Groups.Requests
{
public class UpdateGroupVariableRequest
{
/// <summary>
/// The type of a variable.
/// Available types are: env_var (default) and file
/// </summary>
[JsonProperty("variable_type")]
public string VariableType { get; set; }

/// <summary>
/// The key of a variable
/// </summary>
[JsonProperty("key")]
public string Key { get; set; }

/// <summary>
/// The value of a variable
/// </summary>
[JsonProperty("value")]
public string Value { get; set; }

/// <summary>
/// Whether the variable is protected
/// </summary>
[JsonProperty("protected")]
public bool? Protected { get; set; }

/// <summary>
/// Whether the variable is masked
/// </summary>
[JsonProperty("masked")]
public bool? Masked { get; set; }
}
}
38 changes: 38 additions & 0 deletions src/GitLabApiClient/Models/Groups/Responses/Variable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Newtonsoft.Json;

namespace GitLabApiClient.Models.Groups.Responses
{
public sealed class Variable
{
/// <summary>
/// The type of a variable.
/// Available types are: env_var (default) and file
/// </summary>
[JsonProperty("variable_type")]
public string VariableType { get; set; }

/// <summary>
/// The key of a variable
/// </summary>
[JsonProperty("key")]
public string Key { get; set; }

/// <summary>
/// The value of a variable
/// </summary>
[JsonProperty("value")]
public string Value { get; set; }

/// <summary>
/// Whether the variable is protected
/// </summary>
[JsonProperty("protected")]
public bool Protected { get; set; }

/// <summary>
/// Whether the variable is masked
/// </summary>
[JsonProperty("masked")]
public bool Masked { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/GitLabApiClient/ProjectsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public async Task<IList<User>> GetUsersAsync(ProjectId projectId) =>
/// Retrieves project variables by its id.
/// </summary>
/// <param name="projectId">Id of the project.</param>
public async Task<IList<Variable>> GetVariablesAsync(int projectId) =>
public async Task<IList<Variable>> GetVariablesAsync(ProjectId projectId) =>
await _httpFacade.GetPagedList<Variable>($"projects/{projectId}/variables");

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion test/GitLabApiClient.Test/GitLabClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async void CanLogin()
accessTokenResponse.CreatedAt.Should().NotBeNull();
accessTokenResponse.AccessToken.Should().HaveLength(64);
accessTokenResponse.RefreshToken.Should().HaveLength(64);
accessTokenResponse.TokenType.Should().Be("bearer");
accessTokenResponse.TokenType.Should().BeEquivalentTo("bearer");
var currentSessionAsync = await sut.Users.GetCurrentSessionAsync();
currentSessionAsync.Username.Should().Be(GitLabApiHelper.TestUserName);

Expand Down
91 changes: 91 additions & 0 deletions test/GitLabApiClient.Test/GroupsClientTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using GitLabApiClient.Internal.Queries;
Expand All @@ -19,6 +20,7 @@ public class GroupsClientTest
{
private readonly List<int> _groupIdsToClean = new List<int>();
private List<int> MilestoneIdsToClean { get; } = new List<int>();
private List<string> VariableIdsToClean { get; } = new List<string>();

private readonly GroupsClient _sut = new GroupsClient(
GetFacade(),
Expand Down Expand Up @@ -229,6 +231,92 @@ public async Task CreatedGroupMilestoneCanBeClosed()
updatedMilestone.Should().Match<Milestone>(i => i.State == MilestoneState.Closed);
}

[Fact]
public async Task GroupVariablesRetrieved()
{
//arrange
var createdVariable = await _sut.CreateVariableAsync(GitLabApiHelper.TestGroupId, new CreateGroupVariableRequest
{
VariableType = "env_var",
Key = "SOME_VAR_KEY_RETRIEVE",
Value = "VALUE_VAR",
Masked = true,
Protected = true
});

VariableIdsToClean.Add(createdVariable.Key);

//act
var variables = await _sut.GetVariablesAsync(GitLabApiHelper.TestGroupId);
var variable = variables.First(v => v.Key == createdVariable.Key);

//assert
variables.Should().NotBeEmpty();
variable.Should().Match<Variable>(v =>
v.VariableType == createdVariable.VariableType &&
v.Key == createdVariable.Key &&
v.Value == createdVariable.Value &&
v.Masked == createdVariable.Masked &&
v.Protected == createdVariable.Protected);
}

[Fact]
public async Task GroupVariablesCreated()
{
var request = new CreateGroupVariableRequest
{
VariableType = "env_var",
Key = "SOME_VAR_KEY_CREATED",
Value = "VALUE_VAR",
Masked = true,
Protected = true
};

var variable = await _sut.CreateVariableAsync(GitLabApiHelper.TestGroupId, request);

variable.Should().Match<Variable>(v => v.VariableType == request.VariableType
&& v.Key == request.Key
&& v.Value == request.Value
&& v.Masked == request.Masked
&& v.Protected == request.Protected);

VariableIdsToClean.Add(request.Key);
}

[Fact]
public async Task GroupVariableCanBeUpdated()
{
var request = new CreateGroupVariableRequest
{
VariableType = "env_var",
Key = "SOME_VAR_KEY_TO_UPDATE",
Value = "VALUE_VAR",
Masked = true,
Protected = true
};

var variable = await _sut.CreateVariableAsync(GitLabApiHelper.TestGroupId, request);

VariableIdsToClean.Add(request.Key);

var updateRequest = new UpdateGroupVariableRequest
{
VariableType = "file",
Key = request.Key,
Value = "UpdatedValue",
Masked = request.Masked,
Protected = request.Protected,
};

var variableUpdated = await _sut.UpdateVariableAsync(GitLabApiHelper.TestGroupId, updateRequest);

variableUpdated.Should().Match<Variable>(v => v.VariableType == updateRequest.VariableType
&& v.Key == updateRequest.Key
&& v.Value == updateRequest.Value
&& v.Masked == updateRequest.Masked
&& v.Protected == updateRequest.Protected);
}

[Fact]
public Task InitializeAsync()
=> CleanupGroups();
Expand All @@ -244,6 +332,9 @@ private async Task CleanupGroups()

foreach (int groupId in _groupIdsToClean)
await _sut.DeleteAsync(groupId.ToString());

foreach (string variableId in VariableIdsToClean)
await _sut.DeleteVariableAsync(GitLabApiHelper.TestGroupId, variableId);
}

private static string GetRandomGroupName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class GitLabContainerFixture : IAsyncLifetime
public static string GitlabHost { get; private set; }

private IContainerService _gitlabContainer;
private readonly string _gitlabDockerImage = "gitlab/gitlab-ce:12.5.4-ce.0";
private readonly string _gitlabDockerImage = "gitlab/gitlab-ce:12.10.2-ce.0";

public async Task InitializeAsync()
{
Expand Down