From b8a15788ee615adac9c5bf98d01910d854acb337 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Tue, 11 May 2021 20:57:10 +0100 Subject: [PATCH] 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 --- README.md | 17 +++++++++++--- cmd/main.go | 49 +++++++++++++++++++++------------------- cmd/main_test.go | 55 ++++++++++++++++++++++++++------------------- cmd/root.go | 2 +- config-release.yaml | 1 - config.yaml | 1 - 6 files changed, 73 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 3dc09df..b208fbe 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ Project created overnight, to prove that management of semantic versioning is NO - [Semantic version generator](#semantic-version-generator) - [How does it work](#how-does-it-work) - [Usage](#usage) + - [Authentication](#authentication) - [As a binary](#as-a-binary) - [As a github action](#as-a-github-action) - [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 +#### 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 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 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 -h, --help help for semver-gen -l, --local Use local repository @@ -72,9 +83,9 @@ jobs: fetch-depth: '0' - name: Semver run id: semver - uses: lukaszraczylo/semver-generator@1.0.44 + uses: lukaszraczylo/semver-generator@PLACE_LATEST_TAG_HERE with: - config_file: config.yaml + config_file: semver.yaml # either... repository_local: true # or... diff --git a/cmd/main.go b/cmd/main.go index ad088b7..2d8f4af 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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 } diff --git a/cmd/main_test.go b/cmd/main_test.go index 2299c04..2750f27 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -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) diff --git a/cmd/root.go b/cmd/root.go index 2e19141..8e7b86f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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") diff --git a/config-release.yaml b/config-release.yaml index db2a72b..c168067 100644 --- a/config-release.yaml +++ b/config-release.yaml @@ -7,7 +7,6 @@ wording: - initial - fix minor: - - add - change - improve major: diff --git a/config.yaml b/config.yaml index 4e12444..db2a72b 100644 --- a/config.yaml +++ b/config.yaml @@ -1,7 +1,6 @@ version: 1 force: major: 1 - commit: 97d3682ed94168600926f9ff6da650403d1f3317 wording: patch: - update