clearcase/internal/setup/forge_factories.go

59 lines
1.8 KiB
Go

package setup
import (
"context"
"net/url"
"code.gitea.io/sdk/gitea"
giteaAdapter "forge.cadoles.com/wpetit/clearcase/internal/adapter/gitea"
githubAdapter "forge.cadoles.com/wpetit/clearcase/internal/adapter/github"
"forge.cadoles.com/wpetit/clearcase/internal/config"
"forge.cadoles.com/wpetit/clearcase/internal/core/model"
"forge.cadoles.com/wpetit/clearcase/internal/core/port"
"forge.cadoles.com/wpetit/clearcase/internal/core/service"
"github.com/google/go-github/v69/github"
"github.com/pkg/errors"
)
func getForgeFactories(conf *config.Config) ([]service.ForgeFactory, error) {
forgeFactories := make([]service.ForgeFactory, 0)
if conf.Auth.Providers.Gitea.Key != "" && conf.Auth.Providers.Gitea.Secret != "" {
baseURL, err := url.Parse(conf.Auth.Providers.Gitea.AuthURL)
if err != nil {
return nil, errors.Wrapf(err, "could not parse gitea auth url '%s'", conf.Auth.Providers.Gitea.AuthURL)
}
baseURL.Path = ""
forgeFactories = append(forgeFactories, &authProviderBasedForgeFactory{
provider: "gitea",
create: func(ctx context.Context, user *model.User) (port.Forge, error) {
client, err := gitea.NewClient(baseURL.String(), gitea.SetToken(user.AccessToken))
if err != nil {
return nil, errors.WithStack(err)
}
forge := giteaAdapter.NewForge(client)
return forge, nil
},
})
}
if conf.Auth.Providers.Github.Key != "" && conf.Auth.Providers.Github.Secret != "" {
forgeFactories = append(forgeFactories, &authProviderBasedForgeFactory{
provider: "github",
create: func(ctx context.Context, user *model.User) (port.Forge, error) {
client := github.NewClient(nil).WithAuthToken(user.AccessToken)
forge := githubAdapter.NewForge(client)
return forge, nil
},
})
}
return forgeFactories, nil
}