Skip to content

Commit

Permalink
Merge branch 'master' into add-e2e-workflow-subworkflow
Browse files Browse the repository at this point in the history
  • Loading branch information
MregXN authored Nov 28, 2023
2 parents 4dbd4c2 + 2807ca5 commit b11b164
Show file tree
Hide file tree
Showing 44 changed files with 2,429 additions and 1,508 deletions.
111 changes: 111 additions & 0 deletions .build-tools/cmd/check-lint-version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package cmd

import (
"errors"
"fmt"
"os"
"os/exec"
"regexp"

"github.com/spf13/cobra"
"golang.org/x/mod/semver"
"gopkg.in/yaml.v3"
)

var (
ErrVersionNotSupported = errors.New("version not supported")
ErrVersionNotFound = errors.New("version not found")
)

type GHWorkflow struct {
Jobs struct {
Lint struct {
Env struct {
GOVER string `yaml:"GOVER"`
GOLANGCILINTVER string `yaml:"GOLANGCILINT_VER"`
} `yaml:"env"`
} `yaml:"lint"`
} `yaml:"jobs"`
}

func parseWorkflowVersionFromFile(path string) (string, error) {
var ghWorkflow GHWorkflow

raw, err := os.ReadFile(path)
if err != nil {
return "", err
}
err = yaml.Unmarshal(raw, &ghWorkflow)
if err != nil {
return "", err
}
return ghWorkflow.Jobs.Lint.Env.GOLANGCILINTVER, err
}

func getCurrentVersion() (string, error) {
out, err := exec.Command("golangci-lint", "--version").Output()
if err != nil {
return "", err
}

regex, err := regexp.Compile(`golangci-lint\shas\sversion\sv?([\d+.]+[\d])`)
if err != nil {
return "", err
}

matches := regex.FindStringSubmatch(string(out))

if matches == nil {
return "", fmt.Errorf("no version found: %v", string(out))
}
return fmt.Sprintf("v%s", matches[1]), err
}

func isVersionValid(workflowVersion, currentVersion string) bool {
res := semver.MajorMinor(workflowVersion) == semver.MajorMinor(currentVersion)
return res
}

func compareVersions(path string) (string, error) {
workflowVersion, err := parseWorkflowVersionFromFile(path)
if err != nil {
return fmt.Sprintf("Error parsing workflow version: %v", err), ErrVersionNotFound
}
currentVersion, err := getCurrentVersion()
if err != nil {
return fmt.Sprintf("Error getting current version: %v", err), ErrVersionNotFound
}
validVersion := isVersionValid(workflowVersion, currentVersion)
if !validVersion {
return fmt.Sprintf("Invalid version, expected: %s, current: %s ", workflowVersion, currentVersion), ErrVersionNotSupported
}
return fmt.Sprintf("Linter version is valid (MajorMinor): %s", currentVersion), nil
}

func getCmdCheckLint(cmdType string) *cobra.Command {
// Base command
cmd := &cobra.Command{
Use: cmdType,
Short: "Compare local golangci-lint version against workflow version",
Run: func(cmd *cobra.Command, args []string) {
path := cmd.Flag("path").Value.String()
res, err := compareVersions(path)
fmt.Println(res)
if err != nil {
fmt.Println("Please install the correct version using the guide - https://golangci-lint.run/usage/install/")
if err == ErrVersionNotSupported {
fmt.Println("Alternatively review the golangci-lint version in the workflow file at .github/workflows/dapr.yml")
}
os.Exit(1)
}
},
}
cmd.PersistentFlags().String("path", "../.github/workflows/dapr.yml", "Path to workflow file")
return cmd
}

func init() {
// checkLintCmd represents the checkLint command
checkLintCmd := getCmdCheckLint("check-linter")
rootCmd.AddCommand(checkLintCmd)
}
91 changes: 91 additions & 0 deletions .build-tools/cmd/check-lint-version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package cmd

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseWorkflow(t *testing.T) {
t.Run("parse invalid workflow file", func(t *testing.T) {
parsedVersion, err := parseWorkflowVersionFromFile("../testdata/check-lint-version/invalid.yml")
assert.Equal(t, "", parsedVersion)
assert.Error(t, err)
})

t.Run("parse workflow file with a missing key", func(t *testing.T) {
parsedVersion, err := parseWorkflowVersionFromFile("../testdata/check-lint-version/invalid-test.yml")
assert.Equal(t, "", parsedVersion)
assert.NoError(t, err)
})

t.Run("parse an invalid workflow file", func(t *testing.T) {
parsedVersion, err := parseWorkflowVersionFromFile("../testdata/check-lint-version/invalid-yaml.yml")
assert.Equal(t, "", parsedVersion)
assert.Error(t, err)
})

t.Run("parse testing workflow file", func(t *testing.T) {
parsedVersion, err := parseWorkflowVersionFromFile("../testdata/check-lint-version/valid-test.yml")
assert.Equal(t, "123.123.123", parsedVersion)
assert.NoError(t, err)
})
}

func TestGetCurrentVersion(t *testing.T) {
t.Run("get current version from system", func(t *testing.T) {
currentVersion, err := getCurrentVersion()
assert.Equal(t, "v1.51.2", currentVersion)
assert.NoError(t, err)
})

// TODO: test failure to detect current version

// TODO: test failure to compile regex expression

// TODO: test failure finding matches
}

