Skip to content

Commit

Permalink
Adds Additional Git Samples
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattwebb committed Oct 11, 2016
1 parent 38d4b6d commit 61dac20
Show file tree
Hide file tree
Showing 21 changed files with 365 additions and 1 deletion.
2 changes: 2 additions & 0 deletions VSTSRestApiSamples.UnitTests/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public class Configuration : IConfiguration
public string QueryId { get; set; }
public string FilePath { get; set; }
public string GitRepositoryId { get; set; }
public string GitTargetVersionBranch { get; set; }
public string GitBaseVersionBranch { get; set; }
}
}
30 changes: 30 additions & 0 deletions VSTSRestApiSamples.UnitTests/Git/GitRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,35 @@ public void Git_Repository_GetCommitsByRepositoryId_Success()
request = null;
}

[TestMethod, TestCategory("REST API")]
public void Git_Repository_GetDiffsByRepositoryId_Success()
{
//arrange
GitRepository request = new GitRepository(_configuration);

//act
var response = request.GetDiffsByRepositoryId(
_configuration.GitRepositoryId,
_configuration.GitTargetVersionBranch,
_configuration.GitBaseVersionBranch);

//assert
Assert.AreEqual(HttpStatusCode.OK, response.HttpStatusCode); // This is throwing a bad request, but as far as I can tell it's following the examples online.
}

[TestMethod, TestCategory("REST API")]
public void Git_Repository_GetStatsByRepositoryId_Success()
{
//arrange
GitRepository request = new GitRepository(_configuration);

//act
var response = request.GetStatsByRepositoryId(
_configuration.GitRepositoryId);

//assert
Assert.AreEqual(HttpStatusCode.OK, response.HttpStatusCode);
}

}
}
2 changes: 2 additions & 0 deletions VSTSRestApiSamples.UnitTests/InitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public static IConfiguration GetConfiguration(IConfiguration configuration)
configuration.QueryId = ConfigurationSettings.AppSettings["appsetting.queryid"].ToString();
configuration.FilePath = ConfigurationSettings.AppSettings["appsetting.filepath"].ToString();
configuration.GitRepositoryId = ConfigurationSettings.AppSettings["appsetting.git.repositoryid"].ToString();
configuration.GitTargetVersionBranch = ConfigurationSettings.AppSettings["appsetting.git.targetVersionBranch"].ToString();
configuration.GitBaseVersionBranch = ConfigurationSettings.AppSettings["appsetting.git.baseVersionBranch"].ToString();

return configuration;
}
Expand Down
2 changes: 2 additions & 0 deletions VSTSRestApiSamples.UnitTests/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<add key="appsetting.queryid" value="" />
<add key="appsetting.filepath" value="" />
<add key="appsetting.git.repositoryid" value="" />
<add key="appsetting.git.targetVersionBranch" value="" />
<add key="appsetting.git.baseVersionBranch" value="" />
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
Expand Down
2 changes: 2 additions & 0 deletions VSTSRestApiSamples/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ public class Configuration : IConfiguration
public string QueryId { get; set; }
public string FilePath { get; set; }
public string GitRepositoryId { get; set; }
public string GitTargetVersionBranch { get; set; }
public string GitBaseVersionBranch { get; set; }
}
}
48 changes: 48 additions & 0 deletions VSTSRestApiSamples/Git/Repositories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,53 @@ public GetCommitsByRepositoryIdResponse.Commits GetCommitsByRepositoryId(string
return viewModel;
}
}

public GetDiffsByRepositoryIdResponse.Diffs GetDiffsByRepositoryId(string repositoryId, string targetVersion, string baseVersion)
{
GetDiffsByRepositoryIdResponse.Diffs viewModel = new GetDiffsByRepositoryIdResponse.Diffs();

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 + "/diffs/commits?targetVersion=" + targetVersion + "&baseVersion=" + baseVersion + " &api-version=1.0").Result;

if (response.IsSuccessStatusCode)
{
viewModel = response.Content.ReadAsAsync<GetDiffsByRepositoryIdResponse.Diffs>().Result;
}

