Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
run: dotnet build src/Dutchskull.Aspire.PolyRepo/Dutchskull.Aspire.PolyRepo.csproj --configuration Release --no-restore

- name: Pack
run: dotnet pack src/Dutchskull.Aspire.PolyRepo/Dutchskull.Aspire.PolyRepo.csproj --configuration Release --no-restore --output ./nupkg /p:Version=${{ env.semVer }}
run: dotnet pack src/Dutchskull.Aspire.PolyRepo/Dutchskull.Aspire.PolyRepo.csproj --configuration Release --no-restore --output ./nupkg /p:Version=${{ env.fullSemVer }}

- name: Publish to NuGet
run: dotnet nuget push ./nupkg/*.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NUGET_API_KEY }}
Expand Down
2 changes: 1 addition & 1 deletion GitVersion.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ branches:
increment: Minor

feature:
regex: ^features?[/-](?<BranchName>.+)$
regex: ^features?[/-](?<BranchName>.{1,20})
label: '{BranchName}'
increment: Patch
source-branches:
Expand Down
2 changes: 2 additions & 0 deletions src/Dutchskull.Aspire.PolyRepo/GitConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ internal GitConfig()
public required string Password { get; init; }

public string[] CustomHeaders { get; init; } = [];

public string? Tag { get; init; }
}
16 changes: 15 additions & 1 deletion src/Dutchskull.Aspire.PolyRepo/GitConfigBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class GitConfigBuilder
private string? _url;
private string? _username;
private string[]? _customHeaders;
private string? _tag;

internal GitConfigBuilder WithUrl(string url)
{
Expand Down Expand Up @@ -40,14 +41,27 @@ public GitConfig Build()
_password = string.Empty;
}


if (string.IsNullOrEmpty(_url))
{
throw new Exception("No git url was provided.");
}

_customHeaders ??= [];

return new GitConfig
{
Url = _url,
Username = _username,
Password = _password,
CustomHeaders = _customHeaders
CustomHeaders = _customHeaders,
Tag = _tag
};
}

public GitConfigBuilder WithTag(string? tag)
{
_tag = tag;
return this;
}
}
104 changes: 82 additions & 22 deletions src/Dutchskull.Aspire.PolyRepo/ProcessCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text;
using Dutchskull.Aspire.PolyRepo.Interfaces;
using LibGit2Sharp;
using System.Linq;

namespace Dutchskull.Aspire.PolyRepo;

Expand Down Expand Up @@ -30,13 +31,33 @@ public void CloneGitRepository(GitConfig gitConfig, string resolvedRepositoryPat
{
Username = gitConfig.Username,
Password = gitConfig.Password

},
CustomHeaders = gitConfig.CustomHeaders
}
};

Repository.Clone(gitConfig.Url, resolvedRepositoryPath, cloneOptions);

if (string.IsNullOrWhiteSpace(gitConfig.Tag))
{
return;
}

using Repository repo = new(resolvedRepositoryPath);

Remote? remote = repo.Network.Remotes.FirstOrDefault();
ArgumentNullException.ThrowIfNull(remote);

string[] tagRefSpec = [$"refs/tags/{gitConfig.Tag}:refs/tags/{gitConfig.Tag}"];
Commands.Fetch(repo, remote.Name, tagRefSpec, cloneOptions.FetchOptions, null);

Tag? tag = repo.Tags[gitConfig.Tag] ??
throw new Exception($"Tag '{gitConfig.Tag}' not found in repository after fetch.");

Commit? commit = (tag.PeeledTarget as Commit ?? repo.Lookup<Commit>(tag.Target.Sha)) ??
throw new Exception($"Tag '{gitConfig.Tag}' does not resolve to a commit.");

repo.Reset(ResetMode.Hard, commit);
}

public int NpmInstall(string resolvedRepositoryPath) =>
Expand All @@ -46,11 +67,20 @@ public void PullAndResetRepository(GitConfig gitConfig, string repositoryConfigR
{
using Repository repository = new(repositoryConfigRepositoryPath);

string? branchName = repository.Head.TrackedBranch.FriendlyName;
Remote? remote = repository.Network.Remotes.FirstOrDefault();
if (string.IsNullOrWhiteSpace(gitConfig.Tag))
{
FetchCurrentBranch(gitConfig, repository);

return;
}

FetchCurrentTag(gitConfig, repository);
}

private static void FetchCurrentTag(GitConfig gitConfig, Repository repository)
{
Remote? remote = repository.Network.Remotes.FirstOrDefault();
ArgumentNullException.ThrowIfNull(remote);
ArgumentNullException.ThrowIfNull(branchName);

FetchOptions fetchOptions = new()
{
Expand All @@ -62,15 +92,61 @@ public void PullAndResetRepository(GitConfig gitConfig, string repositoryConfigR
CustomHeaders = gitConfig.CustomHeaders
};

IEnumerable<string> references = remote.FetchRefSpecs.Select(x => x.Specification);
List<string> references = [.. remote.FetchRefSpecs.Select(x => x.Specification)];
references.Add($"refs/tags/{gitConfig.Tag}:refs/tags/{gitConfig.Tag}");

Commands.Fetch(repository, remote.Name, references, fetchOptions, null);

Tag? tag = repository.Tags[gitConfig.Tag] ??
throw new Exception($"Tag '{gitConfig.Tag}' not found after fetch.");

Commit? commit = (tag.PeeledTarget as Commit ?? repository.Lookup<Commit>(tag.Target.Sha)) ??
throw new Exception($"Tag '{gitConfig.Tag}' does not resolve to a commit.");

repository.Reset(ResetMode.Hard, commit);
}

private static void FetchCurrentBranch(GitConfig gitConfig, Repository repository)
{
string? branchName = repository.Head.TrackedBranch?.FriendlyName;
Remote? branchRemote = repository.Network.Remotes.FirstOrDefault();

ArgumentNullException.ThrowIfNull(branchRemote);
ArgumentNullException.ThrowIfNull(branchName);

FetchOptions branchFetchOptions = new()
{
CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials
{
Username = gitConfig.Username,
Password = gitConfig.Password
},
CustomHeaders = gitConfig.CustomHeaders
};

IEnumerable<string> branchReferences = branchRemote.FetchRefSpecs.Select(x => x.Specification);
Commands.Fetch(repository, branchRemote.Name, branchReferences, branchFetchOptions, null);

Branch? remoteBranch = repository.Branches[branchName];
Commit? latestCommit = remoteBranch.Tip;
Commit? latestCommit = remoteBranch?.Tip;

repository.Reset(ResetMode.Hard, latestCommit);
}

private static DataReceivedEventHandler LogData(StringBuilder output, string type)
{
return (sender, e) =>
{
if (string.IsNullOrEmpty(e.Data))
{
return;
}

output.AppendLine(e.Data);
Console.WriteLine($"[{type}]: {e.Data}");
};
}

private static int RunProcess(string fileName, string arguments)
{
Process process = new()
Expand All @@ -90,7 +166,6 @@ private static int RunProcess(string fileName, string arguments)
StringBuilder error = new();

process.OutputDataReceived += LogData(output, "OUTPUT");

process.ErrorDataReceived += LogData(error, "ERROR");

process.Start();
Expand All @@ -101,7 +176,6 @@ private static int RunProcess(string fileName, string arguments)
if (process.ExitCode == 0)
{
Console.WriteLine($"Process {fileName} {arguments} finished successfully.");

return process.ExitCode;
}

Expand All @@ -110,18 +184,4 @@ private static int RunProcess(string fileName, string arguments)

throw new Exception(errorMessage);
}

private static DataReceivedEventHandler LogData(StringBuilder output, string type)
{
return (sender, e) =>
{
if (string.IsNullOrEmpty(e.Data))
{
return;
}

output.AppendLine(e.Data);
Console.WriteLine($"[{type}]: {e.Data}");
};
}
}
10 changes: 10 additions & 0 deletions src/Dutchskull.Aspire.PolyRepo/RepositoryConfigBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class RepositoryConfigBuilder
private bool _keepUpToDate;
private IProcessCommandExecutor? _processCommandsExecutor;
private string _targetPath = ".";
private string? _tag = null;

public RepositoryConfig Build()
{
Expand All @@ -24,6 +25,7 @@ public RepositoryConfig Build()

GitConfigBuilder gitConfigBuilder = new();
gitConfigBuilder.WithUrl(_gitUrl);
gitConfigBuilder.WithTag(_tag);
_gitConfigBuilder?.Invoke(gitConfigBuilder);

return new RepositoryConfig
Expand All @@ -45,6 +47,14 @@ public RepositoryConfigBuilder WithTargetPath(string targetPath)
return this;
}


public RepositoryConfigBuilder WithTag(string tag)
{
_tag = tag;

return this;
}

public RepositoryConfigBuilder WithDefaultBranch(string branch)
{
_branch = branch;
Expand Down