Skip to content

Commit

Permalink
adding get for files and changes
Browse files Browse the repository at this point in the history
adding create and delete for Projects and Repositories
  • Loading branch information
jlouros committed Oct 30, 2014
1 parent dc455ae commit 43dfab4
Show file tree
Hide file tree
Showing 15 changed files with 300 additions and 56 deletions.
136 changes: 117 additions & 19 deletions Atlassian.Stash.Api.IntegrationTests/StashClientTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ public class StashClientTester
{
// data required to run this tests
// please review this variables before you run this tests
private const string EXISTING_PROJECT_NAME = "test";
private const string EXISTING_REPOSITORY_NAME = "testrepository";
private const string EXISTING_COMMIT_ID = "92c374add7dc939383a92128a29a7ef96c8af723";
private const string EXISTING_PROJECT = "test";
private const string EXISTING_REPOSITORY = "testrepository";
private const string EXISTING_COMMIT = "86486c762e901ea5efef1b7d287514b7f2cc0c82";
private const string EXISTING_OLDER_COMMIT = "9b533044d39811db518250b441d472ece955b0e3";

private StashClient _stashClient;

Expand All @@ -27,7 +28,7 @@ public void Initialize()
[TestMethod]
public void Can_GetAllProjects()
{
var response = _stashClient.Projects.GetAll().Result;
var response = _stashClient.Projects.Get().Result;
var projects = response.Values;

Assert.IsNotNull(projects);
Expand All @@ -39,7 +40,7 @@ public void Can_GetAllProjects()
public void Can_GetAllProjects_WithRequestOptions()
{
int requestLimit = 1;
var response = _stashClient.Projects.GetAll(new RequestOptions { Limit = requestLimit, Start = 1 }).Result;
var response = _stashClient.Projects.Get(new RequestOptions { Limit = requestLimit, Start = 1 }).Result;
var projects = response.Values;

Assert.IsNotNull(projects);
Expand All @@ -50,17 +51,17 @@ public void Can_GetAllProjects_WithRequestOptions()
[TestMethod]
public void Can_GetByIdProject()
{
var project = _stashClient.Projects.GetById(EXISTING_PROJECT_NAME).Result;
var project = _stashClient.Projects.GetById(EXISTING_PROJECT).Result;

Assert.IsNotNull(project);
Assert.IsInstanceOfType(project, typeof(Project));
Assert.AreEqual(EXISTING_PROJECT_NAME.ToLower(), project.Name.ToLower());
Assert.AreEqual(EXISTING_PROJECT.ToLower(), project.Name.ToLower());
}

[TestMethod]
public void Can_GetAllRepositories()
{
var response = _stashClient.Repositories.GetAll(EXISTING_PROJECT_NAME).Result;
var response = _stashClient.Repositories.Get(EXISTING_PROJECT).Result;
var repositories = response.Values;

Assert.IsNotNull(repositories);
Expand All @@ -72,7 +73,7 @@ public void Can_GetAllRepositories()
public void Can_GetAllRepositories_WithRequestOptions()
{
int requestLimit = 2;
var response = _stashClient.Repositories.GetAll(EXISTING_PROJECT_NAME, new RequestOptions { Limit = requestLimit }).Result;
var response = _stashClient.Repositories.Get(EXISTING_PROJECT, new RequestOptions { Limit = requestLimit }).Result;
var repositories = response.Values;

Assert.IsNotNull(repositories);
Expand All @@ -83,17 +84,17 @@ public void Can_GetAllRepositories_WithRequestOptions()
[TestMethod]
public void Can_GetByIdRepository()
{
var repository = _stashClient.Repositories.GetById(EXISTING_PROJECT_NAME, EXISTING_REPOSITORY_NAME).Result;
var repository = _stashClient.Repositories.GetById(EXISTING_PROJECT, EXISTING_REPOSITORY).Result;

Assert.IsNotNull(repository);
Assert.IsInstanceOfType(repository, typeof(Repository));
Assert.AreEqual(EXISTING_REPOSITORY_NAME.ToLower(), repository.Name.ToLower());
Assert.AreEqual(EXISTING_REPOSITORY.ToLower(), repository.Name.ToLower());
}

[TestMethod]
public void Can_GetAllTags()
{
var response = _stashClient.Tags.GetAll(EXISTING_PROJECT_NAME, EXISTING_REPOSITORY_NAME).Result;
var response = _stashClient.Repositories.GetTags(EXISTING_PROJECT, EXISTING_REPOSITORY).Result;
var tags = response.Values;

Assert.IsNotNull(tags);
Expand All @@ -105,18 +106,41 @@ public void Can_GetAllTags()
public void Can_GetAllTags_WithRequestOptions()
{
int requestLimit = 1;
var response = _stashClient.Tags.GetAll(EXISTING_PROJECT_NAME, EXISTING_REPOSITORY_NAME, new RequestOptions { Limit = requestLimit }).Result;
var response = _stashClient.Repositories.GetTags(EXISTING_PROJECT, EXISTING_REPOSITORY, new RequestOptions { Limit = requestLimit }).Result;
var tags = response.Values;

Assert.IsNotNull(tags);
Assert.IsInstanceOfType(tags, typeof(IEnumerable<Tag>));
Assert.AreEqual(requestLimit, tags.Count());
}

[TestMethod]
public void Can_GetAllFiles()
{
var response = _stashClient.Repositories.GetFiles(EXISTING_PROJECT, EXISTING_REPOSITORY).Result;
var files = response.Values;

Assert.IsNotNull(files);
Assert.IsInstanceOfType(files, typeof(IEnumerable<string>));
Assert.IsTrue(files.Any());
}

[TestMethod]
public void Can_GetAllFiles_WithRequestOptions()
{
int requestLimit = 1;
var response = _stashClient.Repositories.GetFiles(EXISTING_PROJECT, EXISTING_REPOSITORY, new RequestOptions { Limit = requestLimit }).Result;
var files = response.Values;

Assert.IsNotNull(files);
Assert.IsInstanceOfType(files, typeof(IEnumerable<string>));
Assert.AreEqual(requestLimit, files.Count());
}

[TestMethod]
public void Can_GetAllBranches()
{
var response = _stashClient.Branches.GetAll(EXISTING_PROJECT_NAME, EXISTING_REPOSITORY_NAME).Result;
var response = _stashClient.Branches.GetAll(EXISTING_PROJECT, EXISTING_REPOSITORY).Result;
var branches = response.Values;

Assert.IsNotNull(branches);
Expand All @@ -128,7 +152,7 @@ public void Can_GetAllBranches()
public void Can_GetAllBranches_WithRequestOptions()
{
int requestLimit = 1;
var response = _stashClient.Branches.GetAll(EXISTING_PROJECT_NAME, EXISTING_REPOSITORY_NAME, new RequestOptions { Limit = requestLimit }).Result;
var response = _stashClient.Branches.GetAll(EXISTING_PROJECT, EXISTING_REPOSITORY, new RequestOptions { Limit = requestLimit }).Result;
var branches = response.Values;

Assert.IsNotNull(branches);
Expand All @@ -139,7 +163,7 @@ public void Can_GetAllBranches_WithRequestOptions()
[TestMethod]
public void Can_GetAllCommits()
{
var response = _stashClient.Commits.GetAll(EXISTING_PROJECT_NAME, EXISTING_REPOSITORY_NAME).Result;
var response = _stashClient.Commits.GetAll(EXISTING_PROJECT, EXISTING_REPOSITORY).Result;
var commits = response.Values;

Assert.IsNotNull(commits);
Expand All @@ -151,7 +175,7 @@ public void Can_GetAllCommits()
public void Can_GetAllCommits_WithRequestOptions()
{
int requestLimit = 2;
var response = _stashClient.Commits.GetAll(EXISTING_PROJECT_NAME, EXISTING_REPOSITORY_NAME, new RequestOptions { Limit = requestLimit }).Result;
var response = _stashClient.Commits.GetAll(EXISTING_PROJECT, EXISTING_REPOSITORY, new RequestOptions { Limit = requestLimit }).Result;
var commits = response.Values;

Assert.IsNotNull(commits);
Expand All @@ -162,11 +186,85 @@ public void Can_GetAllCommits_WithRequestOptions()
[TestMethod]
public void Can_GetByIdCommit()
{
var commit = _stashClient.Commits.GetById(EXISTING_PROJECT_NAME, EXISTING_REPOSITORY_NAME, EXISTING_COMMIT_ID).Result;
var commit = _stashClient.Commits.GetById(EXISTING_PROJECT, EXISTING_REPOSITORY, EXISTING_COMMIT).Result;

Assert.IsNotNull(commit);
Assert.IsInstanceOfType(commit, typeof(Commit));
Assert.AreEqual(EXISTING_COMMIT_ID.ToLower(), commit.Id.ToLower());
Assert.AreEqual(EXISTING_COMMIT.ToLower(), commit.Id.ToLower());
}

[TestMethod]
public void Can_GetChangesUntil()
{
var changes = _stashClient.Commits.GetChanges(EXISTING_PROJECT, EXISTING_REPOSITORY, EXISTING_COMMIT).Result;

Assert.IsNotNull(changes);
Assert.IsInstanceOfType(changes, typeof(Changes));
Assert.AreEqual(EXISTING_COMMIT.ToLower(), changes.ToHash.ToLower());
}

[TestMethod]
public void Can_GetChangesUntil_WithRequestOptions()
{
int requestLimit = 1;
var changes = _stashClient.Commits.GetChanges(EXISTING_PROJECT, EXISTING_REPOSITORY, EXISTING_COMMIT, null, new RequestOptions { Limit = requestLimit }).Result;

Assert.IsNotNull(changes);
Assert.IsInstanceOfType(changes, typeof(Changes));
Assert.AreEqual(EXISTING_COMMIT.ToLower(), changes.ToHash.ToLower());
Assert.AreEqual(requestLimit, changes.ListOfChanges.Count());
}

[TestMethod]
public void Can_GetChangesUntil_And_Since()
{
var changes = _stashClient.Commits.GetChanges(EXISTING_PROJECT, EXISTING_REPOSITORY, EXISTING_COMMIT, EXISTING_OLDER_COMMIT).Result;

Assert.IsNotNull(changes);
Assert.IsInstanceOfType(changes, typeof(Changes));
Assert.AreEqual(EXISTING_COMMIT.ToLower(), changes.ToHash.ToLower());
}

[TestMethod]
public void Can_GetChangesUntil_And_Since_WithRequestOptions()
{
int requestLimit = 1;
var changes = _stashClient.Commits.GetChanges(EXISTING_PROJECT, EXISTING_REPOSITORY, EXISTING_COMMIT, EXISTING_OLDER_COMMIT, new RequestOptions { Limit = requestLimit }).Result;

Assert.IsNotNull(changes);
Assert.IsInstanceOfType(changes, typeof(Changes));
Assert.AreEqual(EXISTING_COMMIT.ToLower(), changes.ToHash.ToLower());
Assert.AreEqual(requestLimit, changes.ListOfChanges.Count());
}

#region Feature tests

[TestMethod]
public void Can_CreateProject_Than_DeleteProject()
{
Project newProject = new Project { Key = "ZTEST", Name = "Project of Integration tests", Description = "Project created by integration tests, please delete!" };
var createdProject = _stashClient.Projects.Create(newProject).Result;

Assert.IsNotNull(createdProject);
Assert.IsInstanceOfType(createdProject, typeof(Project));
Assert.AreEqual(newProject.Key.ToLower(), createdProject.Key.ToLower());

_stashClient.Projects.Delete(newProject.Key).Wait();
}

[TestMethod]
public void Can_CreateRepository_Than_DeleteRepository()
{
Repository newRepository = new Repository { Name = "Repository of Integration tests" };
var createdRepository = _stashClient.Repositories.Create(EXISTING_PROJECT, newRepository).Result;

Assert.IsNotNull(createdRepository);
Assert.IsInstanceOfType(createdRepository, typeof(Repository));
Assert.AreEqual(newRepository.Name.ToLower(), createdRepository.Name.ToLower());

_stashClient.Repositories.Delete(EXISTING_PROJECT, createdRepository.Slug).Wait();
}

#endregion
}
}
18 changes: 17 additions & 1 deletion Atlassian.Stash.Api/Api/Commits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public class Commits
{
private const string MANY_COMMITS = "/rest/api/1.0/projects/{0}/repos/{1}/commits";
private const string ONE_COMMIT = "/rest/api/1.0/projects/{0}/repos/{1}/commits/{2}";

private const string CHANGES_UNTIL = "/rest/api/1.0/projects/{0}/repos/{1}/changes?until={2}";
private const string CHANGES_UNTIL_AND_SINCE = "/rest/api/1.0/projects/{0}/repos/{1}/changes?until={2}&since={3}";

private HttpCommunicationWorker _httpWorker;

internal Commits(HttpCommunicationWorker httpWorker)
Expand All @@ -34,5 +36,19 @@ public async Task<Commit> GetById(string projectKey, string repositorySlug, stri

return response;
}

public async Task<Changes> GetChanges(string projectKey, string repositorySlug, string untilCommit, string sinceCommit = null, RequestOptions requestOptions = null)
{
string requestUrl = "";

if (string.IsNullOrWhiteSpace(sinceCommit))
requestUrl = UrlBuilder.FormatRestApiUrl(CHANGES_UNTIL, requestOptions, projectKey, repositorySlug, untilCommit);
else
requestUrl = UrlBuilder.FormatRestApiUrl(CHANGES_UNTIL, requestOptions, projectKey, repositorySlug, untilCommit, sinceCommit);

Changes response = await _httpWorker.GetAsync<Changes>(requestUrl);

return response;
}
}
}
18 changes: 17 additions & 1 deletion Atlassian.Stash.Api/Api/Projects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal Projects(HttpCommunicationWorker httpWorker)
_httpWorker = httpWorker;
}

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

Expand All @@ -34,5 +34,21 @@ public async Task<Project> GetById(string projectKey)

return response;
}

public async Task<Project> Create(Project project)
{
string requestUrl = UrlBuilder.FormatRestApiUrl(MANY_PROJECTS);

Project response = await _httpWorker.PostAsync<Project>(requestUrl, project);

return response;
}

public async Task Delete(string projectKey)
{
string requestUrl = UrlBuilder.FormatRestApiUrl(ONE_PROJECT, null, projectKey);

await _httpWorker.DeleteAsync(requestUrl);
}
}
}
38 changes: 37 additions & 1 deletion Atlassian.Stash.Api/Api/Repositories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ public class Repositories
{
private const string MANY_REPOSITORIES = "/rest/api/1.0/projects/{0}/repos";
private const string ONE_REPOSITORY = "/rest/api/1.0/projects/{0}/repos/{1}";
private const string MANY_TAGS = "/rest/api/1.0/projects/{0}/repos/{1}/tags";
private const string MANY_FILES = "/rest/api/1.0/projects/{0}/repos/{1}/files";

private HttpCommunicationWorker _httpWorker;

internal Repositories(HttpCommunicationWorker httpWorker)
{
_httpWorker = httpWorker;
}
public async Task<ResponseWrapper<Repository>> GetAll(string projectKey, RequestOptions requestOptions = null)
public async Task<ResponseWrapper<Repository>> Get(string projectKey, RequestOptions requestOptions = null)
{
string requestUrl = UrlBuilder.FormatRestApiUrl(MANY_REPOSITORIES, requestOptions, projectKey);

Expand All @@ -33,5 +35,39 @@ public async Task<Repository> GetById(string projectKey, string repositorySlug)

return response;
}

public async Task<ResponseWrapper<Tag>> GetTags(string projectKey, string repositorySlug, RequestOptions requestOptions = null)
{
string requestUrl = UrlBuilder.FormatRestApiUrl(MANY_TAGS, requestOptions, projectKey, repositorySlug);

ResponseWrapper<Tag> response = await _httpWorker.GetAsync<ResponseWrapper<Tag>>(requestUrl);

return response;
}

public async Task<ResponseWrapper<string>> GetFiles(string projectKey, string repositorySlug, RequestOptions requestOptions = null)
{
string requestUrl = UrlBuilder.FormatRestApiUrl(MANY_FILES, requestOptions, projectKey, repositorySlug);

ResponseWrapper<string> response = await _httpWorker.GetAsync<ResponseWrapper<string>>(requestUrl);

return response;
}

public async Task<Repository> Create(string projectKey, Repository repository)
{
string requestUrl = UrlBuilder.FormatRestApiUrl(MANY_REPOSITORIES, null, projectKey);

Repository response = await _httpWorker.PostAsync<Repository>(requestUrl, repository);

return response;
}

public async Task Delete(string projectKey, string repositorySlug)
{
string requestUrl = UrlBuilder.FormatRestApiUrl(ONE_REPOSITORY, null, projectKey, repositorySlug);

await _httpWorker.DeleteAsync(requestUrl);
}
}
}
27 changes: 0 additions & 27 deletions Atlassian.Stash.Api/Api/Tags.cs

This file was deleted.

Loading

0 comments on commit 43dfab4

Please sign in to comment.