viewModel.HttpStatusCode = response.StatusCode;

return viewModel;
}
}

public GetStatsByRepositoryIdResponse.Stats GetStatsByRepositoryId(string repositoryId)
{
GetStatsByRepositoryIdResponse.Stats viewModel = new GetStatsByRepositoryIdResponse.Stats();

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 + "/stats/branches?api-version=2.0").Result;

if (response.IsSuccessStatusCode)
{
viewModel = response.Content.ReadAsAsync<GetStatsByRepositoryIdResponse.Stats>().Result;
}

viewModel.HttpStatusCode = response.StatusCode;

return viewModel;
}
}
}
}
2 changes: 2 additions & 0 deletions VSTSRestApiSamples/IConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ public interface IConfiguration
string QueryId { get; set; }
string FilePath { get; set; }
string GitRepositoryId { get; set; }
string GitTargetVersionBranch { get; set; }
string GitBaseVersionBranch { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Collections.Generic;

namespace VstsRestApiSamples.ViewModels.Git
{
public class GetDiffsByRepositoryIdResponse
{
public class Diffs : BaseViewModel
{
public bool allChangesIncluded { get; set; }
public ChangeCounts changeCounts { get; set; }
public List<Change> changes { get; set; }
public string commonCommit { get; set; }
public int aheadCount { get; set; }
public int behindCount { get; set; }
}

public class ChangeCounts
{
public int Add { get; set; }
public int Edit { get; set; }
}

public class Item
{
public string gitObjectType { get; set; }
public string commitId { get; set; }
public string path { get; set; }
public bool isFolder { get; set; }
public string url { get; set; }
}

public class Change
{
public Item item { get; set; }
public string changeType { get; set; }
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.Generic;

namespace VstsRestApiSamples.ViewModels.Git
{
public class GetStatsByRepositoryIdResponse
{
public class Stats : 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 Commit
{
public string commitId { get; set; }
public Author author { get; set; }
public Committer committer { get; set; }
public string comment { get; set; }
public string url { get; set; }
public List<string> parents { get; set; }
public string treeId { get; set; }
}

public class Value
{
public Commit commit { get; set; }
public string name { get; set; }
public int aheadCount { get; set; }
public int behindCount { get; set; }
public bool isBaseVersion { get; set; }
}

}
}
2 changes: 2 additions & 0 deletions VSTSRestApiSamples/VstsRestApiSamples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
<Compile Include="ProjectsAndTeams\Processes.cs" />
<Compile Include="ViewModels\Git\GetAllRepositoriesResponse.cs" />
<Compile Include="ViewModels\Git\GetCommitsByRepositoryIdResponse.cs" />
<Compile Include="ViewModels\Git\GetDiffsByRepositoryIdResponse.cs" />
<Compile Include="ViewModels\Git\GetStatsByRepositoryIdResponse.cs" />
<Compile Include="ViewModels\Git\GetFolderAndChildrenResponse.cs" />
<Compile Include="ViewModels\Git\GetRepositoryByIdResponse.cs" />
<Compile Include="ViewModels\ProjectsAndTeams\ListofProjectsResponse.cs" />
Expand Down
6 changes: 6 additions & 0 deletions VSTSRestApiSamples/VstsRestApiSamples.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ShowAllFiles</ProjectView>
</PropertyGroup>
</Project>
3 changes: 3 additions & 0 deletions VstsClientLibrariesSamples.Tests/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ public class Configuration : IConfiguration
public string WorkItemIds { get; set; }
public Int32 WorkItemId { get; set; }
public string FilePath { get; set; }
public string GitRepositoryId { get; set; }
public string GitTargetVersionBranch { get; set; }
public string GitBaseVersionBranch { get; set; }
}
}
80 changes: 80 additions & 0 deletions VstsClientLibrariesSamples.Tests/Git/GitSampleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using VstsClientLibrariesSamples.Git;

namespace VstsClientLibrariesSamples.Tests.Git
{
[TestClass]
public class GitSampleTests
{
private IConfiguration _configuration = new Configuration();

[TestInitialize]
public void TestInitialize()
{
InitHelper.GetConfiguration(_configuration);
}

[TestCleanup]
public void TestCleanup()
{
_configuration = null;
}


[TestMethod, TestCategory("Client Libraries")]
public void Sample_GetRepositories_Success()
{
var gitSample = new Sample(_configuration);

try
{
// act
var result = gitSample.GetRepositories();

Assert.IsNotNull(result);
}
catch (NullReferenceException ex)
{
Assert.Inconclusive(ex.Message);
}
}


[TestMethod, TestCategory("Client Libraries")]
public void Sample_GetGitCommitDiffs_Success()
{
var gitSample = new Sample(_configuration);

try
{
// act
var result = gitSample.GetGitCommitDiffs();

Assert.IsNotNull(result);
}
catch (NullReferenceException ex)
{
Assert.Inconclusive(ex.Message);
}
}

[TestMethod, TestCategory("Client Libraries")]
public void Sample_GetGitCommitDiffsByBranch_Success()
{
var gitSample = new Sample(_configuration);

try
{
// act
var result = gitSample.GetGitCommitDiffsByBranch();

Assert.IsNotNull(result);
}
catch (NullReferenceException ex)
{
Assert.Inconclusive(ex.Message);
}
}
}
}
8 changes: 7 additions & 1 deletion VstsClientLibrariesSamples.Tests/InitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Common;

namespace VstsClientLibrariesSamples.Tests
{
Expand All @@ -17,8 +18,13 @@ public static IConfiguration GetConfiguration(IConfiguration configuration)
configuration.Identity = ConfigurationSettings.AppSettings["appsetting.identity"].ToString();
configuration.UriString = ConfigurationSettings.AppSettings["appsetting.uri"].ToString();
configuration.WorkItemIds = ConfigurationSettings.AppSettings["appsetting.workitemids"].ToString();
configuration.WorkItemId = Convert.ToInt32(ConfigurationSettings.AppSettings["appsetting.workitemid"].ToString());
configuration.WorkItemId = ConfigurationSettings.AppSettings["appsetting.workitemid"].IsNullOrEmpty() ?
0 :
Convert.ToInt32(ConfigurationSettings.AppSettings["appsetting.workitemid"].ToString());
configuration.FilePath = ConfigurationSettings.AppSettings["appsetting.filepath"].ToString();
configuration.GitRepositoryId = ConfigurationSettings.AppSettings["appsetting.git.repositoryid"].ToString();
configuration.GitTargetVersionBranch = ConfigurationSettings.AppSettings["appsetting.git.targetVersionBranch"].ToString();
configuration.GitBaseVersionBranch = ConfigurationSettings.AppSettings["appsetting.git.baseVersionBranch"].ToString();

return configuration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
<ItemGroup>
<Compile Include="Configuration.cs" />
<Compile Include="GettingStarted\AuthenticationTest.cs" />
<Compile Include="Git\GitSampleTests.cs" />
<Compile Include="InitHelper.cs" />
<Compile Include="ProjectsAndTeams\ProjectsTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
</Project>
3 changes: 3 additions & 0 deletions VstsClientLibrariesSamples.Tests/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
<add key="appsetting.workitemids" value="" />
<add key="appsetting.workitemid" value="" />
<add key="appsetting.filepath" value="" />
<add key="appsetting.git.repositoryid" value="" />
<add key="appsetting.git.targetVersionBranch" value="" />
<add key="appsetting.git.baseVersionBranch" value="" />
</appSettings>
</configuration>
3 changes: 3 additions & 0 deletions VstsClientLibrariesSamples/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ public class Configuration : IConfiguration
public string WorkItemIds { get; set; }
public Int32 WorkItemId { get; set; }
public string FilePath { get; set; }
public string GitRepositoryId { get; set; }
public string GitTargetVersionBranch { get; set; }
public string GitBaseVersionBranch { get; set; }
}
}
Loading

0 comments on commit 61dac20

Please sign in to comment.