Skip to content

Commit

Permalink
adding manage branches capabilities (CREATE and DELETE)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlouros committed Nov 3, 2014
1 parent 76bec3a commit 23f7bb7
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 17 deletions.
17 changes: 17 additions & 0 deletions Atlassian.Stash.Api.IntegrationTests/StashClientTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
}
}
21 changes: 20 additions & 1 deletion Atlassian.Stash.Api/Api/Branches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -24,5 +26,22 @@ public async Task<ResponseWrapper<Branch>> GetAll(string projectKey, string repo

return response;
}

// branch Utils API
public async Task<Branch> 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);
}
}
}
15 changes: 8 additions & 7 deletions Atlassian.Stash.Api/Atlassian.Stash.Api.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<releaseNotes>
*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'
</releaseNotes>
<copyright>Copyright John Louros 2014</copyright>
<tags>Atlassian Stash API-Wrapper</tags>
Expand Down
13 changes: 11 additions & 2 deletions Atlassian.Stash.Api/Entities/Branch.cs
Original file line number Diff line number Diff line change
@@ -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; }

}
}
6 changes: 3 additions & 3 deletions Atlassian.Stash.Api/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
[assembly: AssemblyVersion("0.1.1.0")]
[assembly: AssemblyFileVersion("0.1.1.0")]
[assembly: AssemblyInformationalVersion("0.1.1-alpha")]
17 changes: 17 additions & 0 deletions Atlassian.Stash.Api/Workers/HttpCommunicationWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(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));
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");


Expand Down

0 comments on commit 23f7bb7

Please sign in to comment.