Skip to content

Commit

Permalink
fix: use commit hash when version is v0.0.0 (#5479)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Feb 27, 2025
1 parent c01a04b commit f69da39
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 17 deletions.
57 changes: 41 additions & 16 deletions pkg/commands/config_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,33 +70,58 @@ func createSchemaURL(flags *pflag.FlagSet, buildInfo BuildInfo) (string, error)
return "", fmt.Errorf("parse version: %w", err)
}

schemaURL = fmt.Sprintf("https://golangci-lint.run/jsonschema/golangci.v%d.%d.jsonschema.json",
version.Segments()[0], version.Segments()[1])
if version.Core().Equal(hcversion.Must(hcversion.NewVersion("v0.0.0"))) {
commit, err := extractCommitHash(buildInfo)
if err != nil {
return "", err
}

case buildInfo.Commit != "" && buildInfo.Commit != "?":
if buildInfo.Commit == "unknown" {
return "", errors.New("unknown commit information")
return fmt.Sprintf("https://raw.githubusercontent.com/golangci/golangci-lint/%s/jsonschema/golangci.next.jsonschema.json",
commit), nil
}

commit := buildInfo.Commit
return fmt.Sprintf("https://golangci-lint.run/jsonschema/golangci.v%d.%d.jsonschema.json",
version.Segments()[0], version.Segments()[1]), nil

if strings.HasPrefix(commit, "(") {
c, _, ok := strings.Cut(strings.TrimPrefix(commit, "("), ",")
if !ok {
return "", errors.New("commit information not found")
}

commit = c
case buildInfo.Commit != "" && buildInfo.Commit != "?":
commit, err := extractCommitHash(buildInfo)
if err != nil {
return "", err
}

schemaURL = fmt.Sprintf("https://raw.githubusercontent.com/golangci/golangci-lint/%s/jsonschema/golangci.next.jsonschema.json",
commit)
return fmt.Sprintf("https://raw.githubusercontent.com/golangci/golangci-lint/%s/jsonschema/golangci.next.jsonschema.json",
commit), nil

default:
return "", errors.New("version not found")
}
}

func extractCommitHash(buildInfo BuildInfo) (string, error) {
if buildInfo.Commit == "" || buildInfo.Commit == "?" {
return "", errors.New("empty commit information")
}

if buildInfo.Commit == "unknown" {
return "", errors.New("unknown commit information")
}

commit := buildInfo.Commit

if strings.HasPrefix(commit, "(") {
c, _, ok := strings.Cut(strings.TrimPrefix(commit, "("), ",")
if !ok {
return "", errors.New("commit information not found")
}

commit = c
}

if commit == "unknown" {
return "", errors.New("unknown commit information")
}

return schemaURL, nil
return commit, nil
}

func validateConfiguration(schemaPath, targetFile string) error {
Expand Down
26 changes: 25 additions & 1 deletion pkg/commands/config_verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ func Test_createSchemaURL(t *testing.T) {
},
expected: "https://raw.githubusercontent.com/golangci/golangci-lint/cd8b11773c6c1f595e8eb98c0d4310af20ae20df/jsonschema/golangci.next.jsonschema.json",
},
{
desc: "v0 version",
info: BuildInfo{
Version: "v0.0.0-20250213211019-0a603e49e5e9",
Commit: `(0a603e49e5e9870f5f9f2035bcbe42cd9620a9d5, modified: "false", mod sum: "123")`,
},
expected: "https://raw.githubusercontent.com/golangci/golangci-lint/0a603e49e5e9870f5f9f2035bcbe42cd9620a9d5/jsonschema/golangci.next.jsonschema.json",
},
{
desc: "dirty",
info: BuildInfo{
Version: "v1.64.6-0.20250225205237-3eecab1ebde9+dirty",
Commit: `(3eecab1ebde9, modified: "false", mod sum: "123")`,
},
expected: "https://golangci-lint.run/jsonschema/golangci.v1.64.jsonschema.json",
},
}

for _, test := range testCases {
Expand Down Expand Up @@ -87,12 +103,20 @@ func Test_createSchemaURL_error(t *testing.T) {
expected string
}{
{
desc: "commit unknown",
desc: "unknown commit",
info: BuildInfo{
Commit: "unknown",
},
expected: "unknown commit information",
},
{
desc: "detailed unknown commit",
info: BuildInfo{
Version: "",
Commit: `(unknown, modified: ?, mod sum: "")`,
},
expected: "unknown commit information",
},
{
desc: "commit ?",
info: BuildInfo{
Expand Down

0 comments on commit f69da39

Please sign in to comment.