-
Notifications
You must be signed in to change notification settings - Fork 81
Comment on FF PRs that look good to merge #6435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dkurepa
wants to merge
19
commits into
dotnet:main
Choose a base branch
from
dkurepa:dkurepa/CodeflowApproval1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+871
−15
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e16614d
bootstrap arcade-services for agentic coding
dkurepa 6b3f3cc
First draft, still no review
dkurepa 618e2f3
Merge remote-tracking branch 'origin/main' into dkurepa/CodeflowApproval
dkurepa a85a621
small improvements
dkurepa f9111da
Run the codeflow code check as it's own job
dkurepa aa86859
Add tests
dkurepa 2a3ade4
Merge remote-tracking branch 'origin/main' into dkurepa/CodeflowAppro…
dkurepa 5576916
Merge remote-tracking branch 'origin/main' into dkurepa/CodeflowAppro…
dkurepa 026ef42
cleanup
dkurepa 10fb18c
test
dkurepa ff31e68
Rename to Codeflow Approval Check
dkurepa a47dee7
Revert test changes
dkurepa 8e9d7e8
Add verifier tests
dkurepa 87e83fc
cleanup
dkurepa e9a8e99
copilot cr changes
dkurepa 54cce80
Check commit authors, rename things, add logging
dkurepa c7b447d
cr changes
dkurepa 1643888
Add distributed redis lock around codeflow approval task
dkurepa 1aa40d9
Improve message
dkurepa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
283 changes: 283 additions & 0 deletions
283
src/Microsoft.DotNet.Darc/DarcLib/VirtualMonoRepo/CodeflowSourceDiffVerifier.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,283 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.DotNet.DarcLib.Helpers; | ||
| using Microsoft.DotNet.DarcLib.Models.VirtualMonoRepo; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| #nullable enable | ||
| namespace Microsoft.DotNet.DarcLib.VirtualMonoRepo; | ||
|
|
||
| public interface ICodeflowSourceDiffVerifier | ||
| { | ||
| /// <summary> | ||
| /// Verifies that a forward-flow codeflow PR (source repo -> VMR) faithfully contains the source | ||
| /// repo's commit diff (oldSha...newSha), accounting for the expected divergences (path remap, | ||
| /// excludes, eng/common, version files, no-ops). | ||
| /// </summary> | ||
| Task<bool> ForwardFlowMatchesSourceDiffAsync( | ||
| string sourceRepoUri, | ||
| string vmrUri, | ||
| string mappingName, | ||
| string oldSha, | ||
| string newSha, | ||
| string vmrTargetBranch, | ||
| string vmrHeadBranch, | ||
| CancellationToken cancellationToken = default); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verifies that a forward-flow codeflow PR faithfully reflects the source repo's commit diff, | ||
| /// accounting for the expected, legitimate divergences between a source repo and its VMR copy | ||
| /// (path remap, cloaked/excluded paths, eng/common, version/metadata files and no-ops). | ||
| /// </summary> | ||
| public class CodeflowSourceDiffVerifier : ICodeflowSourceDiffVerifier | ||
| { | ||
| private readonly IVmrCloneManager _vmrCloneManager; | ||
| private readonly IRepositoryCloneManager _cloneManager; | ||
| private readonly IVmrDependencyTracker _dependencyTracker; | ||
| private readonly ISourceManifest _sourceManifest; | ||
| private readonly ILogger<CodeflowSourceDiffVerifier> _logger; | ||
|
|
||
| public CodeflowSourceDiffVerifier( | ||
| IVmrCloneManager vmrCloneManager, | ||
| IRepositoryCloneManager cloneManager, | ||
| IVmrDependencyTracker dependencyTracker, | ||
| ISourceManifest sourceManifest, | ||
| ILogger<CodeflowSourceDiffVerifier> logger) | ||
| { | ||
| _vmrCloneManager = vmrCloneManager; | ||
| _cloneManager = cloneManager; | ||
| _dependencyTracker = dependencyTracker; | ||
| _sourceManifest = sourceManifest; | ||
| _logger = logger; | ||
| } | ||
|
|
||
| public async Task<bool> ForwardFlowMatchesSourceDiffAsync( | ||
| string sourceRepoUri, | ||
| string vmrUri, | ||
| string mappingName, | ||
| string oldSha, | ||
| string newSha, | ||
| string vmrTargetBranch, | ||
| string vmrHeadBranch, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| _logger.LogInformation( | ||
| "Verifying forward flow PR for {mappingName} against source diff {oldSha}...{newSha}", | ||
| mappingName, | ||
| oldSha, | ||
| newSha); | ||
|
|
||
| var srcMappingPath = VmrInfo.GetRelativeRepoSourcesPath(mappingName); | ||
|
|
||
| ILocalGitRepo vmr = await _vmrCloneManager.PrepareVmrAsync( | ||
| [vmrUri], | ||
| [vmrTargetBranch, vmrHeadBranch], | ||
| vmrHeadBranch, | ||
| resetToRemote: true, | ||
| cancellationToken); | ||
|
|
||
| SourceMapping mapping = _dependencyTracker.GetMapping(mappingName); | ||
|
|
||
| var exclusionPathspecs = GetDiffFilters(mapping, _sourceManifest); | ||
|
|
||
| ILocalGitRepo sourceRepo = await _cloneManager.PrepareCloneAsync( | ||
| mapping, | ||
| [sourceRepoUri], | ||
| [oldSha, newSha], | ||
| newSha, | ||
| resetToRemote: false, | ||
| cancellationToken); | ||
|
|
||
| var srcMappingPrefix = srcMappingPath + "/"; | ||
| HashSet<string> sourceRepoChangedFiles = await GetChangedMappingFilesAsync( | ||
| sourceRepo, mappingName, oldSha, newSha, exclusionPathspecs: exclusionPathspecs, cancellationToken: cancellationToken); | ||
| HashSet<string> vmrPrChangedFiles = await GetChangedMappingFilesAsync( | ||
| vmr, mappingName, vmrTargetBranch, vmrHeadBranch, relativePath: srcMappingPrefix, cancellationToken: cancellationToken); | ||
|
|
||
| var filesChangedInBoth = sourceRepoChangedFiles.Where(vmrPrChangedFiles.Contains).ToList(); | ||
| var sourceRepoOnlyChanges = sourceRepoChangedFiles.Where(f => !vmrPrChangedFiles.Contains(f)).ToList(); | ||
| var filesChangedInPrOnly = vmrPrChangedFiles.Where(f => !sourceRepoChangedFiles.Contains(f)).ToList(); | ||
|
|
||
| if (filesChangedInPrOnly.Count > 0) | ||
| { | ||
| _logger.LogInformation( | ||
| "Source diff verification for {mappingName} failed: {unexpected} file(s) changed in the PR but not in the source diff", | ||
| mappingName, | ||
| filesChangedInPrOnly.Count); | ||
| return false; | ||
| } | ||
|
|
||
| // Per-file content compare on the intersection. | ||
| foreach (var file in filesChangedInBoth) | ||
| { | ||
| if (!await ChangedLinesMatchAsync(sourceRepo, vmr, file, srcMappingPath, oldSha, newSha, vmrTargetBranch, vmrHeadBranch, cancellationToken)) | ||
| { | ||
| _logger.LogInformation( | ||
| "Source diff verification for {mappingName} failed: changes to {file} don't match the source diff", | ||
| mappingName, | ||
| file); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // No-op check on files the source changed but the PR did not. | ||
| foreach (var file in sourceRepoOnlyChanges) | ||
| { | ||
| if (!await IsLegitimateNoOpAsync(sourceRepo, vmr, file, srcMappingPath, newSha, vmrHeadBranch)) | ||
| { | ||
| _logger.LogInformation( | ||
| "Source diff verification for {mappingName} failed: {file} changed in the source diff but is not reflected in the PR", | ||
| mappingName, | ||
| file); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| _logger.LogInformation("Source diff verification for {mappingName} passed", mappingName); | ||
| return true; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Builds the git pathspec exclusion rules the same way VmrDiffOperation.GetDiffFilters does: | ||
| /// the mapping's excludes plus submodule paths under the mapping, turned into git exclusion rules. | ||
| /// </summary> | ||
| private static List<string> GetDiffFilters(SourceMapping mapping, ISourceManifest manifest) | ||
| { | ||
| var submodules = manifest.Submodules | ||
| .Where(s => s.Path.StartsWith(mapping.Name + '/', StringComparison.OrdinalIgnoreCase)) | ||
| .Select(s => s.Path.Substring(mapping.Name.Length + 1)); | ||
|
|
||
| return (mapping.Exclude ?? []) | ||
| .Concat(submodules) | ||
| .Select(VmrPatchHandler.GetExclusionRule) | ||
| .ToList(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Runs a three-dot name-only diff (<paramref name="fromRef"/>...<paramref name="toRef"/>) scoped to the | ||
| /// mapping's location in the repo and returns the mapping-relative paths, after dropping eng/common | ||
| /// (non-arcade) and version/metadata files. | ||
| /// </summary> | ||
| private static async Task<HashSet<string>> GetChangedMappingFilesAsync( | ||
| ILocalGitRepo repo, | ||
| string mappingName, | ||
| string fromRef, | ||
| string toRef, | ||
| string? relativePath = null, | ||
| IReadOnlyCollection<string>? exclusionPathspecs = null, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| IReadOnlyCollection<string> pathspecs = string.IsNullOrEmpty(relativePath) | ||
| ? [".", .. exclusionPathspecs ?? []] | ||
| : [relativePath, .. exclusionPathspecs ?? []]; | ||
|
|
||
| var result = await repo.ExecuteGitCommand( | ||
| ["diff", "--name-only", $"{fromRef}...{toRef}", "--", .. pathspecs], | ||
| cancellationToken); | ||
| result.ThrowIfFailed($"Failed to get the diff between {fromRef} and {toRef}"); | ||
|
|
||
| IEnumerable<string> files = result.GetOutputLines(); | ||
| if (!string.IsNullOrEmpty(relativePath)) | ||
| { | ||
| files = files | ||
| .Select(f => f.Substring(relativePath.Length)); | ||
| } | ||
|
|
||
| return FilterMappingFiles(files, mappingName); | ||
| } | ||
|
|
||
| private static HashSet<string> FilterMappingFiles(IEnumerable<string> files, string mappingName) | ||
| { | ||
| var engCommonPrefix = Constants.CommonScriptFilesPath + "/"; | ||
| var dropEngCommon = mappingName != VmrInfo.ArcadeMappingName; | ||
|
|
||
| return files | ||
| .Where(f => !DependencyFileManager.CodeflowDependencyFiles.Contains(f, StringComparer.OrdinalIgnoreCase)) | ||
| .Where(f => !dropEngCommon || !f.StartsWith(engCommonPrefix, StringComparison.OrdinalIgnoreCase)) | ||
| .ToHashSet(StringComparer.OrdinalIgnoreCase); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Compares the zero-context change lines of a single file between the source diff and the PR. | ||
| /// Lines that belong to the diff format (and are expected to differ) are ignored before comparing. | ||
| /// </summary> | ||
| private static async Task<bool> ChangedLinesMatchAsync( | ||
| ILocalGitRepo sourceRepo, | ||
| ILocalGitRepo vmr, | ||
| string file, | ||
| UnixPath srcMappingPath, | ||
| string oldSha, | ||
| string newSha, | ||
| string vmrTargetBranch, | ||
| string vmrHeadBranch, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| var sourceResult = await sourceRepo.ExecuteGitCommand(["diff", "-U0", $"{oldSha}...{newSha}", "--", file], cancellationToken); | ||
| sourceResult.ThrowIfFailed($"Failed to get the source diff of {file} between {oldSha} and {newSha}"); | ||
|
|
||
| var vmrResult = await vmr.ExecuteGitCommand(["diff", "-U0", $"{vmrTargetBranch}...{vmrHeadBranch}", "--", srcMappingPath / file], cancellationToken); | ||
| vmrResult.ThrowIfFailed($"Failed to get the VMR diff of {file} between {vmrTargetBranch} and {vmrHeadBranch}"); | ||
|
|
||
| var sourceChanges = GetChangeLines(sourceResult.GetOutputLines()); | ||
| var vmrChanges = GetChangeLines(vmrResult.GetOutputLines()); | ||
|
|
||
| return sourceChanges.SequenceEqual(vmrChanges); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Keeps only the +/- change lines from a zero-context diff. Only lines inside a hunk (after an | ||
| /// "@@" header) are collected, so the per-file "--- a/file" / "+++ b/file" headers - which share a | ||
| /// prefix with genuine content lines such as a removed "-- comment" (rendered as "--- comment") - are | ||
| /// excluded structurally rather than by an ambiguous textual prefix match. | ||
| /// </summary> | ||
| private static List<string> GetChangeLines(IReadOnlyCollection<string> lines) | ||
| { | ||
| var changeLines = new List<string>(); | ||
| var insideHunk = false; | ||
|
|
||
| foreach (var line in lines) | ||
| { | ||
| if (line.StartsWith("@@")) | ||
| { | ||
| // start of code changes. we want to start adding lines after this, but not the hunk header itself | ||
| insideHunk = true; | ||
| } | ||
| else if (insideHunk && (line.StartsWith('+') || line.StartsWith('-'))) | ||
| { | ||
| changeLines.Add(line); | ||
| } | ||
| } | ||
|
|
||
| return changeLines; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A file the source changed but the PR did not is only legitimate when the VMR copy is already | ||
| /// at the source's new state (equal content, or both absent for an already-reconciled deletion). | ||
| /// </summary> | ||
| private static async Task<bool> IsLegitimateNoOpAsync( | ||
| ILocalGitRepo sourceRepo, | ||
| ILocalGitRepo vmr, | ||
| string file, | ||
| UnixPath srcMappingPath, | ||
| string newSha, | ||
| string vmrHeadBranch) | ||
| { | ||
| var sourceContent = await sourceRepo.GetFileFromGitAsync(file, newSha); | ||
| var vmrContent = await vmr.GetFileFromGitAsync(srcMappingPath / file, vmrHeadBranch); | ||
|
|
||
| if (sourceContent == null && vmrContent == null) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| return string.Equals(sourceContent, vmrContent, StringComparison.Ordinal); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.