Using With Repository Remote URL And Source Branch Name

To write issues as comments to Azure DevOps pull requests, the Azure DevOps addin needs to be imported. To determine the remote repository URL and source branch of the pull request the Cake.Git addin can be used. For this example the JetBrains InspectCode issue provider is additionally used for reading issues:

build.cake
#addin nuget:?package=Cake.Git&version=5.0.1
#addin nuget:?package=Cake.Issues&version=5.2.0
#addin nuget:?package=Cake.Issues.InspectCode&version=5.2.0
#addin nuget:?package=Cake.Issues.PullRequests&version=5.2.0
#addin nuget:?package=Cake.Issues.PullRequests.AzureDevOps&version=5.2.0
#addin nuget:?package=Cake.AzureDevOps&version=5.0.0

Note

In addition to the Azure DevOps pull request system the Cake.Issues and Cake.Issues.PullRequests core addins and the Cake.AzureDevOps addin need to be added.

Build.csproj
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Cake.Frosting" Version="5.0.0" />
    <PackageReference Include="Cake.Frosting.Git" Version="5.0.1" />
    <PackageReference Include="Cake.Frosting.Issues.InspectCode" Version="5.2.0" />
    <PackageReference Include="Cake.Frosting.Issues.PullRequests.AzureDevOps" Version="5.2.0" />
  </ItemGroup>
</Project>

The following example shows a task which will first determine the remote repository URL and source branch of the pull request and with this information call the AzureDevOpsPullRequests alias, which will authenticate through NTLM to an on-premise Azure DevOps Server instance:

build.cake
Task("Report-IssuesToPullRequest").Does(() =>
{
    var repoRootFolder =
        MakeAbsolute(Directory("./"));
    var currentBranch =
        GitBranchCurrent(repoRootFolder);
    var repoRemoteUrl =
        new Uri(currentBranch.Remotes.Single(x => x.Name == "origin").Url);
    var sourceBranchName = currentBranch.CanonicalName;

    ReportIssuesToPullRequest(
        InspectCodeIssuesFromFilePath(
            @"C:\build\inspectcode.log"),
        AzureDevOpsPullRequests(
            repoRemoteUrl,
            sourceBranchName,
            AzureDevOpsAuthenticationNtlm()),
        repoRootFolder);
});
Program.cs
using Cake.Common.IO;
using Cake.Frosting;
using Cake.Git;

public static class Program
{
    public static int Main(string[] args)
    {
        return new CakeHost()
            .Run(args);
    }
}

[TaskName("Report-IssuesToPullRequest")]
public sealed class ReportIssuesToPullRequestTask : FrostingTask<FrostingContext>
{
    public override void Run(FrostingContext context)
    {
        var repoRootPath =
            context.MakeAbsolute(context.Directory("./"));
        var currentBranch =
            context.GitBranchCurrent(repoRootPath);
        var repoRemoteUrl = 
            new Uri(currentBranch.Remotes.Single(x => x.Name == "origin").Url);
        var sourceBranchName = currentBranch.CanonicalName;

        context.ReportIssuesToPullRequest(
            context.InspectCodeIssuesFromFilePath(
                @"C:\build\inspectcode.log"),
            context.AzureDevOpsPullRequests(
                repoRemoteUrl,
                sourceBranchName,
                context.AzureDevOpsAuthenticationNtlm()),
            repoRootPath);
    }
}