-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathProgram.cs
118 lines (98 loc) · 4.12 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.CommandLine;
using Microsoft.Extensions.Logging;
namespace CreateBaselineUpdatePR;
public class Program
{
public static readonly Argument<string> Repo = new("repo")
{
Description = "The GitHub repository to create the PR in. Should be in the form '<owner>/<repo-name>'",
Arity = ArgumentArity.ExactlyOne
};
public static readonly Argument<string> OriginalFilesDirectory = new("original-files-directory")
{
Description = "The directory where the original test files are located. Should be relative to the repo",
Arity = ArgumentArity.ExactlyOne
};
public static readonly Argument<string> UpdatedFilesDirectory = new("updated-files-directory")
{
Description = "The directory containing the updated test files published by the associated test. Should be absolute or relative to the working directory of the tool.",
Arity = ArgumentArity.ExactlyOne
};
public static readonly Argument<int> BuildId = new("build-id")
{
Description = "The id of the build that published the updated test files.",
Arity = ArgumentArity.ExactlyOne
};
public static readonly Option<string> Title = new("--title", "-t")
{
Description = "The title of the PR.",
Arity = ArgumentArity.ZeroOrOne,
DefaultValueFactory = _ => "Update Test Baselines and Exclusions"
};
public static readonly Option<string> Branch = new("--branch", "-b")
{
Description = "The target branch of the PR.",
Arity = ArgumentArity.ZeroOrOne,
DefaultValueFactory = _ => "main"
};
public static readonly Option<string> GitHubToken = new("--github-token", "-g")
{
Description = "The GitHub token to use to create the PR.",
Arity = ArgumentArity.ZeroOrOne,
DefaultValueFactory = _ => Environment.GetEnvironmentVariable("GH_TOKEN") ?? throw new ArgumentException("GitHub token not provided.")
};
public static readonly Option<LogLevel> Level = new("--log-level", "-l")
{
Description = "The log level to run the tool in.",
Arity = ArgumentArity.ZeroOrOne,
DefaultValueFactory = _ => LogLevel.Information,
Recursive = true
};
public static int ExitCode = 0;
public static async Task<int> Main(string[] args)
{
var sdkDiffTestsCommand = CreateCommand("sdk", "Creates a PR that updates baselines and exclusion files published by the sdk diff tests.");
var licenseScanTestsCommand = CreateCommand("license", "Creates a PR that updates baselines and exclusion files published by the license scan tests.");
var rootCommand = new RootCommand("Tool for creating PRs that update baselines and exclusion files.")
{
Level,
sdkDiffTestsCommand,
licenseScanTestsCommand
};
SetCommandAction(sdkDiffTestsCommand, Pipelines.Sdk);
SetCommandAction(licenseScanTestsCommand, Pipelines.License);
await rootCommand.Parse(args).InvokeAsync();
return ExitCode;
}
private static Command CreateCommand(string name, string description)
{
return new Command(name, description)
{
Repo,
OriginalFilesDirectory,
UpdatedFilesDirectory,
BuildId,
Title,
Branch,
GitHubToken
};
}
private static void SetCommandAction(Command command, Pipelines pipeline)
{
command.SetAction(async (result, CancellationToken) =>
{
Log.Level = result.GetValue(Level);
var creator = new PRCreator(result.GetValue(Repo)!, result.GetValue(GitHubToken)!);
ExitCode = await creator.ExecuteAsync(
result.GetValue(OriginalFilesDirectory)!,
result.GetValue(UpdatedFilesDirectory)!,
result.GetValue(BuildId)!,
result.GetValue(Title)!,
result.GetValue(Branch)!,
pipeline);
});
}
}