Implement webhooks install for gitea

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
Gabriel Adrian Samfira 2025-05-16 20:18:30 +00:00
parent 08511e2e7f
commit 5dfcfc542e
3 changed files with 113 additions and 2 deletions

View file

@ -92,7 +92,7 @@ func (g *githubClient) GetEntityHook(ctx context.Context, id int64) (ret *github
return ret, err
}
func (g *githubClient) CreateEntityHook(ctx context.Context, hook *github.Hook) (ret *github.Hook, err error) {
func (g *githubClient) createGithubEntityHook(ctx context.Context, hook *github.Hook) (ret *github.Hook, err error) {
metrics.GithubOperationCount.WithLabelValues(
"CreateHook", // label: operation
g.entity.LabelScope(), // label: scope
@ -116,6 +116,17 @@ func (g *githubClient) CreateEntityHook(ctx context.Context, hook *github.Hook)
return ret, err
}
func (g *githubClient) CreateEntityHook(ctx context.Context, hook *github.Hook) (ret *github.Hook, err error) {
switch g.entity.Credentials.ForgeType {
case params.GithubEndpointType:
return g.createGithubEntityHook(ctx, hook)
case params.GiteaEndpointType:
return g.createGiteaEntityHook(ctx, hook)
default:
return nil, errors.New("invalid entity type")
}
}
func (g *githubClient) DeleteEntityHook(ctx context.Context, id int64) (ret *github.Response, err error) {
metrics.GithubOperationCount.WithLabelValues(
"DeleteHook", // label: operation

100
util/github/gitea.go Normal file
View file

@ -0,0 +1,100 @@
package github
import (
"context"
"fmt"
"net/http"
"github.com/google/go-github/v71/github"
"github.com/pkg/errors"
"github.com/cloudbase/garm/metrics"
"github.com/cloudbase/garm/params"
)
type createGiteaHookOptions struct {
Type string `json:"type"`
Config map[string]string `json:"config"`
Events []string `json:"events"`
BranchFilter string `json:"branch_filter"`
Active bool `json:"active"`
AuthorizationHeader string `json:"authorization_header"`
}
func (g *githubClient) createGiteaRepoHook(ctx context.Context, owner, name string, hook *github.Hook) (ret *github.Hook, err error) {
u := fmt.Sprintf("repos/%v/%v/hooks", owner, name)
createOpts := &createGiteaHookOptions{
Type: "gitea",
Events: hook.Events,
Active: hook.GetActive(),
BranchFilter: "*",
Config: map[string]string{
"content_type": hook.GetConfig().GetContentType(),
"url": hook.GetConfig().GetURL(),
"http_method": "post",
},
}
req, err := g.cli.NewRequest(http.MethodPost, u, createOpts)
if err != nil {
return nil, fmt.Errorf("failed to construct request: %w", err)
}
hook = new(github.Hook)
_, err = g.cli.Do(ctx, req, hook)
if err != nil {
return nil, fmt.Errorf("request failed for %s: %w", req.URL.String(), err)
}
return hook, nil
}
func (g *githubClient) createGiteaOrgHook(ctx context.Context, owner string, hook *github.Hook) (ret *github.Hook, err error) {
u := fmt.Sprintf("orgs/%v/hooks", owner)
createOpts := &createGiteaHookOptions{
Type: "gitea",
Events: hook.Events,
Active: hook.GetActive(),
BranchFilter: "*",
Config: map[string]string{
"content_type": hook.GetConfig().GetContentType(),
"url": hook.GetConfig().GetURL(),
"http_method": "post",
},
}
req, err := g.cli.NewRequest(http.MethodPost, u, createOpts)
if err != nil {
return nil, fmt.Errorf("failed to construct request: %w", err)
}
hook = new(github.Hook)
_, err = g.cli.Do(ctx, req, hook)
if err != nil {
return nil, fmt.Errorf("request failed for %s: %w", req.URL.String(), err)
}
return hook, nil
}
func (g *githubClient) createGiteaEntityHook(ctx context.Context, hook *github.Hook) (ret *github.Hook, err error) {
metrics.GithubOperationCount.WithLabelValues(
"CreateHook", // label: operation
g.entity.LabelScope(), // label: scope
).Inc()
defer func() {
if err != nil {
metrics.GithubOperationFailedCount.WithLabelValues(
"CreateHook", // label: operation
g.entity.LabelScope(), // label: scope
).Inc()
}
}()
switch g.entity.EntityType {
case params.ForgeEntityTypeRepository:
ret, err = g.createGiteaRepoHook(ctx, g.entity.Owner, g.entity.Name, hook)
case params.ForgeEntityTypeOrganization:
ret, err = g.createGiteaOrgHook(ctx, g.entity.Owner, hook)
default:
return nil, errors.New("invalid entity type")
}
return ret, err
}