Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/dotnet-roslyn-tools/Commands/DartTestCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public override async Task<int> InvokeAsync(ParseResult parseResult, Cancellatio

using var remoteConnections = new RemoteConnections(settings);

return await DartTest.DartTest.RunDartPipeline(
return await Validation.DartTest.RunDartPipeline(
product,
remoteConnections,
logger,
Expand Down
106 changes: 106 additions & 0 deletions src/dotnet-roslyn-tools/Commands/PRValidationCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the License.txt file in the project root for more information.

using System.CommandLine;
using System.CommandLine.Invocation;
using Microsoft.Extensions.Logging;
using Microsoft.RoslynTools.Utilities;
using Microsoft.RoslynTools.VS;

namespace Microsoft.RoslynTools.Commands;

internal static class PRValidationCommand
{
private static readonly string[] s_allProductNames = [.. VSBranchInfo.AllProducts.Where(p => p.PRValidationPipelineName != null).Select(p => p.Name.ToLower())];

private static readonly PRValidationCommandDefaultHandler s_prValidationCommandHandler = new();
internal static readonly Option<int> PrNumberOption = new("--prNumber", "-n")
{
Description = "PR number",
Required = true,
};

internal static readonly Option<string> ShaOption = new("--sha", "-s")
{
Description = "Relevant SHA",
Required = false,
};

internal static readonly Option<string> BranchOption = new("--branch", "-b")
{
Description = "Branch to run pipeline from",
DefaultValueFactory = _ => "main",
Required = false,
};

internal static readonly Option<string> ProductOption = new("--product", "-p")
{
Description = "Product to get info for",
DefaultValueFactory = _ => "roslyn",
Required = false,
};

static PRValidationCommand()
{
ProductOption.AcceptOnlyFromAmong(s_allProductNames);
}

public static Command GetCommand()
{
var command = new Command("pr-val",
@"Runs the PR Validation pipeline for a given PR number and SHA.")
{
PrNumberOption,
ShaOption,
ProductOption,
BranchOption,
CommonOptions.GitHubTokenOption,
CommonOptions.DevDivAzDOTokenOption,
CommonOptions.DncEngAzDOTokenOption,
CommonOptions.IsCIOption,
};

command.Action = s_prValidationCommandHandler;
return command;
}

private class PRValidationCommandDefaultHandler : AsynchronousCommandLineAction
{
public override async Task<int> InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken)
{
var logger = parseResult.SetupLogging();
var settings = parseResult.LoadSettings(logger);

var isMissingAzDOToken = string.IsNullOrEmpty(settings.DevDivAzureDevOpsToken) || string.IsNullOrEmpty(settings.DncEngAzureDevOpsToken);
if (string.IsNullOrEmpty(settings.GitHubToken) ||
(settings.IsCI && isMissingAzDOToken))
{
logger.LogError("Missing authentication token.");
return -1;
}

var pr = parseResult.GetValue(PrNumberOption);
var shaFromPR = parseResult.GetValue(ShaOption);

if (shaFromPR is null)
{
logger.LogInformation("Using most recent SHA");
}

var product = parseResult.GetValue(ProductOption)!;

var branch = parseResult.GetValue(BranchOption);

using var remoteConnections = new RemoteConnections(settings);

return await Validation.PRValidation.RunPRValidationPipeline(
product,
remoteConnections,
logger,
pr,
shaFromPR,
branch).ConfigureAwait(false);
}
}
}
104 changes: 104 additions & 0 deletions src/dotnet-roslyn-tools/Commands/PRValidationSuiteCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the License.txt file in the project root for more information.

using System.CommandLine;
using System.CommandLine.Invocation;
using Microsoft.Extensions.Logging;
using Microsoft.RoslynTools.Utilities;
using Microsoft.RoslynTools.VS;

namespace Microsoft.RoslynTools.Commands;

internal static class PRValidationSuiteCommand
{
private static readonly string[] s_allProductNames = [.. VSBranchInfo.AllProducts.Where(p => p.PRValidationPipelineName != null && p.DartLabPipelineName != null).Select(p => p.Name.ToLower())];

private static readonly PRValidationSuiteCommandDefaultHandler s_prValidationSuiteCommandHandler = new();
internal static readonly Option<int> PrNumberOption = new("--prNumber", "-n")
{
Description = "PR number",
Required = true,
};

internal static readonly Option<string> ShaOption = new("--sha", "-s")
{
Description = "Relevant SHA",
Required = false,
};

internal static readonly Option<string> BranchOption = new("--branch", "-b")
{
Description = "Branch to run pipeline from",
DefaultValueFactory = _ => "main",
Required = false,
};

internal static readonly Option<string> ProductOption = new("--product", "-p")
{
Description = "Product to get info for",
DefaultValueFactory = _ => "roslyn",
Required = false,
};

static PRValidationSuiteCommand()
{
ProductOption.AcceptOnlyFromAmong(s_allProductNames);
}

public static Command GetCommand()
{
var command = new Command("pr-suite",
@"Runs the PR Validation pipeline and Dartlab pipeline for a given PR number and SHA.")
{
PrNumberOption,
ShaOption,
ProductOption,
BranchOption,
CommonOptions.GitHubTokenOption,
CommonOptions.DevDivAzDOTokenOption,
CommonOptions.DncEngAzDOTokenOption,
CommonOptions.IsCIOption,
};

command.Action = s_prValidationSuiteCommandHandler;
return command;
}

private class PRValidationSuiteCommandDefaultHandler : AsynchronousCommandLineAction
{
public override async Task<int> InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken)
{
var logger = parseResult.SetupLogging();
var settings = parseResult.LoadSettings(logger);

var isMissingAzDOToken = string.IsNullOrEmpty(settings.DevDivAzureDevOpsToken) || string.IsNullOrEmpty(settings.DncEngAzureDevOpsToken);
if (string.IsNullOrEmpty(settings.GitHubToken) ||
(settings.IsCI && isMissingAzDOToken))
{
logger.LogError("Missing authentication token.");
return -1;
}

var pr = parseResult.GetValue(PrNumberOption);
var shaFromPR = parseResult.GetValue(ShaOption);

if (shaFromPR is null)
{
logger.LogInformation("Using most recent SHA");
}

var product = parseResult.GetValue(ProductOption)!;

var branch = parseResult.GetValue(BranchOption);

using var remoteConnections = new RemoteConnections(settings);
var dartResult = await Validation.DartTest.RunDartPipeline(product, remoteConnections, logger, pr, shaFromPR).ConfigureAwait(false);
var prValResult = await Validation.PRValidation.RunPRValidationPipeline(product, remoteConnections, logger, pr, shaFromPR, branch).ConfigureAwait(false);

return dartResult == 0 && prValResult == 0
? 0
: -1;
}
}
}
2 changes: 2 additions & 0 deletions src/dotnet-roslyn-tools/Commands/RootRoslynCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public static RootCommand GetRootCommand()
CreateReleaseTagsCommand.GetCommand(),
VSBranchInfoCommand.GetCommand(),
DartTestCommand.GetCommand(),
PRValidationCommand.GetCommand(),
PRValidationSuiteCommand.GetCommand(),
};
command.Description = "The command line tool for performing infrastructure tasks.";
return command;
Expand Down
1 change: 1 addition & 0 deletions src/dotnet-roslyn-tools/Products/FSharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal class FSharp : IProduct
public string? VsPackageName => null;
public string? VsPackagePropsFileName => null;
public string? DartLabPipelineName => null;
public string? PRValidationPipelineName => null;
public string? ArtifactsFolderName => null;
public string[] ArtifactsSubFolderNames => [];
public string SdkPackageName => "Microsoft.FSharp.Compiler";
Expand Down
1 change: 1 addition & 0 deletions src/dotnet-roslyn-tools/Products/IProduct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal interface IProduct
string? VsPackageName { get; }
string? VsPackagePropsFileName { get; }
string? DartLabPipelineName { get; }
string? PRValidationPipelineName { get; }
string? ArtifactsFolderName { get; }
string[] ArtifactsSubFolderNames { get; }

