Skip to content

Commit 03f97b0

Browse files
Copilotpremun
andcommitted
Refactor validation logic to use shared base class for Add/Update subscription operations
Co-authored-by: premun <[email protected]>
1 parent 3aeb2dc commit 03f97b0

File tree

3 files changed

+84
-124
lines changed

3 files changed

+84
-124
lines changed

src/Microsoft.DotNet.Darc/Darc/Operations/AddSubscriptionOperation.cs

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919

2020
namespace Microsoft.DotNet.Darc.Operations;
2121

22-
internal class AddSubscriptionOperation : Operation
22+
internal class AddSubscriptionOperation : SubscriptionOperationBase
2323
{
2424
private readonly AddSubscriptionCommandLineOptions _options;
25-
private readonly ILogger<AddSubscriptionOperation> _logger;
26-
private readonly IBarApiClient _barClient;
2725
private readonly IRemoteFactory _remoteFactory;
2826
private readonly IGitRepoFactory _gitRepoFactory;
2927

@@ -32,11 +30,9 @@ public AddSubscriptionOperation(
3230
ILogger<AddSubscriptionOperation> logger,
3331
IBarApiClient barClient,
3432
IRemoteFactory remoteFactory,
35-
IGitRepoFactory gitRepoFactory)
33+
IGitRepoFactory gitRepoFactory) : base(barClient, logger)
3634
{
3735
_options = options;
38-
_logger = logger;
39-
_barClient = barClient;
4036
_remoteFactory = remoteFactory;
4137
_gitRepoFactory = gitRepoFactory;
4238
}
@@ -361,60 +357,4 @@ await ValidateCodeflowSubscriptionConflicts(
361357
return Constants.ErrorCode;
362358
}
363359
}
364-
365-
/// <summary>
366-
/// Validates that the new codeflow subscription doesn't conflict with existing ones
367-
/// </summary>
368-
private async Task ValidateCodeflowSubscriptionConflicts(
369-
string sourceRepository,
370-
string targetRepository,
371-
string targetBranch,
372-
string sourceDirectory,
373-
string targetDirectory,
374-
Guid? existingSubscriptionId)
375-
{
376-
// Check for backflow conflicts (source directory not empty)
377-
if (!string.IsNullOrEmpty(sourceDirectory))
378-
{
379-
var backflowSubscriptions = await _barClient.GetCodeflowSubscriptionsAsync(
380-
targetRepo: targetRepository,
381-
sourceEnabled: true);
382-
383-
var conflictingBackflowSubscription = backflowSubscriptions.FirstOrDefault(sub =>
384-
!string.IsNullOrEmpty(sub.SourceDirectory) &&
385-
sub.TargetRepository == targetRepository &&
386-
sub.TargetBranch == targetBranch &&
387-
sub.Id != existingSubscriptionId);
388-
389-
if (conflictingBackflowSubscription != null)
390-
{
391-
_logger.LogError($"A backflow subscription '{conflictingBackflowSubscription.Id}' already exists for the same target repository and branch. " +
392-
"Only one backflow subscription is allowed per target repository and branch combination.");
393-
throw new ArgumentException("Codeflow subscription conflict detected.");
394-
}
395-
}
396-
397-
// Check for forward flow conflicts (target directory not empty)
398-
if (!string.IsNullOrEmpty(targetDirectory))
399-
{
400-
var forwardFlowSubscriptions = await _barClient.GetCodeflowSubscriptionsAsync(
401-
targetRepo: targetRepository,
402-
sourceEnabled: true,
403-
targetDirectory: targetDirectory);
404-
405-
var conflictingForwardFlowSubscription = forwardFlowSubscriptions.FirstOrDefault(sub =>
406-
!string.IsNullOrEmpty(sub.TargetDirectory) &&
407-
sub.TargetRepository == targetRepository &&
408-
sub.TargetBranch == targetBranch &&
409-
sub.TargetDirectory == targetDirectory &&
410-
sub.Id != existingSubscriptionId);
411-
412-
if (conflictingForwardFlowSubscription != null)
413-
{
414-
_logger.LogError($"A forward flow subscription '{conflictingForwardFlowSubscription.Id}' already exists for the same VMR repository, branch, and target directory. " +
415-
"Only one forward flow subscription is allowed per VMR repository, branch, and target directory combination.");
416-
throw new ArgumentException("Codeflow subscription conflict detected.");
417-
}
418-
}
419-
}
420360
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using Microsoft.DotNet.DarcLib;
8+
using Microsoft.DotNet.ProductConstructionService.Client;
9+
using Microsoft.DotNet.ProductConstructionService.Client.Models;
10+
using Microsoft.Extensions.Logging;
11+
12+
namespace Microsoft.DotNet.Darc.Operations;
13+
14+
internal abstract class SubscriptionOperationBase : Operation
15+
{
16+
protected readonly IBarApiClient _barClient;
17+
protected readonly ILogger _logger;
18+
19+
protected SubscriptionOperationBase(IBarApiClient barClient, ILogger logger)
20+
{
21+
_barClient = barClient;
22+
_logger = logger;
23+
}
24+
25+
/// <summary>
26+
/// Validates that the codeflow subscription doesn't conflict with existing ones
27+
/// </summary>
28+
protected async Task ValidateCodeflowSubscriptionConflicts(
29+
string sourceRepository,
30+
string targetRepository,
31+
string targetBranch,
32+
string sourceDirectory,
33+
string targetDirectory,
34+
Guid? existingSubscriptionId)
35+
{
36+
// Check for backflow conflicts (source directory not empty)
37+
if (!string.IsNullOrEmpty(sourceDirectory))
38+
{
39+
var backflowSubscriptions = await _barClient.GetCodeflowSubscriptionsAsync(
40+
targetRepo: targetRepository,
41+
sourceEnabled: true);
42+
43+
var conflictingBackflowSubscription = backflowSubscriptions.FirstOrDefault(sub =>
44+
!string.IsNullOrEmpty(sub.SourceDirectory) &&
45+
sub.TargetRepository == targetRepository &&
46+
sub.TargetBranch == targetBranch &&
47+
sub.Id != existingSubscriptionId);
48+
49+
if (conflictingBackflowSubscription != null)
50+
{
51+
_logger.LogError($"A backflow subscription '{conflictingBackflowSubscription.Id}' already exists for the same target repository and branch. " +
52+
"Only one backflow subscription is allowed per target repository and branch combination.");
53+
throw new ArgumentException("Codeflow subscription conflict detected.");
54+
}
55+
}
56+
57+
// Check for forward flow conflicts (target directory not empty)
58+
if (!string.IsNullOrEmpty(targetDirectory))
59+
{
60+
var forwardFlowSubscriptions = await _barClient.GetCodeflowSubscriptionsAsync(
61+
targetRepo: targetRepository,
62+
sourceEnabled: true,
63+
targetDirectory: targetDirectory);
64+
65+
var conflictingForwardFlowSubscription = forwardFlowSubscriptions.FirstOrDefault(sub =>
66+
!string.IsNullOrEmpty(sub.TargetDirectory) &&
67+
sub.TargetRepository == targetRepository &&
68+
sub.TargetBranch == targetBranch &&
69+
sub.TargetDirectory == targetDirectory &&
70+
sub.Id != existingSubscriptionId);
71+
72+
if (conflictingForwardFlowSubscription != null)
73+
{
74+
_logger.LogError($"A forward flow subscription '{conflictingForwardFlowSubscription.Id}' already exists for the same VMR repository, branch, and target directory. " +
75+
"Only one forward flow subscription is allowed per VMR repository, branch, and target directory combination.");
76+
throw new ArgumentException("Codeflow subscription conflict detected.");
77+
}
78+
}
79+
}
80+
}

