Skip to content

Commit

Permalink
Merge pull request #52 from lukaszraczylo/additional-improvements
Browse files Browse the repository at this point in the history
additional improvements
  • Loading branch information
lukaszraczylo authored Feb 25, 2025
2 parents 4a9615e + 3a528b8 commit a9dfd9e
Show file tree
Hide file tree
Showing 24 changed files with 2,110 additions and 824 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Project created overnight, to prove that management of semantic versioning is NO
### Important changes

* From version `1.4.2+` as pointed out in [issue #12](https://github.com/lukaszraczylo/semver-generator/issues/12) commits from merge will not be included in the calculations and commits themselves will bump the version on first match ( starting checks from `patch` upwards ).
* Added support for blacklisting terms to ignore specific commits, branch names, and merge messages from version calculations.

### Usage

Expand Down Expand Up @@ -172,6 +173,11 @@ force:
minor: 0
patch: 1
commit: 69fbe2df696f40281b9104ff073d26186cde1024
blacklist:
- "Merge branch"
- "Merge pull request"
- "feature/"
- "feature:"
wording:
patch:
- update
Expand All @@ -190,10 +196,11 @@ wording:
* `version`: is not respected at the moment, introduced for potential backwards compatibility in future
* `force`: sets the "starting" version, you don't need to specify this section as the default is always `0`
* `force.commit`: allows you to set commit hash from which the calculations should start
* `blacklist`: terms to ignore when processing commits. Any commit containing these terms will be skipped in version calculations. Useful for ignoring merge commits, feature branch names, and other unwanted triggers.
* `wording`: words the program should look for in the git commits to increment (patch|minor|major)

### Good to know

* Word matching uses fuzzy search AND is case INSENSITIVE
* I do not recommend using common words ( like "the" from the example configuration )
* You can specify env variable `LOG_LEVEL=debug` to see what exactly happens during the calculations
* You can specify env variable `LOG_LEVEL=debug` to see what exactly happens during the calculations
2 changes: 1 addition & 1 deletion cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand Down
113 changes: 6 additions & 107 deletions cmd/github.go
Original file line number Diff line number Diff line change
@@ -1,117 +1,16 @@
package cmd

import (
"flag"
"fmt"
"os"
"runtime"

"github.com/lukaszraczylo/ask"
graphql "github.com/lukaszraczylo/go-simple-graphql"
libpack_logger "github.com/lukaszraczylo/graphql-monitoring-proxy/logging"
"github.com/melbahja/got"
"github.com/lukaszraczylo/semver-generator/cmd/utils"
)

func updatePackage() bool {
ghToken, ghTokenSet := os.LookupEnv("GITHUB_TOKEN")
if ghTokenSet {
binaryName := fmt.Sprintf("semver-gen-%s-%s", runtime.GOOS, runtime.GOARCH)
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")
gql.SetOutput("mapstring")
// These functions are now in the utils package
// They are kept here as stubs for backward compatibility

headers := map[string]interface{}{
"Authorization": fmt.Sprintf("Bearer %s", ghToken),
}
variables := map[string]interface{}{
"binaryName": binaryName,
}
var query = `query ($binaryName: String) {
repository(name: "semver-generator", owner: "lukaszraczylo") {
latestRelease {
releaseAssets(first: 10, name: $binaryName) {
edges {
node {
name
downloadUrl
}
}
}
}
}
}`
result, err := gql.Query(query, variables, headers)
if err != nil {
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(&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(&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(&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(&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(&libpack_logger.LogMessage{Message: "Unable to make binary executable", Pairs: map[string]interface{}{"error": err.Error()}})
return false
}
}
}
return true
func updatePackage() bool {
return utils.UpdatePackage()
}

func checkLatestRelease() (string, bool) {
ghToken, ghTokenSet := os.LookupEnv("GITHUB_TOKEN")
if ghTokenSet {
gql := graphql.NewConnection()
gql.SetEndpoint("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", followRenames: true) {
releases(last: 2) {
nodes {
tag {
name
}
}
}
}
}`
result, err := gql.Query(query, variables, headers)
if err != nil {
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("")
if output == "v1" {
output, _ = ask.For(result, "repository.releases.nodes[1].tag.name").String("")
}
return output, true
} else {
return "[no GITHUB_TOKEN set]", false
}
return utils.CheckLatestRelease()
}
6 changes: 3 additions & 3 deletions cmd/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package cmd
import (
"testing"

libpack_logging "github.com/lukaszraczylo/graphql-monitoring-proxy/logging"
"github.com/lukaszraczylo/semver-generator/cmd/utils"
)

func Test_checkLatestRelease(t *testing.T) {
logger = libpack_logging.New()
utils.InitLogger(true)
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.New()
utils.InitLogger(true)
if testing.Short() {
t.Skip("Skipping test in short / CI mode")
}
Expand Down
Loading

0 comments on commit a9dfd9e

Please sign in to comment.