Skip to content

Commit

Permalink
saving current progress
Browse files Browse the repository at this point in the history
Signed-off-by: k4yt3x <[email protected]>
  • Loading branch information
k4yt3x committed Aug 30, 2024
1 parent a0ab543 commit 8f99d16
Show file tree
Hide file tree
Showing 10 changed files with 549 additions and 173 deletions.
4 changes: 2 additions & 2 deletions cmd/autobump/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const defaultConfigURL = "https://raw.githubusercontent.com/rios0rios0/autobump/
"main/configs/autobump.yaml"

var (
ErrMissingLanguagesKeyError = errors.New("missing languages key")
ErrLanguagesKeyMissingError = errors.New("missing languages key")
ErrConfigFileNotFoundError = errors.New("config file not found")
ErrConfigKeyMissingError = errors.New("config keys missing")
)
Expand Down Expand Up @@ -154,7 +154,7 @@ func validateGlobalConfig(globalConfig *GlobalConfig, batch bool) error {
}

if globalConfig.LanguagesConfig == nil {
return ErrMissingLanguagesKeyError
return ErrLanguagesKeyMissingError
}

return nil
Expand Down
95 changes: 95 additions & 0 deletions cmd/autobump/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"errors"
"testing"
)

func TestValidateGlobalConfig_Success(t *testing.T) {
// Arrange
globalConfig := GlobalConfig{
Projects: []ProjectConfig{
{Path: "/home/user/test", ProjectAccessToken: "glpat-ABCDEFGHIJKLMNOPQRST"},

Check failure

Code scanning / SonarCloud

GitLab tokens should not be disclosed High test

Make sure this GitLab token gets revoked, changed, and removed from the code. See more on SonarCloud
},
LanguagesConfig: map[string]LanguageConfig{"Go": {}},
GpgKeyPath: "/home/user/.gnupg/autobump.asc",
GitLabAccessToken: "glpat-ABCDEFGHIJKLMNOPQRST",

Check failure

Code scanning / SonarCloud

GitLab tokens should not be disclosed High test

Make sure this GitLab token gets revoked, changed, and removed from the code. See more on SonarCloud
}

// Act
err := validateGlobalConfig(&globalConfig, false)
// Assert
if err != nil {
t.Errorf("expected no error, got %v", err)
}
}

func TestValidateGlobalConfig_MissingProjectsInBatchMode(t *testing.T) {
// Arrange
globalConfig := GlobalConfig{
LanguagesConfig: map[string]LanguageConfig{"Go": {}},
}

// Act
err := validateGlobalConfig(&globalConfig, true)

// Assert
if !errors.Is(err, ErrConfigKeyMissingError) {
t.Errorf("expected error %v, got %v", ErrConfigKeyMissingError, err)
}
}

func TestValidateGlobalConfig_MissingProjectPath(t *testing.T) {
// Arrange
globalConfig := GlobalConfig{
Projects: []ProjectConfig{
{Path: "", ProjectAccessToken: "token1"},
},
LanguagesConfig: map[string]LanguageConfig{"Go": {}},
}

// Act
err := validateGlobalConfig(&globalConfig, false)

// Assert
if !errors.Is(err, ErrConfigKeyMissingError) {
t.Errorf("expected error %v, got %v", ErrConfigKeyMissingError, err)
}
}

func TestValidateGlobalConfig_MissingProjectAccessTokenInBatchMode(t *testing.T) {
// Arrange
globalConfig := GlobalConfig{
Projects: []ProjectConfig{
{Path: "some/path", ProjectAccessToken: ""},
},
LanguagesConfig: map[string]LanguageConfig{"Go": {}},
GitLabAccessToken: "",
}

// Act
err := validateGlobalConfig(&globalConfig, true)

// Assert
if !errors.Is(err, ErrConfigKeyMissingError) {
t.Errorf("expected error %v, got %v", ErrConfigKeyMissingError, err)
}
}

func TestValidateGlobalConfig_MissingLanguagesConfig(t *testing.T) {
// Arrange
globalConfig := GlobalConfig{
Projects: []ProjectConfig{
{Path: "some/path", ProjectAccessToken: "token1"},
},
LanguagesConfig: nil,
}

// Act
err := validateGlobalConfig(&globalConfig, false)

// Assert
if !errors.Is(err, ErrLanguagesKeyMissingError) {
t.Errorf("expected error %v, got %v", ErrLanguagesKeyMissingError, err)
}
}
41 changes: 18 additions & 23 deletions cmd/autobump/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import (

type ServiceType int

type LatestTag struct {
Tag *semver.Version
Date time.Time
}

const (
UNKNOWN ServiceType = iota
GITHUB
Expand All @@ -37,7 +42,7 @@ const (
)

var (
ErrNoAuthMethod = errors.New("no authentication method found")
ErrNoAuthMethodFound = errors.New("no authentication method found")
ErrAuthNotImplemented = errors.New("authentication method not implemented")
ErrNoRemoteURL = errors.New("no remote URL found for repository")
ErrNoTagsFound = errors.New("no tags found in Git history")
Expand Down Expand Up @@ -206,7 +211,7 @@ func getAuthMethods(
var authMethods []transport.AuthMethod

switch service { //nolint:exhaustive // Unimplemented services are handled by the default case
case GITHUB:
case GITLAB:
// TODO: this lines of code MUST be refactored to avoid code duplication
// project access token
if projectConfig.ProjectAccessToken != "" {
Expand Down Expand Up @@ -250,7 +255,7 @@ func getAuthMethods(

if len(authMethods) == 0 {
log.Error("No authentication credentials found for any authentication method")
return nil, ErrNoAuthMethod
return nil, ErrNoAuthMethodFound
}

return authMethods, nil
Expand Down Expand Up @@ -324,18 +329,8 @@ func getAmountCommits(repo *git.Repository) (int, error) {
return amountCommits, nil
}

type LatestTag struct {
Tag *semver.Version
Date time.Time
}

// getLatestTag find the latest tag in the Git history
func getLatestTag() (*LatestTag, error) {
repo, err := git.PlainOpen(".")
if err != nil {
log.Fatal(err)
}

func getLatestTag(repo *git.Repository) (*LatestTag, error) {
tags, err := repo.Tags()
if err != nil {
log.Fatal(err)
Expand All @@ -347,18 +342,11 @@ func getLatestTag() (*LatestTag, error) {
return nil
})

// get the date time of the tag
commit, err := repo.CommitObject(latestTag.Hash())
latestTagDate := commit.Committer.When
if err != nil {
log.Fatal(err)
}

commits, _ := getAmountCommits(repo)
numCommits, _ := getAmountCommits(repo)
if latestTag == nil {
// if the project is already started with no tags in the history
// TODO: review this section
if commits >= maxAcceptableInitialCommits {
if numCommits >= maxAcceptableInitialCommits {
log.Warnf("No tags found in Git history, falling back to '%s'", defaultGitTag)
version, _ := semver.NewVersion(defaultGitTag)
return &LatestTag{
Expand All @@ -372,6 +360,13 @@ func getLatestTag() (*LatestTag, error) {
return nil, ErrNoTagsFound
}

// get the date time of the tag
commit, err := repo.CommitObject(latestTag.Hash())
latestTagDate := commit.Committer.When
if err != nil {
log.Fatal(err)
}

version, _ := semver.NewVersion(latestTag.Name().Short())
return &LatestTag{
Tag: version,
Expand Down
Loading

0 comments on commit 8f99d16

Please sign in to comment.