Example #1
0
        public GitRevert CreateRevert()
        {
            VssConnection connection = this.Context.Connection;
            GitHttpClient gitClient  = connection.GetClient <GitHttpClient>();

            Guid          projectId  = ClientSampleHelpers.FindAnyProject(this.Context).Id;
            GitRepository repo       = GitSampleHelpers.FindAnyRepository(this.Context, projectId);
            string        branchName = repo.DefaultBranch;
            string        branchNameWithoutRefsHeads = branchName.Remove(0, "refs/heads/".Length);

            // find the latest commit on default
            GitCommitRef latestCommitOnDefault = gitClient.GetCommitsAsync(repo.Id, new GitQueryCommitsCriteria()
            {
                ItemVersion = new GitVersionDescriptor()
                {
                    Version     = branchNameWithoutRefsHeads,
                    VersionType = GitVersionType.Branch
                },
                Top = 1
            }).Result.First();

            // generate a unique name to suggest for the branch
            string suggestedBranchName = "refs/heads/azure-devops-dotnet-samples/" + GitSampleHelpers.ChooseRefsafeName();

            // write down the name for a later sample
            this.Context.SetValue <string>("$gitSamples.suggestedRevertBranchName", suggestedBranchName);

            // revert it relative to default branch
            GitRevert revert = gitClient.CreateRevertAsync(
                new GitAsyncRefOperationParameters()
            {
                OntoRefName      = branchName,
                GeneratedRefName = suggestedBranchName,
                Repository       = repo,
                Source           = new GitAsyncRefOperationSource()
                {
                    CommitList = new GitCommitRef[] { new GitCommitRef()
                                                      {
                                                          CommitId = latestCommitOnDefault.CommitId
                                                      } }
                },
            },
                projectId,
                repo.Id).Result;

            Console.WriteLine("Revert {0} created", revert.RevertId);

            // typically, the next thing you'd do is create a PR for this revert
            return(revert);
        }