From 8c36f4c0421d168c732c978959f183ea8ec96363 Mon Sep 17 00:00:00 2001 From: Luiz Henrique Cassettari Date: Wed, 18 Dec 2024 15:45:02 -0300 Subject: [PATCH] Enable `SkipForked` --- Build/.nuke/build.schema.json | 3 + Build/IShowGitVersion.cs | 21 +++- CHANGELOG.md | 1 + Directory.Build.props | 2 +- .../ricaun.Nuke.Example.Tests.csproj | 2 +- ricaun.Nuke/Components/IGitPreRelease.cs | 1 + ricaun.Nuke/Components/IGitRelease.cs | 1 + ricaun.Nuke/Components/IHazGitRepository.cs | 117 +++++++++++------- ricaun.Nuke/Components/IPack.cs | 1 + ricaun.Nuke/Components/IPrePack.cs | 1 + ricaun.Nuke/Extensions/GitHubExtension.cs | 22 ++++ 11 files changed, 124 insertions(+), 48 deletions(-) diff --git a/Build/.nuke/build.schema.json b/Build/.nuke/build.schema.json index 3fd3e1e..9454c2f 100644 --- a/Build/.nuke/build.schema.json +++ b/Build/.nuke/build.schema.json @@ -111,6 +111,9 @@ "allOf": [ { "properties": { + "EnableForkedRepository": { + "type": "boolean" + }, "Folder": { "type": "string" }, diff --git a/Build/IShowGitVersion.cs b/Build/IShowGitVersion.cs index 490251e..c76782c 100644 --- a/Build/IShowGitVersion.cs +++ b/Build/IShowGitVersion.cs @@ -1,8 +1,9 @@ using Nuke.Common; +using Nuke.Common.Git; using ricaun.Nuke.Components; using ricaun.Nuke.Extensions; -public interface IShowGitVersion : IHazGitVersion, IHazChangelog, IClean, ICompile +public interface IShowGitVersion : IHazGitVersion, IHazGitRepository, IHazChangelog, IClean, ICompile { Target ShowGitVersion => _ => _ .TriggeredBy(Clean) @@ -23,5 +24,23 @@ public interface IShowGitVersion : IHazGitVersion, IHazChangelog, IClean, ICompi System.IO.Path.Combine(BuildAssemblyDirectory, $"latest.json")); } catch { } + + + Serilog.Log.Information("Commit = {Value}", GitRepository.Commit); + Serilog.Log.Information("Branch = {Value}", GitRepository.Branch); + Serilog.Log.Information("Tags = {Value}", GitRepository.Tags); + Serilog.Log.Information("Head = {Value}", GitRepository.Head); + Serilog.Log.Information("Identifier = {Value}", GitRepository.Identifier); + + Serilog.Log.Information("IsForked = {Value}", GitRepository.IsForked()); + + Serilog.Log.Information("main branch = {Value}", GitRepository.IsOnMainBranch()); + Serilog.Log.Information("main/master branch = {Value}", GitRepository.IsOnMainOrMasterBranch()); + Serilog.Log.Information("release/* branch = {Value}", GitRepository.IsOnReleaseBranch()); + Serilog.Log.Information("hotfix/* branch = {Value}", GitRepository.IsOnHotfixBranch()); + + Serilog.Log.Information("Https URL = {Value}", GitRepository.HttpsUrl); + Serilog.Log.Information("SSH URL = {Value}", GitRepository.SshUrl); + }); } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index b7b70f8..c35d0d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Features - Enable sign files using `Azure Key Vault`. - Enable `IAssetRelease` to release assets before `GitRelease` and `GitPreRelease`. +- Enable `SkipForked` to skip release if forked repository. ### Build - Add `IAzureSignTool` to check if `AzureSignToolUtils` is installed. - Add `ILocalAssetRelease` to test `AssetRelease` assets before release. diff --git a/Directory.Build.props b/Directory.Build.props index e3fb86e..787c5a1 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,5 +1,5 @@ - 1.9.0-beta.9 + 1.9.0-rc \ No newline at end of file diff --git a/ricaun.Nuke.Example.Tests/ricaun.Nuke.Example.Tests.csproj b/ricaun.Nuke.Example.Tests/ricaun.Nuke.Example.Tests.csproj index 544eff1..90fdb2a 100644 --- a/ricaun.Nuke.Example.Tests/ricaun.Nuke.Example.Tests.csproj +++ b/ricaun.Nuke.Example.Tests/ricaun.Nuke.Example.Tests.csproj @@ -14,7 +14,7 @@ - net7.0 + net8.0 diff --git a/ricaun.Nuke/Components/IGitPreRelease.cs b/ricaun.Nuke/Components/IGitPreRelease.cs index 6d8d69a..bd6d1fc 100644 --- a/ricaun.Nuke/Components/IGitPreRelease.cs +++ b/ricaun.Nuke/Components/IGitPreRelease.cs @@ -38,6 +38,7 @@ public bool HasPreReleaseFilter(string version) .OnlyWhenStatic(() => GitHubToken.SkipEmpty()) .OnlyWhenStatic(() => IsServerBuild) .OnlyWhenDynamic(() => GitRepository.IsOnDevelopBranch()) + .OnlyWhenDynamic(() => SkipForked()) .Executes(() => { var project = MainProject; diff --git a/ricaun.Nuke/Components/IGitRelease.cs b/ricaun.Nuke/Components/IGitRelease.cs index 8492a0a..e636af6 100644 --- a/ricaun.Nuke/Components/IGitRelease.cs +++ b/ricaun.Nuke/Components/IGitRelease.cs @@ -26,6 +26,7 @@ public interface IGitRelease : IRelease, IHazGitRepository, IHazGitVersion, IHaz .OnlyWhenStatic(() => GitHubToken.SkipEmpty()) .OnlyWhenStatic(() => IsServerBuild) .OnlyWhenDynamic(() => GitRepository.IsOnMainOrMasterBranch()) + .OnlyWhenDynamic(() => SkipForked()) .Executes(() => { var project = MainProject; diff --git a/ricaun.Nuke/Components/IHazGitRepository.cs b/ricaun.Nuke/Components/IHazGitRepository.cs index dfc25a0..82892f3 100644 --- a/ricaun.Nuke/Components/IHazGitRepository.cs +++ b/ricaun.Nuke/Components/IHazGitRepository.cs @@ -1,45 +1,72 @@ -using Nuke.Common; -using Nuke.Common.Git; - -namespace ricaun.Nuke.Components -{ - /// - /// IHazGitRepository - /// - public interface IHazGitRepository : INukeBuild - { - /// - /// GitHubToken - /// - [Secret][Parameter] public string GitHubToken => TryGetValue(() => GitHubToken); - - /// - /// GitRepository - /// - [GitRepository] GitRepository GitRepository => TryGetValue(() => GitRepository); - - /// - /// GetGitRepositoryPackageUrl (default: https://nuget.pkg.github.com/repository_owner/index.json) - /// - /// - public string GetGitRepositoryPackageUrl() - { - if (GitRepository == null) - { - Serilog.Log.Warning($"GitRepository not found."); - return ""; - } - return $@"https://nuget.pkg.github.com/{GetGitRepositoryOwner()}/index.json"; - } - - /// - /// GetGitRepositoryOwner based on the GitRepository.Identifier - /// - /// - public string GetGitRepositoryOwner() - { - if (GitRepository == null) return ""; - return GitRepository.Identifier?.Split("/")[0]; - } - } -} +using Nuke.Common; +using Nuke.Common.Git; +using ricaun.Nuke.Extensions; + +namespace ricaun.Nuke.Components +{ + /// + /// IHazGitRepository + /// + public interface IHazGitRepository : INukeBuild + { + /// + /// GitHubToken + /// + [Secret][Parameter] public string GitHubToken => TryGetValue(() => GitHubToken); + + /// + /// GitRepository + /// + [GitRepository] GitRepository GitRepository => TryGetValue(() => GitRepository); + + /// + /// GetGitRepositoryPackageUrl (default: https://nuget.pkg.github.com/repository_owner/index.json) + /// + /// + public string GetGitRepositoryPackageUrl() + { + if (GitRepository == null) + { + Serilog.Log.Warning($"GitRepository not found."); + return ""; + } + return $@"https://nuget.pkg.github.com/{GetGitRepositoryOwner()}/index.json"; + } + + /// + /// GetGitRepositoryOwner based on the GitRepository.Identifier + /// + /// + public string GetGitRepositoryOwner() + { + if (GitRepository == null) return ""; + return GitRepository.Identifier?.Split("/")[0]; + } + + /// + /// Indicates whether the forked repository is enabled. + /// + [Parameter] + bool EnableForkedRepository => TryGetValue(() => EnableForkedRepository) ?? false; + + /// + /// Determines if the forked repository should be skipped. + /// + /// True if the forked repository should be skipped; otherwise, false. + public bool SkipForked() + { + if (EnableForkedRepository) + return false; + + return IsGitRepositoryForked(); + } + /// + /// IsGitRepositoryForked + /// + /// + public bool IsGitRepositoryForked() + { + return GitRepository.IsForked(); + } + } +} diff --git a/ricaun.Nuke/Components/IPack.cs b/ricaun.Nuke/Components/IPack.cs index ba16b7c..f2ab5ba 100644 --- a/ricaun.Nuke/Components/IPack.cs +++ b/ricaun.Nuke/Components/IPack.cs @@ -29,6 +29,7 @@ public interface IPack : IHazPack, IGitRelease .OnlyWhenStatic(() => NugetApiKey.SkipEmpty()) .OnlyWhenStatic(() => IsServerBuild) .OnlyWhenDynamic(() => GitRepository.IsOnMainOrMasterBranch()) + .OnlyWhenDynamic(() => SkipForked()) .Executes(() => { var releaseDirectory = GetReleaseDirectory(MainProject); diff --git a/ricaun.Nuke/Components/IPrePack.cs b/ricaun.Nuke/Components/IPrePack.cs index 84e2e78..a7b9718 100644 --- a/ricaun.Nuke/Components/IPrePack.cs +++ b/ricaun.Nuke/Components/IPrePack.cs @@ -24,6 +24,7 @@ public interface IPrePack : IPack, IGitPreRelease .OnlyWhenStatic(() => NugetApiKey.SkipEmpty()) .OnlyWhenStatic(() => IsServerBuild) .OnlyWhenDynamic(() => GitRepository.IsOnDevelopBranch()) + .OnlyWhenDynamic(() => SkipForked()) .Executes(() => { var version = MainProject.GetInformationalVersion(); diff --git a/ricaun.Nuke/Extensions/GitHubExtension.cs b/ricaun.Nuke/Extensions/GitHubExtension.cs index a54c463..aec2e67 100644 --- a/ricaun.Nuke/Extensions/GitHubExtension.cs +++ b/ricaun.Nuke/Extensions/GitHubExtension.cs @@ -1,4 +1,5 @@ using Nuke.Common; +using Nuke.Common.Git; using Nuke.Common.IO; using Nuke.Common.Tools.GitHub; using Octokit; @@ -14,6 +15,27 @@ namespace ricaun.Nuke.Extensions /// public static class GitHubExtension { + /// + /// Determines whether the specified Git repository is a fork. + /// + /// The Git repository to check. + /// + /// true if the specified Git repository is a fork; otherwise, false. + /// + public static bool IsForked(this GitRepository gitRepository) + { + if (gitRepository is null) + return false; + + var gitHubOwner = gitRepository.GetGitHubOwner(); + var gitHubName = gitRepository.GetGitHubName(); + var repository = GitHubTasks.GitHubClient.Repository + .Get(gitHubOwner, gitHubName) + .Result; + + return repository.Fork; + } + #region GitHubUtil /// /// Check if Tags already exists.