Skip to content

Commit 35ded2d

Browse files
committed
only try to create pr if update operations were performed
1 parent 74ce553 commit 35ded2d

2 files changed

Lines changed: 94 additions & 1 deletion

File tree

nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Run/UpdateHandlers/CreateSecurityUpdatePullRequestHandlerTests.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,4 +763,97 @@ await TestAsync(
763763
]
764764
);
765765
}
766+
767+
[Fact]
768+
public async Task ErrantFileUpdatesDoNotCauseCallToCreatePullRequest()
769+
{
770+
// if an external tool inadvertently updates files on disk without reporting any update operations, don't try
771+
// to create a PR
772+
await TestAsync(
773+
job: new Job()
774+
{
775+
Dependencies = ["Some.Dependency"],
776+
SecurityAdvisories = [new() { DependencyName = "Some.Dependency", AffectedVersions = [Requirement.Parse("= 1.0.0")] }],
777+
SecurityUpdatesOnly = true,
778+
Source = CreateJobSource("/src"),
779+
},
780+
files: [
781+
("src/project.csproj", "initial project contents"),
782+
("src/packages.config", "initial packages contents"),
783+
],
784+
discoveryWorker: TestDiscoveryWorker.FromResults(
785+
("/src", new WorkspaceDiscoveryResult()
786+
{
787+
Path = "/src",
788+
Projects = [
789+
new()
790+
{
791+
FilePath = "project.csproj",
792+
Dependencies = [
793+
new("Some.Dependency", "1.0.0", DependencyType.PackageReference, TargetFrameworks: ["net9.0"]),
794+
],
795+
ImportedFiles = [],
796+
AdditionalFiles = ["packages.config"],
797+
}
798+
],
799+
})
800+
),
801+
analyzeWorker: new TestAnalyzeWorker(async input =>
802+
{
803+
var repoRoot = input.Item1;
804+
var discovery = input.Item2;
805+
var dependencyInfo = input.Item3;
806+
if (dependencyInfo.Name != "Some.Dependency")
807+
{
808+
throw new NotImplementedException($"Test didn't expect to update dependency {dependencyInfo.Name}");
809+
}
810+
811+
// no update possible but a file was touched on disk
812+
var projectPath = Path.Join(repoRoot, discovery.Path, discovery.Projects.Single().FilePath);
813+
var packagesConfigPath = Path.Join(Path.GetDirectoryName(projectPath), "packages.config");
814+
await File.WriteAllTextAsync(packagesConfigPath, "updated packages contents");
815+
816+
return new AnalysisResult()
817+
{
818+
CanUpdate = false,
819+
UpdatedVersion = "1.0.0",
820+
UpdatedDependencies = [],
821+
};
822+
}),
823+
updaterWorker: new TestUpdaterWorker(async input =>
824+
{
825+
return new UpdateOperationResult()
826+
{
827+
UpdateOperations = [],
828+
};
829+
}),
830+
expectedUpdateHandler: CreateSecurityUpdatePullRequestHandler.Instance,
831+
expectedApiMessages: [
832+
new UpdatedDependencyList()
833+
{
834+
Dependencies = [
835+
new()
836+
{
837+
Name = "Some.Dependency",
838+
Version = "1.0.0",
839+
Requirements = [
840+
new() { Requirement = "1.0.0", File = "/src/project.csproj", Groups = ["dependencies"] },
841+
],
842+
},
843+
],
844+
DependencyFiles = ["/src/packages.config", "/src/project.csproj"],
845+
},
846+
new IncrementMetric()
847+
{
848+
Metric = "updater.started",
849+
Tags = new()
850+
{
851+
["operation"] = "create_security_pr",
852+
}
853+
},
854+
new SecurityUpdateNotFound("Some.Dependency", "1.0.0"),
855+
new MarkAsProcessed("TEST-COMMIT-SHA"),
856+
]
857+
);
858+
}
766859
}

nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Run/UpdateHandlers/CreateSecurityUpdatePullRequestHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public async Task HandleAsync(Job job, DirectoryInfo originalRepoContentsPath, D
157157
}
158158
}
159159

160-
if (updatedDependencyFiles.Length > 0)
160+
if (updateOperationsPerformed.Count > 0 && updatedDependencyFiles.Length > 0)
161161
{
162162
var commitMessage = PullRequestTextGenerator.GetPullRequestCommitMessage(job, [.. updateOperationsPerformed], null);
163163
var prTitle = PullRequestTextGenerator.GetPullRequestTitle(job, [.. updateOperationsPerformed], null);

0 commit comments

Comments
 (0)