Skip to content
Open
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
25 changes: 24 additions & 1 deletion scripts/inconsistentVersionsInGitHubCI.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,30 @@ let targetDir =
|> fst

let inconsistentVersionsInGitHubCI =
FileConventions.DetectInconsistentVersionsInGitHubCI targetDir
let repositoryRootDir = targetDir.Parent.Parent

let globalEnv =
let dotEnvFile =
Path.Join(repositoryRootDir.FullName, ".env") |> FileInfo

if dotEnvFile.Exists then
let lines = File.ReadAllLines dotEnvFile.FullName

lines
|> Seq.filter(fun line ->
not(
System.String.IsNullOrWhiteSpace line || line.StartsWith '#'
)
)
|> Seq.map(fun line ->
let [| key; value |] = line.Split('=', count = 2)
key.Trim(), value.Trim()
)
|> Map.ofSeq
else
Map.empty

FileConventions.DetectInconsistentVersionsInGitHubCI targetDir globalEnv

if inconsistentVersionsInGitHubCI then
failwith
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: CI
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@webwarrior-ws given that the tests themselves depend on files present inside this DummyFiles/ folder, then I'd prefer to include the env vars in an .env file inside this same folder. Also, let's rename this file to include "DotEnv" in the name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functionality of parsing .env file is only in the .fsx script, not in Library.fs. Also that file is expected to be ../../ relative to target dir (.github/workflows).


on: [push, pull_request]

jobs:
jobA:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v2
- name: Setup Pulumi CLI
uses: pulumi/[email protected]
with:
pulumi-version: ${{ env.PULUMI_VERSION }}
- name: Print "Hello World!"
run: echo "Hello World!"
jobB:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v2
- name: Setup Pulumi CLI
uses: pulumi/[email protected]
with:
pulumi-version: 3.40.0
- name: Print "Hello World!"
run: echo "Hello World!"
48 changes: 38 additions & 10 deletions src/FileConventions.Test/FileConventions.Test.fs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ let DetectInconsistentVersionsInGitHubCIWorkflow1() =
))

Assert.That(
DetectInconsistentVersionsInGitHubCIWorkflow fileInfo,
DetectInconsistentVersionsInGitHubCIWorkflow fileInfo Map.empty,
Is.EqualTo false
)

Expand All @@ -307,7 +307,7 @@ let DetectInconsistentVersionsInGitHubCIWorkflow2() =
))

Assert.That(
DetectInconsistentVersionsInGitHubCIWorkflow fileInfo,
DetectInconsistentVersionsInGitHubCIWorkflow fileInfo Map.empty,
Is.EqualTo true
)

Expand All @@ -325,7 +325,7 @@ let DetectInconsistentVersionsInGitHubCIWorkflow3() =
))

Assert.That(
DetectInconsistentVersionsInGitHubCIWorkflow fileInfo,
DetectInconsistentVersionsInGitHubCIWorkflow fileInfo Map.empty,
Is.EqualTo true
)

Expand All @@ -343,7 +343,7 @@ let DetectInconsistentVersionsInGitHubCIWorkflow4() =
))

Assert.That(
DetectInconsistentVersionsInGitHubCIWorkflow fileInfo,
DetectInconsistentVersionsInGitHubCIWorkflow fileInfo Map.empty,
Is.EqualTo false
)

Expand All @@ -370,7 +370,7 @@ let DetectInconsistentVersionsInGitHubCIWorkflow5() =
})

Assert.That(
DetectInconsistentVersionsInGitHubCIWorkflow fileInfo,
DetectInconsistentVersionsInGitHubCIWorkflow fileInfo Map.empty,
Is.EqualTo true
)

Expand All @@ -388,7 +388,7 @@ let DetectInconsistentVersionsInGitHubCIWorkflow6() =
))

Assert.That(
DetectInconsistentVersionsInGitHubCIWorkflow fileInfo,
DetectInconsistentVersionsInGitHubCIWorkflow fileInfo Map.empty,
Is.EqualTo true
)

Expand All @@ -406,10 +406,29 @@ let DetectInconsistentVersionsInGitHubCIWorkflow7() =
))

Assert.That(
DetectInconsistentVersionsInGitHubCIWorkflow fileInfo,
DetectInconsistentVersionsInGitHubCIWorkflow fileInfo Map.empty,
Is.EqualTo true
)

[<Test>]
let DetectInconsistentVersionsInGitHubCIWorkflow8() =
let fileInfo =
(Seq.singleton(
FileInfo(
Path.Combine(
dummyFilesDirectory.FullName,
"DummyCIWithSameSetupPulumiVersionInEnv.yml"
)
)
))

let envDict = Map.ofList [ "PULUMI_VERSION", "3.40.0" ]

