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 basic support to retrieve labels #75

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
66 changes: 66 additions & 0 deletions src/Atlassian.Stash/Api/Labels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Threading.Tasks;
using Atlassian.Stash.Entities;
using Atlassian.Stash.Helpers;
using Atlassian.Stash.Workers;

namespace Atlassian.Stash.Api
{
public class Labels
{
private const string MANY_LABELS = "rest/api/1.0/labels";
private const string ONE_LABEL = "rest/api/1.0/labels/{0}";
private const string LABELED_REPOSITORY = "rest/api/1.0/labels/{0}/labeled";

private HttpCommunicationWorker _httpWorker;

internal Labels(HttpCommunicationWorker httpWorker)
{
_httpWorker = httpWorker;
}

public async Task<ResponseWrapper<Label>> Get(RequestOptions requestOptions = null)
{
string requestUrl = UrlBuilder.FormatRestApiUrl(MANY_LABELS, requestOptions);

ResponseWrapper<Label> response = await _httpWorker.GetAsync<ResponseWrapper<Label>>(requestUrl).ConfigureAwait(false);

return response;
}

public async Task<ResponseWrapper<Label>> Get(string prefix, RequestOptions requestOptions = null)
{
string requestUrl = UrlBuilder.FormatRestApiUrl(MANY_LABELS + "?prefix={0}", requestOptions, prefix);

ResponseWrapper<Label> response = await _httpWorker.GetAsync<ResponseWrapper<Label>>(requestUrl).ConfigureAwait(false);

return response;
}

public async Task<ResponseWrapper<Label>> GetByLabelName(string labelName, RequestOptions requestOptions = null)
{
string requestUrl = UrlBuilder.FormatRestApiUrl(ONE_LABEL, requestOptions, labelName);

ResponseWrapper<Label> response = await _httpWorker.GetAsync<ResponseWrapper<Label>>(requestUrl).ConfigureAwait(false);

return response;
}

public async Task<ResponseWrapper<LabelRepository>> GetRepositories(string labelName, RequestOptions requestOptions = null)
{
string requestUrl = UrlBuilder.FormatRestApiUrl(LABELED_REPOSITORY, requestOptions, labelName);

ResponseWrapper<LabelRepository> response = await _httpWorker.GetAsync<ResponseWrapper<LabelRepository>>(requestUrl).ConfigureAwait(false);

return response;
}

public async Task<ResponseWrapper<LabelRepository>> GetRepositories(string labelName, string type, RequestOptions requestOptions = null)
{
string requestUrl = UrlBuilder.FormatRestApiUrl(LABELED_REPOSITORY + "?type={1}", requestOptions, labelName, type);

ResponseWrapper<LabelRepository> response = await _httpWorker.GetAsync<ResponseWrapper<LabelRepository>>(requestUrl).ConfigureAwait(false);

return response;
}
}
}
10 changes: 10 additions & 0 deletions src/Atlassian.Stash/Entities/Label.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace Atlassian.Stash.Entities
{
public class Label
{
[JsonProperty("name")]
public string Name { get; set; }
}
}
17 changes: 17 additions & 0 deletions src/Atlassian.Stash/Entities/LabelRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Atlassian.Stash.Entities
{
public class LabelRepository : Repository
{
[JsonProperty("labelableType")]
[JsonConverter(typeof(StringEnumConverter))]
public LabelableType LabelableType { get; set; }
}

public enum LabelableType
{
REPOSITORY
}
}
2 changes: 2 additions & 0 deletions src/Atlassian.Stash/StashClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private void InjectDependencies()
this.Commits = new Commits(_httpWorker);
this.PullRequests = new PullRequests(_httpWorker);
this.Forks = new Forks(_httpWorker);
this.Labels = new Labels(_httpWorker);
}

public void SetBasicAuthentication(string username, string password)
Expand All @@ -48,6 +49,7 @@ public void SetBasicAuthentication(string username, string password)
public Commits Commits { get; private set; }
public PullRequests PullRequests { get; private set; }
public Forks Forks { get; private set; }
public Labels Labels { get; private set; }

}
}
15 changes: 13 additions & 2 deletions test/Atlassian.Stash.IntegrationTests/StashClientTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public async Task Can_GetFileContents_In_SubFolder_With_Spaces()
public async Task Can_GetFileContents_From_Branch()
{
var response = await stashClient.Repositories.GetFileContents(EXISTING_PROJECT, EXISTING_REPOSITORY, EXISTING_FILE, MASTER_BRANCH_NAME);

Assert.IsNotNull(response);
Assert.IsTrue(response.FileContents.Count > 0);
Assert.AreEqual(1, response.Size);
}

[TestMethod]
public async Task Can_GetBranchesForCommit()
{
Expand Down Expand Up @@ -856,5 +856,16 @@ public async Task Get_TestUser_User_With_BearAuthenticationToken()
Assert.IsTrue(users.Any());
Assert.IsNotNull(users.Single(user => user.Name == TEST_USERNAME));
}

[TestMethod]
public async Task Can_GetAllLabels()
{
var response = await stashClient.Labels.Get();
var projects = response.Values;

Assert.IsNotNull(projects);
Assert.IsInstanceOfType(projects, typeof(IEnumerable<Label>));
Assert.IsTrue(projects.Any());
}
}
}