diff --git a/src/Runner.Plugins/Repository/v1.0/RepositoryPlugin.cs b/src/Runner.Plugins/Repository/v1.0/RepositoryPlugin.cs index fd78da8499a..f1348532183 100644 --- a/src/Runner.Plugins/Repository/v1.0/RepositoryPlugin.cs +++ b/src/Runner.Plugins/Repository/v1.0/RepositoryPlugin.cs @@ -12,8 +12,6 @@ namespace GitHub.Runner.Plugins.Repository.v1_0 { public class CheckoutTask : IRunnerActionPlugin { - private readonly Regex _validSha1 = new(@"\b[0-9a-f]{40}\b", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled, TimeSpan.FromSeconds(2)); - public async Task RunAsync(RunnerActionPluginExecutionContext executionContext, CancellationToken token) { string runnerWorkspace = executionContext.GetRunnerContext("workspace"); diff --git a/src/Sdk/DTPipelines/Pipelines/Expressions/WellKnownRegularExpressions.cs b/src/Sdk/DTPipelines/Pipelines/Expressions/WellKnownRegularExpressions.cs index 72899fb4253..d4d3c75a64c 100644 --- a/src/Sdk/DTPipelines/Pipelines/Expressions/WellKnownRegularExpressions.cs +++ b/src/Sdk/DTPipelines/Pipelines/Expressions/WellKnownRegularExpressions.cs @@ -8,6 +8,7 @@ public static class WellKnownRegularExpressions public const String Email = nameof(Email); public const String IPv4Address = nameof(IPv4Address); public const String SHA1 = nameof(SHA1); + public const String CommitHash = nameof(CommitHash); public const String Url = nameof(Url); /// @@ -24,7 +25,8 @@ public static Lazy GetRegex(String regexType) case IPv4Address: return s_validIPv4Address; case SHA1: - return s_validSha1; + case CommitHash: + return s_validCommitHash; case Url: return s_validUrl; default: @@ -46,9 +48,9 @@ public static Lazy GetRegex(String regexType) ) ); - // 40 hex characters - private static readonly Lazy s_validSha1 = new Lazy(() => new Regex( - @"\b[0-9a-f]{40}\b", + // 40 or 64 hex characters (SHA-1 or SHA-256 commit hash) + private static readonly Lazy s_validCommitHash = new Lazy(() => new Regex( + @"\b(?:[0-9a-f]{40}|[0-9a-f]{64})\b", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled, RegexUtility.GetRegexTimeOut() ) ); diff --git a/src/Test/L0/Sdk/WellKnownRegularExpressionsL0.cs b/src/Test/L0/Sdk/WellKnownRegularExpressionsL0.cs new file mode 100644 index 00000000000..ffd3846bdb2 --- /dev/null +++ b/src/Test/L0/Sdk/WellKnownRegularExpressionsL0.cs @@ -0,0 +1,100 @@ +using GitHub.DistributedTask.Pipelines.Expressions; +using Xunit; + +namespace GitHub.Runner.Common.Tests.Sdk +{ + public sealed class WellKnownRegularExpressionsL0 + { + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Sdk")] + public void SHA1_Key_Returns_CommitHash_Regex() + { + var regex = WellKnownRegularExpressions.GetRegex(WellKnownRegularExpressions.SHA1); + + Assert.NotNull(regex); + } + + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Sdk")] + public void CommitHash_Key_Returns_CommitHash_Regex() + { + var regex = WellKnownRegularExpressions.GetRegex(WellKnownRegularExpressions.CommitHash); + + Assert.NotNull(regex); + } + + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Sdk")] + public void SHA1_And_CommitHash_Return_Same_Regex() + { + var sha1Regex = WellKnownRegularExpressions.GetRegex(WellKnownRegularExpressions.SHA1); + var commitHashRegex = WellKnownRegularExpressions.GetRegex(WellKnownRegularExpressions.CommitHash); + + Assert.Same(sha1Regex, commitHashRegex); + } + + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Sdk")] + public void Matches_40_Char_Hex() + { + var regex = WellKnownRegularExpressions.GetRegex(WellKnownRegularExpressions.CommitHash); + + Assert.Matches(regex.Value, new string((char)97, 40)); + } + + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Sdk")] + public void Matches_64_Char_Hex() + { + var regex = WellKnownRegularExpressions.GetRegex(WellKnownRegularExpressions.CommitHash); + + Assert.Matches(regex.Value, new string((char)97, 64)); + } + + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Sdk")] + public void Does_Not_Match_63_Char_Hex() + { + var regex = WellKnownRegularExpressions.GetRegex(WellKnownRegularExpressions.CommitHash); + + Assert.DoesNotMatch(regex.Value, new string((char)97, 63)); + } + + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Sdk")] + public void Does_Not_Match_65_Char_Hex() + { + var regex = WellKnownRegularExpressions.GetRegex(WellKnownRegularExpressions.CommitHash); + + Assert.DoesNotMatch(regex.Value, new string((char)97, 65)); + } + + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Sdk")] + public void Matches_Mixed_Case_64_Char() + { + var regex = WellKnownRegularExpressions.GetRegex(WellKnownRegularExpressions.CommitHash); + var value = new string((char)65, 32) + new string((char)98, 32); + + Assert.Matches(regex.Value, value); + } + + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Sdk")] + public void Unknown_Key_Returns_Null() + { + var regex = WellKnownRegularExpressions.GetRegex("UnknownType"); + + Assert.Null(regex); + } + } +}