Fixes the bug when multiple criteria could be used to increment multiple counters.

After the change semver calculator will proceed to the next commit on the first hit.

Also: Update to GraphQL library
This commit is contained in:
2021-12-11 00:40:31 +00:00
parent d29e51ea14
commit e32b4b9544
5 changed files with 121 additions and 63 deletions
+9 -7
View File
@@ -6,7 +6,7 @@ import (
"os"
"runtime"
gql "github.com/lukaszraczylo/simple-gql-client"
graphql "github.com/lukaszraczylo/go-simple-graphql"
"github.com/melbahja/got"
"github.com/tidwall/gjson"
)
@@ -16,9 +16,10 @@ func updatePackage() bool {
if ghTokenSet {
binaryName := fmt.Sprintf("semver-gen-%s-%s", runtime.GOOS, runtime.GOARCH)
fmt.Println("Downloading", binaryName)
gql.GraphQLUrl = "https://api.github.com/graphql"
gql := graphql.NewConnection()
gql.Endpoint = "https://api.github.com/graphql"
headers := map[string]interface{}{
"Authorization": fmt.Sprintf("bearer %s", ghToken),
"Authorization": fmt.Sprintf("Bearer %s", ghToken),
}
variables := map[string]interface{}{
"binaryName": binaryName,
@@ -78,14 +79,15 @@ func updatePackage() bool {
func checkLatestRelease() (string, bool) {
ghToken, ghTokenSet := os.LookupEnv("GITHUB_TOKEN")
if ghTokenSet {
gql.GraphQLUrl = "https://api.github.com/graphql"
gql := graphql.NewConnection()
gql.Endpoint = "https://api.github.com/graphql"
headers := map[string]interface{}{
"Authorization": fmt.Sprintf("bearer %s", ghToken),
}
variables := map[string]interface{}{}
var query = `query {
repository(name: "semver-generator", owner: "lukaszraczylo") {
releases(last: 1) {
repository(name: "semver-generator", owner: "lukaszraczylo", followRenames: true) {
releases(last: 2) {
nodes {
tag {
name
@@ -96,7 +98,7 @@ func checkLatestRelease() (string, bool) {
}`
result, err := gql.Query(query, variables, headers)
if err != nil {
fmt.Println("Query error", err)
fmt.Println("Query error >>", err)
return "", false
}
result = gjson.Get(result, "repository.releases.nodes.0.tag.name").String()
+10 -5
View File
@@ -30,7 +30,7 @@ import (
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/lithammer/fuzzysearch/fuzzy"
"github.com/lukaszraczylo/zero"
"github.com/lukaszraczylo/pandati"
"github.com/spf13/viper"
)
@@ -85,8 +85,9 @@ type CommitDetails struct {
func checkMatches(content []string, targets []string) bool {
var r []string
for _, tgt := range targets {
r = fuzzy.FindFold(tgt, content)
r = fuzzy.FindNormalizedFold(tgt, content)
if len(r) > 0 {
debugPrint(fmt.Sprintln("Found match for ", tgt, "|", strings.Join(r, ",")))
return true
}
}
@@ -113,12 +114,14 @@ func (s *Setup) CalculateSemver() SemVer {
if matchPatch {
s.Semver.Patch++
debugPrint(fmt.Sprintln("Incrementing patch (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver()))
continue
}
if matchReleaseCandidate {
s.Semver.Release++
s.Semver.Patch = 1
s.Semver.EnableReleaseCandidate = true
debugPrint(fmt.Sprintln("Incrementing release candidate (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver()))
continue
}
if matchMinor {
s.Semver.Minor++
@@ -126,6 +129,7 @@ func (s *Setup) CalculateSemver() SemVer {
s.Semver.EnableReleaseCandidate = false
s.Semver.Release = 0
debugPrint(fmt.Sprintln("Incrementing minor (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver()))
continue
}
if matchMajor {
s.Semver.Major++
@@ -134,6 +138,7 @@ func (s *Setup) CalculateSemver() SemVer {
s.Semver.EnableReleaseCandidate = false
s.Semver.Release = 0
debugPrint(fmt.Sprintln("Incrementing major (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver()))
continue
}
}
return s.Semver
@@ -208,15 +213,15 @@ func (s *Setup) Prepare() error {
}
func (s *Setup) ForcedVersioning() {
if !zero.IsZero(s.Force.Major) {
if !pandati.IsZero(s.Force.Major) {
debugPrint(fmt.Sprintln("Forced versioning (MAJOR)", s.Force.Major))
s.Semver.Major = s.Force.Major
}
if !zero.IsZero(s.Force.Minor) {
if !pandati.IsZero(s.Force.Minor) {
debugPrint(fmt.Sprintln("Forced versioning (MINOR)", s.Force.Minor))
s.Semver.Minor = s.Force.Minor
}
if !zero.IsZero(s.Force.Patch) {
if !pandati.IsZero(s.Force.Patch) {
debugPrint(fmt.Sprintln("Forced versioning (PATCH)", s.Force.Patch))
s.Semver.Patch = s.Force.Patch
}
+3 -3
View File
@@ -21,7 +21,7 @@ import (
"testing"
git "github.com/go-git/go-git/v5"
"github.com/lukaszraczylo/zero"
"github.com/lukaszraczylo/pandati"
assertions "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
@@ -242,7 +242,7 @@ func (suite *Tests) TestSetup_ReadConfig() {
} else {
assert.Error(err, "Error should be present in "+tt.name)
}
assert.Equal(tt.wordingEmpty, zero.IsZero(s.Wording), "Unexpected wording count "+tt.name+":", s.Wording)
assert.Equal(tt.wordingEmpty, pandati.IsZero(s.Wording), "Unexpected wording count "+tt.name+":", s.Wording)
})
}
}
@@ -341,7 +341,7 @@ func (suite *Tests) TestSetup_ListCommits() {
} else {
assert.Error(err, "Error should be present in "+tt.name)
}
assert.Equal(tt.noCommits, zero.IsZero(listOfCommits), "Unexpected commits count"+tt.name)
assert.Equal(tt.noCommits, pandati.IsZero(listOfCommits), "Unexpected commits count"+tt.name)
})
}
}