Skip to content

Commit 9ea32af

Browse files
Georgy GorelkoGeorgy Gorelko
authored andcommitted
Refactor branch policy check logic to improve clarity and reduce redundancy
1 parent ea2b2be commit 9ea32af

File tree

1 file changed

+66
-75
lines changed

1 file changed

+66
-75
lines changed

src/Octoshift/Services/AdoPipelineTriggerService.cs

Lines changed: 66 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -116,107 +116,98 @@ public async Task<bool> IsPipelineRequiredByBranchPolicy(string adoOrg, string t
116116

117117
try
118118
{
119-
// Use repository ID directly if available, otherwise look it up by name
120-
string repositoryId;
121-
var isRepositoryDisabled = false;
119+
var (repositoryId, isRepositoryDisabled) = await GetRepositoryIdAndStatus(adoOrg, teamProject, repoName, repoId, pipelineId);
122120

123-
if (!string.IsNullOrEmpty(repoId))
121+
if (string.IsNullOrEmpty(repositoryId))
124122
{
125-
_log.LogVerbose($"Using repository ID from pipeline definition for branch policy check: {repoId}");
126-
repositoryId = repoId;
127-
128-
// Check if repository is disabled by fetching its details
129-
var (_, disabled) = await GetRepositoryInfoWithCache(adoOrg, teamProject, repoId, repoName);
130-
isRepositoryDisabled = disabled;
131-
}
132-
else
133-
{
134-
// Get repository information by name (with caching)
135-
var (id, disabled) = await GetRepositoryInfoWithCache(adoOrg, teamProject, null, repoName);
136-
repositoryId = id;
137-
isRepositoryDisabled = disabled;
138-
139-
if (string.IsNullOrEmpty(repositoryId))
140-
{
141-
_log.LogWarning($"Repository ID not found for {adoOrg}/{teamProject}/{repoName}. Branch policy check cannot be performed for pipeline {pipelineId}.");
142-
return false;
143-
}
123+
return false;
144124
}
145125

146-
// Skip branch policy check if repository is disabled
147126
if (isRepositoryDisabled)
148127
{
149128
var repoIdentifier = repoName ?? repoId;
150129
_log.LogWarning($"Repository {adoOrg}/{teamProject}/{repoIdentifier} is disabled. Branch policy check skipped for pipeline {pipelineId}. Pipeline trigger configuration may not preserve branch policy requirements.");
151130
return false;
152131
}
153132

154-
// Get branch policies for the repository (with caching)
155-
var policyData = await GetBranchPoliciesWithCache(adoOrg, teamProject, repositoryId);
156-
157-
if (policyData?.Value == null || policyData.Value.Count == 0)
158-
{
159-
var repoIdentifier = repoName ?? repoId ?? "unknown";
160-
_log.LogVerbose($"No branch policies found for repository {adoOrg}/{teamProject}/{repoIdentifier}. ADO Pipeline ID = {pipelineId} is not required by branch policy.");
161-
return false;
162-
}
163-
164-
// Look for enabled build validation policies that reference our pipeline
165-
var isPipelineRequired = policyData.Value.Any(policy =>
166-
policy.Type?.DisplayName == "Build" &&
167-
policy.IsEnabled &&
168-
policy.Settings?.BuildDefinitionId == pipelineId.ToString());
169-
170-
if (isPipelineRequired)
171-
{
172-
var repoIdentifier = repoName ?? repoId ?? "unknown";
173-
_log.LogVerbose($"ADO Pipeline ID = {pipelineId} is required by branch policy in {adoOrg}/{teamProject}/{repoIdentifier}. Build status reporting will be enabled to support branch protection.");
174-
}
175-
else
176-
{
177-
var repoIdentifier = repoName ?? repoId ?? "unknown";
178-
_log.LogVerbose($"ADO Pipeline ID = {pipelineId} is not required by any branch policies in {adoOrg}/{teamProject}/{repoIdentifier}.");
179-
}
180-
181-
return isPipelineRequired;
133+
return await CheckBranchPoliciesForPipeline(adoOrg, teamProject, repositoryId, repoName, repoId, pipelineId);
182134
}
183-
catch (HttpRequestException ex)
135+
catch (Exception ex) when (ex is HttpRequestException or TaskCanceledException or JsonException or ArgumentException or InvalidOperationException)
184136
{
185-
// If we can't determine branch policy status due to network issues, default to false
186-
var repoIdentifier = repoName ?? repoId ?? "unknown";
187-
_log.LogWarning($"HTTP error during branch policy check for pipeline {pipelineId} in {adoOrg}/{teamProject}/{repoIdentifier}: {ex.Message}. Pipeline trigger configuration may not preserve branch policy requirements.");
137+
LogBranchPolicyCheckError(ex, adoOrg, teamProject, repoName, repoId, pipelineId);
188138
return false;
189139
}
190-
catch (TaskCanceledException ex)
140+
}
141+
142+
private async Task<(string repositoryId, bool isDisabled)> GetRepositoryIdAndStatus(string adoOrg, string teamProject, string repoName, string repoId, int pipelineId)
143+
{
144+
if (!string.IsNullOrEmpty(repoId))
191145
{
192-
// If branch policy checking times out, consider check failed
193-
var repoIdentifier = repoName ?? repoId ?? "unknown";
194-
_log.LogWarning($"Branch policy check timed out for pipeline {pipelineId} in {adoOrg}/{teamProject}/{repoIdentifier}: {ex.Message}. Pipeline trigger configuration may not preserve branch policy requirements.");
195-
return false;
146+
_log.LogVerbose($"Using repository ID from pipeline definition for branch policy check: {repoId}");
147+
var (_, disabled) = await GetRepositoryInfoWithCache(adoOrg, teamProject, repoId, repoName);
148+
return (repoId, disabled);
196149
}
197-
catch (JsonException ex)
150+
151+
var (id, disabledStatus) = await GetRepositoryInfoWithCache(adoOrg, teamProject, null, repoName);
152+
if (string.IsNullOrEmpty(id))
198153
{
199-
// If we can't determine branch policy status due to JSON parsing issues, default to false
200-
var repoIdentifier = repoName ?? repoId ?? "unknown";
201-
_log.LogWarning($"JSON parsing error during branch policy check for pipeline {pipelineId} in {adoOrg}/{teamProject}/{repoIdentifier}: {ex.Message}. Pipeline trigger configuration may not preserve branch policy requirements.");
202-
return false;
154+
_log.LogWarning($"Repository ID not found for {adoOrg}/{teamProject}/{repoName}. Branch policy check cannot be performed for pipeline {pipelineId}.");
155+
return (null, false);
203156
}
204-
catch (ArgumentException ex)
157+
158+
return (id, disabledStatus);
159+
}
160+
161+
private async Task<bool> CheckBranchPoliciesForPipeline(string adoOrg, string teamProject, string repositoryId, string repoName, string repoId, int pipelineId)
162+
{
163+
var policyData = await GetBranchPoliciesWithCache(adoOrg, teamProject, repositoryId);
164+
165+
if (policyData?.Value == null || policyData.Value.Count == 0)
205166
{
206-
// If we can't determine branch policy status due to invalid arguments, default to false
207167
var repoIdentifier = repoName ?? repoId ?? "unknown";
208-
_log.LogWarning($"Invalid argument error during branch policy check for pipeline {pipelineId} in {adoOrg}/{teamProject}/{repoIdentifier}: {ex.Message}. Pipeline trigger configuration may not preserve branch policy requirements.");
168+
_log.LogVerbose($"No branch policies found for repository {adoOrg}/{teamProject}/{repoIdentifier}. ADO Pipeline ID = {pipelineId} is not required by branch policy.");
209169
return false;
210170
}
211-
catch (InvalidOperationException ex)
171+
172+
var isPipelineRequired = policyData.Value.Any(policy =>
173+
policy.Type?.DisplayName == "Build" &&
174+
policy.IsEnabled &&
175+
policy.Settings?.BuildDefinitionId == pipelineId.ToString());
176+
177+
LogBranchPolicyCheckResult(isPipelineRequired, adoOrg, teamProject, repoName, repoId, pipelineId);
178+
return isPipelineRequired;
179+
}
180+
181+
private void LogBranchPolicyCheckResult(bool isPipelineRequired, string adoOrg, string teamProject, string repoName, string repoId, int pipelineId)
182+
{
183+
var repoIdentifier = repoName ?? repoId ?? "unknown";
184+
185+
if (isPipelineRequired)
186+
{
187+
_log.LogVerbose($"ADO Pipeline ID = {pipelineId} is required by branch policy in {adoOrg}/{teamProject}/{repoIdentifier}. Build status reporting will be enabled to support branch protection.");
188+
}
189+
else
212190
{
213-
// If branch policy checking fails due to invalid state, consider check failed
214-
var repoIdentifier = repoName ?? repoId ?? "unknown";
215-
_log.LogWarning($"Invalid operation error during branch policy check for pipeline {pipelineId} in {adoOrg}/{teamProject}/{repoIdentifier}: {ex.Message}. Pipeline trigger configuration may not preserve branch policy requirements.");
216-
return false;
191+
_log.LogVerbose($"ADO Pipeline ID = {pipelineId} is not required by any branch policies in {adoOrg}/{teamProject}/{repoIdentifier}.");
217192
}
218193
}
219194

