Skip to content

Commit

Permalink
Update dependencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszraczylo committed Jul 23, 2024
1 parent 3d75e16 commit f51cb01
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 97 deletions.
17 changes: 9 additions & 8 deletions cmd/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ 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"
)

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")
Expand Down Expand Up @@ -43,36 +44,36 @@ 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") == "" {
downloadedBinaryPath := fmt.Sprintf("/tmp/%s", binaryName)
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
}
}
Expand Down Expand Up @@ -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("")
Expand Down
4 changes: 2 additions & 2 deletions cmd/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
Expand Down
128 changes: 101 additions & 27 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var (
err error
repo *Setup
PKG_VERSION string
logger *libpack_logger.LogConfig
logger *libpack_logger.Logger
)

type Wording struct {
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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])
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -154,22 +172,31 @@ 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 {
s.Semver.Minor++
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 {
Expand All @@ -178,22 +205,30 @@ 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
}
}
return s.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)
Expand All @@ -220,26 +255,38 @@ 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 {
s.Commits = tmpResults
}
}

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
}

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)
Expand All @@ -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
}
}
Expand All @@ -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
}
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ 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
params.varRepoBranch = "main"
}

func TestSuite(t *testing.T) {
logger = libpack_logging.NewLogger()
logger = libpack_logging.New()
testCurrentPath, _ = os.Getwd()
suite.Run(t, new(Tests))
}
Expand Down
Loading

0 comments on commit f51cb01

Please sign in to comment.