mirror of https://github.com/harness/drone.git
Feat: implemented gitee client (#3156)
* Feature: implemented gitee client implemented gitee client * update Gitee struct * inject netrc.Password when driver is gitee * auto genarate gitee redirectURL * update go-login and go-scmpull/3163/head
parent
6b0a6d4f7e
commit
2b77d96600
|
@ -86,6 +86,7 @@ type (
|
||||||
GitLab GitLab
|
GitLab GitLab
|
||||||
Gogs Gogs
|
Gogs Gogs
|
||||||
Stash Stash
|
Stash Stash
|
||||||
|
Gitee Gitee
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cloning provides the cloning configuration.
|
// Cloning provides the cloning configuration.
|
||||||
|
@ -363,6 +364,18 @@ type (
|
||||||
Debug bool `envconfig:"DRONE_GITHUB_DEBUG"`
|
Debug bool `envconfig:"DRONE_GITHUB_DEBUG"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Gitee providers the gitee client configuration.
|
||||||
|
Gitee struct {
|
||||||
|
Server string `envconfig:"DRONE_GITEE_SERVER" default:"https://gitee.com"`
|
||||||
|
APIServer string `envconfig:"DRONE_GITEE_API_SERVER" default:"https://gitee.com/api/v5"`
|
||||||
|
ClientID string `envconfig:"DRONE_GITEE_CLIENT_ID"`
|
||||||
|
ClientSecret string `envconfig:"DRONE_GITEE_CLIENT_SECRET"`
|
||||||
|
RedirectURL string `envconfig:"DRONE_GITEE_REDIRECT_URL"`
|
||||||
|
SkipVerify bool `envconfig:"DRONE_GITEE_SKIP_VERIFY"`
|
||||||
|
Scope []string `envconfig:"DRONE_GITEE_SCOPE" default:"user_info,projects,pull_requests,hook"`
|
||||||
|
Debug bool `envconfig:"DRONE_GITEE_DEBUG"`
|
||||||
|
}
|
||||||
|
|
||||||
// GitLab provides the gitlab client configuration.
|
// GitLab provides the gitlab client configuration.
|
||||||
GitLab struct {
|
GitLab struct {
|
||||||
Server string `envconfig:"DRONE_GITLAB_SERVER" default:"https://gitlab.com"`
|
Server string `envconfig:"DRONE_GITLAB_SERVER" default:"https://gitlab.com"`
|
||||||
|
|
|
@ -28,6 +28,7 @@ import (
|
||||||
"github.com/drone/go-scm/scm"
|
"github.com/drone/go-scm/scm"
|
||||||
"github.com/drone/go-scm/scm/driver/bitbucket"
|
"github.com/drone/go-scm/scm/driver/bitbucket"
|
||||||
"github.com/drone/go-scm/scm/driver/gitea"
|
"github.com/drone/go-scm/scm/driver/gitea"
|
||||||
|
"github.com/drone/go-scm/scm/driver/gitee"
|
||||||
"github.com/drone/go-scm/scm/driver/github"
|
"github.com/drone/go-scm/scm/driver/github"
|
||||||
"github.com/drone/go-scm/scm/driver/gitlab"
|
"github.com/drone/go-scm/scm/driver/gitlab"
|
||||||
"github.com/drone/go-scm/scm/driver/gogs"
|
"github.com/drone/go-scm/scm/driver/gogs"
|
||||||
|
@ -53,6 +54,8 @@ func provideClient(config config.Config) *scm.Client {
|
||||||
return provideBitbucketClient(config)
|
return provideBitbucketClient(config)
|
||||||
case config.Github.ClientID != "":
|
case config.Github.ClientID != "":
|
||||||
return provideGithubClient(config)
|
return provideGithubClient(config)
|
||||||
|
case config.Gitee.ClientID != "":
|
||||||
|
return provideGiteeClient(config)
|
||||||
case config.Gitea.Server != "":
|
case config.Gitea.Server != "":
|
||||||
return provideGiteaClient(config)
|
return provideGiteaClient(config)
|
||||||
case config.GitLab.ClientID != "":
|
case config.GitLab.ClientID != "":
|
||||||
|
@ -107,6 +110,26 @@ func provideGithubClient(config config.Config) *scm.Client {
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// provideGiteeClient is a Wire provider function that returns
|
||||||
|
// a Gitee client based on the environment configuration.
|
||||||
|
func provideGiteeClient(config config.Config) *scm.Client {
|
||||||
|
client, err := gitee.New(config.Gitee.APIServer)
|
||||||
|
if err != nil {
|
||||||
|
logrus.WithError(err).
|
||||||
|
Fatalln("main: cannot create the Gitee client")
|
||||||
|
}
|
||||||
|
if config.Gitee.Debug {
|
||||||
|
client.DumpResponse = httputil.DumpResponse
|
||||||
|
}
|
||||||
|
client.Client = &http.Client{
|
||||||
|
Transport: &oauth2.Transport{
|
||||||
|
Source: oauth2.ContextTokenSource(),
|
||||||
|
Base: defaultTransport(config.Gitee.SkipVerify),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
// provideGiteaClient is a Wire provider function that returns
|
// provideGiteaClient is a Wire provider function that returns
|
||||||
// a Gitea client based on the environment configuration.
|
// a Gitea client based on the environment configuration.
|
||||||
func provideGiteaClient(config config.Config) *scm.Client {
|
func provideGiteaClient(config config.Config) *scm.Client {
|
||||||
|
|
|
@ -19,6 +19,7 @@ import (
|
||||||
"github.com/drone/go-login/login"
|
"github.com/drone/go-login/login"
|
||||||
"github.com/drone/go-login/login/bitbucket"
|
"github.com/drone/go-login/login/bitbucket"
|
||||||
"github.com/drone/go-login/login/gitea"
|
"github.com/drone/go-login/login/gitea"
|
||||||
|
"github.com/drone/go-login/login/gitee"
|
||||||
"github.com/drone/go-login/login/github"
|
"github.com/drone/go-login/login/github"
|
||||||
"github.com/drone/go-login/login/gitlab"
|
"github.com/drone/go-login/login/gitlab"
|
||||||
"github.com/drone/go-login/login/gogs"
|
"github.com/drone/go-login/login/gogs"
|
||||||
|
@ -44,6 +45,8 @@ func provideLogin(config config.Config) login.Middleware {
|
||||||
return provideBitbucketLogin(config)
|
return provideBitbucketLogin(config)
|
||||||
case config.Github.ClientID != "":
|
case config.Github.ClientID != "":
|
||||||
return provideGithubLogin(config)
|
return provideGithubLogin(config)
|
||||||
|
case config.Gitee.ClientID != "":
|
||||||
|
return provideGiteeLogin(config)
|
||||||
case config.Gitea.Server != "":
|
case config.Gitea.Server != "":
|
||||||
return provideGiteaLogin(config)
|
return provideGiteaLogin(config)
|
||||||
case config.GitLab.ClientID != "":
|
case config.GitLab.ClientID != "":
|
||||||
|
@ -87,6 +90,26 @@ func provideGithubLogin(config config.Config) login.Middleware {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// provideGiteeLogin is a Wire provider function that returns
|
||||||
|
// a Gitee authenticator based on the environment configuration.
|
||||||
|
func provideGiteeLogin(config config.Config) login.Middleware {
|
||||||
|
if config.Gitee.ClientID == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
redirectURL := config.Gitee.RedirectURL
|
||||||
|
if redirectURL == "" {
|
||||||
|
redirectURL = config.Server.Addr + "/login"
|
||||||
|
}
|
||||||
|
return &gitee.Config{
|
||||||
|
ClientID: config.Gitee.ClientID,
|
||||||
|
ClientSecret: config.Gitee.ClientSecret,
|
||||||
|
RedirectURL: redirectURL,
|
||||||
|
Server: config.Gitee.Server,
|
||||||
|
Scope: config.Gitee.Scope,
|
||||||
|
Client: defaultClient(config.Gitee.SkipVerify),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// provideGiteaLogin is a Wire provider function that returns
|
// provideGiteaLogin is a Wire provider function that returns
|
||||||
// a Gitea authenticator based on the environment configuration.
|
// a Gitea authenticator based on the environment configuration.
|
||||||
func provideGiteaLogin(config config.Config) login.Middleware {
|
func provideGiteaLogin(config config.Config) login.Middleware {
|
||||||
|
|
4
go.mod
4
go.mod
|
@ -19,8 +19,8 @@ require (
|
||||||
github.com/drone/envsubst v1.0.3-0.20200709231038-aa43e1c1a629
|
github.com/drone/envsubst v1.0.3-0.20200709231038-aa43e1c1a629
|
||||||
github.com/drone/funcmap v0.0.0-20210823160631-9e9dec149056
|
github.com/drone/funcmap v0.0.0-20210823160631-9e9dec149056
|
||||||
github.com/drone/go-license v1.0.2
|
github.com/drone/go-license v1.0.2
|
||||||
github.com/drone/go-login v1.0.4-0.20190311170324-2a4df4f242a2
|
github.com/drone/go-login v1.1.0
|
||||||
github.com/drone/go-scm v1.15.2
|
github.com/drone/go-scm v1.16.1
|
||||||
github.com/drone/signal v1.0.0
|
github.com/drone/signal v1.0.0
|
||||||
github.com/dustin/go-humanize v1.0.0
|
github.com/dustin/go-humanize v1.0.0
|
||||||
github.com/go-chi/chi v3.3.3+incompatible
|
github.com/go-chi/chi v3.3.3+incompatible
|
||||||
|
|
8
go.sum
8
go.sum
|
@ -90,10 +90,10 @@ github.com/drone/funcmap v0.0.0-20210823160631-9e9dec149056 h1:SCJwMR0FMA0aKwAnt
|
||||||
github.com/drone/funcmap v0.0.0-20210823160631-9e9dec149056/go.mod h1:Hph0/pT6ZxbujnE1Z6/08p5I0XXuOsppqF6NQlGOK0E=
|
github.com/drone/funcmap v0.0.0-20210823160631-9e9dec149056/go.mod h1:Hph0/pT6ZxbujnE1Z6/08p5I0XXuOsppqF6NQlGOK0E=
|
||||||
github.com/drone/go-license v1.0.2 h1:7OwndfYk+Lp/cGHkxe4HUn/Ysrrw3WYH2pnd99yrkok=
|
github.com/drone/go-license v1.0.2 h1:7OwndfYk+Lp/cGHkxe4HUn/Ysrrw3WYH2pnd99yrkok=
|
||||||
github.com/drone/go-license v1.0.2/go.mod h1:fGRHf+F1cEaw3YVYiJ6js3G3dVhcxyS617RnNRUMsms=
|
github.com/drone/go-license v1.0.2/go.mod h1:fGRHf+F1cEaw3YVYiJ6js3G3dVhcxyS617RnNRUMsms=
|
||||||
github.com/drone/go-login v1.0.4-0.20190311170324-2a4df4f242a2 h1:RGpgNkowJc5LAVn/ZONx70qmnaTA0z/3hHPzTBdAEO8=
|
github.com/drone/go-login v1.1.0 h1:anQFRh2Z5ketEJ/LvL6SJ6rIwDdfysGXK5bSXkFLInI=
|
||||||
github.com/drone/go-login v1.0.4-0.20190311170324-2a4df4f242a2/go.mod h1:FLxy9vRzLbyBxoCJYxGbG9R0WGn6OyuvBmAtYNt43uw=
|
github.com/drone/go-login v1.1.0/go.mod h1:FLxy9vRzLbyBxoCJYxGbG9R0WGn6OyuvBmAtYNt43uw=
|
||||||
github.com/drone/go-scm v1.15.2 h1:H5iDEGTYZwb/n3sx+ekbXHOwqi/3sYTJJunJUiXLAVA=
|
github.com/drone/go-scm v1.16.1 h1:OuCJ/d9iUzxSDqryOn5EW2sdOK92gSSMLhMe9Vf+r3c=
|
||||||
github.com/drone/go-scm v1.15.2/go.mod h1:lXwfbyrIJwFFME5TpzavkwO2T5X8yBK6t6cve7g91x0=
|
github.com/drone/go-scm v1.16.1/go.mod h1:DFIJJjhMj0TSXPz+0ni4nyZ9gtTtC40Vh/TGRugtyWw=
|
||||||
github.com/drone/signal v1.0.0 h1:NrnM2M/4yAuU/tXs6RP1a1ZfxnaHwYkd0kJurA1p6uI=
|
github.com/drone/signal v1.0.0 h1:NrnM2M/4yAuU/tXs6RP1a1ZfxnaHwYkd0kJurA1p6uI=
|
||||||
github.com/drone/signal v1.0.0/go.mod h1:S8t92eFT0g4WUgEc/LxG+LCuiskpMNsG0ajAMGnyZpc=
|
github.com/drone/signal v1.0.0/go.mod h1:S8t92eFT0g4WUgEc/LxG+LCuiskpMNsG0ajAMGnyZpc=
|
||||||
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
|
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
|
||||||
|
|
|
@ -77,7 +77,7 @@ func (s *Service) Create(ctx context.Context, user *core.User, repo *core.Reposi
|
||||||
}
|
}
|
||||||
|
|
||||||
switch s.client.Driver {
|
switch s.client.Driver {
|
||||||
case scm.DriverGitlab:
|
case scm.DriverGitlab, scm.DriverGitee:
|
||||||
netrc.Login = "oauth2"
|
netrc.Login = "oauth2"
|
||||||
netrc.Password = user.Token
|
netrc.Password = user.Token
|
||||||
case scm.DriverBitbucket:
|
case scm.DriverBitbucket:
|
||||||
|
|
|
@ -141,6 +141,37 @@ func TestNetrc_Bitbucket(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNetrc_Gitee(t *testing.T) {
|
||||||
|
controller := gomock.NewController(t)
|
||||||
|
defer controller.Finish()
|
||||||
|
|
||||||
|
mockRepo := &core.Repository{Private: true, HTTPURL: "https://gitee.com/kit101/drone-yml-test"}
|
||||||
|
mockUser := &core.User{
|
||||||
|
Token: "755bb80e5b",
|
||||||
|
Refresh: "e08f3fa43e",
|
||||||
|
}
|
||||||
|
mockRenewer := mock.NewMockRenewer(controller)
|
||||||
|
mockRenewer.EXPECT().Renew(gomock.Any(), mockUser, true)
|
||||||
|
|
||||||
|
s := Service{
|
||||||
|
renewer: mockRenewer,
|
||||||
|
client: &scm.Client{Driver: scm.DriverGitee},
|
||||||
|
}
|
||||||
|
got, err := s.Create(noContext, mockUser, mockRepo)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
want := &core.Netrc{
|
||||||
|
Machine: "gitee.com",
|
||||||
|
Login: "oauth2",
|
||||||
|
Password: "755bb80e5b",
|
||||||
|
}
|
||||||
|
if diff := cmp.Diff(got, want); diff != "" {
|
||||||
|
t.Errorf(diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNetrc_Nil(t *testing.T) {
|
func TestNetrc_Nil(t *testing.T) {
|
||||||
s := Service{
|
s := Service{
|
||||||
private: false,
|
private: false,
|
||||||
|
|
Loading…
Reference in New Issue