Skip to content
This repository was archived by the owner on May 9, 2025. It is now read-only.

Commit e185b91

Browse files
Skarlsophoban01
andauthored
feat: create branch protection rules for gitea (#44)
* feat: create branch protection rules for gitea * Update pkg/providers/gitea/gitea.go Co-authored-by: Piaras Hoban <[email protected]> --------- Co-authored-by: Piaras Hoban <[email protected]>
1 parent df6be72 commit e185b91

File tree

4 files changed

+92
-15
lines changed

4 files changed

+92
-15
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package v1alpha1
2+
3+
const (
4+
// StatusCheckName defines the name of the check a PullRequest will have.
5+
StatusCheckName = "mpas/validation-check"
6+
)

pkg/providers/gitea/gitea.go

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import (
1212
"fmt"
1313

1414
"code.gitea.io/sdk/gitea"
15-
v1 "k8s.io/api/core/v1"
16-
"k8s.io/apimachinery/pkg/types"
17-
"sigs.k8s.io/controller-runtime/pkg/client"
18-
1915
deliveryv1alpha1 "github.com/open-component-model/git-controller/apis/delivery/v1alpha1"
2016
mpasv1alpha1 "github.com/open-component-model/git-controller/apis/mpas/v1alpha1"
2117
"github.com/open-component-model/git-controller/pkg/providers"
18+
v1 "k8s.io/api/core/v1"
19+
"k8s.io/apimachinery/pkg/types"
20+
"sigs.k8s.io/controller-runtime/pkg/client"
21+
"sigs.k8s.io/controller-runtime/pkg/log"
2222
)
2323

2424
const (
@@ -33,7 +33,7 @@ type Client struct {
3333
next providers.Provider
3434
}
3535

36-
// NewClient creates a new GitHub client.
36+
// NewClient creates a new Gitea client.
3737
func NewClient(client client.Client, next providers.Provider) *Client {
3838
return &Client{
3939
client: client,
@@ -170,7 +170,7 @@ func (c *Client) CreatePullRequest(ctx context.Context, branch string, sync deli
170170
domain = repository.Spec.Domain
171171
}
172172

173-
client, err := gitea.NewClient(domain, gitea.SetToken(string(token)))
173+
gclient, err := gitea.NewClient(domain, gitea.SetToken(string(token)))
174174
if err != nil {
175175
return -1, fmt.Errorf("failed to create gitea client: %w", err)
176176
}
@@ -193,7 +193,7 @@ func (c *Client) CreatePullRequest(ctx context.Context, branch string, sync deli
193193
description = sync.Spec.PullRequestTemplate.Description
194194
}
195195

196-
pr, _, err := client.CreatePullRequest(repository.Spec.Owner, repository.GetName(), gitea.CreatePullRequestOption{
196+
pr, _, err := gclient.CreatePullRequest(repository.Spec.Owner, repository.GetName(), gitea.CreatePullRequestOption{
197197
Head: branch,
198198
Base: base,
199199
Title: title,
@@ -206,6 +206,62 @@ func (c *Client) CreatePullRequest(ctx context.Context, branch string, sync deli
206206
return int(pr.ID), nil
207207
}
208208

209-
func (c *Client) CreateBranchProtection(ctx context.Context, obj mpasv1alpha1.Repository) error {
210-
return providers.NotSupportedError
209+
func (c *Client) CreateBranchProtection(ctx context.Context, repository mpasv1alpha1.Repository) error {
210+
logger := log.FromContext(ctx)
211+
212+
logger.Info("using gitea provider to set up branch protection")
213+
214+
if repository.Spec.Provider != providerType {
215+
if c.next == nil {
216+
return fmt.Errorf("can't handle provider type '%s' and no next provider is configured", repository.Spec.Provider)
217+
}
218+
219+
return c.next.CreateBranchProtection(ctx, repository)
220+
}
221+
222+
//TODO: use safe auth strategy post MVP
223+
secret := &v1.Secret{}
224+
if err := c.client.Get(ctx, types.NamespacedName{
225+
Name: repository.Spec.Credentials.SecretRef.Name,
226+
Namespace: repository.Namespace,
227+
}, secret); err != nil {
228+
return fmt.Errorf("failed to get secret: %w", err)
229+
}
230+
231+
token, ok := secret.Data[tokenKey]
232+
if !ok {
233+
return fmt.Errorf("token '%s' not found in secret", tokenKey)
234+
}
235+
236+
logger.Info("got secret")
237+
238+
domain := defaultDomain
239+
if repository.Spec.Domain != "" {
240+
domain = repository.Spec.Domain
241+
}
242+
243+
logger.Info("default domain set", "domain", domain)
244+
245+
gclient, err := gitea.NewClient(domain, gitea.SetToken(string(token)))
246+
if err != nil {
247+
return fmt.Errorf("failed to create gitea client: %w", err)
248+
}
249+
250+
defaultBranch := "main"
251+
if repository.Spec.DefaultBranch != "" {
252+
defaultBranch = repository.Spec.DefaultBranch
253+
}
254+
255+
logger.Info("using default branch", "branch", defaultBranch)
256+
257+
if _, _, err := gclient.CreateBranchProtection(repository.Spec.Owner, repository.Name, gitea.CreateBranchProtectionOption{
258+
BranchName: defaultBranch,
259+
EnablePush: true,
260+
EnableStatusCheck: true,
261+
StatusCheckContexts: []string{deliveryv1alpha1.StatusCheckName},
262+
}); err != nil {
263+
return fmt.Errorf("failed to create branch protection: %w", err)
264+
}
265+
266+
return nil
211267
}

pkg/providers/github/github.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ import (
2323
)
2424

2525
const (
26-
tokenKey = "password"
27-
providerType = "github"
28-
defaultDomain = github.DefaultDomain
29-
statusCheckName = "mpas/validation-check"
26+
tokenKey = "password"
27+
providerType = "github"
28+
defaultDomain = github.DefaultDomain
3029
)
3130

3231
// Client github.
@@ -77,6 +76,14 @@ func (c *Client) CreateRepository(ctx context.Context, obj mpasv1alpha1.Reposito
7776
}
7877

7978
func (c *Client) CreateBranchProtection(ctx context.Context, obj mpasv1alpha1.Repository) error {
79+
if obj.Spec.Provider != providerType {
80+
if c.next == nil {
81+
return fmt.Errorf("can't handle provider type '%s' and no next provider is configured", obj.Spec.Provider)
82+
}
83+
84+
return c.next.CreateBranchProtection(ctx, obj)
85+
}
86+
8087
token, err := c.retrieveAccessToken(ctx, obj)
8188
if err != nil {
8289
return fmt.Errorf("failed to retrieve token: %w", err)
@@ -91,7 +98,7 @@ func (c *Client) CreateBranchProtection(ctx context.Context, obj mpasv1alpha1.Re
9198
Strict: true,
9299
Checks: []*ggithub.RequiredStatusCheck{
93100
{
94-
Context: statusCheckName,
101+
Context: deliveryv1alpha1.StatusCheckName,
95102
},
96103
},
97104
},
@@ -193,7 +200,7 @@ func (c *Client) createCheckRun(ctx context.Context, repository mpasv1alpha1.Rep
193200
_, _, err = g.Repositories.CreateStatus(ctx, repository.Spec.Owner, repository.Name, *pr.Head.SHA, &ggithub.RepoStatus{
194201
State: ggithub.String("pending"),
195202
Description: ggithub.String("MPAS Validation Check"),
196-
Context: ggithub.String(statusCheckName),
203+
Context: ggithub.String(deliveryv1alpha1.StatusCheckName),
197204
})
198205

199206
if err != nil {

pkg/providers/gitlab/gitlab.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,13 @@ func (c *Client) CreatePullRequest(ctx context.Context, branch string, sync deli
126126
}
127127

128128
func (c *Client) CreateBranchProtection(ctx context.Context, obj mpasv1alpha1.Repository) error {
129+
if obj.Spec.Provider != providerType {
130+
if c.next == nil {
131+
return fmt.Errorf("can't handle provider type '%s' and no next provider is configured", obj.Spec.Provider)
132+
}
133+
134+
return c.next.CreateBranchProtection(ctx, obj)
135+
}
136+
129137
return providers.NotSupportedError
130138
}

0 commit comments

Comments
 (0)