mirror of
https://github.com/lukaszraczylo/semver-generator.git
synced 2026-06-11 23:29:30 +00:00
Update dependencies.
This commit is contained in:
+9
-8
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/lukaszraczylo/ask"
|
||||
graphql "github.com/lukaszraczylo/go-simple-graphql"
|
||||
libpack_logger "github.com/lukaszraczylo/graphql-monitoring-proxy/logging"
|
||||
"github.com/melbahja/got"
|
||||
)
|
||||
|
||||
@@ -15,7 +16,7 @@ func updatePackage() bool {
|
||||
ghToken, ghTokenSet := os.LookupEnv("GITHUB_TOKEN")
|
||||
if ghTokenSet {
|
||||
binaryName := fmt.Sprintf("semver-gen-%s-%s", runtime.GOOS, runtime.GOARCH)
|
||||
logger.Info("semver-gen", map[string]interface{}{"message": "Checking for updates"})
|
||||
logger.Info(&libpack_logger.LogMessage{Message: "Checking for updates", Pairs: map[string]interface{}{"binaryName": binaryName}})
|
||||
gql := graphql.NewConnection()
|
||||
|
||||
gql.SetEndpoint("https://api.github.com/graphql")
|
||||
@@ -43,13 +44,13 @@ func updatePackage() bool {
|
||||
}`
|
||||
result, err := gql.Query(query, variables, headers)
|
||||
if err != nil {
|
||||
logger.Error("semver-gen", map[string]interface{}{"error": err.Error(), "message": "Unable to query GitHub API"})
|
||||
logger.Error(&libpack_logger.LogMessage{Message: "Unable to query GitHub API", Pairs: map[string]interface{}{"error": err.Error()}})
|
||||
return false
|
||||
}
|
||||
|
||||
output, ok := ask.For(result, "repository.latestRelease.releaseAssets.edges[0].node.downloadUrl").String("")
|
||||
if !ok {
|
||||
logger.Error("semver-gen", map[string]interface{}{"error": "Unable to obtain download url for the binary", "binary": binaryName, "output": output})
|
||||
logger.Error(&libpack_logger.LogMessage{Message: "Unable to obtain download url for the binary", Pairs: map[string]interface{}{"binary": binaryName, "output": output}})
|
||||
return false
|
||||
}
|
||||
if flag.Lookup("test.v") == nil && os.Getenv("CI") == "" {
|
||||
@@ -57,22 +58,22 @@ func updatePackage() bool {
|
||||
g := got.New()
|
||||
err = g.Download(output, downloadedBinaryPath)
|
||||
if err != nil {
|
||||
logger.Error("semver-gen", map[string]interface{}{"error": err.Error(), "message": "Unable to download binary", "binaryPath": downloadedBinaryPath})
|
||||
logger.Error(&libpack_logger.LogMessage{Message: "Unable to download binary", Pairs: map[string]interface{}{"error": err.Error(), "binaryPath": downloadedBinaryPath}})
|
||||
return false
|
||||
}
|
||||
currentBinary, err := os.Executable()
|
||||
if err != nil {
|
||||
logger.Error("semver-gen", map[string]interface{}{"error": err.Error(), "message": "Unable to obtain current binary path"})
|
||||
logger.Error(&libpack_logger.LogMessage{Message: "Unable to obtain current binary path", Pairs: map[string]interface{}{"error": err.Error()}})
|
||||
return false
|
||||
}
|
||||
err = os.Rename(downloadedBinaryPath, currentBinary)
|
||||
if err != nil {
|
||||
logger.Error("semver-gen", map[string]interface{}{"error": err.Error(), "message": "Unable to overwrite current binary"})
|
||||
logger.Error(&libpack_logger.LogMessage{Message: "Unable to overwrite current binary", Pairs: map[string]interface{}{"error": err.Error()}})
|
||||
return false
|
||||
}
|
||||
err = os.Chmod(currentBinary, 0777)
|
||||
if err != nil {
|
||||
logger.Error("semver-gen", map[string]interface{}{"error": err.Error(), "message": "Unable to make binary executable"})
|
||||
logger.Error(&libpack_logger.LogMessage{Message: "Unable to make binary executable", Pairs: map[string]interface{}{"error": err.Error()}})
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -102,7 +103,7 @@ func checkLatestRelease() (string, bool) {
|
||||
}`
|
||||
result, err := gql.Query(query, variables, headers)
|
||||
if err != nil {
|
||||
logger.Error("semver-gen", map[string]interface{}{"error": err.Error(), "message": "Unable to query GitHub API"})
|
||||
logger.Error(&libpack_logger.LogMessage{Message: "Unable to query GitHub API", Pairs: map[string]interface{}{"error": err.Error()}})
|
||||
return "", false
|
||||
}
|
||||
output, _ := ask.For(result, "repository.releases.nodes[0].tag.name").String("")
|
||||
|
||||
+2
-2
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func Test_checkLatestRelease(t *testing.T) {
|
||||
logger = libpack_logging.NewLogger()
|
||||
logger = libpack_logging.New()
|
||||
tests := []struct {
|
||||
name string
|
||||
want string
|
||||
@@ -29,7 +29,7 @@ func Test_checkLatestRelease(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_updatePackage(t *testing.T) {
|
||||
logger = libpack_logging.NewLogger()
|
||||
logger = libpack_logging.New()
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping test in short / CI mode")
|
||||
}
|
||||
|
||||
+101
-27
@@ -41,7 +41,7 @@ var (
|
||||
err error
|
||||
repo *Setup
|
||||
PKG_VERSION string
|
||||
logger *libpack_logger.LogConfig
|
||||
logger *libpack_logger.Logger
|
||||
)
|
||||
|
||||
type Wording struct {
|
||||
@@ -97,14 +97,20 @@ type TagDetails struct {
|
||||
|
||||
func checkMatches(content []string, targets []string) bool {
|
||||
if fuzzy.MatchNormalizedFold(strings.Join(content, " "), "Merge branch") {
|
||||
logger.Debug("semver-gen", map[string]interface{}{"message": "Merge detected, ignoring commits within", "content": strings.Join(content, " ")})
|
||||
logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Merge detected, ignoring commits within",
|
||||
Pairs: map[string]interface{}{"content": strings.Join(content, " ")},
|
||||
})
|
||||
return false
|
||||
}
|
||||
var r []string
|
||||
for _, tgt := range targets {
|
||||
r = fuzzy.FindNormalizedFold(tgt, content)
|
||||
if len(r) > 0 {
|
||||
logger.Debug("semver-gen", map[string]interface{}{"message": "Found match", "target": tgt, "match": strings.Join(r, ","), "content": strings.Join(content, " ")})
|
||||
logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Found match",
|
||||
Pairs: map[string]interface{}{"target": tgt, "match": strings.Join(r, ","), "content": strings.Join(content, " ")},
|
||||
})
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -114,11 +120,17 @@ func checkMatches(content []string, targets []string) bool {
|
||||
var extractNumber = regexp.MustCompile("[0-9]+")
|
||||
|
||||
func parseExistingSemver(tagName string, currentSemver SemVer) (semanticVersion SemVer) {
|
||||
logger.Debug("semver-gen", map[string]interface{}{"message": "Parsing existing semver", "tag": tagName})
|
||||
logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Parsing existing semver",
|
||||
Pairs: map[string]interface{}{"tag": tagName},
|
||||
})
|
||||
var tagNameParts []string
|
||||
tagNameParts = strings.Split(tagName, ".")
|
||||
if len(tagNameParts) < 3 {
|
||||
logger.Debug("semver-gen", map[string]interface{}{"message": "Unable to parse incompatible semver ( non x.y.z )"})
|
||||
logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Unable to parse incompatible semver ( non x.y.z )",
|
||||
Pairs: map[string]interface{}{"tag": tagName},
|
||||
})
|
||||
return currentSemver
|
||||
}
|
||||
semanticVersion.Major, _ = strconv.Atoi(extractNumber.FindAllString(tagNameParts[0], -1)[0])
|
||||
@@ -136,7 +148,10 @@ func (s *Setup) CalculateSemver() SemVer {
|
||||
if params.varExisting || s.Force.Existing {
|
||||
for _, tagHash := range s.Tags {
|
||||
if commit.Hash == tagHash.Hash {
|
||||
logger.Debug("semver-gen", map[string]interface{}{"message": "Found existing tag", "tag": tagHash.Name, "commit": strings.TrimSuffix(commit.Message, "\n")})
|
||||
logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Found existing tag",
|
||||
Pairs: map[string]interface{}{"tag": tagHash.Name, "commit": strings.TrimSuffix(commit.Message, "\n")},
|
||||
})
|
||||
s.Semver = parseExistingSemver(tagHash.Name, s.Semver)
|
||||
continue
|
||||
}
|
||||
@@ -145,7 +160,10 @@ func (s *Setup) CalculateSemver() SemVer {
|
||||
|
||||
if !params.varStrict && !s.Force.Strict {
|
||||
s.Semver.Patch++
|
||||
logger.Debug("semver-gen", map[string]interface{}{"message": "Incrementing patch (DEFAULT)", "commit": strings.TrimSuffix(commit.Message, "\n"), "semver": s.getSemver()})
|
||||
logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Incrementing patch (DEFAULT)",
|
||||
Pairs: map[string]interface{}{"commit": strings.TrimSuffix(commit.Message, "\n"), "semver": s.getSemver()},
|
||||
})
|
||||
}
|
||||
commitSlice := strings.Fields(commit.Message)
|
||||
matchPatch := checkMatches(commitSlice, s.Wording.Patch)
|
||||
@@ -154,14 +172,20 @@ func (s *Setup) CalculateSemver() SemVer {
|
||||
matchReleaseCandidate := checkMatches(commitSlice, s.Wording.Release)
|
||||
if matchPatch {
|
||||
s.Semver.Patch++
|
||||
logger.Debug("semver-gen", map[string]interface{}{"message": "Incrementing patch (WORDING)", "commit": strings.TrimSuffix(commit.Message, "\n"), "semver": s.getSemver()})
|
||||
logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Incrementing patch (WORDING)",
|
||||
Pairs: map[string]interface{}{"commit": strings.TrimSuffix(commit.Message, "\n"), "semver": s.getSemver()},
|
||||
})
|
||||
continue
|
||||
}
|
||||
if matchReleaseCandidate {
|
||||
s.Semver.Release++
|
||||
s.Semver.Patch = 1
|
||||
s.Semver.EnableReleaseCandidate = true
|
||||
logger.Debug("semver-gen", map[string]interface{}{"message": "Incrementing release candidate (WORDING)", "commit": strings.TrimSuffix(commit.Message, "\n"), "semver": s.getSemver()})
|
||||
logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Incrementing release candidate (WORDING)",
|
||||
Pairs: map[string]interface{}{"commit": strings.TrimSuffix(commit.Message, "\n"), "semver": s.getSemver()},
|
||||
})
|
||||
continue
|
||||
}
|
||||
if matchMinor {
|
||||
@@ -169,7 +193,10 @@ func (s *Setup) CalculateSemver() SemVer {
|
||||
s.Semver.Patch = 1
|
||||
s.Semver.EnableReleaseCandidate = false
|
||||
s.Semver.Release = 0
|
||||
logger.Debug("semver-gen", map[string]interface{}{"message": "Incrementing minor (WORDING)", "commit": strings.TrimSuffix(commit.Message, "\n"), "semver": s.getSemver()})
|
||||
logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Incrementing minor (WORDING)",
|
||||
Pairs: map[string]interface{}{"commit": strings.TrimSuffix(commit.Message, "\n"), "semver": s.getSemver()},
|
||||
})
|
||||
continue
|
||||
}
|
||||
if matchMajor {
|
||||
@@ -178,7 +205,10 @@ func (s *Setup) CalculateSemver() SemVer {
|
||||
s.Semver.Patch = 1
|
||||
s.Semver.EnableReleaseCandidate = false
|
||||
s.Semver.Release = 0
|
||||
logger.Debug("semver-gen", map[string]interface{}{"message": "Incrementing major (WORDING)", "commit": strings.TrimSuffix(commit.Message, "\n"), "semver": s.getSemver()})
|
||||
logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Incrementing major (WORDING)",
|
||||
Pairs: map[string]interface{}{"commit": strings.TrimSuffix(commit.Message, "\n"), "semver": s.getSemver()},
|
||||
})
|
||||
continue
|
||||
}
|
||||
}
|
||||
@@ -186,14 +216,19 @@ func (s *Setup) CalculateSemver() SemVer {
|
||||
}
|
||||
|
||||
func (s *Setup) ListExistingTags() {
|
||||
logger.Debug("semver-gen", map[string]interface{}{"message": "Listing existing tags"})
|
||||
logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Listing existing tags",
|
||||
})
|
||||
refs, err := s.RepositoryHandler.Tags()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := refs.ForEach(func(ref *plumbing.Reference) error {
|
||||
s.Tags = append(s.Tags, TagDetails{Name: ref.Name().Short(), Hash: ref.Hash().String()})
|
||||
logger.Debug("semver-gen", map[string]interface{}{"message": "Found tag", "tag": ref.Name().Short(), "hash": ref.Hash().String()})
|
||||
logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Found tag",
|
||||
Pairs: map[string]interface{}{"tag": ref.Name().Short(), "hash": ref.Hash().String()},
|
||||
})
|
||||
return nil
|
||||
}); err != nil {
|
||||
panic(err)
|
||||
@@ -220,10 +255,16 @@ func (s *Setup) ListCommits() ([]CommitDetails, error) {
|
||||
return nil
|
||||
})
|
||||
|
||||
logger.Debug("semver-gen", map[string]interface{}{"message": "Commits before cut", "commits": tmpResults})
|
||||
logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Listing commits",
|
||||
Pairs: map[string]interface{}{"commits": tmpResults},
|
||||
})
|
||||
for commitId, cmt := range tmpResults {
|
||||
if s.Force.Commit != "" && cmt.Hash == s.Force.Commit {
|
||||
logger.Debug("semver-gen", map[string]interface{}{"message": "Found commit match", "commit": cmt.Hash, "index": commitId})
|
||||
logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Found commit match",
|
||||
Pairs: map[string]interface{}{"commit": cmt.Hash, "index": commitId},
|
||||
})
|
||||
s.Commits = tmpResults[commitId:]
|
||||
break
|
||||
} else {
|
||||
@@ -231,7 +272,10 @@ func (s *Setup) ListCommits() ([]CommitDetails, error) {
|
||||
}
|
||||
}
|
||||
|
||||
logger.Debug("semver-gen", map[string]interface{}{"message": "Commits after cut", "commits": s.Commits})
|
||||
logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Commits after cut",
|
||||
Pairs: map[string]interface{}{"commits": s.Commits},
|
||||
})
|
||||
return s.Commits, err
|
||||
}
|
||||
|
||||
@@ -239,7 +283,10 @@ func (s *Setup) Prepare() error {
|
||||
if !repo.UseLocal {
|
||||
u, err := url.Parse(s.RepositoryName)
|
||||
if err != nil {
|
||||
logger.Error("semver-gen", map[string]interface{}{"message": "Unable to parse repository URL", "error": err.Error(), "url": s.RepositoryName})
|
||||
logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "Unable to parse repository URL",
|
||||
Pairs: map[string]interface{}{"error": err.Error(), "url": s.RepositoryName},
|
||||
})
|
||||
return err
|
||||
}
|
||||
s.RepositoryLocalPath = fmt.Sprintf("/tmp/semver/%s/%s", u.Path, s.RepositoryBranch)
|
||||
@@ -255,14 +302,20 @@ func (s *Setup) Prepare() error {
|
||||
Tags: git.AllTags,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("semver-gen", map[string]interface{}{"message": "Unable to clone repository", "error": err.Error(), "url": s.RepositoryName})
|
||||
logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "Unable to clone repository",
|
||||
Pairs: map[string]interface{}{"error": err.Error(), "url": s.RepositoryName},
|
||||
})
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
s.RepositoryLocalPath = "./"
|
||||
s.RepositoryHandler, err = git.PlainOpen(s.RepositoryLocalPath)
|
||||
if err != nil {
|
||||
logger.Error("semver-gen", map[string]interface{}{"message": "Unable to open local repository", "error": err.Error(), "path": s.RepositoryLocalPath})
|
||||
logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "Unable to open local repository",
|
||||
Pairs: map[string]interface{}{"error": err.Error(), "path": s.RepositoryLocalPath},
|
||||
})
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -272,15 +325,24 @@ func (s *Setup) Prepare() error {
|
||||
|
||||
func (s *Setup) ForcedVersioning() {
|
||||
if !pandati.IsZero(s.Force.Major) {
|
||||
logger.Debug("semver-gen", map[string]interface{}{"message": "Forced versioning (MAJOR)", "major": s.Force.Major})
|
||||
logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Forced versioning (MAJOR)",
|
||||
Pairs: map[string]interface{}{"major": s.Force.Major},
|
||||
})
|
||||
s.Semver.Major = s.Force.Major
|
||||
}
|
||||
if !pandati.IsZero(s.Force.Minor) {
|
||||
logger.Debug("semver-gen", map[string]interface{}{"message": "Forced versioning (MINOR)", "minor": s.Force.Minor})
|
||||
logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Forced versioning (MINOR)",
|
||||
Pairs: map[string]interface{}{"minor": s.Force.Minor},
|
||||
})
|
||||
s.Semver.Minor = s.Force.Minor
|
||||
}
|
||||
if !pandati.IsZero(s.Force.Patch) {
|
||||
logger.Debug("semver-gen", map[string]interface{}{"message": "Forced versioning (PATCH)", "patch": s.Force.Patch})
|
||||
logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Forced versioning (PATCH)",
|
||||
Pairs: map[string]interface{}{"patch": s.Force.Minor},
|
||||
})
|
||||
s.Semver.Patch = s.Force.Patch
|
||||
}
|
||||
}
|
||||
@@ -306,16 +368,22 @@ func (s *Setup) getSemver() (semverReturned string) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
logger = libpack_logger.NewLogger()
|
||||
logger = libpack_logger.New()
|
||||
if params.varShowVersion {
|
||||
var outdatedMsg string
|
||||
latestRelease, latestRelaseOk := checkLatestRelease()
|
||||
if PKG_VERSION != latestRelease && latestRelaseOk {
|
||||
outdatedMsg = fmt.Sprintf("(Latest available: %s)", latestRelease)
|
||||
}
|
||||
logger.Info("semver-gen", map[string]interface{}{"version": PKG_VERSION, "outdated": outdatedMsg})
|
||||
logger.Info(&libpack_logger.LogMessage{
|
||||
Message: "semver-gen",
|
||||
Pairs: map[string]interface{}{"version": PKG_VERSION, "outdated": outdatedMsg},
|
||||
})
|
||||
if outdatedMsg != "" {
|
||||
logger.Info("semver-gen", map[string]interface{}{"message": "You can update automatically with: semver-gen -u"})
|
||||
logger.Info(&libpack_logger.LogMessage{
|
||||
Message: "semver-gen",
|
||||
Pairs: map[string]interface{}{"message": "You can update automatically with: semver-gen -u"},
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -326,11 +394,17 @@ func main() {
|
||||
if repo.Generate || params.varGenerateInTest {
|
||||
err := repo.ReadConfig(repo.LocalConfigFile)
|
||||
if err != nil {
|
||||
logger.Error("semver-gen", map[string]interface{}{"message": "Unable to find config file semver.yaml. Using defaults and flags.", "file": repo.LocalConfigFile})
|
||||
logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "Unable to find config file semver.yaml. Using defaults and flags.",
|
||||
Pairs: map[string]interface{}{"file": repo.LocalConfigFile},
|
||||
})
|
||||
}
|
||||
err = repo.Prepare()
|
||||
if err != nil {
|
||||
logger.Critical("semver-gen", map[string]interface{}{"message": "Unable to prepare repository", "error": err.Error()})
|
||||
logger.Critical(&libpack_logger.LogMessage{
|
||||
Message: "Unable to prepare repository",
|
||||
Pairs: map[string]interface{}{"error": err.Error()},
|
||||
})
|
||||
}
|
||||
repo.ListCommits()
|
||||
if params.varExisting || repo.Force.Existing {
|
||||
|
||||
+2
-2
@@ -39,7 +39,7 @@ var (
|
||||
func (suite *Tests) SetupTest() {
|
||||
err := os.Chdir(testCurrentPath)
|
||||
if err != nil {
|
||||
logger.Critical("main", map[string]interface{}{"error": err.Error(), "message": "Unable to change directory to test directory"})
|
||||
logger.Critical(&libpack_logging.LogMessage{Message: "Unable to change directory to test directory", Pairs: map[string]any{"error": err}})
|
||||
}
|
||||
assert = assertions.New(suite.T())
|
||||
params.varDebug = true
|
||||
@@ -47,7 +47,7 @@ func (suite *Tests) SetupTest() {
|
||||
}
|
||||
|
||||
func TestSuite(t *testing.T) {
|
||||
logger = libpack_logging.NewLogger()
|
||||
logger = libpack_logging.New()
|
||||
testCurrentPath, _ = os.Getwd()
|
||||
suite.Run(t, new(Tests))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user