-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e2978d
commit c70d912
Showing
3 changed files
with
104 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,13 @@ import ( | |
|
||
"github.com/Masterminds/semver/v3" | ||
"github.com/go-git/go-git/v5" | ||
"github.com/go-git/go-git/v5/config" | ||
"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/storer" | ||
"github.com/go-git/go-git/v5/plumbing/transport" | ||
"github.com/go-git/go-git/v5/plumbing/transport/http" | ||
"github.com/go-git/go-git/v5/plumbing/transport/ssh" | ||
"github.com/go-semantic-release/semantic-release/v2/pkg/provider" | ||
"github.com/go-semantic-release/semantic-release/v2/pkg/semrel" | ||
) | ||
|
@@ -21,6 +25,8 @@ type Repository struct { | |
defaultBranch string | ||
taggerName string | ||
taggerEmail string | ||
remoteName string | ||
auth transport.AuthMethod | ||
repo *git.Repository | ||
} | ||
|
||
|
@@ -40,6 +46,30 @@ func (repo *Repository) Init(config map[string]string) error { | |
repo.taggerEmail = "[email protected]" | ||
} | ||
|
||
repo.remoteName = config["remote_name"] | ||
if repo.remoteName == "" { | ||
repo.remoteName = "origin" | ||
} | ||
|
||
if config["auth_username"] == "" { | ||
config["auth_username"] = "git" | ||
} | ||
|
||
if config["auth"] == "basic" { | ||
repo.auth = &http.BasicAuth{ | ||
Username: config["auth_username"], | ||
Password: config["auth_password"], | ||
} | ||
} else if config["auth"] == "ssh" { | ||
auth, err := ssh.NewPublicKeysFromFile(config["auth_username"], config["auth_private_key"], config["auth_password"]) | ||
if err != nil { | ||
return err | ||
} | ||
repo.auth = auth | ||
} else { | ||
repo.auth = nil | ||
} | ||
|
||
gitPath := config["git_path"] | ||
if gitPath == "" { | ||
gitPath = "." | ||
|
@@ -144,7 +174,14 @@ func (repo *Repository) CreateRelease(release *provider.CreateReleaseConfig) err | |
if err != nil { | ||
return err | ||
} | ||
return nil | ||
err = repo.repo.Push(&git.PushOptions{ | ||
RemoteName: repo.remoteName, | ||
RefSpecs: []config.RefSpec{ | ||
config.RefSpec(fmt.Sprintf("refs/tags/%s:refs/tags/%s", tag, tag)), | ||
}, | ||
Auth: repo.auth, | ||
}) | ||
return err | ||
} | ||
|
||
func (repo *Repository) Name() string { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,32 +8,49 @@ import ( | |
"time" | ||
|
||
"github.com/go-git/go-git/v5" | ||
"github.com/go-git/go-git/v5/config" | ||
"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/go-semantic-release/semantic-release/v2/pkg/provider" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewRepository(t *testing.T) { | ||
var testGitPath string | ||
|
||
func TestGit(t *testing.T) { | ||
var err error | ||
testGitPath, err = setupRepo() | ||
require.NoError(t, err) | ||
t.Run("NewRepository", newRepository) | ||
t.Run("GetInfo", getInfo) | ||
t.Run("GetReleases", getReleases) | ||
t.Run("GetCommits", getCommits) | ||
t.Run("CreateRelease", createRelease) | ||
} | ||
|
||
func newRepository(t *testing.T) { | ||
require := require.New(t) | ||
repo := &Repository{} | ||
err := repo.Init(map[string]string{}) | ||
require.EqualError(err, "repository does not exist") | ||
|
||
gitPath, err := setupRepo() | ||
require.NoError(err) | ||
|
||
repo = &Repository{} | ||
err = repo.Init(map[string]string{ | ||
"git_path": gitPath, | ||
"git_path": testGitPath, | ||
"default_branch": "development", | ||
"tagger_name": "test", | ||
"tagger_email": "[email protected]", | ||
"auth": "basic", | ||
"auth_username": "test", | ||
"auth_password": "test", | ||
}) | ||
|
||
require.NoError(err) | ||
require.Equal(repo.defaultBranch, "development") | ||
require.Equal(repo.taggerName, "test") | ||
require.Equal(repo.taggerEmail, "[email protected]") | ||
require.Equal("development", repo.defaultBranch) | ||
require.Equal("test", repo.taggerName) | ||
require.Equal("[email protected]", repo.taggerEmail) | ||
require.NotNil(repo.auth) | ||
} | ||
|
||
func setupRepo() (string, error) { | ||
|
@@ -45,6 +62,14 @@ func setupRepo() (string, error) { | |
if err != nil { | ||
return "", err | ||
} | ||
|
||
_, err = repo.CreateRemote(&config.RemoteConfig{ | ||
Name: "origin", | ||
URLs: []string{"http://localhost:3000/test/test.git"}, | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
w, err := repo.Worktree() | ||
if err != nil { | ||
return "", err | ||
|
@@ -91,38 +116,51 @@ func setupRepo() (string, error) { | |
return "", err | ||
} | ||
|
||
return dir, nil | ||
} | ||
|
||
func createRepo() (*Repository, string, error) { | ||
gitPath, err := setupRepo() | ||
err = repo.Push(&git.PushOptions{ | ||
RemoteName: "origin", | ||
RefSpecs: []config.RefSpec{ | ||
"refs/heads/*:refs/heads/*", | ||
"refs/tags/*:refs/tags/*", | ||
}, | ||
Auth: &http.BasicAuth{ | ||
Username: "test", | ||
Password: "test", | ||
}, | ||
Force: true, | ||
}) | ||
if err != nil { | ||
return nil, "", err | ||
return "", err | ||
} | ||
return dir, nil | ||
} | ||
|
||
func createRepo() (*Repository, error) { | ||
repo := &Repository{} | ||
err = repo.Init(map[string]string{ | ||
"git_path": gitPath, | ||
err := repo.Init(map[string]string{ | ||
"git_path": testGitPath, | ||
"auth": "basic", | ||
"auth_username": "test", | ||
"auth_password": "test", | ||
}) | ||
if err != nil { | ||
return nil, "", err | ||
return nil, err | ||
} | ||
|
||
return repo, gitPath, nil | ||
return repo, nil | ||
} | ||
|
||
func TestGetInfo(t *testing.T) { | ||
func getInfo(t *testing.T) { | ||
require := require.New(t) | ||
repo, _, err := createRepo() | ||
repo, err := createRepo() | ||
require.NoError(err) | ||
repoInfo, err := repo.GetInfo() | ||
require.NoError(err) | ||
require.Equal("master", repoInfo.DefaultBranch) | ||
} | ||
|
||
func TestGetCommits(t *testing.T) { | ||
func getCommits(t *testing.T) { | ||
require := require.New(t) | ||
repo, _, err := createRepo() | ||
repo, err := createRepo() | ||
require.NoError(err) | ||
commits, err := repo.GetCommits("", "master") | ||
require.NoError(err) | ||
|
@@ -133,12 +171,12 @@ func TestGetCommits(t *testing.T) { | |
} | ||
} | ||
|
||
func TestGithubCreateRelease(t *testing.T) { | ||
func createRelease(t *testing.T) { | ||
require := require.New(t) | ||
repo, gitPath, err := createRepo() | ||
repo, err := createRepo() | ||
require.NoError(err) | ||
|
||
gRepo, err := git.PlainOpen(gitPath) | ||
gRepo, err := git.PlainOpen(testGitPath) | ||
require.NoError(err) | ||
head, err := gRepo.Head() | ||
require.NoError(err) | ||
|
@@ -159,9 +197,9 @@ func TestGithubCreateRelease(t *testing.T) { | |
require.Equal("new feature\n", tagObj.Message) | ||
} | ||
|
||
func TestGetReleases(t *testing.T) { | ||
func getReleases(t *testing.T) { | ||
require := require.New(t) | ||
repo, _, err := createRepo() | ||
repo, err := createRepo() | ||
require.NoError(err) | ||
|
||
releases, err := repo.GetReleases("") | ||
|