Expand Down
1 change: 1 addition & 0 deletions src/dotnet-roslyn-tools/Products/Razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal class Razor : IProduct
public string? VsPackageName => null;
public string? VsPackagePropsFileName => null;
public string? DartLabPipelineName => null;
public string? PRValidationPipelineName => null;
public string? ArtifactsFolderName => null;
public string[] ArtifactsSubFolderNames => [];

Expand Down
1 change: 1 addition & 0 deletions src/dotnet-roslyn-tools/Products/Roslyn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal class Roslyn : IProduct
public string? VsPackageName => "VS.ExternalAPIs.Roslyn";
public string? VsPackagePropsFileName => "src/ConfigData/Packages/roslyn.props";
public string? DartLabPipelineName => "Roslyn Integration CI DartLab";
public string? PRValidationPipelineName => "Roslyn PR Validation";
public string? ArtifactsFolderName => "PackageArtifacts";
public string[] ArtifactsSubFolderNames => ["PackageArtifacts/PreRelease", "PackageArtifacts/Release"];

Expand Down
1 change: 1 addition & 0 deletions src/dotnet-roslyn-tools/Products/TypeScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal class TypeScript : IProduct
public string? VsPackageName => "VS.ExternalAPIs.TypeScript.SourceMapReader.dev15";
public string? VsPackagePropsFileName => "src/ConfigData/Packages/TypeScriptSupport.props";
public string? DartLabPipelineName => null;
public string? PRValidationPipelineName => null;
public string? ArtifactsFolderName => null;
public string[] ArtifactsSubFolderNames => [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using Microsoft.RoslynTools.Utilities;
using Microsoft.RoslynTools.VS;

namespace Microsoft.RoslynTools.DartTest;
namespace Microsoft.RoslynTools.Validation;

internal static class DartTest
{
Expand All @@ -33,7 +33,7 @@ public static async Task<int> RunDartPipeline(
// If the user doesn't pass the SHA, retrieve the most recent from the PR.
if (sha is null)
{
sha = await GetLatestShaFromPullRequestAsync(product, remoteConnections.GitHubClient, prNumber, logger, cancellationToken).ConfigureAwait(false);
sha = await Utilities.GetLatestShaFromPullRequestAsync(product, remoteConnections.GitHubClient, prNumber, logger, cancellationToken).ConfigureAwait(false);
if (sha is null)
{
logger.LogError("Could not find a SHA for the given PR number.");
Expand Down Expand Up @@ -78,29 +78,10 @@ public static async Task<int> RunDartPipeline(
return 0;
}

private static async Task<string?> GetLatestShaFromPullRequestAsync(IProduct product, HttpClient gitHubClient, int prNumber, ILogger logger, CancellationToken cancellationToken)
{
var requestUri = $"/repos/dotnet/{product.Name.ToLower()}/pulls/{prNumber}";
var response = await gitHubClient.GetAsync(requestUri, cancellationToken).ConfigureAwait(false);

if (response.IsSuccessStatusCode)
{
var prData = await response.Content.ReadFromJsonAsync<JsonObject>(cancellationToken: cancellationToken).ConfigureAwait(false);
return prData?["head"]?["sha"]?.ToString();
}
else
{
logger.LogError("Failed to retrieve PR data from GitHub. Status code: {StatusCode}", response.StatusCode);
}

return null;
}

private static async Task PushPRToInternalAsync(IProduct product, int prNumber, string azureBranchName, ILogger logger, string sha, string targetDirectory, CancellationToken cancellationToken)
{
var initCommand = $"init";
await RunGitCommandAsync(initCommand, logger, targetDirectory, cancellationToken).ConfigureAwait(false);
;

var addGithubRemoteCommand = $"remote add {product.Name.ToLower()} {product.RepoHttpBaseUrl}.git";
await RunGitCommandAsync(addGithubRemoteCommand, logger, targetDirectory, cancellationToken).ConfigureAwait(false);
Expand Down
68 changes: 68 additions & 0 deletions src/dotnet-roslyn-tools/Validation/PRValidation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the License.txt file in the project root for more information.

using Microsoft.Azure.Pipelines.WebApi;
using Microsoft.Extensions.Logging;
using Microsoft.RoslynTools.Utilities;
using Microsoft.RoslynTools.VS;

namespace Microsoft.RoslynTools.Validation;

internal static class PRValidation
{
public static async Task<int> RunPRValidationPipeline(
string productName,
RemoteConnections remoteConnections,
ILogger logger,
int prNumber,
string? sha,
string? branch)
{
try
{
var cancellationToken = CancellationToken.None;
var product = VSBranchInfo.AllProducts.Single(p => p.Name.Equals(productName, StringComparison.OrdinalIgnoreCase));

// If the user doesn't pass the SHA, retrieve the most recent from the PR.
if (sha is null)
{
sha = await Utilities.GetLatestShaFromPullRequestAsync(product, remoteConnections.GitHubClient, prNumber, logger, cancellationToken).ConfigureAwait(false);
if (sha is null)
{
logger.LogError("Could not find a SHA for the given PR number.");
return -1;
}
}

var repositoryParams = new Dictionary<string, RepositoryResourceParameters>
{
{
"self", new RepositoryResourceParameters
{
RefName = $"refs/heads/{branch}",
Version = sha
}
}
};

var runPipelineParameters = new RunPipelineParameters
{
Resources = new RunResourcesParameters
{

},
TemplateParameters = new Dictionary<string, string> { { "PRNumber", prNumber.ToString() }, { "CommitSHA", sha } }
};

await remoteConnections.DevDivConnection.TryRunPipelineAsync(product.PRValidationPipelineName, repositoryParams, runPipelineParameters, logger).ConfigureAwait(false);
}
catch (Exception e)
{
logger.LogError(e, "{Message}", e.Message);
return -1;
}

return 0;
}
}
Loading