package setup import ( "context" "net/url" "code.gitea.io/sdk/gitea" giteaAdapter "forge.cadoles.com/wpetit/clearcase/internal/adapter/gitea" "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/bornholm/genai/llm/provider" "github.com/pkg/errors" _ "github.com/bornholm/genai/llm/provider/openai" _ "github.com/bornholm/genai/llm/provider/openrouter" ) func NewIssueManagerFromConfig(ctx context.Context, conf *config.Config) (*service.IssueManager, error) { llmCtx := provider.FromMap(ctx, "", map[string]string{ string(provider.ContextKeyAPIBaseURL): conf.LLM.Provider.BaseURL, string(provider.ContextKeyAPIKey): conf.LLM.Provider.Key, string(provider.ContextKeyModel): conf.LLM.Provider.Model, }) client, err := provider.Create(llmCtx, provider.Name(conf.LLM.Provider.Name)) if err != nil { return nil, errors.Wrapf(err, "could not create llm client '%s'", conf.LLM.Provider.Name) } 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 }, }) } issueManager := service.NewIssueManager(client, forgeFactories...) return issueManager, nil }