Skip to content

Commit c19bf2f

Browse files
Merge pull request #416 from ChristopherHX/optional-forge-type-for-gitea
Create Repo / Org make --forge-type optional
2 parents 18ef27b + efd725e commit c19bf2f

8 files changed

Lines changed: 53 additions & 26 deletions

File tree

cmd/garm-cli/cmd/organization.go

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

cmd/garm-cli/cmd/repository.go

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

doc/gitea.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,7 @@ garm-cli repo add \
295295
--name testrepo \
296296
--owner testorg \
297297
--random-webhook-secret \
298-
--install-webhook \
299-
--forge-type gitea
298+
--install-webhook
300299
```
301300
302301
Make a note of the repo UUID. You will need it when adding a pool.

params/params.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ const (
7878
)
7979

8080
const (
81+
AutoEndpointType EndpointType = ""
8182
GithubEndpointType EndpointType = "github"
8283
GiteaEndpointType EndpointType = "gitea"
8384
)

params/requests.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,6 @@ type CreateRepoParams struct {
4848
ForgeType EndpointType `json:"forge_type,omitempty"`
4949
}
5050

51-
func (c CreateRepoParams) GetForgeType() EndpointType {
52-
switch c.ForgeType {
53-
case GithubEndpointType, GiteaEndpointType:
54-
return c.ForgeType
55-
default:
56-
return GithubEndpointType
57-
}
58-
}
59-
6051
func (c *CreateRepoParams) Validate() error {
6152
if c.Owner == "" {
6253
return runnerErrors.NewBadRequestError("missing owner")
@@ -73,6 +64,13 @@ func (c *CreateRepoParams) Validate() error {
7364
return runnerErrors.NewMissingSecretError("missing secret")
7465
}
7566

67+
switch c.ForgeType {
68+
case GithubEndpointType, GiteaEndpointType, AutoEndpointType:
69+
break
70+
default:
71+
return runnerErrors.NewBadRequestError("invalid forge type")
72+
}
73+
7674
switch c.PoolBalancerType {
7775
case PoolBalancerTypeRoundRobin, PoolBalancerTypePack, PoolBalancerTypeNone:
7876
default:
@@ -90,15 +88,6 @@ type CreateOrgParams struct {
9088
ForgeType EndpointType `json:"forge_type,omitempty"`
9189
}
9290

93-
func (c CreateOrgParams) GetForgeType() EndpointType {
94-
switch c.ForgeType {
95-
case GithubEndpointType, GiteaEndpointType:
96-
return c.ForgeType
97-
default:
98-
return GithubEndpointType
99-
}
100-
}
101-
10291
func (c *CreateOrgParams) Validate() error {
10392
if c.Name == "" {
10493
return runnerErrors.NewBadRequestError("missing org name")
@@ -111,6 +100,13 @@ func (c *CreateOrgParams) Validate() error {
111100
return runnerErrors.NewMissingSecretError("missing secret")
112101
}
113102

103+
switch c.ForgeType {
104+
case GithubEndpointType, GiteaEndpointType, AutoEndpointType:
105+
break
106+
default:
107+
return runnerErrors.NewBadRequestError("invalid forge type")
108+
}
109+
114110
switch c.PoolBalancerType {
115111
case PoolBalancerTypeRoundRobin, PoolBalancerTypePack, PoolBalancerTypeNone:
116112
default:

runner/common.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package runner
2+
3+
import (
4+
"context"
5+
6+
"github.com/pkg/errors"
7+
8+
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
9+
"github.com/cloudbase/garm/params"
10+
)
11+
12+
func (r *Runner) ResolveForgeCredentialByName(ctx context.Context, credentialsName string) (params.ForgeCredentials, error) {
13+
githubCred, err := r.store.GetGithubCredentialsByName(ctx, credentialsName, false)
14+
if err != nil && !errors.Is(err, runnerErrors.ErrNotFound) {
15+
return params.ForgeCredentials{}, errors.Wrap(err, "fetching github credentials")
16+
}
17+
giteaCred, err := r.store.GetGiteaCredentialsByName(ctx, credentialsName, false)
18+
if err != nil && !errors.Is(err, runnerErrors.ErrNotFound) {
19+
return params.ForgeCredentials{}, errors.Wrap(err, "fetching gitea credentials")
20+
}
21+
if githubCred.ID != 0 && giteaCred.ID != 0 {
22+
return params.ForgeCredentials{}, runnerErrors.NewBadRequestError("credentials %s are defined for both GitHub and Gitea, please specify the forge type", credentialsName)
23+
}
24+
if githubCred.ID != 0 {
25+
return githubCred, nil
26+
}
27+
if giteaCred.ID != 0 {
28+
return giteaCred, nil
29+
}
30+
return params.ForgeCredentials{}, runnerErrors.NewBadRequestError("credentials %s not found", credentialsName)
31+
}

runner/organizations.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ func (r *Runner) CreateOrganization(ctx context.Context, param params.CreateOrgP
3939
}
4040

4141
var creds params.ForgeCredentials
42-
switch param.GetForgeType() {
42+
switch param.ForgeType {
4343
case params.GithubEndpointType:
4444
slog.DebugContext(ctx, "getting github credentials")
4545
creds, err = r.store.GetGithubCredentialsByName(ctx, param.CredentialsName, true)
4646
case params.GiteaEndpointType:
4747
slog.DebugContext(ctx, "getting gitea credentials")
4848
creds, err = r.store.GetGiteaCredentialsByName(ctx, param.CredentialsName, true)
4949
default:
50-
return params.Organization{}, runnerErrors.NewBadRequestError("invalid forge type: %s", param.GetForgeType())
50+
creds, err = r.ResolveForgeCredentialByName(ctx, param.CredentialsName)
5151
}
5252

5353
if err != nil {

runner/repositories.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ func (r *Runner) CreateRepository(ctx context.Context, param params.CreateRepoPa
3939
}
4040

4141
var creds params.ForgeCredentials
42-
switch param.GetForgeType() {
42+
switch param.ForgeType {
4343
case params.GithubEndpointType:
4444
creds, err = r.store.GetGithubCredentialsByName(ctx, param.CredentialsName, true)
4545
case params.GiteaEndpointType:
4646
creds, err = r.store.GetGiteaCredentialsByName(ctx, param.CredentialsName, true)
4747
default:
48-
return params.Repository{}, runnerErrors.NewBadRequestError("invalid forge type: %s", param.GetForgeType())
48+
creds, err = r.ResolveForgeCredentialByName(ctx, param.CredentialsName)
4949
}
5050

5151
if err != nil {

0 commit comments

Comments
 (0)