From 18bece7cc878948876ac28edc301d704c032b7cc Mon Sep 17 00:00:00 2001 From: realmarv Date: Wed, 29 Mar 2023 14:16:06 +0330 Subject: [PATCH] FileConventions: implement the function Implement DetectNotUsingKebabCaseInGitHubCIJobs function. --- scripts/eofConvention.fsx | 1 + .../inconsistentVersionsInFSharpScripts.fsx | 1 + scripts/inconsistentVersionsInGitHubCI.fsx | 1 + scripts/mixedLineEndings.fsx | 1 + .../nonVerboseFlagsInGitHubCIAndScripts.fsx | 1 + scripts/shebangConvention.fsx | 1 + scripts/unpinnedDotnetPackageVersions.fsx | 1 + scripts/unpinnedDotnetToolInstallVersions.fsx | 1 + .../unpinnedGitHubActionsImageVersions.fsx | 1 + .../unpinnedNugetPackageReferenceVersions.fsx | 1 + scripts/wrapLatestCommitMsg.fsx | 2 + src/FileConventions/FileConventions.fsproj | 4 ++ src/FileConventions/Library.fs | 50 ++++++++++++++++++- 13 files changed, 65 insertions(+), 1 deletion(-) diff --git a/scripts/eofConvention.fsx b/scripts/eofConvention.fsx index 32bfb983f..385f97b80 100644 --- a/scripts/eofConvention.fsx +++ b/scripts/eofConvention.fsx @@ -3,6 +3,7 @@ open System.IO open System +#r "nuget: YamlDotNet, Version=13.0.2" #load "../src/FileConventions/Helpers.fs" #load "../src/FileConventions/Library.fs" diff --git a/scripts/inconsistentVersionsInFSharpScripts.fsx b/scripts/inconsistentVersionsInFSharpScripts.fsx index e73479968..3b45857dc 100644 --- a/scripts/inconsistentVersionsInFSharpScripts.fsx +++ b/scripts/inconsistentVersionsInFSharpScripts.fsx @@ -3,6 +3,7 @@ open System.IO open System.Linq +#r "nuget: YamlDotNet, Version=13.0.2" #load "../src/FileConventions/Library.fs" #load "../src/FileConventions/Helpers.fs" diff --git a/scripts/inconsistentVersionsInGitHubCI.fsx b/scripts/inconsistentVersionsInGitHubCI.fsx index 80bd9a1c5..6d302f82a 100644 --- a/scripts/inconsistentVersionsInGitHubCI.fsx +++ b/scripts/inconsistentVersionsInGitHubCI.fsx @@ -2,6 +2,7 @@ open System.IO +#r "nuget: YamlDotNet, Version=13.0.2" #load "../src/FileConventions/Library.fs" #load "../src/FileConventions/Helpers.fs" diff --git a/scripts/mixedLineEndings.fsx b/scripts/mixedLineEndings.fsx index 7b404c98d..2edb095c0 100644 --- a/scripts/mixedLineEndings.fsx +++ b/scripts/mixedLineEndings.fsx @@ -3,6 +3,7 @@ open System open System.IO +#r "nuget: YamlDotNet, Version=13.0.2" #load "../src/FileConventions/Library.fs" #load "../src/FileConventions/Helpers.fs" diff --git a/scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx b/scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx index 6ffb88019..15f551603 100644 --- a/scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx +++ b/scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx @@ -3,6 +3,7 @@ open System open System.IO +#r "nuget: YamlDotNet, Version=13.0.2" #load "../src/FileConventions/Library.fs" #load "../src/FileConventions/Helpers.fs" diff --git a/scripts/shebangConvention.fsx b/scripts/shebangConvention.fsx index 89e1f0fb5..e77822f8f 100644 --- a/scripts/shebangConvention.fsx +++ b/scripts/shebangConvention.fsx @@ -3,6 +3,7 @@ open System open System.IO +#r "nuget: YamlDotNet, Version=13.0.2" #load "../src/FileConventions/Library.fs" #load "../src/FileConventions/Helpers.fs" diff --git a/scripts/unpinnedDotnetPackageVersions.fsx b/scripts/unpinnedDotnetPackageVersions.fsx index f40429602..d86389e0f 100644 --- a/scripts/unpinnedDotnetPackageVersions.fsx +++ b/scripts/unpinnedDotnetPackageVersions.fsx @@ -3,6 +3,7 @@ open System open System.IO +#r "nuget: YamlDotNet, Version=13.0.2" #load "../src/FileConventions/Library.fs" #load "../src/FileConventions/Helpers.fs" diff --git a/scripts/unpinnedDotnetToolInstallVersions.fsx b/scripts/unpinnedDotnetToolInstallVersions.fsx index 0d99b0f4d..ffddbd95d 100644 --- a/scripts/unpinnedDotnetToolInstallVersions.fsx +++ b/scripts/unpinnedDotnetToolInstallVersions.fsx @@ -3,6 +3,7 @@ open System open System.IO +#r "nuget: YamlDotNet, Version=13.0.2" #load "../src/FileConventions/Library.fs" #load "../src/FileConventions/Helpers.fs" diff --git a/scripts/unpinnedGitHubActionsImageVersions.fsx b/scripts/unpinnedGitHubActionsImageVersions.fsx index 451bdeb62..ba9b07e56 100644 --- a/scripts/unpinnedGitHubActionsImageVersions.fsx +++ b/scripts/unpinnedGitHubActionsImageVersions.fsx @@ -3,6 +3,7 @@ open System open System.IO +#r "nuget: YamlDotNet, Version=13.0.2" #load "../src/FileConventions/Library.fs" #load "../src/FileConventions/Helpers.fs" diff --git a/scripts/unpinnedNugetPackageReferenceVersions.fsx b/scripts/unpinnedNugetPackageReferenceVersions.fsx index 15aa408eb..091f2a307 100644 --- a/scripts/unpinnedNugetPackageReferenceVersions.fsx +++ b/scripts/unpinnedNugetPackageReferenceVersions.fsx @@ -3,6 +3,7 @@ open System open System.IO +#r "nuget: YamlDotNet, Version=13.0.2" #load "../src/FileConventions/Library.fs" #load "../src/FileConventions/Helpers.fs" diff --git a/scripts/wrapLatestCommitMsg.fsx b/scripts/wrapLatestCommitMsg.fsx index bf38c0d64..795a69b76 100644 --- a/scripts/wrapLatestCommitMsg.fsx +++ b/scripts/wrapLatestCommitMsg.fsx @@ -4,6 +4,8 @@ open System.IO open System open System.Text.RegularExpressions +#r "nuget: YamlDotNet, Version=13.0.2" + #load "../src/FileConventions/Library.fs" #r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62" diff --git a/src/FileConventions/FileConventions.fsproj b/src/FileConventions/FileConventions.fsproj index 02d61ddad..ec9ae9b95 100644 --- a/src/FileConventions/FileConventions.fsproj +++ b/src/FileConventions/FileConventions.fsproj @@ -10,4 +10,8 @@ + + + + diff --git a/src/FileConventions/Library.fs b/src/FileConventions/Library.fs index e3cd5b0b5..7b20bc718 100644 --- a/src/FileConventions/Library.fs +++ b/src/FileConventions/Library.fs @@ -5,6 +5,9 @@ open System.IO open System.Linq open System.Text.RegularExpressions +open YamlDotNet +open YamlDotNet.RepresentationModel + let HasCorrectShebang(fileInfo: FileInfo) = let fileText = File.ReadLines fileInfo.FullName @@ -391,4 +394,49 @@ let DetectNotUsingSnakeCaseInScriptName(fileInfo: FileInfo) = let DetectNotUsingKebabCaseInGitHubCIJobs(fileInfo: FileInfo) = assert (fileInfo.FullName.EndsWith ".yml") - false + + let read yaml = + use reader = new StringReader(yaml) + let stream = YamlStream() + stream.Load(reader) + stream.Documents + + let doc = read(File.ReadAllText fileInfo.FullName) + + let ymlNode = doc.[0].RootNode + + // Borrowed from https://stackoverflow.com/questions/46697298/whats-the-best-way-to-parse-yaml-in-f-on-net-core + let getMapping(ymlNode: YamlNode) = + let node = ymlNode :?> YamlMappingNode + + let mapping = + node.Children + |> Seq.map(fun kvp -> + let keyNode = kvp.Key :?> YamlScalarNode + keyNode.Value, kvp.Value + ) + |> Map.ofSeq + + mapping + + let jobNames = + match ymlNode.NodeType with + | YamlNodeType.Mapping -> + let mapping = getMapping ymlNode + let maybeJobs = mapping.TryFind "jobs" + + match maybeJobs with + | Some jobs -> + let mapping = getMapping jobs + (mapping |> Map.toSeq |> Seq.map fst) + | None -> Seq.empty + | _ -> Seq.empty + + jobNames + |> Seq.map(fun jobName -> + let isKebabCase = + jobName.ToLower() = jobName && not(jobName.Contains("_")) + + isKebabCase + ) + |> Seq.contains false