Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/garm-cli/cmd/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
2 changes: 1 addition & 1 deletion cmd/garm-cli/cmd/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
3 changes: 1 addition & 2 deletions doc/gitea.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const (
)

const (
AutoEndpointType EndpointType = ""
GithubEndpointType EndpointType = "github"
GiteaEndpointType EndpointType = "gitea"
)
Expand Down
32 changes: 14 additions & 18 deletions params/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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:
Expand All @@ -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")
Expand All @@ -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:
Expand Down
31 changes: 31 additions & 0 deletions runner/common.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 2 additions & 2 deletions runner/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ 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)
case params.GiteaEndpointType:
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 {
Expand Down
4 changes: 2 additions & 2 deletions runner/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading