|
| 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