-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from LinuxSuRen/gitlab-support
Add gitlab and gitea support
- Loading branch information
Showing
10 changed files
with
203 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ | |
bin/ | ||
tmp/ | ||
.idea/ | ||
data/ |
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
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 |
---|---|---|
@@ -1,16 +1,54 @@ | ||
package internal_scm | ||
|
||
import "github.com/kubesphere-sigs/ks-releaser/api/v1alpha1" | ||
import ( | ||
"context" | ||
"github.com/jenkins-x/go-scm/scm" | ||
"github.com/kubesphere-sigs/ks-releaser/api/v1alpha1" | ||
) | ||
|
||
type GitReleaser interface { | ||
Release(version, commitish string, draft, prerelease bool) (err error) | ||
} | ||
|
||
func release(client *scm.Client, repo, version, commitish string, draft, prerelease bool) (err error) { | ||
releaseInput := &scm.ReleaseInput{ | ||
Title: version, | ||
Tag: version, | ||
Commitish: commitish, | ||
Draft: draft, | ||
Prerelease: prerelease, | ||
} | ||
|
||
// just publish the draft release if it is existing | ||
release := findRelease(client, repo, version) | ||
if release == nil { | ||
_, _, err = client.Releases.Create(context.TODO(), repo, releaseInput) | ||
} else if release.Draft { | ||
releaseInput.Description = release.Description | ||
releaseInput.Title = release.Title | ||
_, _, err = client.Releases.Update(context.TODO(), repo, release.ID, releaseInput) | ||
} | ||
// ignore the existing release | ||
return | ||
} | ||
|
||
func findRelease(client *scm.Client, repo, version string) (release *scm.Release) { | ||
var err error | ||
if release, _, err = client.Releases.FindByTag(context.TODO(), repo, version); err != nil { | ||
release = nil | ||
} | ||
return | ||
} | ||
|
||
// GetGitProvider returns the GitReleaser implement by kind | ||
func GetGitProvider(kind, repo, token string) GitReleaser { | ||
func GetGitProvider(kind, server, repo, token string) GitReleaser { | ||
switch v1alpha1.Provider(kind) { | ||
case v1alpha1.ProviderGitHub: | ||
return NewGitHub(repo, token) | ||
case v1alpha1.ProviderGitlab: | ||
return NewGitlab(repo, token) | ||
case v1alpha1.ProviderGitea: | ||
return NewGitea(server, repo, token) | ||
} | ||
return nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package internal_scm | ||
|
||
import ( | ||
"fmt" | ||
"github.com/jenkins-x/go-scm/scm" | ||
"github.com/jenkins-x/go-scm/scm/driver/gitea" | ||
) | ||
|
||
type Gitea struct { | ||
server string | ||
repo string | ||
token string | ||
|
||
client *scm.Client | ||
} | ||
|
||
func NewGitea(server, repo, token string) *Gitea { | ||
return &Gitea{ | ||
server: server, | ||
repo: repo, | ||
token: token, | ||
} | ||
} | ||
|
||
func (r *Gitea) Release(version, commitish string, draft, prerelease bool) (err error) { | ||
var client *scm.Client | ||
if client, err = gitea.NewWithToken(r.server, r.token); err != nil || client == nil { | ||
err = fmt.Errorf("failed to create gitea client, error: %v", err) | ||
} else { | ||
err = release(client, r.repo, version, commitish, draft, prerelease) | ||
} | ||
return | ||
} |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package internal_scm | ||
|
||
import ( | ||
"github.com/jenkins-x/go-scm/scm/driver/gitlab" | ||
"github.com/jenkins-x/go-scm/scm/transport" | ||
"net/http" | ||
) | ||
|
||
type Gitlab struct { | ||
repo string | ||
token string | ||
} | ||
|
||
// NewGitlab creates a new instance | ||
func NewGitlab(repo, token string) *Gitlab { | ||
return &Gitlab{ | ||
repo: repo, | ||
token: token, | ||
} | ||
} | ||
|
||
func (r *Gitlab) Release(version, commitish string, draft, prerelease bool) (err error) { | ||
client := gitlab.NewDefault() | ||
client.Client = &http.Client{ | ||
Transport: &transport.BearerToken{ | ||
Token: r.token, | ||
}, | ||
} | ||
err = release(client, r.repo, version, commitish, draft, prerelease) | ||
return | ||
} |
Oops, something went wrong.