Skip to content
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

Fix E2E code flow tests (fetching refs, updating version files) #4190

Merged
merged 6 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions docs/DevGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
('https://github.com/maestro-auth-test/maestro-test', 289474),
('https://github.com/maestro-auth-test/maestro-test2', 289474),
('https://github.com/maestro-auth-test/maestro-test3', 289474),
('https://github.com/maestro-auth-test/maestro-test-vmr', 289474),
('https://github.com/maestro-auth-test/arcade', 289474),
('https://github.com/maestro-auth-test/dnceng-vmr', 289474);
```
Expand Down
8 changes: 8 additions & 0 deletions src/Microsoft.DotNet.Darc/DarcLib/ILocalGitClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ Task CommitAsync(
/// <returns>List of currently modified staged files</returns>
Task<string[]> GetStagedFiles(string repoPath);

/// <summary>
/// Determines if a given path is a git repository.
/// </summary>
/// <param name="repoPath">Path to a git repository</param>
/// <param name="gitRef">Git reference to check for</param>
/// <returns>True if the path is a git repository, false otherwise</returns>
Task<bool> GitRefExists(string repoPath, string gitRef, CancellationToken cancellationToken = default);

/// <summary>
/// Fetches from all remotes.
/// </summary>
Expand Down
19 changes: 18 additions & 1 deletion src/Microsoft.DotNet.Darc/DarcLib/LocalGitClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace Microsoft.DotNet.DarcLib;
/// </summary>
public class LocalGitClient : ILocalGitClient
{
private static readonly Regex ShaRegex = new(@"^[0-9a-fA-F]{40}$", RegexOptions.Compiled);
dkurepa marked this conversation as resolved.
Show resolved Hide resolved

private readonly IRemoteTokenProvider _remoteConfiguration;
private readonly ITelemetryRecorder _telemetryRecorder;
private readonly IProcessManager _processManager;
Expand Down Expand Up @@ -210,7 +212,6 @@ public async Task<GitObjectType> GetObjectTypeAsync(string repoPath, string obje
};

var result = await _processManager.ExecuteGit(repoPath, args);
result.ThrowIfFailed($"Failed to find object {objectSha} in {repoPath}");

return result.StandardOutput.Trim() switch
{
Expand Down Expand Up @@ -443,6 +444,22 @@ public async Task<string> BlameLineAsync(string repoPath, string relativeFilePat
return result.StandardOutput.Trim().Split(' ').First();
}

public async Task<bool> GitRefExists(string repoPath, string gitRef, CancellationToken cancellationToken = default)
dkurepa marked this conversation as resolved.
Show resolved Hide resolved
{
// If the ref is a SHA, we can check it directly via git cat-file -t
if (ShaRegex.IsMatch(gitRef))
{
var objectType = await GetObjectTypeAsync(repoPath, gitRef);
return objectType != GitObjectType.Unknown;
}

// If it's a branch name, we need to scan all fetched remotes too
// git cat-file -t doesn't work because we would have to query for [remote name]/gitRef
var result = await RunGitCommandAsync(repoPath, ["branch", "-a", "--list", "*/" + gitRef], cancellationToken);
result.ThrowIfFailed($"Failed to verify if git ref '{gitRef}' exists in {repoPath}");
return result.StandardOutput.Contains(gitRef);
}

public async Task<bool> HasWorkingTreeChangesAsync(string repoPath)
{
var result = await _processManager.ExecuteGit(repoPath, ["diff", "--exit-code"]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
premun marked this conversation as resolved.
Show resolved Hide resolved
using System.Threading;
using System.Threading.Tasks;
using LibGit2Sharp;
Expand Down Expand Up @@ -79,14 +80,13 @@ protected async Task<ILocalGitRepo> PrepareCloneInternalAsync(
var missingCommit = false;

// Verify that all requested commits are available
foreach (string commit in refsToVerify.ToArray())
foreach (string gitRef in refsToVerify.ToArray())
{
try
{
var objectType = await _localGitRepo.GetObjectTypeAsync(path, commit);
if (objectType == GitObjectType.Commit)
if (await _localGitRepo.GitRefExists(path, gitRef, cancellationToken))
{
refsToVerify.Remove(commit);
refsToVerify.Remove(gitRef);
}
}
catch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public class VmrDependencyTracker : IVmrDependencyTracker
{
private readonly AllVersionsPropsFile _repoVersions;
private readonly ISourceManifest _sourceManifest;
private readonly LocalPath _allVersionsFilePath;
private readonly IVmrInfo _vmrInfo;
private readonly IFileSystem _fileSystem;
private readonly ISourceMappingParser _sourceMappingParser;
Expand All @@ -63,7 +62,6 @@ public VmrDependencyTracker(
ISourceManifest sourceManifest)
{
_vmrInfo = vmrInfo;
_allVersionsFilePath = vmrInfo.VmrPath / VmrInfo.GitInfoSourcesDir / AllVersionsPropsFile.FileName;
_sourceManifest = sourceManifest;
_repoVersions = new AllVersionsPropsFile(sourceManifest.Repositories);
_fileSystem = fileSystem;
Expand Down Expand Up @@ -94,7 +92,7 @@ public async Task InitializeSourceMappings(string? sourceMappingsPath = null)
public void UpdateDependencyVersion(VmrDependencyUpdate update)
{
_repoVersions.UpdateVersion(update.Mapping.Name, update.TargetRevision, update.TargetVersion);
_repoVersions.SerializeToXml(_allVersionsFilePath);
_repoVersions.SerializeToXml(_vmrInfo.AllVersionsFilePath);

_sourceManifest.UpdateVersion(update.Mapping.Name, update.RemoteUri, update.TargetRevision, update.TargetVersion);
_fileSystem.WriteToFile(_vmrInfo.SourceManifestPath, _sourceManifest.ToJson());
Expand Down Expand Up @@ -126,7 +124,7 @@ public bool RemoveRepositoryVersion(string repo)

if (_repoVersions.DeleteVersion(repo))
{
_repoVersions.SerializeToXml(_allVersionsFilePath);
_repoVersions.SerializeToXml(_vmrInfo.AllVersionsFilePath);
hasChanges = true;
}

Expand Down
7 changes: 7 additions & 0 deletions src/Microsoft.DotNet.Darc/DarcLib/VirtualMonoRepo/VmrInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public interface IVmrInfo
/// Gets a full path leading to sources belonging to a given repo
/// </summary>
NativePath GetRepoSourcesPath(string mappingName);

/// <summary>
/// Path to the AllRepoVersions.props
/// </summary>
NativePath AllVersionsFilePath { get; }
}

public class VmrInfo : IVmrInfo
Expand Down Expand Up @@ -137,4 +142,6 @@ public VmrInfo(NativePath vmrPath, NativePath tmpPath)
public static UnixPath GetRelativeRepoSourcesPath(string mappingName) => RelativeSourcesDir / mappingName;

public NativePath SourceManifestPath { get; private set; }

public NativePath AllVersionsFilePath => VmrPath / GitInfoSourcesDir / AllVersionsPropsFile.FileName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public async Task UnsetReminderAsync(bool isCodeFlow)
{
await client.DeleteWorkItemAsync(receipt.MessageId, receipt.PopReceipt);
}
catch (RequestFailedException e) when (e.Message.Contains("The specified message does not exist") || e.Message.Contains("did not match the pop receipt"))
catch (RequestFailedException e) when (e.Message.Contains("The specified message does not exist")
|| e.Message.Contains("did not match the pop receipt"))
{
// The message was already deleted, so we can ignore this exception.
}
Expand Down
1 change: 1 addition & 0 deletions src/ProductConstructionService/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
('https://github.com/maestro-auth-test/maestro-test', 289474),
('https://github.com/maestro-auth-test/maestro-test2', 289474),
('https://github.com/maestro-auth-test/maestro-test3', 289474),
('https://github.com/maestro-auth-test/maestro-test-vmr', 289474),
('https://github.com/maestro-auth-test/arcade', 289474),
('https://github.com/maestro-auth-test/dnceng-vmr', 289474);
```
Expand Down
Loading