Skip to content

Commit

Permalink
Merge pull request #11089 from dependabot/dev/brettfo/nuget-pass-job
Browse files Browse the repository at this point in the history
pass job file path to analyze command
  • Loading branch information
randhircs authored Dec 10, 2024
2 parents 5338897 + 626a388 commit ff29929
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public async Task FindsUpdatedPackageAndReturnsTheCorrectData()
await RunAsync(path =>
[
"analyze",
"--job-path",
Path.Combine(path, "job.json"),
"--repo-root",
path,
"--discovery-file-path",
Expand Down Expand Up @@ -144,6 +146,8 @@ public async Task DotNetToolsJsonCanBeAnalyzed()
await RunAsync(path =>
[
"analyze",
"--job-path",
Path.Combine(path, "job.json"),
"--repo-root",
path,
"--discovery-file-path",
Expand Down Expand Up @@ -231,6 +235,8 @@ public async Task GlobalJsonCanBeAnalyzed()
await RunAsync(path =>
[
"analyze",
"--job-path",
Path.Combine(path, "job.json"),
"--repo-root",
path,
"--discovery-file-path",
Expand Down Expand Up @@ -308,8 +314,16 @@ await RunAsync(path =>
);
}

private static async Task RunAsync(Func<string, string[]> getArgs, string dependencyName, TestFile[] initialFiles, ExpectedAnalysisResult expectedResult, MockNuGetPackage[]? packages = null)
private static async Task RunAsync(
Func<string, string[]> getArgs,
string dependencyName,
TestFile[] initialFiles,
ExpectedAnalysisResult expectedResult,
MockNuGetPackage[]? packages = null,
ExperimentsManager? experimentsManager = null
)
{
experimentsManager ??= new ExperimentsManager();
var actualResult = await RunAnalyzerAsync(dependencyName, initialFiles, async path =>
{
var sb = new StringBuilder();
Expand All @@ -322,8 +336,19 @@ private static async Task RunAsync(Func<string, string[]> getArgs, string depend

try
{
await UpdateWorkerTestBase.MockJobFileInDirectory(path, experimentsManager);
await UpdateWorkerTestBase.MockNuGetPackagesInDirectory(packages, path);
var args = getArgs(path);

// manually pull out the experiments manager for the validate step below
for (int i = 0; i < args.Length - 1; i++)
{
if (args[i] == "--job-path")
{
experimentsManager = await ExperimentsManager.FromJobFileAsync(args[i + 1], new TestLogger());
}
}

var result = await Program.Main(args);
if (result != 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,8 @@ private static async Task RunAsync(
TestFile[] initialFiles,
ExpectedWorkspaceDiscoveryResult expectedResult,
MockNuGetPackage[]? packages = null,
ExperimentsManager? experimentsManager = null)
ExperimentsManager? experimentsManager = null
)
{
experimentsManager ??= new ExperimentsManager();
var actualResult = await RunDiscoveryAsync(initialFiles, async path =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace NuGetUpdater.Cli.Commands;

internal static class AnalyzeCommand
{
internal static readonly Option<FileInfo> JobPathOption = new("--job-path") { IsRequired = true };
internal static readonly Option<DirectoryInfo> RepoRootOption = new("--repo-root") { IsRequired = true };
internal static readonly Option<FileInfo> DependencyFilePathOption = new("--dependency-file-path") { IsRequired = true };
internal static readonly Option<FileInfo> DiscoveryFilePathOption = new("--discovery-file-path") { IsRequired = true };
Expand All @@ -16,6 +17,7 @@ internal static Command GetCommand(Action<int> setExitCode)
{
Command command = new("analyze", "Determines how to update a dependency based on the workspace discovery information.")
{
JobPathOption,
RepoRootOption,
DependencyFilePathOption,
DiscoveryFilePathOption,
Expand All @@ -24,11 +26,13 @@ internal static Command GetCommand(Action<int> setExitCode)

command.TreatUnmatchedTokensAsErrors = true;

command.SetHandler(async (repoRoot, discoveryPath, dependencyPath, analysisDirectory) =>
command.SetHandler(async (jobPath, repoRoot, discoveryPath, dependencyPath, analysisDirectory) =>
{
var worker = new AnalyzeWorker(new ConsoleLogger());
var logger = new ConsoleLogger();
var experimentsManager = await ExperimentsManager.FromJobFileAsync(jobPath.FullName, logger);
var worker = new AnalyzeWorker(experimentsManager, logger);
await worker.RunAsync(repoRoot.FullName, discoveryPath.FullName, dependencyPath.FullName, analysisDirectory.FullName);
}, RepoRootOption, DiscoveryFilePathOption, DependencyFilePathOption, AnalysisFolderOption);
}, JobPathOption, RepoRootOption, DiscoveryFilePathOption, DependencyFilePathOption, AnalysisFolderOption);

return command;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal static Command GetCommand(Action<int> setExitCode)
var logger = new ConsoleLogger();
var experimentsManager = await ExperimentsManager.FromJobFileAsync(jobPath.FullName, logger);
var discoverWorker = new DiscoveryWorker(experimentsManager, logger);
var analyzeWorker = new AnalyzeWorker(logger);
var analyzeWorker = new AnalyzeWorker(experimentsManager, logger);
var updateWorker = new UpdaterWorker(experimentsManager, logger);
var worker = new RunWorker(apiHandler, discoverWorker, analyzeWorker, updateWorker, logger);
await worker.RunAsync(jobPath, repoContentsPath, baseCommitSha, outputPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ protected static async Task TestAnalyzeAsync(
DependencyInfo dependencyInfo,
ExpectedAnalysisResult expectedResult,
MockNuGetPackage[]? packages = null,
TestFile[]? extraFiles = null)
TestFile[]? extraFiles = null,
ExperimentsManager? experimentsManager = null
)
{
var relativeDependencyPath = $"./dependabot/dependency/{dependencyInfo.Name}.json";

Expand All @@ -28,6 +30,7 @@ protected static async Task TestAnalyzeAsync(
(relativeDependencyPath, JsonSerializer.Serialize(dependencyInfo, AnalyzeWorker.SerializerOptions)),
];

experimentsManager ??= new ExperimentsManager();
var allFiles = files.Concat(extraFiles ?? []).ToArray();
var actualResult = await RunAnalyzerAsync(dependencyInfo.Name, allFiles, async directoryPath =>
{
Expand All @@ -36,7 +39,7 @@ protected static async Task TestAnalyzeAsync(
var discoveryPath = Path.GetFullPath(DiscoveryWorker.DiscoveryResultFileName, directoryPath);
var dependencyPath = Path.GetFullPath(relativeDependencyPath, directoryPath);

var worker = new AnalyzeWorker(new TestLogger());
var worker = new AnalyzeWorker(experimentsManager, new TestLogger());
var result = await worker.RunWithErrorHandlingAsync(directoryPath, discoveryPath, dependencyPath);
return result;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1730,7 +1730,7 @@ private static async Task RunAsync(Job job, TestFile[] files, IDiscoveryWorker?
var testApiHandler = new TestApiHandler();
var logger = new TestLogger();
discoveryWorker ??= new DiscoveryWorker(experimentsManager, logger);
analyzeWorker ??= new AnalyzeWorker(logger);
analyzeWorker ??= new AnalyzeWorker(experimentsManager, logger);
updaterWorker ??= new UpdaterWorker(experimentsManager, logger);

var worker = new RunWorker(testApiHandler, discoveryWorker, analyzeWorker, updaterWorker, logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public partial class AnalyzeWorker : IAnalyzeWorker
{
public const string AnalysisDirectoryName = "./.dependabot/analysis";

private readonly ExperimentsManager _experimentsManager;
private readonly ILogger _logger;

internal static readonly JsonSerializerOptions SerializerOptions = new()
Expand All @@ -24,8 +25,9 @@ public partial class AnalyzeWorker : IAnalyzeWorker
Converters = { new JsonStringEnumConverter(), new RequirementArrayConverter() },
};

public AnalyzeWorker(ILogger logger)
public AnalyzeWorker(ExperimentsManager experimentsManager, ILogger logger)
{
_experimentsManager = experimentsManager;
_logger = logger;
}

Expand Down
19 changes: 14 additions & 5 deletions nuget/lib/dependabot/nuget/native_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def self.get_nuget_discover_tool_command(job_path:, repo_root:, workspace_path:,
fingerprint = [
exe_path,
"discover",
"--job-path",
"<job-path>",
"--repo-root",
"<repo-root>",
"--workspace",
Expand Down Expand Up @@ -116,15 +118,17 @@ def self.run_nuget_discover_tool(job_path:, repo_root:, workspace_path:, output_
end

sig do
params(repo_root: String, discovery_file_path: String, dependency_file_path: String,
params(job_path: String, repo_root: String, discovery_file_path: String, dependency_file_path: String,
analysis_folder_path: String).returns([String, String])
end
def self.get_nuget_analyze_tool_command(repo_root:, discovery_file_path:, dependency_file_path:,
def self.get_nuget_analyze_tool_command(job_path:, repo_root:, discovery_file_path:, dependency_file_path:,
analysis_folder_path:)
exe_path = File.join(native_helpers_root, "NuGetUpdater", "NuGetUpdater.Cli")
command_parts = [
exe_path,
"analyze",
"--job-path",
job_path,
"--repo-root",
repo_root,
"--discovery-file-path",
Expand All @@ -140,6 +144,8 @@ def self.get_nuget_analyze_tool_command(repo_root:, discovery_file_path:, depend
fingerprint = [
exe_path,
"analyze",
"--job-path",
"<job-path>",
"--discovery-file-path",
"<discovery-file-path>",
"--dependency-file-path",
Expand All @@ -153,13 +159,14 @@ def self.get_nuget_analyze_tool_command(repo_root:, discovery_file_path:, depend

sig do
params(
repo_root: String, discovery_file_path: String, dependency_file_path: String,
job_path: String, repo_root: String, discovery_file_path: String, dependency_file_path: String,
analysis_folder_path: String, credentials: T::Array[Dependabot::Credential]
).void
end
def self.run_nuget_analyze_tool(repo_root:, discovery_file_path:, dependency_file_path:,
def self.run_nuget_analyze_tool(job_path:, repo_root:, discovery_file_path:, dependency_file_path:,
analysis_folder_path:, credentials:)
(command, fingerprint) = get_nuget_analyze_tool_command(repo_root: repo_root,
(command, fingerprint) = get_nuget_analyze_tool_command(job_path: job_path,
repo_root: repo_root,
discovery_file_path: discovery_file_path,
dependency_file_path: dependency_file_path,
analysis_folder_path: analysis_folder_path)
Expand Down Expand Up @@ -205,6 +212,8 @@ def self.get_nuget_updater_tool_command(job_path:, repo_root:, proj_path:, depen
fingerprint = [
exe_path,
"update",
"--job-path",
"<job-path>",
"--repo-root",
"<repo-root>",
"--solution-or-project",
Expand Down
8 changes: 7 additions & 1 deletion nuget/lib/dependabot/nuget/update_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ def requirements_unlocked_or_can_be?

private

sig { returns(String) }
def job_file_path
ENV.fetch("DEPENDABOT_JOB_PATH")
end

sig { returns(AnalysisJsonReader) }
def update_analysis
@update_analysis ||= T.let(request_analysis, T.nilable(AnalysisJsonReader))
Expand Down Expand Up @@ -103,7 +108,8 @@ def request_analysis

write_dependency_info

NativeHelpers.run_nuget_analyze_tool(repo_root: T.must(repo_contents_path),
NativeHelpers.run_nuget_analyze_tool(job_path: job_file_path,
repo_root: T.must(repo_contents_path),
discovery_file_path: discovery_file_path,
dependency_file_path: dependency_file_path,
analysis_folder_path: analysis_folder_path,
Expand Down

0 comments on commit ff29929

Please sign in to comment.