Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/Maestro/SubscriptionActorService/PullRequestActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.ServiceFabric.Actors;
using Microsoft.ServiceFabric.Actors.Runtime;
using Microsoft.VisualStudio.Services.Common;
using ProductConstructionService.Client;
using ProductConstructionService.Client.Models;
using SubscriptionActorService.StateModel;
Expand Down Expand Up @@ -872,8 +873,11 @@ await AddDependencyFlowEventsAsync(
MergePolicyCheckResult.PendingPolicies,
pr.Url);

var requiredDescriptionUpdates =
await ReplaceFromDependency(darcRemote, targetRepository, targetBranch, targetRepositoryUpdates);

pullRequest.Description = await _pullRequestBuilder.CalculatePRDescriptionAndCommitUpdatesAsync(
targetRepositoryUpdates.RequiredUpdates,
requiredDescriptionUpdates,
pullRequest.Description,
targetRepository,
pullRequest.HeadBranch);
Expand Down Expand Up @@ -1075,6 +1079,49 @@ private async Task<RepositoryBranchUpdate> GetRepositoryBranchUpdate()

private static string GetNewBranchName(string targetBranch) => $"darc-{targetBranch}-{Guid.NewGuid()}";

/// <summary>
/// Given a set of updates, replace the `from` version of every dependency update with the corresponding version
/// from the target branch
/// </summary>
/// <param name="darcRemote">Darc client used to fetch target branch dependencies.</param>
/// <param name="targetRepository">Target repository to fetch the dependencies from.</param>
/// <param name="targetBranch">Target branch to fetch the dependencies from.</param>
/// <param name="targetRepositoryUpdates">Incoming updates to the repository</param>
/// <returns>
/// Asset update and the corresponding list of altered dependencies
/// </returns>
/// <remarks>
/// This method is intended for use in situations where we want to keep the information about the original dependency
/// version, such as when updating PR descriptions.
/// </remarks>
private static async Task<List<(UpdateAssetsParameters update, List<DependencyUpdate> deps)>> ReplaceFromDependency(
IRemote darcRemote,
string targetRepository,
string targetBranch,
TargetRepoDependencyUpdate targetRepositoryUpdates
)
{
List<DependencyDetail> targetBranchDeps = (await darcRemote.GetDependenciesAsync(targetRepository, targetBranch)).ToList();

List<(UpdateAssetsParameters update, List<DependencyUpdate> deps)> alteredUpdates = [];
foreach (var requiredUpdate in targetRepositoryUpdates.RequiredUpdates)
{
var updatedDependencies = requiredUpdate.deps
.Select(dependency => new DependencyUpdate()
{
From = targetBranchDeps
.Where(replace => dependency.From.Name == replace.Name)
.FirstOrDefault(dependency.From),
To = dependency.To,
})
.ToList();

alteredUpdates.Add((requiredUpdate.update, updatedDependencies));
}

return alteredUpdates;
}

#region Code flow subscriptions

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Net;
using Maestro.Contracts;
using Maestro.Data.Models;
Expand Down Expand Up @@ -649,8 +650,11 @@ await AddDependencyFlowEventsAsync(
MergePolicyCheckResult.PendingPolicies,
pr.Url);

var requiredDescriptionUpdates =
await ReplaceFromDependency(darcRemote, targetRepository, targetBranch, targetRepositoryUpdates);

pullRequest.Description = await _pullRequestBuilder.CalculatePRDescriptionAndCommitUpdatesAsync(
targetRepositoryUpdates.RequiredUpdates,
requiredDescriptionUpdates,
pullRequest.Description,
targetRepository,
pullRequest.HeadBranch);
Expand Down Expand Up @@ -836,6 +840,49 @@ private async Task ClearAllStateAsync()
await _pullRequestUpdateReminders.UnsetReminderAsync();
}

/// <summary>
/// Given a set of updates, replace the `from` version of every dependency update with the corresponding version
/// from the target branch
/// </summary>
/// <param name="darcRemote">Darc client used to fetch target branch dependencies.</param>
/// <param name="targetRepository">Target repository to fetch the dependencies from.</param>
/// <param name="targetBranch">Target branch to fetch the dependencies from.</param>
/// <param name="targetRepositoryUpdates">Incoming updates to the repository</param>
/// <returns>
/// Subscription update and the corresponding list of altered dependencies
/// </returns>
/// <remarks>
/// This method is intended for use in situations where we want to keep the information about the original dependency
/// version, such as when updating PR descriptions.
/// </remarks>
private static async Task<List<(SubscriptionUpdateWorkItem update, List<DependencyUpdate> deps)>> ReplaceFromDependency(
IRemote darcRemote,
string targetRepository,
string targetBranch,
TargetRepoDependencyUpdate targetRepositoryUpdates
)
{
List<DependencyDetail> targetBranchDeps = (await darcRemote.GetDependenciesAsync(targetRepository, targetBranch)).ToList();

List<(SubscriptionUpdateWorkItem update, List<DependencyUpdate> deps)> alteredUpdates = [];
foreach (var requiredUpdate in targetRepositoryUpdates.RequiredUpdates)
{
var updatedDependencies = requiredUpdate.deps
.Select(dependency => new DependencyUpdate()
{
From = targetBranchDeps
.Where(replace => dependency.From.Name == replace.Name)
.FirstOrDefault(dependency.From),
To = dependency.To,
})
.ToList();

alteredUpdates.Add((requiredUpdate.update, updatedDependencies));
}

return alteredUpdates;
}

#region Code flow subscriptions

/// <summary>
Expand Down
9 changes: 6 additions & 3 deletions test/Maestro.ScenarioTests/MaestroScenarioTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void SetTestParameters(TestParameters parameters)
if (!string.IsNullOrEmpty(_parameters.MaestroToken))
{
_baseDarcRunArgs.AddRange(["-p", _parameters.MaestroToken]);
}
}
}

protected async Task<Octokit.PullRequest> WaitForPullRequestAsync(string targetRepo, string targetBranch)
Expand Down Expand Up @@ -487,7 +487,10 @@ protected async Task AddDependenciesToLocalRepo(string repoPath, string name, st
{
using (ChangeDirectory(repoPath))
{
await RunDarcAsync(["add-dependency", "--name", name, "--type", isToolset ? "toolset" : "product", "--repo", repoUri, "--version", "0.0.1"]);
await RunDarcAsync(
[
"add-dependency", "--name", name, "--version", "0.0.1", "--type", isToolset ? "toolset" : "product", "--repo", repoUri
]);
}
}
protected async Task<string> GetTestChannelsAsync()
Expand Down Expand Up @@ -682,7 +685,7 @@ protected async Task AddDependenciesToLocalRepo(string repoPath, List<AssetData>

protected async Task<string> GatherDrop(int buildId, string outputDir, bool includeReleased, string extraAssetsRegex)
{
string[] args = [ "gather-drop", "--id", buildId.ToString(), "--dry-run", "--output-dir", outputDir ];
string[] args = ["gather-drop", "--id", buildId.ToString(), "--dry-run", "--output-dir", outputDir];

if (includeReleased)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,10 @@ protected async Task AddDependenciesToLocalRepo(string repoPath, List<AssetData>
{
foreach (AssetData asset in dependencies)
{
List<string> parameters = ["add-dependency", "--name", asset.Name, "--type", "product", "--repo", repoUri];
List<string> parameters =
[
"add-dependency", "--name", asset.Name,"--type", "product", "--repo", repoUri,
];

if (!string.IsNullOrEmpty(coherentParent))
{
Expand Down