Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Added samples for the Git Commits endpoint. #148

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
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<GitCommitRef> GetAllCommits()
{
GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context);

return this.Context.Connection.GetClient<GitHttpClient>()
.GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria()).Result;
}

[ClientSampleMethod]
public List<GitCommitRef> GetCommitsByAuthor()
{
GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context);

return this.Context.Connection.GetClient<GitHttpClient>()
.GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria()
{
Author = "Norman Paulk"
}).Result;
}

[ClientSampleMethod]
public List<GitCommitRef> GetCommitsByCommitter()
{
GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context);

return this.Context.Connection.GetClient<GitHttpClient>()
.GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria()
{
Committer = "[email protected]"
}).Result;
}

[ClientSampleMethod]
public List<GitCommitRef> GetCommitsOnABranch()
{
GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context);

return this.Context.Connection.GetClient<GitHttpClient>()
.GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria()
{
ItemVersion = new GitVersionDescriptor()
{
VersionType = GitVersionType.Branch,
VersionOptions = GitVersionOptions.None,
Version = "master"
}
}).Result;
}

[ClientSampleMethod]
public List<GitCommitRef> GetCommitsOnABranchAndInAPath()
{
GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context);

return this.Context.Connection.GetClient<GitHttpClient>()
.GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria()
{
ItemVersion = new GitVersionDescriptor()
{
VersionType = GitVersionType.Branch,
VersionOptions = GitVersionOptions.None,
Version = "master"
},
ItemPath = "/debug.log"
}).Result;
}

[ClientSampleMethod]
public List<GitCommitRef> GetCommitsInDateRange()
{
GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context);

return this.Context.Connection.GetClient<GitHttpClient>()
.GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria()
{
FromDate = new DateTime(2018, 6, 14).ToString(),
ToDate = new DateTime(2018, 6, 16).ToString()
}).Result;
}

[ClientSampleMethod]
public List<GitCommitRef> GetCommitsReachableFromACommit()
{
GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context);

return this.Context.Connection.GetClient<GitHttpClient>()
.GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria()
{
// Earliest commit in the graph to search.
CompareVersion = m_oldestDescriptor
}).Result;
}

[ClientSampleMethod]
public List<GitCommitRef> GetCommitsReachableFromACommitAndInPath()
{
GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context);

return this.Context.Connection.GetClient<GitHttpClient>()
.GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria()
{
CompareVersion = m_tipCommitDescriptor,
ItemVersion = m_oldestDescriptor,
ItemPath = "/README.md",
}).Result;
}

[ClientSampleMethod]
public List<GitCommitRef> GetCommitsPaging()
{
GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context);

return this.Context.Connection.GetClient<GitHttpClient>()
.GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria() { }, skip: 1, top: 2).Result;
}

[ClientSampleMethod]
public List<GitCommitRef> GetCommitsMultipleById()
{
GitRepository repo = GitSampleHelpers.FindAnyRepositoryOnAnyProject(this.Context);

return this.Context.Connection.GetClient<GitHttpClient>()
.GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria()
{
Ids = new List<string>()
{
"9991b4f66def4c0a9ad8f9f27043ece7eddcf1c7",
"9c1e8b082e2c2f041bdd5db76d6bf5c11572524f"
}
}).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"
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down