Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examine env. var contents in GithubCI files for version consistency check #177

Merged
Merged
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
1 change: 1 addition & 0 deletions scripts/eofConvention.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ open System.IO
open System

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"
#r "nuget: YamlDotNet, Version=16.1.3"

#load "../src/FileConventions/Helpers.fs"
#load "../src/FileConventions/Library.fs"
Expand Down
1 change: 1 addition & 0 deletions scripts/executableConvention.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -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=16.1.3"
#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
1 change: 1 addition & 0 deletions scripts/inconsistentVersionsInFSharpScripts.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ open System.IO
open System.Linq

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"
#r "nuget: YamlDotNet, Version=16.1.3"

#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"
Expand Down
1 change: 1 addition & 0 deletions scripts/inconsistentVersionsInGitHubCI.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"
#r "nuget: YamlDotNet, Version=16.1.3"

#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"
Expand Down
1 change: 1 addition & 0 deletions scripts/mixedLineEndings.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -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=16.1.3"

#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"
Expand Down
1 change: 1 addition & 0 deletions scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -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=16.1.3"

#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"
Expand Down
1 change: 1 addition & 0 deletions scripts/shebangConvention.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -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=16.1.3"

#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"
Expand Down
1 change: 1 addition & 0 deletions scripts/unpinnedDotnetToolInstallVersions.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -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=16.1.3"

#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"
Expand Down
1 change: 1 addition & 0 deletions scripts/unpinnedGitHubActionsImageVersions.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -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=16.1.3"

#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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=16.1.3"

#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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=16.1.3"

#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"
Expand Down
1 change: 1 addition & 0 deletions scripts/wrapLatestCommitMsg.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ open System.Text.RegularExpressions
open System.Linq

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"
#r "nuget: YamlDotNet, Version=16.1.3"

#load "../src/FileConventions/Library.fs"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: CI

on: [push, pull_request]

env:
PULUMI_VERSION: 2.0.0

jobs:
jobA:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v2
- name: Setup Pulumi CLI
with:
pulumi-version: ${{ PULUMI_VERSION }}
- name: Print "Hello World!"
run: echo "Hello World!"
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: CI

on: [push, pull_request]

env:
PULUMI_VERSION: 2.0.1

jobs:
jobA:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v2
- name: Setup Pulumi CLI
with:
pulumi-version: ${{ PULUMI_VERSION }}
- name: Print "Hello World!"
run: echo "Hello World!"
8 changes: 8 additions & 0 deletions src/FileConventions.Test/FileConventions.Test.fs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,14 @@ let DetectInconsistentVersionsInGitHubCI1() =

Assert.That(DetectInconsistentVersionsInGitHubCI fileInfo, Is.EqualTo true)

[<Test>]
let DetectInconsistentVersionsInGitHubCI2() =
let fileInfo =
DirectoryInfo(
Path.Combine(dummyFilesDirectory.FullName, "DummyWorkflowsWithEnv")
)

Assert.That(DetectInconsistentVersionsInGitHubCI fileInfo, Is.EqualTo true)

[<Test>]
let DetectInconsistentVersionsInNugetRefsInFSharpScripts1() =
Expand Down
1 change: 1 addition & 0 deletions src/FileConventions/FileConventions.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<ItemGroup>
<PackageReference Include="Mono.Posix" Version="7.1.0-final.1.21458.1" />
<PackageReference Include="Mono.Unix" Version="7.1.0-final.1.21458.1" />
<PackageReference Include="YamlDotNet" Version="16.1.3" />
</ItemGroup>

</Project>
105 changes: 95 additions & 10 deletions src/FileConventions/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ open System.Text.RegularExpressions

open Mono
open Mono.Unix.Native
open YamlDotNet.RepresentationModel

