Merge pull request #416 from ChristopherHX/optional-forge-type-for-gitea

Create Repo / Org make --forge-type optional
This commit is contained in:
Gabriel 2025-05-29 20:04:51 +03:00 committed by GitHub
commit c19bf2f9f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 53 additions and 26 deletions

View file

@ -308,7 +308,7 @@ func init() {
orgAddCmd.Flags().StringVar(&orgName, "name", "", "The name of the organization")
orgAddCmd.Flags().StringVar(&poolBalancerType, "pool-balancer-type", string(params.PoolBalancerTypeRoundRobin), "The balancing strategy to use when creating runners in pools matching requested labels.")
orgAddCmd.Flags().StringVar(&orgWebhookSecret, "webhook-secret", "", "The webhook secret for this organization")
orgAddCmd.Flags().StringVar(&forgeType, "forge-type", string(params.GithubEndpointType), "The forge type of the organization. Supported values: github, gitea.")
orgAddCmd.Flags().StringVar(&forgeType, "forge-type", "", "The forge type of the organization. Supported values: github, gitea.")
orgAddCmd.Flags().StringVar(&orgCreds, "credentials", "", "Credentials name. See credentials list.")
orgAddCmd.Flags().BoolVar(&orgRandomWebhookSecret, "random-webhook-secret", false, "Generate a random webhook secret for this organization.")
orgAddCmd.Flags().BoolVar(&installOrgWebhook, "install-webhook", false, "Install the webhook as part of the add operation.")

View file

@ -312,7 +312,7 @@ func init() {
repoAddCmd.Flags().StringVar(&repoOwner, "owner", "", "The owner of this repository")
repoAddCmd.Flags().StringVar(&poolBalancerType, "pool-balancer-type", string(params.PoolBalancerTypeRoundRobin), "The balancing strategy to use when creating runners in pools matching requested labels.")
repoAddCmd.Flags().StringVar(&repoName, "name", "", "The name of the repository")
repoAddCmd.Flags().StringVar(&forgeType, "forge-type", string(params.GithubEndpointType), "The forge type of the repository. Supported values: github, gitea.")
repoAddCmd.Flags().StringVar(&forgeType, "forge-type", "", "The forge type of the repository. Supported values: github, gitea.")
repoAddCmd.Flags().StringVar(&repoWebhookSecret, "webhook-secret", "", "The webhook secret for this repository")
repoAddCmd.Flags().StringVar(&repoCreds, "credentials", "", "Credentials name. See credentials list.")
repoAddCmd.Flags().BoolVar(&randomWebhookSecret, "random-webhook-secret", false, "Generate a random webhook secret for this repository.")

View file

@ -295,8 +295,7 @@ garm-cli repo add \
--name testrepo \
--owner testorg \
--random-webhook-secret \
--install-webhook \
--forge-type gitea
--install-webhook
```
Make a note of the repo UUID. You will need it when adding a pool.

View file

@ -78,6 +78,7 @@ const (
)
const (
AutoEndpointType EndpointType = ""
GithubEndpointType EndpointType = "github"
GiteaEndpointType EndpointType = "gitea"
)

View file

@ -48,15 +48,6 @@ type CreateRepoParams struct {
ForgeType EndpointType `json:"forge_type,omitempty"`
}
func (c CreateRepoParams) GetForgeType() EndpointType {
switch c.ForgeType {
case GithubEndpointType, GiteaEndpointType:
return c.ForgeType
default:
return GithubEndpointType
}
}
func (c *CreateRepoParams) Validate() error {
if c.Owner == "" {
return runnerErrors.NewBadRequestError("missing owner")
@ -73,6 +64,13 @@ func (c *CreateRepoParams) Validate() error {
return runnerErrors.NewMissingSecretError("missing secret")
}
switch c.ForgeType {
case GithubEndpointType, GiteaEndpointType, AutoEndpointType:
break
default:
return runnerErrors.NewBadRequestError("invalid forge type")
}
switch c.PoolBalancerType {
case PoolBalancerTypeRoundRobin, PoolBalancerTypePack, PoolBalancerTypeNone:
default:
@ -90,15 +88,6 @@ type CreateOrgParams struct {
ForgeType EndpointType `json:"forge_type,omitempty"`
}
func (c CreateOrgParams) GetForgeType() EndpointType {
switch c.ForgeType {
case GithubEndpointType, GiteaEndpointType:
return c.ForgeType
default:
return GithubEndpointType
}
}
func (c *CreateOrgParams) Validate() error {
if c.Name == "" {
return runnerErrors.NewBadRequestError("missing org name")
@ -111,6 +100,13 @@ func (c *CreateOrgParams) Validate() error {
return runnerErrors.NewMissingSecretError("missing secret")
}
switch c.ForgeType {
case GithubEndpointType, GiteaEndpointType, AutoEndpointType:
break
default:
return runnerErrors.NewBadRequestError("invalid forge type")
}
switch c.PoolBalancerType {
case PoolBalancerTypeRoundRobin, PoolBalancerTypePack, PoolBalancerTypeNone:
default:

31
runner/common.go Normal file
View file

@ -0,0 +1,31 @@
package runner
import (
"context"
"github.com/pkg/errors"
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
"github.com/cloudbase/garm/params"
)
func (r *Runner) ResolveForgeCredentialByName(ctx context.Context, credentialsName string) (params.ForgeCredentials, error) {
githubCred, err := r.store.GetGithubCredentialsByName(ctx, credentialsName, false)
if err != nil && !errors.Is(err, runnerErrors.ErrNotFound) {
return params.ForgeCredentials{}, errors.Wrap(err, "fetching github credentials")
}
giteaCred, err := r.store.GetGiteaCredentialsByName(ctx, credentialsName, false)
if err != nil && !errors.Is(err, runnerErrors.ErrNotFound) {
return params.ForgeCredentials{}, errors.Wrap(err, "fetching gitea credentials")
}
if githubCred.ID != 0 && giteaCred.ID != 0 {
return params.ForgeCredentials{}, runnerErrors.NewBadRequestError("credentials %s are defined for both GitHub and Gitea, please specify the forge type", credentialsName)
}
if githubCred.ID != 0 {
return githubCred, nil
}
if giteaCred.ID != 0 {
return giteaCred, nil
}
return params.ForgeCredentials{}, runnerErrors.NewBadRequestError("credentials %s not found", credentialsName)
}

View file

@ -39,7 +39,7 @@ func (r *Runner) CreateOrganization(ctx context.Context, param params.CreateOrgP
}
var creds params.ForgeCredentials
switch param.GetForgeType() {
switch param.ForgeType {
case params.GithubEndpointType:
slog.DebugContext(ctx, "getting github credentials")
creds, err = r.store.GetGithubCredentialsByName(ctx, param.CredentialsName, true)
@ -47,7 +47,7 @@ func (r *Runner) CreateOrganization(ctx context.Context, param params.CreateOrgP
slog.DebugContext(ctx, "getting gitea credentials")
creds, err = r.store.GetGiteaCredentialsByName(ctx, param.CredentialsName, true)
default:
return params.Organization{}, runnerErrors.NewBadRequestError("invalid forge type: %s", param.GetForgeType())
creds, err = r.ResolveForgeCredentialByName(ctx, param.CredentialsName)
}
if err != nil {

View file

@ -39,13 +39,13 @@ func (r *Runner) CreateRepository(ctx context.Context, param params.CreateRepoPa
}
var creds params.ForgeCredentials
switch param.GetForgeType() {
switch param.ForgeType {
case params.GithubEndpointType:
creds, err = r.store.GetGithubCredentialsByName(ctx, param.CredentialsName, true)
case params.GiteaEndpointType:
creds, err = r.store.GetGiteaCredentialsByName(ctx, param.CredentialsName, true)
default:
return params.Repository{}, runnerErrors.NewBadRequestError("invalid forge type: %s", param.GetForgeType())
creds, err = r.ResolveForgeCredentialByName(ctx, param.CredentialsName)
}
if err != nil {