-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into add-e2e-workflow-subworkflow
- Loading branch information
Showing
44 changed files
with
2,429 additions
and
1,508 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
testfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.go text eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ./... |
Oops, something went wrong.