Skip to content

Commit 5c0cc56

Browse files
authored
Fix FileSystem rights requested for writing changelogs (#3055)
* Fix FileSystem rights requested for writing changelogs * Fix tests
1 parent 3b1a8f5 commit 5c0cc56

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

src/services/Elastic.Changelog/Creation/ChangelogCreationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public class ChangelogCreationService(
7474
private readonly CreateChangelogArgumentsValidator _validator = new(configurationContext);
7575
private readonly PrInfoProcessor _prProcessor = new(githubPrService, logFactory.CreateLogger<PrInfoProcessor>());
7676
private readonly IssueInfoProcessor _issueProcessor = new(githubPrService, logFactory.CreateLogger<IssueInfoProcessor>());
77-
private readonly ChangelogFileWriter _fileWriter = new(fileSystem ?? FileSystemFactory.RealRead, logFactory.CreateLogger<ChangelogFileWriter>());
77+
private readonly ChangelogFileWriter _fileWriter = new(fileSystem ?? FileSystemFactory.RealWrite, logFactory.CreateLogger<ChangelogFileWriter>());
7878
private readonly ProductInferService _productInferService = new(
7979
configurationContext.ProductsConfiguration);
8080

tests/Elastic.Changelog.Tests/Creation/ChangelogCreationServiceTests.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using System.IO.Abstractions.TestingHelpers;
56
using AwesomeAssertions;
67
using Elastic.Changelog.Creation;
78
using Elastic.Changelog.GitHub;
@@ -183,4 +184,50 @@ public async Task CreateChangelog_CIWithoutProducts_NoPrProductLabels_FailsWithP
183184
Collector.Errors.Should().Be(1);
184185
Collector.Diagnostics.Should().Contain(d => d.Message.Contains("At least one product is required"));
185186
}
187+
188+
/// <summary>
189+
/// When --output points to a temp directory (e.g. /tmp/changelog-staging in CI),
190+
/// the service must use a write-scoped filesystem that allows temp paths.
191+
/// Regression test for ScopedFileSystemException on temp output.
192+
/// </summary>
193+
[Fact]
194+
public async Task CreateChangelog_TempOutputDirectory_Succeeds()
195+
{
196+
var mockFs = new MockFileSystem(new MockFileSystemOptions { CurrentDirectory = Paths.WorkingDirectoryRoot.FullName });
197+
var writeFs = FileSystemFactory.ScopeCurrentWorkingDirectoryForWrite(mockFs);
198+
199+
var configPath = Path.Join(Paths.WorkingDirectoryRoot.FullName, "config", "changelog.yml");
200+
writeFs.Directory.CreateDirectory(writeFs.Path.GetDirectoryName(configPath)!);
201+
await writeFs.File.WriteAllTextAsync(configPath, ConfigWithProductLabels, TestContext.Current.CancellationToken);
202+
203+
// Use the real system temp path so AllowedSpecialFolder.Temp matches cross-platform.
204+
// MockFileSystem's GetTempPath() returns a hardcoded "C:\temp" that diverges from the
205+
// real temp on Windows CI (D:\Temp), causing scope validation to fail.
206+
var tempOutput = Path.Join(Path.GetTempPath(), "changelog-staging");
207+
208+
var env = FakeCIEnv(
209+
prNumber: "1044",
210+
title: "move upstream update script to .ci",
211+
type: "bug-fix",
212+
owner: "elastic",
213+
repo: "elastic-otel-java",
214+
products: "elasticsearch"
215+
);
216+
217+
var service = new ChangelogCreationService(LoggerFactory, ConfigurationContext, _mockGitHub, writeFs, env);
218+
var input = new CreateChangelogArguments
219+
{
220+
Products = [],
221+
Config = configPath,
222+
Output = tempOutput,
223+
Concise = true
224+
};
225+
226+
var result = await service.CreateChangelog(Collector, input, TestContext.Current.CancellationToken);
227+
228+
result.Should().BeTrue();
229+
Collector.Errors.Should().Be(0);
230+
writeFs.Directory.Exists(tempOutput).Should().BeTrue();
231+
writeFs.Directory.GetFiles(tempOutput, "*.yaml").Should().NotBeEmpty();
232+
}
186233
}

0 commit comments

Comments
 (0)