General improvements

* Fix calculations as per https://github.com/lukaszraczylo/semver-generator/issues/8
* Change name of default config file as per https://github.com/lukaszraczylo/semver-generator/issues/2
* Add access to private repositories as per https://github.com/lukaszraczylo/semver-generator/issues/3
* Update documentation
This commit is contained in:
2021-05-11 20:57:10 +01:00
parent ee2ab9fa0c
commit b8a15788ee
6 changed files with 73 additions and 52 deletions
+14 -3
View File
@@ -7,6 +7,7 @@ Project created overnight, to prove that management of semantic versioning is NO
- [Semantic version generator](#semantic-version-generator) - [Semantic version generator](#semantic-version-generator)
- [How does it work](#how-does-it-work) - [How does it work](#how-does-it-work)
- [Usage](#usage) - [Usage](#usage)
- [Authentication](#authentication)
- [As a binary](#as-a-binary) - [As a binary](#as-a-binary)
- [As a github action](#as-a-github-action) - [As a github action](#as-a-github-action)
- [As a docker container](#as-a-docker-container) - [As a docker container](#as-a-docker-container)
@@ -22,6 +23,16 @@ Project created overnight, to prove that management of semantic versioning is NO
### Usage ### Usage
#### Authentication
If you intend to use this project with remote repositories ( regardless of them being private or public ) you need to authenticate with your repository.
To do so you can utilise the following environment variables ( they are NOT github specific, for other providers you can use the password )
```bash
export GITHUB_USERNAME=lukaszraczylo
export GITHUB_TOKEN=yourPersonalApiToken
```
#### As a binary #### As a binary
You can download latest versions of the binaries from the [release page](https://github.com/lukaszraczylo/semver-generator/releases/latest). You can download latest versions of the binaries from the [release page](https://github.com/lukaszraczylo/semver-generator/releases/latest).
@@ -48,7 +59,7 @@ Available Commands:
help Help about any command help Help about any command
Flags: Flags:
-c, --config string Path to config file (default "config.yaml") -c, --config string Path to config file (default "semver.yaml")
-d, --debug Enable debug mode -d, --debug Enable debug mode
-h, --help help for semver-gen -h, --help help for semver-gen
-l, --local Use local repository -l, --local Use local repository
@@ -72,9 +83,9 @@ jobs:
fetch-depth: '0' fetch-depth: '0'
- name: Semver run - name: Semver run
id: semver id: semver
uses: lukaszraczylo/semver-generator@1.0.44 uses: lukaszraczylo/semver-generator@PLACE_LATEST_TAG_HERE
with: with:
config_file: config.yaml config_file: semver.yaml
# either... # either...
repository_local: true repository_local: true
# or... # or...
+26 -23
View File
@@ -27,6 +27,7 @@ import (
git "github.com/go-git/go-git/v5" git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object" "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/lithammer/fuzzysearch/fuzzy"
"github.com/lukaszraczylo/zero" "github.com/lukaszraczylo/zero"
"github.com/spf13/viper" "github.com/spf13/viper"
@@ -81,40 +82,43 @@ func checkMatches(content []string, targets []string) bool {
var r []string var r []string
for _, tgt := range targets { for _, tgt := range targets {
r = fuzzy.FindFold(tgt, content) r = fuzzy.FindFold(tgt, content)
if len(r) > 0 {
return true
}
} }
return (len(r) > 0) return false
} }
func (s *Setup) CalculateSemver() SemVer { func (s *Setup) CalculateSemver() SemVer {
for _, commit := range s.Commits { for _, commit := range s.Commits {
s.Semver.Patch++ s.Semver.Patch++
if varDebug { if varDebug {
fmt.Println("DEBUG: Incrementing patch (DEFAULT) on ", commit.Message) fmt.Println("DEBUG: Incrementing patch (DEFAULT) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())
} }
commitSlice := strings.Split(commit.Message, " ") commitSlice := strings.Fields(commit.Message)
matchPatch := checkMatches(commitSlice, s.Wording.Patch) matchPatch := checkMatches(commitSlice, s.Wording.Patch)
matchMinor := checkMatches(commitSlice, s.Wording.Minor) matchMinor := checkMatches(commitSlice, s.Wording.Minor)
matchMajor := checkMatches(commitSlice, s.Wording.Major) matchMajor := checkMatches(commitSlice, s.Wording.Major)
if matchPatch { if matchPatch {
if varDebug {
fmt.Println("DEBUG: Incrementing patch (WORDING) on ", commit.Message)
}
s.Semver.Patch++ s.Semver.Patch++
if varDebug {
fmt.Println("DEBUG: Incrementing patch (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())
}
} }
if matchMinor { if matchMinor {
if varDebug {
fmt.Println("DEBUG: Incrementing minor (WORDING) on ", commit.Message)
}
s.Semver.Minor++ s.Semver.Minor++
s.Semver.Patch = 1 s.Semver.Patch = 1
if varDebug {
fmt.Println("DEBUG: Incrementing minor (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())
}
} }
if matchMajor { if matchMajor {
if varDebug {
fmt.Println("DEBUG: Incrementing major (WORDING) on ", commit.Message)
}
s.Semver.Major++ s.Semver.Major++
s.Semver.Minor = 0 s.Semver.Minor = 0
s.Semver.Patch = 1 s.Semver.Patch = 1
if varDebug {
fmt.Println("DEBUG: Incrementing major (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())
}
} }
} }
return s.Semver return s.Semver
@@ -124,14 +128,7 @@ func (s *Setup) ListCommits() ([]CommitDetails, error) {
var ref *plumbing.Reference var ref *plumbing.Reference
var err error var err error
// if zero.IsZero(s.Force.Commit) {
ref, err = s.RepositoryHandler.Head() ref, err = s.RepositoryHandler.Head()
// } else {
// if varDebug {
// fmt.Println("DEBUG: Forced start from commit", s.Force.Commit)
// }
// ref = plumbing.NewHashReference("start_commit", plumbing.NewHash(s.Force.Commit))
// }
if err != nil { if err != nil {
return []CommitDetails{}, err return []CommitDetails{}, err
} }
@@ -170,25 +167,31 @@ func (s *Setup) Prepare() error {
if !repo.UseLocal { if !repo.UseLocal {
u, err := url.Parse(s.RepositoryName) u, err := url.Parse(s.RepositoryName)
if err != nil { if err != nil {
fmt.Println("Unable to parse repository URL", err.Error()) fmt.Println("Unable to parse repository URL", s.RepositoryName, "Error:", err.Error())
return err return err
} }
s.RepositoryLocalPath = fmt.Sprintf("/tmp/foo/%s", u.Path) s.RepositoryLocalPath = fmt.Sprintf("/tmp/semver/%s", u.Path)
os.RemoveAll(s.RepositoryLocalPath) os.RemoveAll(s.RepositoryLocalPath)
s.RepositoryHandler, err = git.PlainClone(s.RepositoryLocalPath, false, &git.CloneOptions{ s.RepositoryHandler, err = git.PlainClone(s.RepositoryLocalPath, false, &git.CloneOptions{
URL: s.RepositoryName, URL: s.RepositoryName,
Auth: &http.BasicAuth{
Username: os.Getenv("GITHUB_USERNAME"),
Password: os.Getenv("GITHUB_TOKEN"),
},
}) })
if err != nil { if err != nil {
fmt.Println("Unable to reach repository", err.Error()) fmt.Println("Unable to reach repository", s.RepositoryName, "Error:", err.Error())
return err return err
} }
} else { } else {
s.RepositoryLocalPath = "./"
s.RepositoryHandler, err = git.PlainOpen(s.RepositoryLocalPath) s.RepositoryHandler, err = git.PlainOpen(s.RepositoryLocalPath)
if err != nil { if err != nil {
fmt.Println("Unable to reach repository", err.Error()) fmt.Println("Unable to reach repository", s.RepositoryName, "Error:", err.Error())
return err return err
} }
} }
os.Chdir(s.RepositoryLocalPath)
return err return err
} }
+32 -23
View File
@@ -31,14 +31,17 @@ type Tests struct {
} }
var ( var (
assert *assertions.Assertions assert *assertions.Assertions
testCurrentPath string
) )
func (suite *Tests) SetupTest() { func (suite *Tests) SetupTest() {
os.Chdir(testCurrentPath)
assert = assertions.New(suite.T()) assert = assertions.New(suite.T())
} }
func TestSuite(t *testing.T) { func TestSuite(t *testing.T) {
testCurrentPath, _ = os.Getwd()
suite.Run(t, new(Tests)) suite.Run(t, new(Tests))
} }
@@ -257,6 +260,7 @@ func (suite *Tests) TestSetup_ListCommits() {
RepositoryName string RepositoryName string
RepositoryLocalPath string RepositoryLocalPath string
RepositoryHandler *git.Repository RepositoryHandler *git.Repository
LocalConfigFile string
Commits []CommitDetails Commits []CommitDetails
Semver SemVer Semver SemVer
Wording Wording Wording Wording
@@ -299,10 +303,10 @@ func (suite *Tests) TestSetup_ListCommits() {
} }
for _, tt := range tests { for _, tt := range tests {
suite.T().Run(tt.name, func(t *testing.T) { suite.T().Run(tt.name, func(t *testing.T) {
s := &Setup{ s := &Setup{}
RepositoryName: tt.fields.RepositoryName, s.ReadConfig(tt.fields.LocalConfigFile)
Force: tt.fields.Force, s.RepositoryName = tt.fields.RepositoryName
} s.Force = tt.fields.Force
s.Prepare() s.Prepare()
listOfCommits, err := s.ListCommits() listOfCommits, err := s.ListCommits()
if !tt.wantErr { if !tt.wantErr {
@@ -317,8 +321,9 @@ func (suite *Tests) TestSetup_ListCommits() {
func (suite *Tests) TestSetup_CalculateSemver() { func (suite *Tests) TestSetup_CalculateSemver() {
type fields struct { type fields struct {
RepositoryName string RepositoryName string
Force Force Force Force
LocalConfigFile string
} }
type wantSemver struct { type wantSemver struct {
Major int Major int
@@ -333,50 +338,54 @@ func (suite *Tests) TestSetup_CalculateSemver() {
{ {
name: "Test on existing repository", name: "Test on existing repository",
fields: fields{ fields: fields{
RepositoryName: "https://github.com/lukaszraczylo/simple-gql-client", RepositoryName: "https://github.com/lukaszraczylo/semver-generator-test-repo",
LocalConfigFile: "meta.yaml",
Force: Force{
Commit: "",
},
}, },
wantSemver: wantSemver{ wantSemver: wantSemver{
Major: 1, Major: 0,
Minor: 3, Minor: 0,
Patch: 1, Patch: 7,
}, },
}, },
{ {
name: "Test on existing repository, starting with certain hash", name: "Test on existing repository, starting with certain hash",
fields: fields{ fields: fields{
RepositoryName: "https://github.com/lukaszraczylo/simple-gql-client", RepositoryName: "https://github.com/lukaszraczylo/semver-generator-test-repo",
LocalConfigFile: "meta.yaml",
Force: Force{ Force: Force{
Commit: "97d3682ed94168600926f9ff6da650403d1f3317", Commit: "45f9a23cec39e94503841638aee3efecd45111cf",
}, },
}, },
wantSemver: wantSemver{ wantSemver: wantSemver{
Major: 1, Major: 2,
Minor: 3, Minor: 4,
Patch: 1, Patch: 1,
}, },
}, },
{ {
name: "Test on non-existing repository", name: "Test on non-existing repository",
fields: fields{ fields: fields{
RepositoryName: "https://github.com/lukaszraczylo/simple-gql-client-dead", RepositoryName: "https://github.com/lukaszraczylo/semver-generator-test-repo-dead",
}, },
wantSemver: wantSemver{ wantSemver: wantSemver{
Major: 1, // 1 because config file enforces MAJOR version Major: 1, // 1 because config file enforces MAJOR version
Minor: 0, Minor: 1, // 1 because config file enforces MINOR version
Patch: 0, Patch: 0,
}, },
}, },
} }
for _, tt := range tests { for _, tt := range tests {
suite.T().Run(tt.name, func(t *testing.T) { suite.T().Run(tt.name, func(t *testing.T) {
s := &Setup{ s := &Setup{}
RepositoryName: tt.fields.RepositoryName, s.ReadConfig(tt.fields.LocalConfigFile)
Force: tt.fields.Force, s.RepositoryName = tt.fields.RepositoryName
}
s.ReadConfig("../config.yaml")
s.Prepare() s.Prepare()
s.ListCommits()
s.ForcedVersioning() s.ForcedVersioning()
s.Force = tt.fields.Force
s.ListCommits()
semver := s.CalculateSemver() semver := s.CalculateSemver()
assert.Equal(tt.wantSemver.Major, semver.Major, "Unexpected MAJOR semver result in "+tt.name) assert.Equal(tt.wantSemver.Major, semver.Major, "Unexpected MAJOR semver result in "+tt.name)
assert.Equal(tt.wantSemver.Minor, semver.Minor, "Unexpected MINOR semver result in "+tt.name) assert.Equal(tt.wantSemver.Minor, semver.Minor, "Unexpected MINOR semver result in "+tt.name)
+1 -1
View File
@@ -66,7 +66,7 @@ func init() {
if !strings.HasSuffix(os.Args[0], ".test") { if !strings.HasSuffix(os.Args[0], ".test") {
cobra.OnInitialize(repo.setupCobra) cobra.OnInitialize(repo.setupCobra)
rootCmd.PersistentFlags().StringVarP(&varRepoName, "repository", "r", "https://github.com/lukaszraczylo/simple-gql-client", "Remote repository URL.") rootCmd.PersistentFlags().StringVarP(&varRepoName, "repository", "r", "https://github.com/lukaszraczylo/simple-gql-client", "Remote repository URL.")
rootCmd.PersistentFlags().StringVarP(&varLocalCfg, "config", "c", "config.yaml", "Path to config file") rootCmd.PersistentFlags().StringVarP(&varLocalCfg, "config", "c", "semver.yaml", "Path to config file")
rootCmd.PersistentFlags().BoolVarP(&varUseLocal, "local", "l", false, "Use local repository") rootCmd.PersistentFlags().BoolVarP(&varUseLocal, "local", "l", false, "Use local repository")
rootCmd.PersistentFlags().BoolVarP(&varShowVersion, "version", "v", false, "Display version") rootCmd.PersistentFlags().BoolVarP(&varShowVersion, "version", "v", false, "Display version")
rootCmd.PersistentFlags().BoolVarP(&varDebug, "debug", "d", false, "Enable debug mode") rootCmd.PersistentFlags().BoolVarP(&varDebug, "debug", "d", false, "Enable debug mode")
-1
View File
@@ -7,7 +7,6 @@ wording:
- initial - initial
- fix - fix
minor: minor:
- add
- change - change
- improve - improve
major: major:
-1
View File
@@ -1,7 +1,6 @@
version: 1 version: 1
force: force:
major: 1 major: 1
commit: 97d3682ed94168600926f9ff6da650403d1f3317
wording: wording:
patch: patch:
- update - update