Skip to content

Commit 5dfcfc5

Browse files
Implement webhooks install for gitea
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
1 parent 08511e2 commit 5dfcfc5

File tree

3 files changed

+113
-2
lines changed

3 files changed

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

0 commit comments

Comments
 (0)