feat(releaser): add git only as releaser, will create a new tag with version only

This commit is contained in:
Nightapes
2020-01-05 18:41:44 +01:00
parent 6211095c38
commit 42fc522a43
19 changed files with 284 additions and 109 deletions

View File

@@ -0,0 +1,101 @@
package git
import (
"fmt"
"time"
"github.com/Nightapes/go-semantic-release/internal/gitutil"
"github.com/Nightapes/go-semantic-release/internal/shared"
"github.com/Nightapes/go-semantic-release/pkg/config"
"gopkg.in/src-d/go-git.v4"
gitConfig "gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
log "github.com/sirupsen/logrus"
)
// GITONLY identifer for git interface
const GITONLY = "git"
// Client type struct
type Client struct {
config *config.GitProvider
log *log.Entry
git *gitutil.GitUtil
}
// New initialize a new gitRelease
func New(config *config.GitProvider, git *gitutil.GitUtil, checkConfig bool) (*Client, error) {
logger := log.WithField("releaser", GITONLY)
if config.Email == "" && checkConfig {
return nil, fmt.Errorf("git email not set")
}
if config.Username == "" && checkConfig {
return nil, fmt.Errorf("git username not set")
}
if !config.SSH && config.Auth == "" && checkConfig {
return nil, fmt.Errorf("git auth not set")
}
if config.SSH {
return nil, fmt.Errorf("git ssh not supported yet")
}
return &Client{
config: config,
log: logger,
git: git,
}, nil
}
//GetCommitURL for git
func (g *Client) GetCommitURL() string {
return ""
}
//GetCompareURL for git
func (g *Client) GetCompareURL(oldVersion, newVersion string) string {
return ""
}
// CreateRelease creates release on remote
func (g *Client) CreateRelease(releaseVersion *shared.ReleaseVersion, generatedChangelog *shared.GeneratedChangelog) error {
tag := "v" + releaseVersion.Next.Version.String()
g.log.Infof("create release with version %s", tag)
head, err := g.git.Repository.Head()
if err != nil {
return err
}
_, err = g.git.Repository.CreateTag(tag, head.Hash(), &git.CreateTagOptions{Message: "Release " + tag, Tagger: &object.Signature{
Name: g.config.Username,
Email: g.config.Email,
When: time.Now(),
}})
if err != nil {
return err
}
g.log.Infof("Created release")
return g.git.Repository.Push(&git.PushOptions{
Auth: &http.BasicAuth{
Username: g.config.Username,
Password: g.config.Auth,
},
RefSpecs: []gitConfig.RefSpec{"refs/tags/*:refs/tags/*"},
})
}
// UploadAssets uploads specified assets
func (g *Client) UploadAssets(repoDir string, assets []config.Asset) error {
return nil
}

View File

