Skip to content

Commit 0f1c878

Browse files
authored
fix source/target directory + add source-commit option (#5334)
#5335
1 parent a30f727 commit 0f1c878

2 files changed

Lines changed: 73 additions & 33 deletions

File tree

tools/ProductConstructionService.ReproTool/Operations/FlowCommitOperation.cs

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,34 +57,16 @@ internal override async Task RunAsync()
5757
Channel? channel = channels.FirstOrDefault(c => c.Name == _options.Channel)
5858
?? await _localPcsApi.Channels.CreateChannelAsync("test", _options.Channel);
5959

60-
var (repoName, owner) = GitRepoUrlUtils.GetRepoNameAndOwner(_options.SourceRepository);
61-
var (sourceRepoName, sourceOwner) = (repoName, owner);
60+
var (sourceRepo, sourceOwner) = GitRepoUrlUtils.GetRepoNameAndOwner(_options.SourceRepository);
61+
var (targetRepo, targetOwner) = GitRepoUrlUtils.GetRepoNameAndOwner(_options.TargetRepository);
6262

63-
bool? isBackflow = null;
64-
try
65-
{
66-
(repoName, owner) = GitRepoUrlUtils.GetRepoNameAndOwner(_options.TargetRepository);
67-
await _ghClient.Repository.Content.GetAllContents(owner, repoName, SourceMappingsPath);
68-
isBackflow = false;
69-
}
70-
catch { }
71-
72-
if (!isBackflow.HasValue)
73-
{
74-
try
75-
{
76-
(repoName, owner) = GitRepoUrlUtils.GetRepoNameAndOwner(_options.SourceRepository);
77-
await _ghClient.Repository.Content.GetAllContents(owner, repoName, SourceMappingsPath);
78-
isBackflow = true;
79-
}
80-
catch { }
81-
}
63+
bool isForwardFlow = await IsForwardFlow(sourceOwner, sourceRepo, targetOwner, targetRepo);
8264

8365
var subscriptions = await _localPcsApi.Subscriptions.ListSubscriptionsAsync(
8466
channelId: channel.Id,
8567
sourceRepository: _options.SourceRepository,
8668
targetRepository: _options.TargetRepository,
87-
sourceEnabled: isBackflow.HasValue);
69+
sourceEnabled: true);
8870

8971
Subscription subscription = subscriptions.FirstOrDefault(s => s.TargetBranch == _options.TargetBranch)
9072
?? await _localPcsApi.Subscriptions.CreateAsync(
@@ -99,17 +81,33 @@ internal override async Task RunAsync()
9981
},
10082
null)
10183
{
102-
SourceEnabled = isBackflow.HasValue,
103-
SourceDirectory = isBackflow == true ? repoName : null,
104-
TargetDirectory = isBackflow == false ? sourceRepoName : null,
84+
SourceEnabled = true,
85+
SourceDirectory = isForwardFlow ? null : targetRepo,
86+
TargetDirectory = isForwardFlow ? sourceRepo : null,
10587
});
10688

107-
var commit = (await _ghClient.Repository.Branch.Get(sourceOwner, sourceRepoName, _options.SourceBranch)).Commit;
89+
string sourceCommit;
90+
91+
if (string.IsNullOrEmpty(_options.SourceCommit) && string.IsNullOrEmpty(_options.SourceBranch))
92+
{
93+
throw new ArgumentException("Please provide a source-branch or source-commit value.");
94+
}
95+
96+
if (string.IsNullOrEmpty(_options.SourceCommit))
97+
{
98+
sourceCommit = (await _ghClient.Repository.Branch.Get(sourceOwner, sourceRepo, _options.SourceBranch))
99+
.Commit.Sha;
100+
}
101+
else
102+
{
103+
sourceCommit = _options.SourceCommit;
104+
}
108105