195+
private void LogBranchPolicyCheckError(Exception ex, string adoOrg, string teamProject, string repoName, string repoId, int pipelineId)
196+
{
197+
var repoIdentifier = repoName ?? repoId ?? "unknown";
198+
var errorType = ex switch
199+
{
200+
HttpRequestException => "HTTP error",
201+
TaskCanceledException => "Branch policy check timed out",
202+
JsonException => "JSON parsing error",
203+
ArgumentException => "Invalid argument error",
204+
InvalidOperationException => "Invalid operation error",
205+
_ => "Error"
206+
};
207+
208+
_log.LogWarning($"{errorType} during branch policy check for pipeline {pipelineId} in {adoOrg}/{teamProject}/{repoIdentifier}: {ex.Message}. Pipeline trigger configuration may not preserve branch policy requirements.");
209+
}
210+
220211
#region Private Helper Methods - Trigger Configuration Logic
221212

222213
private void LogBranchPolicyCheckResults(int pipelineId, bool isPipelineRequiredByBranchPolicy)
@@ -545,7 +536,7 @@ private bool HasTriggerType(JToken originalTriggers, string triggerType)
545536
// Log as verbose since the caller will log a more specific warning about the disabled repository
546537
// Return (null, true) to indicate repository ID is unknown but repository is disabled
547538
_log.LogVerbose($"Repository {adoOrg}/{teamProject}/{identifier} returned 404 - likely disabled or not found.");
548-
var info = (null, true); // Mark as disabled with null ID since identifier may be a name
539+
(string id, bool isDisabled) info = (null, true); // Mark as disabled with null ID since identifier may be a name
549540
_repositoryCache[cacheKey] = info;
550541
return info;
551542
}

0 commit comments

Comments
 (0)