forked from microsoft/azure-devops-dotnet-samples
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
449 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"ExpandedNodes": [ | ||
"" | ||
] | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using VstsRestApiSamples.Git; | ||
using System.Net; | ||
|
||
namespace VstsRestApiSamples.Tests.Git | ||
{ | ||
[TestClass] | ||
public class GitRepositoryTest | ||
{ | ||
private IConfiguration _configuration = new Configuration(); | ||
|
||
[TestInitialize] | ||
public void TestInitialize() | ||
{ | ||
InitHelper.GetConfiguration(_configuration); | ||
} | ||
|
||
[TestCleanup] | ||
public void TestCleanup() | ||
{ | ||
_configuration = null; | ||
} | ||
|
||
[TestMethod, TestCategory("REST API")] | ||
public void Git_Repository_GetAllRepositories_Success() | ||
{ | ||
//arrange | ||
GitRepository request = new GitRepository(_configuration); | ||
|
||
//act | ||
var response = request.GetAllRepositories(); | ||
|
||
//assert | ||
Assert.AreEqual(HttpStatusCode.OK, response.HttpStatusCode); | ||
|
||
request = null; | ||
} | ||
|
||
[TestMethod, TestCategory("REST API")] | ||
public void Git_Repository_GetAllRepositoryById_Success() | ||
{ | ||
//arrange | ||
GitRepository request = new GitRepository(_configuration); | ||
|
||
//act | ||
var response = request.GetRepositoryById(_configuration.GitRepositoryId); | ||
|
||
//assert | ||
Assert.AreEqual(HttpStatusCode.OK, response.HttpStatusCode); | ||
|
||
request = null; | ||
} | ||
|
||
[TestMethod, TestCategory("REST API")] | ||
public void Git_Repository_GetFolderAndChildren_Success() | ||
{ | ||
//arrange | ||
GitRepository request = new GitRepository(_configuration); | ||
|
||
//act | ||
var response = request.GetFolderAndChildren(_configuration.GitRepositoryId, "/"); | ||
|
||
//assert | ||
Assert.AreEqual(HttpStatusCode.OK, response.HttpStatusCode); | ||
|
||
request = null; | ||
} | ||
|
||
[TestMethod, TestCategory("REST API")] | ||
public void Git_Repository_GetCommitsByRepositoryId_Success() | ||
{ | ||
//arrange | ||
GitRepository request = new GitRepository(_configuration); | ||
|
||
//act | ||
var response = request.GetCommitsByRepositoryId(_configuration.GitRepositoryId); | ||
|
||
//assert | ||
Assert.AreEqual(HttpStatusCode.OK, response.HttpStatusCode); | ||
|
||
request = null; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using VstsRestApiSamples.ViewModels.Git; | ||
|
||
namespace VstsRestApiSamples.Git | ||
{ | ||
public class GitRepository | ||
{ | ||
readonly IConfiguration _configuration; | ||
readonly string _credentials; | ||
|
||
public GitRepository(IConfiguration configuration) | ||
{ | ||
_configuration = configuration; | ||
_credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", _configuration.PersonalAccessToken))); | ||
} | ||
|
||
public GetAllRepositoriesResponse.Repositories GetAllRepositories() | ||
{ | ||
GetAllRepositoriesResponse.Repositories viewModel = new GetAllRepositoriesResponse.Repositories(); | ||
|
||
using (var client = new HttpClient()) | ||
{ | ||
client.BaseAddress = new Uri(_configuration.UriString); | ||
client.DefaultRequestHeaders.Accept.Clear(); | ||
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); | ||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials); | ||
|
||
HttpResponseMessage response = client.GetAsync("/_apis/git/repositories/?api-version=2.0").Result; | ||
|
||
if (response.IsSuccessStatusCode) | ||
{ | ||
viewModel = response.Content.ReadAsAsync<GetAllRepositoriesResponse.Repositories>().Result; | ||
} | ||
|
||
viewModel.HttpStatusCode = response.StatusCode; | ||
|
||
return viewModel; | ||
} | ||
} | ||
|
||
public GetRepositoryByIdResponse.Repository GetRepositoryById(string repositoryId) | ||
{ | ||
GetRepositoryByIdResponse.Repository viewModel = new GetRepositoryByIdResponse.Repository(); | ||
|
||
using (var client = new HttpClient()) | ||
{ | ||
client.BaseAddress = new Uri(_configuration.UriString); | ||
client.DefaultRequestHeaders.Accept.Clear(); | ||
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); | ||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials); | ||
|
||
HttpResponseMessage response = client.GetAsync("/_apis/git/repositories/" + repositoryId + "?api-version=2.0").Result; | ||
|
||
if (response.IsSuccessStatusCode) | ||
{ | ||
viewModel = response.Content.ReadAsAsync<GetRepositoryByIdResponse.Repository>().Result; | ||
} | ||
|
||
viewModel.HttpStatusCode = response.StatusCode; | ||
|
||
return viewModel; | ||
} | ||
} | ||
|
||
public GetFolderAndChildrenResponse.FolderAndChildren GetFolderAndChildren(string repositoryId, string scopePath) | ||
{ | ||
GetFolderAndChildrenResponse.FolderAndChildren viewModel = new GetFolderAndChildrenResponse.FolderAndChildren(); | ||
|
||
using (var client = new HttpClient()) | ||
{ | ||
client.BaseAddress = new Uri(_configuration.UriString); | ||
client.DefaultRequestHeaders.Accept.Clear(); | ||
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); | ||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials); | ||
|
||
HttpResponseMessage response = client.GetAsync("/_apis/git/repositories/" + repositoryId + "/items?scopePath=" + scopePath + "&recursionLevel=Full&includeContentMetadata=true&api-version=2.0").Result; | ||
|
||
if (response.IsSuccessStatusCode) | ||
{ | ||
viewModel = response.Content.ReadAsAsync<GetFolderAndChildrenResponse.FolderAndChildren>().Result; | ||
} | ||
|
||
viewModel.HttpStatusCode = response.StatusCode; | ||
|
||
return viewModel; | ||
} | ||
} | ||
|
||
public GetCommitsByRepositoryIdResponse.Commits GetCommitsByRepositoryId(string repositoryId) | ||
{ | ||
GetCommitsByRepositoryIdResponse.Commits viewModel = new GetCommitsByRepositoryIdResponse.Commits(); | ||
|
||
using (var client = new HttpClient()) | ||
{ | ||
client.BaseAddress = new Uri(_configuration.UriString); | ||
client.DefaultRequestHeaders.Accept.Clear(); | ||
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); | ||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials); | ||
|
||
HttpResponseMessage response = client.GetAsync("/_apis/git/repositories/" + repositoryId + "/commits?api-version=2.0").Result; | ||
|
||
if (response.IsSuccessStatusCode) | ||
{ | ||
viewModel = response.Content.ReadAsAsync<GetCommitsByRepositoryIdResponse.Commits>().Result; | ||
} | ||
|
||
viewModel.HttpStatusCode = response.StatusCode; | ||
|
||
return viewModel; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
VSTSRestApiSamples/ViewModels/Git/GetAllRepositoriesResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
| ||
using System.Collections.Generic; | ||
|
||
namespace VstsRestApiSamples.ViewModels.Git | ||
{ | ||
public class GetAllRepositoriesResponse | ||
{ | ||
public class Repositories : BaseViewModel | ||
{ | ||
public List<Value> value { get; set; } | ||
public int count { get; set; } | ||
} | ||
|
||
public class Project | ||
{ | ||
public string id { get; set; } | ||
public string name { get; set; } | ||
public string description { get; set; } | ||
public string url { get; set; } | ||
public string state { get; set; } | ||
public int revision { get; set; } | ||
} | ||
|
||
public class Value | ||
{ | ||
public string id { get; set; } | ||
public string name { get; set; } | ||
public string url { get; set; } | ||
public Project project { get; set; } | ||
public string defaultBranch { get; set; } | ||
public string remoteUrl { get; set; } | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
VSTSRestApiSamples/ViewModels/Git/GetCommitsByRepositoryIdResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace VstsRestApiSamples.ViewModels.Git | ||
{ | ||
public class GetCommitsByRepositoryIdResponse | ||
{ | ||
public class Commits : BaseViewModel | ||
{ | ||
public int count { get; set; } | ||
public List<Value> value { get; set; } | ||
} | ||
public class Author | ||
{ | ||
public string name { get; set; } | ||
public string email { get; set; } | ||
public string date { get; set; } | ||
} | ||
|
||
public class Committer | ||
{ | ||
public string name { get; set; } | ||
public string email { get; set; } | ||
public string date { get; set; } | ||
} | ||
|
||
public class ChangeCounts | ||
{ | ||
public int Add { get; set; } | ||
public int Edit { get; set; } | ||
} | ||
|
||
public class Value | ||
{ | ||
public string commitId { get; set; } | ||
public Author author { get; set; } | ||
public Committer committer { get; set; } | ||
public string comment { get; set; } | ||
public ChangeCounts changeCounts { get; set; } | ||
public string url { get; set; } | ||
public string remoteUrl { get; set; } | ||
} | ||
|
||
|
||
} | ||
} |
Oops, something went wrong.