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
+26 -23
View File
@@ -27,6 +27,7 @@ import (
git "github.com/go-git/go-git/v5"
"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/transport/http"
"github.com/lithammer/fuzzysearch/fuzzy"
"github.com/lukaszraczylo/zero"
"github.com/spf13/viper"
@@ -81,40 +82,43 @@ func checkMatches(content []string, targets []string) bool {
var r []string
for _, tgt := range targets {
r = fuzzy.FindFold(tgt, content)
if len(r) > 0 {
return true
}
}
return (len(r) > 0)
return false
}
func (s *Setup) CalculateSemver() SemVer {
for _, commit := range s.Commits {
s.Semver.Patch++
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)
matchMinor := checkMatches(commitSlice, s.Wording.Minor)
matchMajor := checkMatches(commitSlice, s.Wording.Major)
if matchPatch {
if varDebug {
fmt.Println("DEBUG: Incrementing patch (WORDING) on ", commit.Message)
}
s.Semver.Patch++
if varDebug {
fmt.Println("DEBUG: Incrementing patch (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())
}
}
if matchMinor {
if varDebug {
fmt.Println("DEBUG: Incrementing minor (WORDING) on ", commit.Message)
}
s.Semver.Minor++
s.Semver.Patch = 1
if varDebug {
fmt.Println("DEBUG: Incrementing minor (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())
}
}
if matchMajor {
if varDebug {
fmt.Println("DEBUG: Incrementing major (WORDING) on ", commit.Message)
}
s.Semver.Major++
s.Semver.Minor = 0
s.Semver.Patch = 1
if varDebug {
fmt.Println("DEBUG: Incrementing major (WORDING) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())
}
}
}
return s.Semver
@@ -124,14 +128,7 @@ func (s *Setup) ListCommits() ([]CommitDetails, error) {
var ref *plumbing.Reference
var err error
// if zero.IsZero(s.Force.Commit) {
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 {
return []CommitDetails{}, err
}
@@ -170,25 +167,31 @@ func (s *Setup) Prepare() error {
if !repo.UseLocal {
u, err := url.Parse(s.RepositoryName)
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
}
s.RepositoryLocalPath = fmt.Sprintf("/tmp/foo/%s", u.Path)
s.RepositoryLocalPath = fmt.Sprintf("/tmp/semver/%s", u.Path)
os.RemoveAll(s.RepositoryLocalPath)
s.RepositoryHandler, err = git.PlainClone(s.RepositoryLocalPath, false, &git.CloneOptions{
URL: s.RepositoryName,
Auth: &http.BasicAuth{
Username: os.Getenv("GITHUB_USERNAME"),
Password: os.Getenv("GITHUB_TOKEN"),
},
})
if err != nil {
fmt.Println("Unable to reach repository", err.Error())
fmt.Println("Unable to reach repository", s.RepositoryName, "Error:", err.Error())
return err
}
} else {
s.RepositoryLocalPath = "./"
s.RepositoryHandler, err = git.PlainOpen(s.RepositoryLocalPath)
if err != nil {
fmt.Println("Unable to reach repository", err.Error())
fmt.Println("Unable to reach repository", s.RepositoryName, "Error:", err.Error())
return err
}
}
os.Chdir(s.RepositoryLocalPath)
return err
}
+32 -23
View File
@@ -31,14 +31,17 @@ type Tests struct {
}
var (
assert *assertions.Assertions
assert *assertions.Assertions
testCurrentPath string
)
func (suite *Tests) SetupTest() {
os.Chdir(testCurrentPath)
assert = assertions.New(suite.T())
}
func TestSuite(t *testing.T) {
testCurrentPath, _ = os.Getwd()
suite.Run(t, new(Tests))
}
@@ -257,6 +260,7 @@ func (suite *Tests) TestSetup_ListCommits() {
RepositoryName string
RepositoryLocalPath string
RepositoryHandler *git.Repository
LocalConfigFile string
Commits []CommitDetails
Semver SemVer
Wording Wording
@@ -299,10 +303,10 @@ func (suite *Tests) TestSetup_ListCommits() {
}
for _, tt := range tests {
suite.T().Run(tt.name, func(t *testing.T) {
s := &Setup{
RepositoryName: tt.fields.RepositoryName,
Force: tt.fields.Force,
}
s := &Setup{}
s.ReadConfig(tt.fields.LocalConfigFile)
s.RepositoryName = tt.fields.RepositoryName
s.Force = tt.fields.Force
s.Prepare()
listOfCommits, err := s.ListCommits()
if !tt.wantErr {
@@ -317,8 +321,9 @@ func (suite *Tests) TestSetup_ListCommits() {
func (suite *Tests) TestSetup_CalculateSemver() {
type fields struct {
RepositoryName string
Force Force
RepositoryName string
Force Force
LocalConfigFile string
}
type wantSemver struct {
Major int
@@ -333,50 +338,54 @@ func (suite *Tests) TestSetup_CalculateSemver() {
{
name: "Test on existing repository",
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{
Major: 1,
Minor: 3,
Patch: 1,
Major: 0,
Minor: 0,
Patch: 7,
},
},
{
name: "Test on existing repository, starting with certain hash",
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: "97d3682ed94168600926f9ff6da650403d1f3317",
Commit: "45f9a23cec39e94503841638aee3efecd45111cf",
},
},
wantSemver: wantSemver{
Major: 1,
Minor: 3,
Major: 2,
Minor: 4,
Patch: 1,
},
},
{
name: "Test on non-existing repository",
fields: fields{
RepositoryName: "https://github.com/lukaszraczylo/simple-gql-client-dead",
RepositoryName: "https://github.com/lukaszraczylo/semver-generator-test-repo-dead",
},
wantSemver: wantSemver{
Major: 1, // 1 because config file enforces MAJOR version
Minor: 0,
Minor: 1, // 1 because config file enforces MINOR version
Patch: 0,
},
},
}
for _, tt := range tests {
suite.T().Run(tt.name, func(t *testing.T) {
s := &Setup{
RepositoryName: tt.fields.RepositoryName,
Force: tt.fields.Force,
}
s.ReadConfig("../config.yaml")
s := &Setup{}
s.ReadConfig(tt.fields.LocalConfigFile)
s.RepositoryName = tt.fields.RepositoryName
s.Prepare()
s.ListCommits()
s.ForcedVersioning()
s.Force = tt.fields.Force
s.ListCommits()
semver := s.CalculateSemver()
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)
+1 -1
View File
@@ -66,7 +66,7 @@ func init() {
if !strings.HasSuffix(os.Args[0], ".test") {
cobra.OnInitialize(repo.setupCobra)
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(&varShowVersion, "version", "v", false, "Display version")
rootCmd.PersistentFlags().BoolVarP(&varDebug, "debug", "d", false, "Enable debug mode")