src/Microsoft.DotNet.Darc/Darc/Operations/UpdateSubscriptionOperation.cs

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,19 @@
1919

2020
namespace Microsoft.DotNet.Darc.Operations;
2121

22-
internal class UpdateSubscriptionOperation : Operation
22+
internal class UpdateSubscriptionOperation : SubscriptionOperationBase
2323
{
2424
private readonly UpdateSubscriptionCommandLineOptions _options;
25-
private readonly IBarApiClient _barClient;
2625
private readonly IGitRepoFactory _gitRepoFactory;
27-
private readonly ILogger<UpdateSubscriptionOperation> _logger;
2826

2927
public UpdateSubscriptionOperation(
3028
UpdateSubscriptionCommandLineOptions options,
3129
IBarApiClient barClient,
3230
IGitRepoFactory gitRepoFactory,
33-
ILogger<UpdateSubscriptionOperation> logger)
31+
ILogger<UpdateSubscriptionOperation> logger) : base(barClient, logger)
3432
{
3533
_options = options;
36-
_barClient = barClient;
3734
_gitRepoFactory = gitRepoFactory;
38-
_logger = logger;
3935
}
4036

4137
/// <summary>
@@ -366,62 +362,6 @@ private bool UpdatingMergePoliciesViaCommandLine()
366362
|| _options.CodeFlowCheckMergePolicy
367363
|| _options.VersionDetailsPropsMergePolicy;
368364