109106
_logger.LogInformation("Creating build for {repo}@{branch} (commit {commit})",
110107
_options.SourceRepository,
111108
_options.SourceBranch,
112-
Microsoft.DotNet.DarcLib.Commit.GetShortSha(commit.Sha));
109+
Microsoft.DotNet.DarcLib.Commit.GetShortSha(sourceCommit));
110+
113111
List<AssetData> assets;
114112
if (_options.RealBuildId > 0)
115113
{
@@ -126,13 +124,16 @@ internal override async Task RunAsync()
126124
];
127125
}
128126

127+
_logger.LogInformation("Source commit is {}", sourceCommit);
128+
_logger.LogInformation("Subscription is forward-flow: {}", isForwardFlow);
129+
129130
var build = await _localPcsApi.Builds.CreateAsync(new BuildData(
130-
commit.Sha,
131+
sourceCommit,
131132
"dnceng",
132133
"internal",
133134
$"{DateTime.UtcNow:yyyyMMdd}.{new Random().Next(1, 75)}",
134-
$"https://dev.azure.com/dnceng/internal/_git/{owner}-{repoName}",
135-
_options.SourceBranch,
135+
$"https://dev.azure.com/dnceng/internal/_git/{sourceOwner}-{sourceRepo}",
136+
_options.SourceBranch ?? sourceCommit,
136137
released: false,
137138
stable: false)
138139
{
@@ -149,4 +150,40 @@ internal override async Task RunAsync()
149150

150151
_logger.LogInformation("Subscription triggered. Wait for a PR in {url}", $"{_options.TargetRepository}/pulls");
151152
}
153+
154+
private async Task<bool> IsVmr(string repoName, string repoOwner)
155+
{
156+
try
157+
{
158+
await _ghClient.Repository.Content.GetAllContents(repoOwner, repoName, SourceManifestPath);
159+
return true;
160+
}
161+
catch (Octokit.ApiException e)
162+
{
163+
if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
164+
{
165+
return false;
166+
}
167+
throw;
168+
}
169+
}
170+
171+
private async Task<bool> IsForwardFlow(
172+
string sourceOwner,
173+
string sourceRepo,
174+
string targetOwner,
175+
string targetRepo)
176+
{
177+
if (await IsVmr(targetRepo, targetOwner))
178+
{
179+
return true;
180+
}
181+
182+
if (await IsVmr(sourceRepo, sourceOwner))
183+
{
184+
return false;
185+
}
186+
187+
throw new InvalidOperationException("Neither the source nor the target repository appears to be a VMR.");
188+
}
152189
}

tools/ProductConstructionService.ReproTool/Options/FlowCommitOptions.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ internal class FlowCommitOptions : Options
1616
[Option("source-repo", HelpText = "Repo (full URI) to flow the commit from (must be under maestro-auth-test/)", Required = true)]
1717
public required string SourceRepository { get; init; }
1818

19-
[Option("source-branch", HelpText = "Branch whose commit will be flown", Required = true)]
20-
public required string SourceBranch { get; init; }
19+
[Option("source-branch", HelpText = "Either --source-branch or --source-commit is required. Branch whose commit will be flown. Ignored if source-commit is provided.", Required = false)]
20+
public string? SourceBranch { get; init; }
21+
22+
[Option("source-commit", HelpText = "Either --source-branch or --source-commit is required. Source commit that will be flown.", Required = false)]
23+
public string? SourceCommit { get; init; }
2124

2225
[Option("target-repo", HelpText = "Repo (full URI) to flow the commit to (must be under maestro-auth-test/)", Required = true)]
2326
public required string TargetRepository { get; init; }
@@ -28,7 +31,7 @@ internal class FlowCommitOptions : Options
2831
[Option("packages", HelpText = "Name(s) of package(s) to include in the flown build", Required = false)]
2932
public IEnumerable<string> Packages { get; init; } = [];
3033

31-
[Option("realBuildId", HelpText = "Build to take assets from. Shouldn't be combined with packages", Required = false)]
34+
[Option("assets-from-build", HelpText = "A real build id from which to take assets. Shouldn't be provided if using the --packages option", Required = false)]
3235
public int RealBuildId { get; init; }
3336

3437
internal override Operation GetOperation(IServiceProvider sp)

0 commit comments

Comments
 (0)