Skip to content

Commit feb86c4

Browse files
Implement webhooks install for gitea
Signed-off-by: Gabriel Adrian Samfira <[email protected]>
1 parent 08511e2 commit feb86c4

File tree

3 files changed

+112
-2
lines changed

3 files changed

+112
-2
lines changed

database/watcher/filters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func WithEntityJobFilter(ghEntity params.ForgeEntity) dbCommon.PayloadFilterFunc
182182
}
183183
}
184184

185-
// WithGithubCredentialsFilter returns a filter function that filters payloads by Github credentials.
185+
// WithForgeCredentialsFilter returns a filter function that filters payloads by Github credentials.
186186
func WithForgeCredentialsFilter(creds params.ForgeCredentials) dbCommon.PayloadFilterFunc {
187187
return func(payload dbCommon.ChangePayload) bool {
188188
var idGetter params.IDGetter

util/github/client.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (g *githubClient) GetEntityHook(ctx context.Context, id int64) (ret *github
9292
return ret, err
9393
}
9494

95-
func (g *githubClient) CreateEntityHook(ctx context.Context, hook *github.Hook) (ret *github.Hook, err error) {
95+
func (g *githubClient) createGithubEntityHook(ctx context.Context, hook *github.Hook) (ret *github.Hook, err error) {
9696
metrics.GithubOperationCount.WithLabelValues(
9797
"CreateHook", // label: operation
9898
g.entity.LabelScope(), // label: scope
@@ -116,6 +116,17 @@ func (g *githubClient) CreateEntityHook(ctx context.Context, hook *github.Hook)
116116
return ret, err
117117
}
118118

119+
func (g *githubClient) CreateEntityHook(ctx context.Context, hook *github.Hook) (ret *github.Hook, err error) {
120+
switch g.entity.Credentials.ForgeType {
121+
case params.GithubEndpointType:
122+
return g.createGithubEntityHook(ctx, hook)
123+
case params.GiteaEndpointType:
124+
return g.createGiteaEntityHook(ctx, hook)
125+
default:
126+
return nil, errors.New("invalid entity type")
127+
}
128+
}
129+
119130
func (g *githubClient) DeleteEntityHook(ctx context.Context, id int64) (ret *github.Response, err error) {
120131
metrics.GithubOperationCount.WithLabelValues(
121132
"DeleteHook", // label: operation

util/github/gitea.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
8+
"github.com/cloudbase/garm/metrics"
9+
"github.com/cloudbase/garm/params"
10+
"github.com/google/go-github/v71/github"
11+
"github.com/pkg/errors"
12+
)
13+
14+
type createGiteaHookOptions struct {
15+
Type string `json:"type"`
16+
Config map[string]string `json:"config"`
17+
Events []string `json:"events"`
18+
BranchFilter string `json:"branch_filter"`
19+
Active bool `json:"active"`
20+
AuthorizationHeader string `json:"authorization_header"`
21+
}
22+
23+
func (g *githubClient) createGiteaRepoHook(ctx context.Context, owner, name string, hook *github.Hook) (ret *github.Hook, err error) {
24+
u := fmt.Sprintf("repos/%v/%v/hooks", owner, name)
25+
createOpts := &createGiteaHookOptions{
26+
Type: "gitea",
27+
Events: hook.Events,
28+
Active: hook.GetActive(),
29+
BranchFilter: "*",
30+
Config: map[string]string{
31+
"content_type": hook.GetConfig().GetContentType(),
32+
"url": hook.GetConfig().GetURL(),
33+
"http_method": "post",
34+
},
35+
}
36+
37+
req, err := g.cli.NewRequest(http.MethodPost, u, createOpts)
38+
if err != nil {
39+
return nil, fmt.Errorf("failed to construct request: %w", err)
40+
}
41+
42+
hook = new(github.Hook)
43+
_, err = g.cli.Do(ctx, req, hook)
44+
if err != nil {
45+
return nil, fmt.Errorf("request failed for %s: %w", req.URL.String(), err)
46+
}
47+
return hook, nil
48+
}
49+
50+
func (g *githubClient) createGiteaOrgHook(ctx context.Context, owner string, hook *github.Hook) (ret *github.Hook, err error) {
51+
u := fmt.Sprintf("orgs/%v/hooks", owner)
52+
createOpts := &createGiteaHookOptions{
53+
Type: "gitea",
54+
Events: hook.Events,
55+
Active: hook.GetActive(),
56+
BranchFilter: "*",
57+
Config: map[string]string{
58+
"content_type": hook.GetConfig().GetContentType(),
59+
"url": hook.GetConfig().GetURL(),
60+
"http_method": "post",
61+
},
62+
}
63+
64+
req, err := g.cli.NewRequest(http.MethodPost, u, createOpts)
65+
if err != nil {
66+
return nil, fmt.Errorf("failed to construct request: %w", err)
67+
}
68+
69+
hook = new(github.Hook)
70+
_, err = g.cli.Do(ctx, req, hook)
71+
if err != nil {
72+
return nil, fmt.Errorf("request failed for %s: %w", req.URL.String(), err)
73+
}
74+
return hook, nil
75+
}
76+
77+
func (g *githubClient) createGiteaEntityHook(ctx context.Context, hook *github.Hook) (ret *github.Hook, err error) {
78+
metrics.GithubOperationCount.WithLabelValues(
79+
"CreateHook", // label: operation
80+
g.entity.LabelScope(), // label: scope
81+
).Inc()
82+
defer func() {
83+
if err != nil {
84+
metrics.GithubOperationFailedCount.WithLabelValues(
85+
"CreateHook", // label: operation
86+
g.entity.LabelScope(), // label: scope
87+
).Inc()
88+
}
89+
}()
90+
switch g.entity.EntityType {
91+
case params.ForgeEntityTypeRepository:
92+
ret, err = g.createGiteaRepoHook(ctx, g.entity.Owner, g.entity.Name, hook)
93+
case params.ForgeEntityTypeOrganization:
94+
ret, err = g.createGiteaOrgHook(ctx, g.entity.Owner, hook)
95+
default:
96+
return nil, errors.New("invalid entity type")
97+
}
98+
return ret, err
99+
}

0 commit comments

Comments
 (0)