Assert.That(
DetectInconsistentVersionsInGitHubCIWorkflow fileInfo envDict,
Is.EqualTo false
)


[<Test>]
let DetectInconsistentVersionsInGitHubCI1() =
Expand All @@ -418,7 +437,10 @@ let DetectInconsistentVersionsInGitHubCI1() =
Path.Combine(dummyFilesDirectory.FullName, "DummyWorkflows")
)

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

[<Test>]
let DetectInconsistentVersionsInGitHubCI2() =
Expand All @@ -427,7 +449,10 @@ let DetectInconsistentVersionsInGitHubCI2() =
Path.Combine(dummyFilesDirectory.FullName, "DummyWorkflowsWithEnv")
)

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

[<Test>]
let DetectInconsistentVersionsInGitHubCI3() =
Expand All @@ -439,7 +464,10 @@ let DetectInconsistentVersionsInGitHubCI3() =
)
)

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

[<Test>]
let DetectInconsistentVersionsInNugetRefsInFSharpScripts1() =
Expand Down
1 change: 1 addition & 0 deletions src/FileConventions.Test/FileConventions.Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@
<None Include="DummyFiles\DummyCIWithSameSetupPulumiVersion.yml" />
<None Include="DummyFiles\DummyCIWithSetupPulumiVersionV2.0.0.yml" />
<None Include="DummyFiles\DummyCIWithSetupPulumiVersionV2.0.1.yml" />
<None Include="DummyFiles\DummyCIWithSameSetupPulumiVersionInEnv.yml" />
</ItemGroup>
</Project>
66 changes: 42 additions & 24 deletions src/FileConventions/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ let private GetVersionsMapFromFiles
let private DetectInconsistentVersionsInYamlFiles
(fileInfos: seq<FileInfo>)
(extractVersionsFunction: YamlNode -> seq<string * string>)
(globalEnv: Map<string, string>)
=
let envVarRegex =
Regex(@"\s*\$\{\{\s*([^\s\}]+)\s*\}\}\s*", RegexOptions.Compiled)
Expand All @@ -382,33 +383,44 @@ let private DetectInconsistentVersionsInYamlFiles
let matches =
Seq.collect extractVersionsFunction yamlDoc.AllNodes

let yamlDict = yamlDoc :?> YamlMappingNode

let localEnv =
match yamlDict.Children.TryGetValue "env" with
| true, (:? YamlMappingNode as node) -> node
| _ -> YamlMappingNode()

let envDict =
localEnv.Children
|> Seq.fold
(fun acc pair ->
acc
|> Map.add
(pair.Key :?> YamlScalarNode).Value
(pair.Value :?> YamlScalarNode).Value
)
globalEnv

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

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

match yamlDict.Children.TryGetValue "env" with
| true, (:? YamlMappingNode as envDict) ->
let referenceString =
variableRegexMatch.Groups.[1].Value

let envVarName =
if referenceString.StartsWith "env." then
referenceString.[4..]
else
referenceString

match
envDict.Children.TryGetValue envVarName
with
| true, envVarValue ->
(envVarValue :?> YamlScalarNode).Value
| false, _ -> value
| _ -> value
let referenceString =
variableRegexMatch.Groups.[1].Value

let envVarName =
if referenceString.StartsWith "env." then
referenceString.[4..]
else
referenceString

match envDict.TryGetValue envVarName with
| true, envVarValue -> envVarValue
| false, _ ->
failwithf "env. var %s not found" envVarName
else
value

Expand All @@ -426,7 +438,10 @@ let private DetectInconsistentVersionsInYamlFiles
|> Seq.map(fun item -> Seq.length item.Value > 1)
|> Seq.contains true

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

Expand Down Expand Up @@ -462,15 +477,18 @@ let DetectInconsistentVersionsInGitHubCIWorkflow(fileInfos: seq<FileInfo>) =
)
| _ -> Seq.empty

DetectInconsistentVersionsInYamlFiles fileInfos extractVersions
DetectInconsistentVersionsInYamlFiles fileInfos extractVersions globalEnv

let DetectInconsistentVersionsInGitHubCI(dir: DirectoryInfo) =
let DetectInconsistentVersionsInGitHubCI
(dir: DirectoryInfo)
(globalEnv: Map<string, string>)
=
let ymlFiles = dir.GetFiles("*.yml", SearchOption.AllDirectories)

if Seq.isEmpty ymlFiles then
false
else
DetectInconsistentVersionsInGitHubCIWorkflow ymlFiles
DetectInconsistentVersionsInGitHubCIWorkflow ymlFiles globalEnv

let GetVersionsMapForNugetRefsInFSharpScripts(fileInfos: seq<FileInfo>) =
fileInfos
Expand Down