func TestIsVersionValid(t *testing.T) {
t.Run("compare versions - exactly equal to", func(t *testing.T) {
assert.Equal(t, true, isVersionValid("v1.54.2", "v1.54.2"))
})

t.Run("compare versions - patch version greater (workflow)", func(t *testing.T) {
assert.Equal(t, true, isVersionValid("v1.54.3", "v1.54.2"))
})

t.Run("compare versions - patch version greater (installed)", func(t *testing.T) {
assert.Equal(t, true, isVersionValid("v1.54.2", "v1.54.3"))
})

t.Run("compare versions - invalid (installed)", func(t *testing.T) {
assert.Equal(t, false, isVersionValid("v1.54.2", "v1.52.2"))
})

t.Run("compare versions - invalid (workflow)", func(t *testing.T) {
assert.Equal(t, false, isVersionValid("v1.52.2", "v1.54.2"))
})
}

func TestCompareVersions(t *testing.T) {
t.Run("Valid comparison", func(t *testing.T) {
res, err := compareVersions("../../.github/workflows/dapr.yml")
assert.Contains(t, res, "Linter version is valid")
assert.NoError(t, err)
})

t.Run("Invalid comparison", func(t *testing.T) {
res, err := compareVersions("../testdata/check-lint-version/invalid-test.yml")
assert.Contains(t, res, "Invalid version")
assert.Error(t, err)
})

// TODO: test function for failure to get the current version using getCurrentVersion()

t.Run("Invalid path for comparison", func(t *testing.T) {
res, err := compareVersions("../testdata/check-lint-version/invalid-test-incorrect-path.yml")
assert.Contains(t, res, "Error parsing workflow")
assert.Error(t, err)
})
}
6 changes: 6 additions & 0 deletions .build-tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@ require (
github.com/google/go-containerregistry v0.11.1-0.20220802162123-c1f9836a4fa9
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.7.0
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/containerd/stargz-snapshotter/estargz v0.12.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/cli v20.10.17+incompatible // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/docker v20.10.17+incompatible // indirect
github.com/docker/docker-credential-helpers v0.6.4 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.15.8 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.3-0.20220729202839-6ad7100eb087 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/vbatts/tar-split v0.11.2 // indirect
Expand Down
11 changes: 11 additions & 0 deletions .build-tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ github.com/containerd/stargz-snapshotter/estargz v0.12.0 h1:idtwRTLjk2erqiYhPWy2
github.com/containerd/stargz-snapshotter/estargz v0.12.0/go.mod h1:AIQ59TewBFJ4GOPEQXujcrJ/EKxh5xXZegW1rkR1P/M=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/danieljoos/wincred v1.1.0/go.mod h1:XYlo+eRTsVA9aHGp7NGjFkPla4m+DCL7hqDjlFjiygg=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -23,16 +24,23 @@ github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLf
github.com/klauspost/compress v1.15.7/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/klauspost/compress v1.15.8 h1:JahtItbkWjf2jzm/T+qgMxkP9EMHsqEUA6vCMGmXvhA=
github.com/klauspost/compress v1.15.8/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.0.3-0.20220729202839-6ad7100eb087 h1:vm7/Jb0eH7oibgUngG/ljkvHBxF+mHlekCvVFyLGOc8=
github.com/opencontainers/image-spec v1.0.3-0.20220729202839-6ad7100eb087/go.mod h1:K/JAU0m27RFhDRX4PcFdIKntROP6y5Ed6O91aZYDQfs=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand All @@ -59,6 +67,8 @@ github.com/vbatts/tar-split v0.11.2/go.mod h1:vV3ZuO2yWSVsz+pfFzDG/upWH1JhjOiEaW
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
Expand All @@ -68,6 +78,7 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
12 changes: 12 additions & 0 deletions .build-tools/testdata/check-lint-version/invalid-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Test

on:
push:
pull_request:
branches:
- main

jobs:
build:
env:
NOGOLANGCILINT_VER: "123.123.123"
1 change: 1 addition & 0 deletions .build-tools/testdata/check-lint-version/invalid-yaml.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testfile
12 changes: 12 additions & 0 deletions .build-tools/testdata/check-lint-version/valid-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Test

on:
push:
pull_request:
branches:
- main

jobs:
lint:
env:
GOLANGCILINT_VER: "123.123.123"
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.go text eol=lf
49 changes: 49 additions & 0 deletions .github/workflows/test-tooling.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Test Tooling

on:
push:
paths: # Explicitly declare which paths
- ".github/workflows/test-tooling.yml"
- ".build-tools/*"
pull_request:
branches:
- master
paths: # Explicitly declare which paths
- ".github/workflows/test-tooling.yml"
- ".build-tools/*"

jobs:
lint:
name: Test (${{ matrix.os}})

strategy:
fail-fast: false
matrix:
os:
- "ubuntu-latest"
- "windows-latest"
- "macos-latest"
runs-on: ${{ matrix.os }}
env:
GOLANGCILINT_VER: "v1.51.2" # Make sure to bump /.build-tools/check-lint-version/main_test.go

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup
uses: actions/setup-go@v4
with:
go-version-file: './.build-tools/go.mod'


- name: Tidy
working-directory: ./.build-tools
run: go mod tidy

- name: Install Linter
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "$(go env GOPATH)/bin" ${{ env.GOLANGCILINT_VER }}

- name: Test
working-directory: ./.build-tools
run: go test ./...
Loading

0 comments on commit b11b164

Please sign in to comment.