Skip to content

Commit 348f821

Browse files
feat: print git provider
1 parent ee9dac3 commit 348f821

File tree

6 files changed

+41
-30
lines changed

6 files changed

+41
-30
lines changed

cmd/semantic-release/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ func cliHandler(c *cli.Context) error {
6262
)
6363

6464
if conf.GitLab {
65-
repo, err = semrel.NewGitlabRepository(c.Context, conf.GitLabBaseURL, conf.Slug, conf.Token, ci.GetCurrentBranch(), conf.GitLabProjectID)
65+
repo, err = semrel.NewGitLabRepository(c.Context, conf.GitLabBaseURL, conf.Slug, conf.Token, ci.GetCurrentBranch(), conf.GitLabProjectID)
6666
} else {
67-
repo, err = semrel.NewGithubRepository(c.Context, conf.GheHost, conf.Slug, conf.Token)
67+
repo, err = semrel.NewGitHubRepository(c.Context, conf.GheHost, conf.Slug, conf.Token)
6868
}
6969

70+
logger.Printf("releasing on: %s\n", repo.Provider())
71+
7072
exitIfError(err)
7173

7274
logger.Println("getting default branch...")

pkg/semrel/github.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ import (
1212
"golang.org/x/oauth2"
1313
)
1414

15-
type GithubRepository struct {
15+
type GitHubRepository struct {
1616
owner string
1717
repo string
1818
Ctx context.Context
1919
Client *github.Client
2020
}
2121

22-
func NewGithubRepository(ctx context.Context, gheHost, slug, token string) (*GithubRepository, error) {
22+
func NewGitHubRepository(ctx context.Context, gheHost, slug, token string) (*GitHubRepository, error) {
2323
if !strings.Contains(slug, "/") {
2424
return nil, errors.New("invalid slug")
2525
}
26-
repo := new(GithubRepository)
26+
repo := new(GitHubRepository)
2727
split := strings.Split(slug, "/")
2828
repo.owner = split[0]
2929
repo.repo = split[1]
@@ -42,15 +42,15 @@ func NewGithubRepository(ctx context.Context, gheHost, slug, token string) (*Git
4242
return repo, nil
4343
}
4444

45-
func (repo *GithubRepository) GetInfo() (string, bool, error) {
45+
func (repo *GitHubRepository) GetInfo() (string, bool, error) {
4646
r, _, err := repo.Client.Repositories.Get(repo.Ctx, repo.owner, repo.repo)
4747
if err != nil {
4848
return "", false, err
4949
}
5050
return r.GetDefaultBranch(), r.GetPrivate(), nil
5151
}
5252

53-
func (repo *GithubRepository) GetCommits(sha string) ([]*Commit, error) {
53+
func (repo *GitHubRepository) GetCommits(sha string) ([]*Commit, error) {
5454
opts := &github.CommitsListOptions{
5555
SHA: sha,
5656
ListOptions: github.ListOptions{PerPage: 100},
@@ -66,7 +66,7 @@ func (repo *GithubRepository) GetCommits(sha string) ([]*Commit, error) {
6666
return ret, nil
6767
}
6868

69-
func (repo *GithubRepository) GetLatestRelease(vrange string, re *regexp.Regexp) (*Release, error) {
69+
func (repo *GitHubRepository) GetLatestRelease(vrange string, re *regexp.Regexp) (*Release, error) {
7070
allReleases := make(Releases, 0)
7171
opts := &github.ReferenceListOptions{"tags", github.ListOptions{PerPage: 100}}
7272
for {
@@ -97,7 +97,7 @@ func (repo *GithubRepository) GetLatestRelease(vrange string, re *regexp.Regexp)
9797
return allReleases.GetLatestRelease(vrange)
9898
}
9999

100-
func (repo *GithubRepository) CreateRelease(changelog string, newVersion *semver.Version, prerelease bool, branch, sha string) error {
100+
func (repo *GitHubRepository) CreateRelease(changelog string, newVersion *semver.Version, prerelease bool, branch, sha string) error {
101101
tag := fmt.Sprintf("v%s", newVersion.String())
102102
isPrerelease := prerelease || newVersion.Prerelease() != ""
103103

@@ -146,10 +146,14 @@ func parseGithubCommit(commit *github.RepositoryCommit) *Commit {
146146
return c
147147
}
148148

149-
func (repo *GithubRepository) Owner() string {
149+
func (repo *GitHubRepository) Owner() string {
150150
return repo.owner
151151
}
152152

153-
func (repo *GithubRepository) Repo() string {
153+
func (repo *GitHubRepository) Repo() string {
154154
return repo.repo
155155
}
156+
157+
func (repo *GitHubRepository) Provider() string {
158+
return "GitHub"
159+
}

pkg/semrel/github_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ import (
1515
)
1616

1717
func TestNewGithubRepository(t *testing.T) {
18-
repo, err := NewGithubRepository(context.TODO(), "", "", "")
18+
repo, err := NewGitHubRepository(context.TODO(), "", "", "")
1919
if repo != nil || err == nil {
2020
t.Fatal("invalid initialization")
2121
}
22-
repo, err = NewGithubRepository(context.TODO(), "", "owner/test-repo", "token")
22+
repo, err = NewGitHubRepository(context.TODO(), "", "owner/test-repo", "token")
2323
if repo == nil || err != nil {
2424
t.Fatal("invalid initialization")
2525
}
26-
repo, err = NewGithubRepository(context.TODO(), "github.enterprise", "owner/test-repo", "token")
26+
repo, err = NewGitHubRepository(context.TODO(), "github.enterprise", "owner/test-repo", "token")
2727
if repo.Client.BaseURL.Host != "github.enterprise" || err != nil {
2828
t.Fatal("invalid enterprise initialization")
2929
}
@@ -100,8 +100,8 @@ func githubHandler(w http.ResponseWriter, r *http.Request) {
100100
http.Error(w, "invalid route", http.StatusNotImplemented)
101101
}
102102

103-
func getNewGithubTestRepo(t *testing.T) (*GithubRepository, *httptest.Server) {
104-
repo, err := NewGithubRepository(context.TODO(), "", "owner/test-repo", "token")
103+
func getNewGithubTestRepo(t *testing.T) (*GitHubRepository, *httptest.Server) {
104+
repo, err := NewGitHubRepository(context.TODO(), "", "owner/test-repo", "token")
105105
if err != nil {
106106
t.Fatal(err)
107107
return nil, nil

pkg/semrel/gitlab.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
gitlab "github.com/xanzy/go-gitlab"
1111
)
1212

13-
type GitlabRepository struct {
13+
type GitLabRepository struct {
1414
owner string
1515
repo string
1616
projectID string
@@ -19,12 +19,12 @@ type GitlabRepository struct {
1919
client *gitlab.Client
2020
}
2121

22-
func NewGitlabRepository(ctx context.Context, gitlabBaseUrl, slug, token, branch string, projectID string) (*GitlabRepository, error) {
22+
func NewGitLabRepository(ctx context.Context, gitlabBaseUrl, slug, token, branch string, projectID string) (*GitLabRepository, error) {
2323
if projectID == "" {
2424
return nil, fmt.Errorf("project id is required")
2525
}
2626

27-
repo := new(GitlabRepository)
27+
repo := new(GitLabRepository)
2828
repo.projectID = projectID
2929
repo.Ctx = ctx
3030
repo.branch = branch
@@ -55,7 +55,7 @@ func NewGitlabRepository(ctx context.Context, gitlabBaseUrl, slug, token, branch
5555
return repo, nil
5656
}
5757

58-
func (repo *GitlabRepository) GetInfo() (string, bool, error) {
58+
func (repo *GitLabRepository) GetInfo() (string, bool, error) {
5959
project, _, err := repo.client.Projects.GetProject(repo.projectID, nil)
6060

6161
if err != nil {
@@ -65,7 +65,7 @@ func (repo *GitlabRepository) GetInfo() (string, bool, error) {
6565
return project.DefaultBranch, project.Visibility == gitlab.PrivateVisibility, nil
6666
}
6767

68-
func (repo *GitlabRepository) GetCommits(sha string) ([]*Commit, error) {
68+
func (repo *GitLabRepository) GetCommits(sha string) ([]*Commit, error) {
6969
opts := &gitlab.ListCommitsOptions{
7070
ListOptions: gitlab.ListOptions{
7171
Page: 1,
@@ -98,7 +98,7 @@ func (repo *GitlabRepository) GetCommits(sha string) ([]*Commit, error) {
9898
return allCommits, nil
9999
}
100100

101-
func (repo *GitlabRepository) GetLatestRelease(vrange string, re *regexp.Regexp) (*Release, error) {
101+
func (repo *GitLabRepository) GetLatestRelease(vrange string, re *regexp.Regexp) (*Release, error) {
102102
allReleases := make(Releases, 0)
103103

104104
opts := &gitlab.ListTagsOptions{
@@ -140,7 +140,7 @@ func (repo *GitlabRepository) GetLatestRelease(vrange string, re *regexp.Regexp)
140140
return allReleases.GetLatestRelease(vrange)
141141
}
142142

143-
func (repo *GitlabRepository) CreateRelease(changelog string, newVersion *semver.Version, prerelease bool, branch, sha string) error {
143+
func (repo *GitLabRepository) CreateRelease(changelog string, newVersion *semver.Version, prerelease bool, branch, sha string) error {
144144
tag := fmt.Sprintf("v%s", newVersion.String())
145145

146146
// Gitlab does not have any notion of pre-releases
@@ -173,10 +173,14 @@ func parseGitlabCommit(commit *gitlab.Commit) *Commit {
173173
return c
174174
}
175175

176-
func (repo *GitlabRepository) Owner() string {
176+
func (repo *GitLabRepository) Owner() string {
177177
return repo.owner
178178
}
179179

180-
func (repo *GitlabRepository) Repo() string {
180+
func (repo *GitLabRepository) Repo() string {
181181
return repo.repo
182182
}
183+
184+
func (repo *GitLabRepository) Provider() string {
185+
return "GitLab"
186+
}

pkg/semrel/gitlab_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ import (
1515
)
1616

1717
func TestNewGitlabRepository(t *testing.T) {
18-
repo, err := NewGitlabRepository(context.TODO(), "", "", "", "", "")
18+
repo, err := NewGitLabRepository(context.TODO(), "", "", "", "", "")
1919
if repo != nil || err == nil {
2020
t.Fatal("invalid initialization")
2121
}
22-
repo, err = NewGitlabRepository(context.TODO(), "", "owner/test-repo", "token", "", "1")
22+
repo, err = NewGitLabRepository(context.TODO(), "", "owner/test-repo", "token", "", "1")
2323
if repo == nil || err != nil || repo.Owner() != "owner" || repo.Repo() != "test-repo" {
2424
t.Fatal("invalid initialization")
2525
}
26-
repo, err = NewGitlabRepository(context.TODO(), "https://mygitlab.com", "owner/test-repo", "token", "", "1")
26+
repo, err = NewGitLabRepository(context.TODO(), "https://mygitlab.com", "owner/test-repo", "token", "", "1")
2727
if repo.client.BaseURL().String() != "https://mygitlab.com/api/v4/" || err != nil {
2828
t.Fatal("invalid custom instance initialization")
2929
}
@@ -102,9 +102,9 @@ func GitlabHandler(w http.ResponseWriter, r *http.Request) {
102102
http.Error(w, "invalid route", http.StatusNotImplemented)
103103
}
104104

105-
func getNewGitlabTestRepo(t *testing.T) (*GitlabRepository, *httptest.Server) {
105+
func getNewGitlabTestRepo(t *testing.T) (*GitLabRepository, *httptest.Server) {
106106
ts := httptest.NewServer(http.HandlerFunc(GitlabHandler))
107-
repo, err := NewGitlabRepository(context.TODO(), ts.URL, "gitlab-examples-ci", "token", "", strconv.Itoa(GITLAB_PROJECT_ID))
107+
repo, err := NewGitLabRepository(context.TODO(), ts.URL, "gitlab-examples-ci", "token", "", strconv.Itoa(GITLAB_PROJECT_ID))
108108
if err != nil {
109109
t.Fatal(err)
110110
return nil, nil

pkg/semrel/semrel.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ type Repository interface {
9999
CreateRelease(changelog string, newVersion *semver.Version, prerelease bool, branch, sha string) error
100100
Owner() string
101101
Repo() string
102+
Provider() string
102103
}
103104

104105
func CalculateChange(commits []*Commit, latestRelease *Release) Change {

0 commit comments

Comments
 (0)