369-
/// <summary>
370-
/// Validates that the updated codeflow subscription doesn't conflict with existing ones
371-
/// </summary>
372-
private async Task ValidateCodeflowSubscriptionConflicts(
373-
string sourceRepository,
374-
string targetRepository,
375-
string targetBranch,
376-
string sourceDirectory,
377-
string targetDirectory,
378-
Guid? existingSubscriptionId)
379-
{
380-
// Check for backflow conflicts (source directory not empty)
381-
if (!string.IsNullOrEmpty(sourceDirectory))
382-
{
383-
var backflowSubscriptions = await _barClient.GetCodeflowSubscriptionsAsync(
384-
targetRepo: targetRepository,
385-
sourceEnabled: true);
386-
387-
var conflictingBackflowSubscription = backflowSubscriptions.FirstOrDefault(sub =>
388-
!string.IsNullOrEmpty(sub.SourceDirectory) &&
389-
sub.TargetRepository == targetRepository &&
390-
sub.TargetBranch == targetBranch &&
391-
sub.Id != existingSubscriptionId);
392-
393-
if (conflictingBackflowSubscription != null)
394-
{
395-
_logger.LogError($"A backflow subscription '{conflictingBackflowSubscription.Id}' already exists for the same target repository and branch. " +
396-
"Only one backflow subscription is allowed per target repository and branch combination.");
397-
throw new ArgumentException("Codeflow subscription conflict detected.");
398-
}
399-
}
400-
401-
// Check for forward flow conflicts (target directory not empty)
402-
if (!string.IsNullOrEmpty(targetDirectory))
403-
{
404-
var forwardFlowSubscriptions = await _barClient.GetCodeflowSubscriptionsAsync(
405-
targetRepo: targetRepository,
406-
sourceEnabled: true,
407-
targetDirectory: targetDirectory);
408-
409-
var conflictingForwardFlowSubscription = forwardFlowSubscriptions.FirstOrDefault(sub =>
410-
!string.IsNullOrEmpty(sub.TargetDirectory) &&
411-
sub.TargetRepository == targetRepository &&
412-
sub.TargetBranch == targetBranch &&
413-
sub.TargetDirectory == targetDirectory &&
414-
sub.Id != existingSubscriptionId);
415-
416-
if (conflictingForwardFlowSubscription != null)
417-
{
418-
_logger.LogError($"A forward flow subscription '{conflictingForwardFlowSubscription.Id}' already exists for the same VMR repository, branch, and target directory. " +
419-
"Only one forward flow subscription is allowed per VMR repository, branch, and target directory combination.");
420-
throw new ArgumentException("Codeflow subscription conflict detected.");
421-
}
422-
}
423-
}
424-
425365
private IEnumerable<string> GetExistingIgnoreChecks(MergePolicy mergePolicy) => mergePolicy
426366
.Properties
427367
.GetValueOrDefault(MergePolicyConstants.IgnoreChecksMergePolicyPropertyName)?

0 commit comments

Comments
 (0)