@@ -29,17 +29,27 @@ type Client struct {
}
// New initialize a new GitHubRelease
func New(c *config.GitHubProvider) (*Client, error) {
var err error
func New(c *config.GitHubProvider, checkConfig bool) (*Client, error) {
if c.AccessToken, err = util.GetAccessToken("GITHUB_TOKEN"); err != nil {
token, err := util.GetAccessToken("GITHUB_TOKEN")
if err != nil && checkConfig {
return &Client{}, err
}
c.AccessToken = token
ctx := context.Background()
httpClient := util.CreateBearerHTTPClient(ctx, c.AccessToken)
var client *github.Client
baseURL := "https://github.com"
if c.Repo == "" && checkConfig {
return nil, fmt.Errorf("github repro is not set")
}
if c.User == "" && checkConfig {
return nil, fmt.Errorf("github user is not set")
}
if c.CustomURL == "" {
client = github.NewClient(httpClient)
} else {
@@ -54,7 +64,7 @@ func New(c *config.GitHubProvider) (*Client, error) {
context: ctx,
baseURL: baseURL,
log: log.WithField("releaser", GITHUB),
}, err
}, nil
}
//GetCommitURL for github
@@ -67,26 +77,10 @@ func (g *Client) GetCompareURL(oldVersion, newVersion string) string {
return fmt.Sprintf("%s/%s/%s/compare/%s...%s", g.baseURL, g.config.User, g.config.Repo, oldVersion, newVersion)
}
//ValidateConfig for github
func (g *Client) ValidateConfig() error {
g.log.Debugf("validate GitHub provider config")
if g.config.Repo == "" {
return fmt.Errorf("github Repro is not set")
}
if g.config.User == "" {
return fmt.Errorf("github User is not set")
}
return nil
}
// CreateRelease creates release on remote
func (g *Client) CreateRelease(releaseVersion *shared.ReleaseVersion, generatedChangelog *shared.GeneratedChangelog) error {
tag := releaseVersion.Next.Version.String()
tag := "v" + releaseVersion.Next.Version.String()
g.log.Debugf("create release with version %s", tag)
prerelease := releaseVersion.Next.Version.Prerelease() != ""

View File

@@ -146,7 +146,7 @@ func TestNew(t *testing.T) {
os.Setenv("GITHUB_TOKEN", "XXX")
}
_, err := github.New(&testOject.config)
_, err := github.New(&testOject.config, true)
assert.Equal(t, testOject.valid, err == nil)
os.Unsetenv("GITHUB_TOKEN")
@@ -157,7 +157,7 @@ func TestNew(t *testing.T) {
func TestGetCommitURL(t *testing.T) {
os.Setenv("GITHUB_TOKEN", "XX")
for _, testOject := range testNewClient {
client, _ := github.New(&testOject.config)
client, _ := github.New(&testOject.config, false)
actualURL := client.GetCommitURL()
if testOject.config.CustomURL != "" {
expectedURL := fmt.Sprintf("%s/%s/%s/commit/{{hash}}", testOject.config.CustomURL, testOject.config.User, testOject.config.Repo)
@@ -175,7 +175,7 @@ func TestGetCommitURL(t *testing.T) {
func TestGetCompareURL(t *testing.T) {
os.Setenv("GITHUB_TOKEN", "XX")
for _, testOject := range testNewClient {
client, _ := github.New(&testOject.config)
client, _ := github.New(&testOject.config, false)
actualURL := client.GetCompareURL("1", "2")
if testOject.config.CustomURL != "" {
expectedURL := fmt.Sprintf("%s/%s/%s/compare/%s...%s", testOject.config.CustomURL, testOject.config.User, testOject.config.Repo, "1", "2")
@@ -190,18 +190,6 @@ func TestGetCompareURL(t *testing.T) {
}
func TestValidateConfig(t *testing.T) {
os.Setenv("GITHUB_TOKEN", "XX")
for _, testOject := range testHelperMethod {
client, _ := github.New(&testOject.config)
err := client.ValidateConfig()
assert.Equal(t, testOject.valid, err == nil)
}
os.Unsetenv("GITHUB_TOKEN")
}
func TestCreateRelease(t *testing.T) {
os.Setenv("GITHUB_TOKEN", "XX")
@@ -209,7 +197,7 @@ func TestCreateRelease(t *testing.T) {
if testObejct.valid {
server := initHTTPServer(testObejct.requestResponseCode, testObejct.requestResponseBody)
testObejct.config.CustomURL = server.URL
client, _ := github.New(&testObejct.config)
client, _ := github.New(&testObejct.config, false)
err := client.CreateRelease(testObejct.releaseVersion, testObejct.generatedChangelog)
if err != nil {
@@ -221,7 +209,7 @@ func TestCreateRelease(t *testing.T) {
} else {
testObejct.config.CustomURL = "http://foo"
client, _ := github.New(&testObejct.config)
client, _ := github.New(&testObejct.config, false)
err := client.CreateRelease(testObejct.releaseVersion, testObejct.generatedChangelog)
if err != nil {

View File

@@ -34,9 +34,9 @@ type Client struct {
}
// New initialize a new gitlabRelease
func New(config *config.GitLabProvider) (*Client, error) {
func New(config *config.GitLabProvider, checkConfig bool) (*Client, error) {
accessToken, err := util.GetAccessToken(fmt.Sprintf("%s_ACCESS_TOKEN", strings.ToUpper(GITLAB)))
if err != nil {
if err != nil && checkConfig {
return nil, err
}
@@ -52,7 +52,7 @@ func New(config *config.GitLabProvider) (*Client, error) {
logger.Debugf("validate gitlab provider config")
if config.Repo == "" {
if config.Repo == "" && checkConfig {
return nil, fmt.Errorf("gitlab Repro is not set")
}
@@ -85,16 +85,10 @@ func (g *Client) GetCompareURL(oldVersion, newVersion string) string {
return fmt.Sprintf("%s/%s/compare/%s...%s", g.baseURL, g.config.Repo, oldVersion, newVersion)
}
//ValidateConfig for gitlab
func (g *Client) ValidateConfig() error {
return nil
}
// CreateRelease creates release on remote
func (g *Client) CreateRelease(releaseVersion *shared.ReleaseVersion, generatedChangelog *shared.GeneratedChangelog) error {
tag := releaseVersion.Next.Version.String()
tag := "v" + releaseVersion.Next.Version.String()
g.Release = tag
g.log.Infof("create release with version %s", tag)
url := fmt.Sprintf("%s/projects/%s/releases", g.apiURL, util.PathEscape(g.config.Repo))

View File

@@ -24,7 +24,7 @@ func TestGetCommitURL(t *testing.T) {
client, err := gitlab.New(&config.GitLabProvider{
CustomURL: "https://localhost/",
Repo: "test/test",
})
}, true)
assert.NoError(t, err)
assert.Equal(t, "https://localhost/test/test/commit/{{hash}}", client.GetCommitURL())
}
@@ -35,7 +35,7 @@ func TestGetCompareURL(t *testing.T) {
client, err := gitlab.New(&config.GitLabProvider{
CustomURL: "https://localhost/",
Repo: "test/test",
})
}, true)
assert.NoError(t, err)
assert.Equal(t, "https://localhost/test/test/compare/1.0.0...1.0.1", client.GetCompareURL("1.0.0", "1.0.1"))
}
@@ -45,7 +45,7 @@ func TestValidateConfig_EmptyRepro(t *testing.T) {
defer os.Unsetenv("GITLAB_ACCESS_TOKEN")
_, err := gitlab.New(&config.GitLabProvider{
CustomURL: "https://localhost/",
})
}, true)
assert.Error(t, err)
}
@@ -55,7 +55,7 @@ func TestValidateConfig_DefaultURL(t *testing.T) {
config := &config.GitLabProvider{
Repo: "localhost/test",
}
_, err := gitlab.New(config)
_, err := gitlab.New(config, true)
assert.NoError(t, err)
assert.Equal(t, "https://gitlab.com", config.CustomURL)
}
@@ -67,7 +67,7 @@ func TestValidateConfig_CustomURL(t *testing.T) {
Repo: "/localhost/test/",
CustomURL: "https://localhost/",
}
_, err := gitlab.New(config)
_, err := gitlab.New(config, true)
assert.NoError(t, err)
assert.Equal(t, "https://localhost", config.CustomURL)
assert.Equal(t, "localhost/test", config.Repo)
@@ -108,7 +108,7 @@ func TestCreateRelease(t *testing.T) {
},
responseBody: "{}",
responseCode: 200,
requestBody: `{"tag_name":"2.0.0","name":"title","ref":"master","description":"content"}`,
requestBody: `{"tag_name":"v2.0.0","name":"title","ref":"master","description":"content"}`,
valid: true,
},
{
@@ -132,7 +132,7 @@ func TestCreateRelease(t *testing.T) {
},
responseBody: "{}",
responseCode: 500,
requestBody: `{"tag_name":"2.0.0","name":"title","ref":"master","description":"content"}`,
requestBody: `{"tag_name":"v2.0.0","name":"title","ref":"master","description":"content"}`,
valid: false,
},
{
@@ -157,7 +157,7 @@ func TestCreateRelease(t *testing.T) {
},
responseCode: 400,
responseBody: "{}",
requestBody: `{"tag_name":"2.0.0","name":"title","ref":"master","description":"content"}`,
requestBody: `{"tag_name":"v2.0.0","name":"title","ref":"master","description":"content"}`,
valid: false,
},
}
@@ -190,7 +190,7 @@ func TestCreateRelease(t *testing.T) {
}
os.Setenv("GITLAB_ACCESS_TOKEN", "aToken")
defer os.Unsetenv("GITLAB_ACCESS_TOKEN")
client, err := gitlab.New(&testObject.config)
client, err := gitlab.New(&testObject.config, false)
assert.NoError(t, err)
err = client.CreateRelease(testObject.releaseVersion, testObject.generatedChangelog)
@@ -317,7 +317,7 @@ func TestUploadAssets(t *testing.T) {
}
os.Setenv("GITLAB_ACCESS_TOKEN", "aToken")
defer os.Unsetenv("GITLAB_ACCESS_TOKEN")
client, err := gitlab.New(&testObject.config)
client, err := gitlab.New(&testObject.config, false)
assert.NoError(t, err)
client.Release = "1.0.0"

View File

@@ -3,6 +3,8 @@ package releaser
import (
"fmt"
"github.com/Nightapes/go-semantic-release/internal/gitutil"
"github.com/Nightapes/go-semantic-release/internal/releaser/git"
"github.com/Nightapes/go-semantic-release/internal/releaser/github"
"github.com/Nightapes/go-semantic-release/internal/releaser/gitlab"
"github.com/Nightapes/go-semantic-release/internal/shared"
@@ -14,33 +16,37 @@ import (
// Releasers struct type
type Releasers struct {
config *config.ReleaseConfig
git *gitutil.GitUtil
}
// Releaser interface for providers
type Releaser interface {
ValidateConfig() error
CreateRelease(*shared.ReleaseVersion, *shared.GeneratedChangelog) error
UploadAssets(repoDir string, assets []config.Asset) error
GetCommitURL() string
GetCompareURL(oldVersion, newVersion string) string
}
// New initialize a Relerser
func New(c *config.ReleaseConfig) *Releasers {
// New initialize a releaser
func New(c *config.ReleaseConfig, git *gitutil.GitUtil) *Releasers {
return &Releasers{
config: c,
git: git,
}
}
//GetReleaser returns an initialized releaser
func (r *Releasers) GetReleaser() (Releaser, error) {
func (r *Releasers) GetReleaser(checkConfig bool) (Releaser, error) {
switch r.config.Release {
case github.GITHUB:
log.Debugf("initialize new %s-provider", github.GITHUB)
return github.New(&r.config.GitHubProvider)
return github.New(&r.config.GitHubProvider, checkConfig)
case gitlab.GITLAB:
log.Debugf("initialize new %s-provider", gitlab.GITLAB)
return gitlab.New(&r.config.GitLabProvider)
return gitlab.New(&r.config.GitLabProvider, checkConfig)
case git.GITONLY:
log.Debugf("initialize new %s-provider", git.GITONLY)
return git.New(&r.config.GitProvider, r.git, checkConfig)
}
return nil, fmt.Errorf("could not initialize a releaser from this type: %s", r.config.Release)
}