From 23f7bb7d53ab3849437e7e6c2307b23058ecbab5 Mon Sep 17 00:00:00 2001 From: jlouros Date: Mon, 3 Nov 2014 10:33:38 -0500 Subject: [PATCH] adding manage branches capabilities (CREATE and DELETE) --- .../StashClientTester.cs | 17 +++++++++++++++ Atlassian.Stash.Api/Api/Branches.cs | 21 ++++++++++++++++++- .../Atlassian.Stash.Api.nuspec | 15 ++++++------- Atlassian.Stash.Api/Entities/Branch.cs | 13 ++++++++++-- .../Properties/AssemblyInfo.cs | 6 +++--- .../Workers/HttpCommunicationWorker.cs | 17 +++++++++++++++ README.md | 8 +++---- 7 files changed, 80 insertions(+), 17 deletions(-) diff --git a/Atlassian.Stash.Api.IntegrationTests/StashClientTester.cs b/Atlassian.Stash.Api.IntegrationTests/StashClientTester.cs index 39db07e..c03d544 100644 --- a/Atlassian.Stash.Api.IntegrationTests/StashClientTester.cs +++ b/Atlassian.Stash.Api.IntegrationTests/StashClientTester.cs @@ -15,6 +15,7 @@ public class StashClientTester private const string EXISTING_REPOSITORY = "testrepository"; private const string EXISTING_COMMIT = "86486c762e901ea5efef1b7d287514b7f2cc0c82"; private const string EXISTING_OLDER_COMMIT = "9b533044d39811db518250b441d472ece955b0e3"; + private const string EXISTING_BRANCH_REFERENCE = "refs/heads/master"; private StashClient _stashClient; @@ -265,6 +266,22 @@ public void Can_CreateRepository_Than_DeleteRepository() _stashClient.Repositories.Delete(EXISTING_PROJECT, createdRepository.Slug).Wait(); } + [TestMethod] + public void Can_CreateBranch_Than_DeleteBranch() + { + Branch newBranch = new Branch { Name = "test-repo", StartPoint = EXISTING_BRANCH_REFERENCE }; + var createdBranch = _stashClient.Branches.Create(EXISTING_PROJECT, EXISTING_REPOSITORY, newBranch).Result; + + Assert.IsNotNull(createdBranch); + Assert.IsInstanceOfType(createdBranch, typeof(Branch)); + Assert.AreEqual(newBranch.Name.ToLower(), createdBranch.DisplayId.ToLower()); + + + Branch deleteBranch = new Branch { Name = newBranch.Name, DryRun = false }; + + _stashClient.Branches.Delete(EXISTING_PROJECT, EXISTING_REPOSITORY, deleteBranch).Wait(); + } + #endregion } } diff --git a/Atlassian.Stash.Api/Api/Branches.cs b/Atlassian.Stash.Api/Api/Branches.cs index 16d3367..24ec702 100644 --- a/Atlassian.Stash.Api/Api/Branches.cs +++ b/Atlassian.Stash.Api/Api/Branches.cs @@ -8,7 +8,9 @@ namespace Atlassian.Stash.Api.Api public class Branches { private const string MANY_BRANCHES = "/rest/api/1.0/projects/{0}/repos/{1}/branches"; - + + private const string MANAGE_BRANCHES = "/rest/branch-utils/1.0/projects/{0}/repos/{1}/branches"; + private HttpCommunicationWorker _httpWorker; internal Branches(HttpCommunicationWorker httpWorker) @@ -24,5 +26,22 @@ public async Task> GetAll(string projectKey, string repo return response; } + + // branch Utils API + public async Task Create(string projectKey, string repositorySlug, Branch branch) + { + string requestUrl = UrlBuilder.FormatRestApiUrl(MANAGE_BRANCHES, null, projectKey, repositorySlug); + + Branch response = await _httpWorker.PostAsync(requestUrl, branch); + + return response; + } + + public async Task Delete(string projectKey, string repositorySlug, Branch branch) + { + string requestUrl = UrlBuilder.FormatRestApiUrl(MANAGE_BRANCHES, null, projectKey, repositorySlug); + + await _httpWorker.DeleteAsyncWithJsonContent(requestUrl, branch); + } } } diff --git a/Atlassian.Stash.Api/Atlassian.Stash.Api.nuspec b/Atlassian.Stash.Api/Atlassian.Stash.Api.nuspec index 87fc92e..602e57d 100644 --- a/Atlassian.Stash.Api/Atlassian.Stash.Api.nuspec +++ b/Atlassian.Stash.Api/Atlassian.Stash.Api.nuspec @@ -11,15 +11,16 @@ false $description$ - *Warning this package isn't backwards compatible* - Functionality changes: - - More comprehensive API abstraction. - - API actions organized by Stash area (like projects, repositories, branches, etc) + Features: + - manage Stash 'Projects' (get information, create and delete capabilities) + - manage Stash 'Repositories' (get information, create and delete capabilities) + - manage Stash 'Branches' (get information, create and delete capabilities) + - get information about 'repository files' + - get information about 'Commits' and 'commit changes' + - get information about 'Tags' New features: - - refactored StashClient class. Use it to access APIs actions (StashClient.Projects.{Action}) - - Added GET capabilities for 'branches' and 'commits' - - Added DELETE capabilities for 'projects' and 'repositories' + - adding CREATE and DELETE capabilities to 'branches' Copyright John Louros 2014 Atlassian Stash API-Wrapper diff --git a/Atlassian.Stash.Api/Entities/Branch.cs b/Atlassian.Stash.Api/Entities/Branch.cs index c6595eb..0e1e937 100644 --- a/Atlassian.Stash.Api/Entities/Branch.cs +++ b/Atlassian.Stash.Api/Entities/Branch.cs @@ -1,12 +1,21 @@ - +using Newtonsoft.Json; + namespace Atlassian.Stash.Api.Entities { + // todo: Review class, since Create <> Get <> Delete objects public class Branch { public string Id { get; set; } public string DisplayId { get; set; } public string LatestChangeset { get; set; } public bool IsDefault { get; set; } - } + [JsonProperty("name")] + public string Name { get; set; } + [JsonProperty("startPoint")] + public string StartPoint { get; set; } + [JsonProperty("dryRun")] + public bool? DryRun { get; set; } + + } } diff --git a/Atlassian.Stash.Api/Properties/AssemblyInfo.cs b/Atlassian.Stash.Api/Properties/AssemblyInfo.cs index eccc438..2f67f09 100644 --- a/Atlassian.Stash.Api/Properties/AssemblyInfo.cs +++ b/Atlassian.Stash.Api/Properties/AssemblyInfo.cs @@ -31,6 +31,6 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.1.0.0")] -[assembly: AssemblyFileVersion("0.1.0.0")] -[assembly: AssemblyInformationalVersion("0.1.0-alpha")] \ No newline at end of file +[assembly: AssemblyVersion("0.1.1.0")] +[assembly: AssemblyFileVersion("0.1.1.0")] +[assembly: AssemblyInformationalVersion("0.1.1-alpha")] \ No newline at end of file diff --git a/Atlassian.Stash.Api/Workers/HttpCommunicationWorker.cs b/Atlassian.Stash.Api/Workers/HttpCommunicationWorker.cs index 5bf8d64..b2f6f78 100644 --- a/Atlassian.Stash.Api/Workers/HttpCommunicationWorker.cs +++ b/Atlassian.Stash.Api/Workers/HttpCommunicationWorker.cs @@ -73,6 +73,23 @@ public async Task DeleteAsync(string requestUrl) { HttpResponseMessage httpResponse = await _httpClient.DeleteAsync(requestUrl); + if (httpResponse.StatusCode != HttpStatusCode.NoContent && httpResponse.StatusCode != HttpStatusCode.Accepted) + { + throw new Exception(string.Format("DELETE operation unsuccessful! Got HTTP status code '{0}'", httpResponse.StatusCode)); + } + } + + public async Task DeleteAsyncWithJsonContent(string requestUrl, T data) + { + var requestMessage = new HttpRequestMessage(HttpMethod.Delete, requestUrl); + + string jsonData = JsonConvert.SerializeObject(data); + + requestMessage.Content = new StringContent(jsonData, System.Text.Encoding.UTF8, "application/json"); + + HttpResponseMessage httpResponse = await _httpClient.SendAsync(requestMessage); + + if (httpResponse.StatusCode != HttpStatusCode.NoContent && httpResponse.StatusCode != HttpStatusCode.Accepted) { throw new Exception(string.Format("DELETE operation unsuccessful! Got HTTP status code '{0}'", httpResponse.StatusCode)); diff --git a/README.md b/README.md index 6204bdb..3dbfba5 100644 --- a/README.md +++ b/README.md @@ -17,19 +17,19 @@ https://www.nuget.org/packages/Atlassian.Stash.Api/ ##Sample Usage -Create a Stash connection + //Create a Stash connection // Stash client connection using basic authentication var client = new StashClient("http://your_stash_server_url:7990/", "username", "password"); -Gets a list of projects (by default a maximum of 25 results will be return) + //Gets a list of projects (by default a maximum of 25 results will be return) // we recommend use of async/await instead of forcing synchronous execution var projects = client.Projects.Get().Result; -Gets a list of repositories from project "PROJKEY" (by default a maximum of 25 results will be return) + //Gets a list of repositories from project "PROJKEY" (by default a maximum of 25 results will be return) // using async var repositories = await client.Repositories.Get("PROJKEY"); -Delete repository "REPOSLUG" from project "PROJKEY" + //Delete repository "REPOSLUG" from project "PROJKEY" await client.Repositories.Delete("PROJKEY", "REPOSLUG");