From bc797c58f4d85edd248e5faec8a32228928231ef Mon Sep 17 00:00:00 2001 From: Shady Ibraheem Date: Wed, 27 Jun 2018 16:48:46 -0400 Subject: [PATCH 1/2] Added samples for the Git Commits endpoint. --- .../Git/CommitsSample.cs | 141 ++++++++++++++++++ .../Git/GitSampleHelpers.cs | 6 + 2 files changed, 147 insertions(+) create mode 100644 ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/Git/CommitsSample.cs diff --git a/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/Git/CommitsSample.cs b/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/Git/CommitsSample.cs new file mode 100644 index 00000000..5ab9512b --- /dev/null +++ b/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/Git/CommitsSample.cs @@ -0,0 +1,141 @@ +using System; +using System.Collections.Generic; +using Microsoft.TeamFoundation.SourceControl.WebApi; + +namespace Microsoft.TeamServices.Samples.Client.Git +{ + [ClientSample(GitWebApiConstants.AreaName, "commits")] + public class CommitsSample : ClientSample + { + [ClientSampleMethod] + public List GetAllCommits() + { + GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context); + + return this.Context.Connection.GetClient() + .GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria()).Result; + } + + [ClientSampleMethod] + public List GetCommitsByAuthor() + { + GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context); + + return this.Context.Connection.GetClient() + .GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria() + { + Author = "Norman Paulk" + }).Result; + } + + [ClientSampleMethod] + public List GetCommitsByCommitter() + { + GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context); + + return this.Context.Connection.GetClient() + .GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria() + { + Committer = "Fabrikamfiber16@hotmail.com" + }).Result; + } + + [ClientSampleMethod] + public List GetCommitsOnABranch() + { + GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context); + + return this.Context.Connection.GetClient() + .GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria() + { + ItemVersion = new GitVersionDescriptor() + { + VersionType = GitVersionType.Branch, + VersionOptions = GitVersionOptions.None, + Version = "master" + } + }).Result; + } + + [ClientSampleMethod] + public List GetCommitsOnABranchAndInAPath() + { + GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context); + + return this.Context.Connection.GetClient() + .GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria() + { + ItemVersion = new GitVersionDescriptor() + { + VersionType = GitVersionType.Branch, + VersionOptions = GitVersionOptions.None, + Version = "master" + }, + ItemPath = "/debug.log" + }).Result; + } + + [ClientSampleMethod] + public List GetCommitsInDateRange() + { + GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context); + + return this.Context.Connection.GetClient() + .GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria() + { + FromDate = new DateTime(2018, 6, 14).ToString(), + ToDate = new DateTime(2018, 6, 16).ToString() + }).Result; + } + + [ClientSampleMethod] + public List GetCommitsReachableFromACommit() + { + GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context); + + return this.Context.Connection.GetClient() + .GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria() + { + // Earliest commit in the graph to search. + CompareVersion = m_oldestDescriptor + }).Result; + } + + [ClientSampleMethod] + public List GetCommitsReachableFromACommitAndInPath() + { + GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context); + + return this.Context.Connection.GetClient() + .GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria() + { + CompareVersion = m_tipCommitDescriptor, + ItemVersion = m_oldestDescriptor, + ItemPath = "/README.md", + }).Result; + } + + [ClientSampleMethod] + public List GetCommitsPaging() + { + GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context); + + return this.Context.Connection.GetClient() + .GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria() { }, skip: 1, top: 2).Result; + } + + private GitVersionDescriptor m_oldestDescriptor = new GitVersionDescriptor() + { + VersionType = GitVersionType.Commit, + VersionOptions = GitVersionOptions.None, + Version = "4fa42e1a7b0215cc70cd4e927cb70c422123af84" + }; + + private GitVersionDescriptor m_tipCommitDescriptor = new GitVersionDescriptor() + { + VersionType = GitVersionType.Branch, + VersionOptions = GitVersionOptions.None, + Version = "master" + }; + } +} diff --git a/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/Git/GitSampleHelpers.cs b/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/Git/GitSampleHelpers.cs index f47ed7c1..b95fc359 100644 --- a/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/Git/GitSampleHelpers.cs +++ b/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/Git/GitSampleHelpers.cs @@ -11,6 +11,12 @@ namespace Microsoft.TeamServices.Samples.Client.Git { public class GitSampleHelpers { + public static GitRepository FindAnyRepositoryOnAnyProject(ClientSampleContext context) + { + Guid projectId = ClientSampleHelpers.FindAnyProject(context).Id; + return FindAnyRepository(context, projectId); + } + public static GitRepository FindAnyRepository(ClientSampleContext context, Guid projectId) { GitRepository repo; From ab4028bf5c8830ea276a930f8fb0199c044118c3 Mon Sep 17 00:00:00 2001 From: Shady Ibraheem Date: Fri, 29 Jun 2018 11:51:33 -0400 Subject: [PATCH 2/2] Adds a sample to show how to query commits by Id. --- .../Git/CommitsSample.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/Git/CommitsSample.cs b/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/Git/CommitsSample.cs index 5ab9512b..daf8274e 100644 --- a/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/Git/CommitsSample.cs +++ b/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/Git/CommitsSample.cs @@ -124,6 +124,22 @@ public List GetCommitsPaging() .GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria() { }, skip: 1, top: 2).Result; } + [ClientSampleMethod] + public List GetCommitsMultipleById() + { + GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context); + + return this.Context.Connection.GetClient() + .GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria() + { + Ids = new List() + { + "9991b4f66def4c0a9ad8f9f27043ece7eddcf1c7", + "9c1e8b082e2c2f041bdd5db76d6bf5c11572524f" + } + }).Result; + } + private GitVersionDescriptor m_oldestDescriptor = new GitVersionDescriptor() { VersionType = GitVersionType.Commit,