diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index e389d9f80..c04de7c4d 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -32,7 +32,7 @@ jobs: - name: Compile the conventions solution run: dotnet build --configuration Release conventions.sln - name: Compile F# scripts - run: dotnet fsi scripts/compileFSharpScripts.fsx + run: dotnet fsi scripts/compile_fsharp_scripts.fsx file-conventions-tests: name: Run FileConventions-lib unit tests @@ -166,30 +166,32 @@ jobs: apt install --yes --no-install-recommends dotnet6 - name: Check all files end with EOL - run: dotnet fsi scripts/eofConvention.fsx + run: dotnet fsi scripts/eof_convention.fsx - name: Check all .fsx scripts have shebang - run: dotnet fsi scripts/shebangConvention.fsx + run: dotnet fsi scripts/shebang_convention.fsx - name: Check all F# scripts have execute permission - run: dotnet fsi scripts/executableConvention.fsx + run: dotnet fsi scripts/executable_convention.fsx - name: Check there are no mixed line-endings in any files - run: dotnet fsi scripts/mixedLineEndings.fsx + run: dotnet fsi scripts/mixed_line_endings.fsx - name: Check there are no unpinned GitHubActions image versions - run: dotnet fsi scripts/unpinnedGitHubActionsImageVersions.fsx + run: dotnet fsi scripts/unpinned_github_actions_image_versions.fsx - name: Check there are no unpinned dotnet package versions - run: dotnet fsi scripts/unpinnedDotnetPackageVersions.fsx + run: dotnet fsi scripts/unpinned_dotnet_package_versions.fsx - name: Check there are no unpinned nuget package reference versions in F# scripts - run: dotnet fsi scripts/unpinnedNugetPackageReferenceVersions.fsx + run: dotnet fsi scripts/unpinned_nuget_package_reference_versions.fsx - name: Check there are no unpinned versions in `dotnet tool install` commands - run: dotnet fsi scripts/unpinnedDotnetToolInstallVersions.fsx + run: dotnet fsi scripts/unpinned_dotnet_tool_install_versions.fsx + - name: Check if script names (.fsx, .bat, and .sh files) are snake_case and CI job names are kebab-case. + run: dotnet fsi scripts/not_respecting_some_naming_conventions.fsx - name: Check commits 1 by 1 if: github.event_name == 'pull_request' - run: dotnet fsi scripts/checkCommits1by1.fsx + run: dotnet fsi scripts/check_commits_1by1.fsx - name: Check there are no inconsistent versions GitHubCI files - run: dotnet fsi scripts/inconsistentVersionsInGitHubCI.fsx + run: dotnet fsi scripts/inconsistent_versions_in_githubci.fsx - name: Check there are no inconsistent versions in nuget package references of F# scripts - run: dotnet fsi scripts/inconsistentVersionsInFSharpScripts.fsx + run: dotnet fsi scripts/inconsistent_versions_in_fsharp_scripts.fsx - name: Check there are no non-verbose flags in scripts and CI YML files - run: dotnet fsi scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx + run: dotnet fsi scripts/non_verbose_flags_in_githubci_and_scripts.fsx - name: Install prettier run: npm install prettier@2.8.3 - name: Change file permissions diff --git a/scripts/checkCommits1by1.fsx b/scripts/check_commits_1by1.fsx similarity index 99% rename from scripts/checkCommits1by1.fsx rename to scripts/check_commits_1by1.fsx index 353f91950..3e672d544 100755 --- a/scripts/checkCommits1by1.fsx +++ b/scripts/check_commits_1by1.fsx @@ -920,7 +920,7 @@ if notUsedGitPush1by1 then sprintf "Please push the commits one by one to make sure every commit has a CI status; using this script is recommended:%s%s" Environment.NewLine - "https://github.com/nblockchain/conventions/blob/master/scripts/gitPush1by1.fsx" + "https://github.com/nblockchain/conventions/blob/master/scripts/git_push_1by1.fsx" Console.Error.WriteLine errMsg Environment.Exit 2 diff --git a/scripts/compileFSharpScripts.fsx b/scripts/compile_fsharp_scripts.fsx similarity index 100% rename from scripts/compileFSharpScripts.fsx rename to scripts/compile_fsharp_scripts.fsx diff --git a/scripts/eofConvention.fsx b/scripts/eof_convention.fsx similarity index 93% rename from scripts/eofConvention.fsx rename to scripts/eof_convention.fsx index 3f2b712b9..f2c37233c 100755 --- a/scripts/eofConvention.fsx +++ b/scripts/eof_convention.fsx @@ -4,7 +4,7 @@ open System.IO open System #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - +#r "nuget: YamlDotNet, Version=13.0.2" #load "../src/FileConventions/Helpers.fs" #load "../src/FileConventions/Library.fs" diff --git a/scripts/executableConvention.fsx b/scripts/executable_convention.fsx similarity index 93% rename from scripts/executableConvention.fsx rename to scripts/executable_convention.fsx index cecaf6b5b..9789f7e96 100755 --- a/scripts/executableConvention.fsx +++ b/scripts/executable_convention.fsx @@ -4,6 +4,7 @@ open System open System.IO #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" +#r "nuget: YamlDotNet, Version=13.0.2" #load "../src/FileConventions/Library.fs" #load "../src/FileConventions/Helpers.fs" diff --git a/scripts/gitPush1by1.fsx b/scripts/git_push_1by1.fsx similarity index 100% rename from scripts/gitPush1by1.fsx rename to scripts/git_push_1by1.fsx diff --git a/scripts/inconsistentVersionsInFSharpScripts.fsx b/scripts/inconsistent_versions_in_fsharp_scripts.fsx similarity index 94% rename from scripts/inconsistentVersionsInFSharpScripts.fsx rename to scripts/inconsistent_versions_in_fsharp_scripts.fsx index 93922496e..b62c8a038 100755 --- a/scripts/inconsistentVersionsInFSharpScripts.fsx +++ b/scripts/inconsistent_versions_in_fsharp_scripts.fsx @@ -4,7 +4,7 @@ open System.IO open System.Linq #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - +#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/inconsistent_versions_in_githubci.fsx similarity index 97% rename from scripts/inconsistentVersionsInGitHubCI.fsx rename to scripts/inconsistent_versions_in_githubci.fsx index f8041a45d..17e06f884 100755 --- a/scripts/inconsistentVersionsInGitHubCI.fsx +++ b/scripts/inconsistent_versions_in_githubci.fsx @@ -3,7 +3,7 @@ open System.IO #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - +#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/mixed_line_endings.fsx similarity index 92% rename from scripts/mixedLineEndings.fsx rename to scripts/mixed_line_endings.fsx index 427826a61..777d627a7 100755 --- a/scripts/mixedLineEndings.fsx +++ b/scripts/mixed_line_endings.fsx @@ -4,7 +4,7 @@ open System open System.IO #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - +#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/non_verbose_flags_in_githubci_and_scripts.fsx similarity index 94% rename from scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx rename to scripts/non_verbose_flags_in_githubci_and_scripts.fsx index 54bd123d5..41d9a17d2 100755 --- a/scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx +++ b/scripts/non_verbose_flags_in_githubci_and_scripts.fsx @@ -4,7 +4,7 @@ open System open System.IO #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - +#r "nuget: YamlDotNet, Version=13.0.2" #load "../src/FileConventions/Library.fs" #load "../src/FileConventions/Helpers.fs" diff --git a/scripts/not_respecting_some_naming_conventions.fsx b/scripts/not_respecting_some_naming_conventions.fsx new file mode 100755 index 000000000..71a2b5e4d --- /dev/null +++ b/scripts/not_respecting_some_naming_conventions.fsx @@ -0,0 +1,42 @@ +#!/usr/bin/env -S dotnet fsi + +open System +open System.IO + +#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" +#r "nuget: YamlDotNet, Version=13.0.2" +#load "../src/FileConventions/Library.fs" +#load "../src/FileConventions/Helpers.fs" + +let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo + +let invalidYmlFiles = + Helpers.GetInvalidFiles + rootDir + "*.yml" + FileConventions.DetectNotUsingKebabCaseInGitHubCIJobs + +Helpers.AssertNoInvalidFiles + invalidYmlFiles + "Please use kebab-case for CI job names in the following files:" + +let scriptExtensions = + seq { + ".fsx" + ".bat" + ".sh" + } + +let scriptsWithInvalidNames = + scriptExtensions + |> Seq.map(fun extension -> + Helpers.GetInvalidFiles + rootDir + ("*" + extension) + FileConventions.DetectNotUsingSnakeCaseInScriptName + ) + |> Seq.concat + +Helpers.AssertNoInvalidFiles + scriptsWithInvalidNames + "Please use snake_case for naming the following scripts:" diff --git a/scripts/shebangConvention.fsx b/scripts/shebang_convention.fsx similarity index 93% rename from scripts/shebangConvention.fsx rename to scripts/shebang_convention.fsx index 965a9aad3..b3b5752f9 100755 --- a/scripts/shebangConvention.fsx +++ b/scripts/shebang_convention.fsx @@ -4,7 +4,7 @@ open System open System.IO #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - +#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/unpinned_dotnet_package_versions.fsx similarity index 94% rename from scripts/unpinnedDotnetPackageVersions.fsx rename to scripts/unpinned_dotnet_package_versions.fsx index 56da33c01..b6f065072 100755 --- a/scripts/unpinnedDotnetPackageVersions.fsx +++ b/scripts/unpinned_dotnet_package_versions.fsx @@ -4,7 +4,7 @@ open System open System.IO #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - +#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/unpinned_dotnet_tool_install_versions.fsx similarity index 93% rename from scripts/unpinnedDotnetToolInstallVersions.fsx rename to scripts/unpinned_dotnet_tool_install_versions.fsx index 92f35dcc6..de6e3fad6 100755 --- a/scripts/unpinnedDotnetToolInstallVersions.fsx +++ b/scripts/unpinned_dotnet_tool_install_versions.fsx @@ -4,7 +4,7 @@ open System open System.IO #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - +#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/unpinned_github_actions_image_versions.fsx similarity index 95% rename from scripts/unpinnedGitHubActionsImageVersions.fsx rename to scripts/unpinned_github_actions_image_versions.fsx index 07d31cc97..e3eabd47f 100755 --- a/scripts/unpinnedGitHubActionsImageVersions.fsx +++ b/scripts/unpinned_github_actions_image_versions.fsx @@ -4,7 +4,7 @@ open System open System.IO #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - +#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/unpinned_nuget_package_reference_versions.fsx similarity index 94% rename from scripts/unpinnedNugetPackageReferenceVersions.fsx rename to scripts/unpinned_nuget_package_reference_versions.fsx index 72e0e74b5..54d9766e3 100755 --- a/scripts/unpinnedNugetPackageReferenceVersions.fsx +++ b/scripts/unpinned_nuget_package_reference_versions.fsx @@ -4,7 +4,7 @@ open System open System.IO #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - +#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/wrap_latest_commit_msg.fsx similarity index 97% rename from scripts/wrapLatestCommitMsg.fsx rename to scripts/wrap_latest_commit_msg.fsx index 5b6592b0e..709bf6ae9 100755 --- a/scripts/wrapLatestCommitMsg.fsx +++ b/scripts/wrap_latest_commit_msg.fsx @@ -5,6 +5,7 @@ open System open System.Text.RegularExpressions #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" +#r "nuget: YamlDotNet, Version=13.0.2" #load "../src/FileConventions/Library.fs" diff --git a/src/FileConventions.Test/DummyFiles/DummyCIWithKebabCaseJobName.yml b/src/FileConventions.Test/DummyFiles/DummyCIWithKebabCaseJobName.yml new file mode 100644 index 000000000..ec5f065b4 --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/DummyCIWithKebabCaseJobName.yml @@ -0,0 +1,11 @@ +name: CI + +on: [push, pull_request] + +jobs: + file-conventions: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v2 + - name: Print "Hello World!" + run: echo "Hello World!" diff --git a/src/FileConventions.Test/DummyFiles/DummyCIWithMultipleJobsAndKebabCaseJobNames.yml b/src/FileConventions.Test/DummyFiles/DummyCIWithMultipleJobsAndKebabCaseJobNames.yml new file mode 100644 index 000000000..0f6532c0d --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/DummyCIWithMultipleJobsAndKebabCaseJobNames.yml @@ -0,0 +1,18 @@ +name: CI + +on: [push, pull_request] + +jobs: + first-job: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v2 + - name: Print "Hello World!" + run: echo "Hello World!" + + second-job: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v2 + - name: Print "Hello World!" + run: echo "Hello World!" diff --git a/src/FileConventions.Test/DummyFiles/DummyCIWithMultipleJobsAndOnePascalCaseJobName.yml b/src/FileConventions.Test/DummyFiles/DummyCIWithMultipleJobsAndOnePascalCaseJobName.yml new file mode 100644 index 000000000..3eb5e7e66 --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/DummyCIWithMultipleJobsAndOnePascalCaseJobName.yml @@ -0,0 +1,18 @@ +name: CI + +on: [push, pull_request] + +jobs: + first-job: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v2 + - name: Print "Hello World!" + run: echo "Hello World!" + + SecondJob: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v2 + - name: Print "Hello World!" + run: echo "Hello World!" diff --git a/src/FileConventions.Test/DummyFiles/DummyCIWithPascalCaseJobName.yml b/src/FileConventions.Test/DummyFiles/DummyCIWithPascalCaseJobName.yml new file mode 100644 index 000000000..763ad1f01 --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/DummyCIWithPascalCaseJobName.yml @@ -0,0 +1,11 @@ +name: CI + +on: [push, pull_request] + +jobs: + FileConventions: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v2 + - name: Print "Hello World!" + run: echo "Hello World!" diff --git a/src/FileConventions.Test/DummyFiles/DummyPascalCase.fsx b/src/FileConventions.Test/DummyFiles/DummyPascalCase.fsx new file mode 100644 index 000000000..23c3e513e --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/DummyPascalCase.fsx @@ -0,0 +1,3 @@ +#!/usr/bin/env -S dotnet fsi + +printfn "Hello World!" diff --git a/src/FileConventions.Test/DummyFiles/dummy_snake_case.fsx b/src/FileConventions.Test/DummyFiles/dummy_snake_case.fsx new file mode 100644 index 000000000..23c3e513e --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/dummy_snake_case.fsx @@ -0,0 +1,3 @@ +#!/usr/bin/env -S dotnet fsi + +printfn "Hello World!" diff --git a/src/FileConventions.Test/DummyFiles/kebab-case.fsx b/src/FileConventions.Test/DummyFiles/kebab-case.fsx new file mode 100644 index 000000000..23c3e513e --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/kebab-case.fsx @@ -0,0 +1,3 @@ +#!/usr/bin/env -S dotnet fsi + +printfn "Hello World!" diff --git a/src/FileConventions.Test/FileConventions.Test.fs b/src/FileConventions.Test/FileConventions.Test.fs index e32fe7a42..5e9114561 100644 --- a/src/FileConventions.Test/FileConventions.Test.fs +++ b/src/FileConventions.Test/FileConventions.Test.fs @@ -586,3 +586,89 @@ let IsExecutableTest2() = )) Assert.That(IsExecutable fileInfo, Is.EqualTo false) + + +[] +let DetectNotUsingSnakeCaseInScriptName1() = + let fileInfo = + (FileInfo( + Path.Combine(dummyFilesDirectory.FullName, "dummy_snake_case.fsx") + )) + + Assert.That(DetectNotUsingSnakeCaseInScriptName fileInfo, Is.EqualTo false) + + +[] +let DetectNotUsingSnakeCaseInScriptName2() = + let fileInfo = + (FileInfo( + Path.Combine(dummyFilesDirectory.FullName, "DummyPascalCase.fsx") + )) + + Assert.That(DetectNotUsingSnakeCaseInScriptName fileInfo, Is.EqualTo true) + + +[] +let DetectNotUsingSnakeCaseInScriptName3() = + let fileInfo = + (FileInfo(Path.Combine(dummyFilesDirectory.FullName, "kebab-case.fsx"))) + + Assert.That(DetectNotUsingSnakeCaseInScriptName fileInfo, Is.EqualTo true) + + +[] +let DetectNotUsingKebabCaseInGitHubCIJobs1() = + let fileInfo = + (FileInfo( + Path.Combine( + dummyFilesDirectory.FullName, + "DummyCIWithPascalCaseJobName.yml" + ) + )) + + Assert.That(DetectNotUsingKebabCaseInGitHubCIJobs fileInfo, Is.EqualTo true) + + +[] +let DetectNotUsingKebabCaseInGitHubCIJobs2() = + let fileInfo = + (FileInfo( + Path.Combine( + dummyFilesDirectory.FullName, + "DummyCIWithKebabCaseJobName.yml" + ) + )) + + Assert.That( + DetectNotUsingKebabCaseInGitHubCIJobs fileInfo, + Is.EqualTo false + ) + + +[] +let DetectNotUsingKebabCaseInGitHubCIJobs3() = + let fileInfo = + (FileInfo( + Path.Combine( + dummyFilesDirectory.FullName, + "DummyCIWithMultipleJobsAndKebabCaseJobNames.yml" + ) + )) + + Assert.That( + DetectNotUsingKebabCaseInGitHubCIJobs fileInfo, + Is.EqualTo false + ) + + +[] +let DetectNotUsingKebabCaseInGitHubCIJobs4() = + let fileInfo = + (FileInfo( + Path.Combine( + dummyFilesDirectory.FullName, + "DummyCIWithMultipleJobsAndOnePascalCaseJobName.yml" + ) + )) + + Assert.That(DetectNotUsingKebabCaseInGitHubCIJobs fileInfo, Is.EqualTo true) diff --git a/src/FileConventions/FileConventions.fsproj b/src/FileConventions/FileConventions.fsproj index 9c8c6d177..962df27b0 100644 --- a/src/FileConventions/FileConventions.fsproj +++ b/src/FileConventions/FileConventions.fsproj @@ -13,6 +13,7 @@ + diff --git a/src/FileConventions/Library.fs b/src/FileConventions/Library.fs index 6a5ea3748..4dd6e4fda 100644 --- a/src/FileConventions/Library.fs +++ b/src/FileConventions/Library.fs @@ -7,6 +7,8 @@ open System.Text.RegularExpressions open Mono open Mono.Unix.Native +open YamlDotNet +open YamlDotNet.RepresentationModel let HasCorrectShebang(fileInfo: FileInfo) = let fileText = File.ReadLines fileInfo.FullName @@ -390,3 +392,57 @@ let NonVerboseFlags(fileInfo: FileInfo) = let IsExecutable(fileInfo: FileInfo) = let hasExecuteAccess = Syscall.access(fileInfo.FullName, AccessModes.X_OK) hasExecuteAccess = 0 + +let DetectNotUsingSnakeCaseInScriptName(fileInfo: FileInfo) = + let fileName = fileInfo.Name + let snakeCase = fileName.ToLower() = fileName && not(fileName.Contains "-") + not(snakeCase) + +let DetectNotUsingKebabCaseInGitHubCIJobs(fileInfo: FileInfo) = + assert (fileInfo.FullName.EndsWith ".yml") + + 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