let SplitByEOLs (text: string) (numberOfEOLs: uint) =
if numberOfEOLs = 0u then
Expand Down Expand Up @@ -300,21 +301,105 @@ let private DetectInconsistentVersions
|> Seq.map(fun item -> Seq.length item.Value > 1)
|> Seq.contains true

let private DetectInconsistentVersionsInYamlFiles
(fileInfos: seq<FileInfo>)
(extractVersionsFunction: YamlNode -> seq<string * string>)
=
let envVarRegex =
Regex(@"\s*\$\{\{\s*([^\s\}]+)\s*\}\}\s*", RegexOptions.Compiled)

let yamlDocuments =
Seq.map
(fun (fileInfo: FileInfo) ->
let yaml = YamlStream()
use reader = new StreamReader(fileInfo.FullName)
yaml.Load reader
yaml.Documents[0].RootNode
)
fileInfos

let versionMap =
Seq.fold
(fun mapping (yamlDoc: YamlNode) ->
let matches =
Seq.collect extractVersionsFunction yamlDoc.AllNodes

matches
|> Seq.fold
(fun acc (key, value) ->
let actualValue =
let variableRegexMatch = envVarRegex.Match value

if variableRegexMatch.Success then
let yamlDict = yamlDoc :?> YamlMappingNode

let envDict =
yamlDict.Children.["env"]
:?> YamlMappingNode

let envVarName =
variableRegexMatch.Groups.[1].Value

match envDict.Children.TryGetValue envVarName
with
| true, envVarValue ->
(envVarValue :?> YamlScalarNode).Value
| false, _ ->
failwithf "env. var %s not found" envVarName
else
value

match Map.tryFind key acc with
| Some prevSet ->
Map.add key (Set.add actualValue prevSet) acc
| None -> Map.add key (Set.singleton actualValue) acc
)
mapping
)
Map.empty
yamlDocuments

versionMap
|> Seq.map(fun item -> Seq.length item.Value > 1)
|> Seq.contains true

let DetectInconsistentVersionsInGitHubCIWorkflow(fileInfos: seq<FileInfo>) =
fileInfos
|> Seq.iter(fun fileInfo -> assert (fileInfo.FullName.EndsWith ".yml"))

let inconsistentVersionsType1 =
DetectInconsistentVersions
fileInfos
"\\swith:\\s*([^\\s]*)-version:\\s*([^\\s]*)\\s"

let inconsistentVersionsType2 =
DetectInconsistentVersions
fileInfos
"\\suses:\\s*([^\\s]*)@v([^\\s]*)\\s"
let extractVersions(node: YamlNode) =
match node with
| :? YamlMappingNode as yamlDict ->
yamlDict.Children
|> Seq.collect(fun (KeyValue(keyNode, valueNode)) ->
match keyNode, valueNode with
| (:? YamlScalarNode as keyScalar),
(:? YamlScalarNode as valueScalar) when
keyScalar.Value = "uses"
->
match valueScalar.Value.Split "@v" with
| [| name; version |] -> Seq.singleton(name, version)
| _ -> Seq.empty
| (:? YamlScalarNode as keyScalar),
(:? YamlMappingNode as valueMapping) when
keyScalar.Value = "with"
->
valueMapping.Children
|> Seq.choose(fun (KeyValue(innerKeyNode, innerValueNode)) ->
match innerKeyNode, innerValueNode with
| (:? YamlScalarNode as keyScalar),
(:? YamlScalarNode as valueScalar) ->
match keyScalar.Value.Split '-' with
| [| name; "version" |] ->
Some(name, valueScalar.Value)
| _ -> None
| _ -> None
)
| _ -> Seq.empty
)
| _ -> Seq.empty

inconsistentVersionsType1 || inconsistentVersionsType2
DetectInconsistentVersionsInYamlFiles fileInfos extractVersions

let DetectInconsistentVersionsInGitHubCI(dir: DirectoryInfo) =
let ymlFiles = dir.GetFiles("*.yml", SearchOption.AllDirectories)
Expand Down