mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-01-01 13:18:41 +00:00
d494b6a959
This is the first step towards support for multiple forges (#138). It inserts a forge using the currently existing env varaibles into db and uses this forge from db later on in all places of the code. closes #621 addresses #138 # TODO - [x] add forges table - [x] add id of forge to repo - [x] use forge of repo - [x] add forge from env vars to db if not exists - [x] migrate repo.ForgeID to the newly generated forge - [x] support cache with forge from repo - [x] maybe add forge loading cache? (use LRU cache for forges, I expect users to have less than 10 forges normally) --------- Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
134 lines
3.6 KiB
Go
134 lines
3.6 KiB
Go
package setup
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge"
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge/addon"
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge/bitbucket"
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge/bitbucketdatacenter"
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge/gitea"
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge/github"
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge/gitlab"
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
)
|
|
|
|
func Forge(forge *model.Forge) (forge.Forge, error) {
|
|
switch forge.Type {
|
|
case model.ForgeTypeAddon:
|
|
return setupAddon(forge)
|
|
case model.ForgeTypeGithub:
|
|
return setupGitHub(forge)
|
|
case model.ForgeTypeGitlab:
|
|
return setupGitLab(forge)
|
|
case model.ForgeTypeBitbucket:
|
|
return setupBitbucket(forge)
|
|
case model.ForgeTypeGitea:
|
|
return setupGitea(forge)
|
|
case model.ForgeTypeBitbucketDatacenter:
|
|
return setupBitbucketDatacenter(forge)
|
|
default:
|
|
return nil, fmt.Errorf("forge not configured")
|
|
}
|
|
}
|
|
|
|
func setupBitbucket(forge *model.Forge) (forge.Forge, error) {
|
|
opts := &bitbucket.Opts{
|
|
Client: forge.Client,
|
|
Secret: forge.ClientSecret,
|
|
}
|
|
log.Trace().Msgf("Forge (bitbucket) opts: %#v", opts)
|
|
return bitbucket.New(opts)
|
|
}
|
|
|
|
func setupGitea(forge *model.Forge) (forge.Forge, error) {
|
|
server, err := url.Parse(forge.URL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
oauthURL, ok := forge.AdditionalOptions["oauth-server"].(string)
|
|
if !ok {
|
|
return nil, fmt.Errorf("missing oauth-server")
|
|
}
|
|
|
|
opts := gitea.Opts{
|
|
URL: strings.TrimRight(server.String(), "/"),
|
|
Client: forge.Client,
|
|
Secret: forge.ClientSecret,
|
|
SkipVerify: forge.SkipVerify,
|
|
OAuth2URL: oauthURL,
|
|
}
|
|
if len(opts.URL) == 0 {
|
|
return nil, fmt.Errorf("WOODPECKER_GITEA_URL must be set")
|
|
}
|
|
log.Trace().Msgf("Forge (gitea) opts: %#v", opts)
|
|
return gitea.New(opts)
|
|
}
|
|
|
|
func setupGitLab(forge *model.Forge) (forge.Forge, error) {
|
|
return gitlab.New(gitlab.Opts{
|
|
URL: forge.URL,
|
|
ClientID: forge.Client,
|
|
ClientSecret: forge.ClientSecret,
|
|
SkipVerify: forge.SkipVerify,
|
|
})
|
|
}
|
|
|
|
func setupGitHub(forge *model.Forge) (forge.Forge, error) {
|
|
mergeRef, ok := forge.AdditionalOptions["merge-ref"].(bool)
|
|
if !ok {
|
|
return nil, fmt.Errorf("missing merge-ref")
|
|
}
|
|
|
|
publicOnly, ok := forge.AdditionalOptions["public-only"].(bool)
|
|
if !ok {
|
|
return nil, fmt.Errorf("missing public-only")
|
|
}
|
|
|
|
opts := github.Opts{
|
|
URL: forge.URL,
|
|
Client: forge.Client,
|
|
Secret: forge.ClientSecret,
|
|
SkipVerify: forge.SkipVerify,
|
|
MergeRef: mergeRef,
|
|
OnlyPublic: publicOnly,
|
|
}
|
|
log.Trace().Msgf("Forge (github) opts: %#v", opts)
|
|
return github.New(opts)
|
|
}
|
|
|
|
func setupBitbucketDatacenter(forge *model.Forge) (forge.Forge, error) {
|
|
gitUsername, ok := forge.AdditionalOptions["git-username"].(string)
|
|
if !ok {
|
|
return nil, fmt.Errorf("missing git-username")
|
|
}
|
|
gitPassword, ok := forge.AdditionalOptions["git-password"].(string)
|
|
if !ok {
|
|
return nil, fmt.Errorf("missing git-password")
|
|
}
|
|
|
|
opts := bitbucketdatacenter.Opts{
|
|
URL: forge.URL,
|
|
ClientID: forge.Client,
|
|
ClientSecret: forge.ClientSecret,
|
|
Username: gitUsername,
|
|
Password: gitPassword,
|
|
}
|
|
log.Trace().Msgf("Forge (bitbucketdatacenter) opts: %#v", opts)
|
|
return bitbucketdatacenter.New(opts)
|
|
}
|
|
|
|
func setupAddon(forge *model.Forge) (forge.Forge, error) {
|
|
executable, ok := forge.AdditionalOptions["executable"].(string)
|
|
if !ok {
|
|
return nil, fmt.Errorf("missing git-username")
|
|
}
|
|
|
|
log.Trace().Msgf("Forge (addon) executable: %#v", executable)
|
|
return addon.Load(executable)
|
|
}
|