BlueGit

timfa bluegit

Files

bluegit / internal / server / secret_choices.go

secret_choices.go

feat: sta gemengde secretnamen toe en kies ze bij een webhook
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
c102b10c History
secret_choices.go 2.1 KiB Download Raw
1package server
2
3import (
4 "context"
5 "net/http"
6
7 "gitlab.com/Timfa/bluegit/internal/store"
8)
9
10// SecretChoice is een geheimnaam die een webhook mag noemen.
11// De waarde zit er niet bij.
12type SecretChoice struct {
13 Name string `json:"name"`
14 Source string `json:"source"`
15 Label string `json:"label"`
16}
17
18func (s *Server) repoSecretChoices(r *http.Request, repo store.Repo) []SecretChoice {
19 var out []SecretChoice
20 if repo.OwnerKind == "group" {
21 if g, err := s.Store.GroupByID(r.Context(), repo.OwnerID); err == nil {
22 out = append(out, s.choicesFrom(r, "group", g.ID, s.tr(r, "wh.src.group", g.Path))...)
23 }
24 }
25 out = append(out, s.choicesFrom(r, "repo", repo.ID, s.tr(r, "wh.src.repo", repo.FullPath()))...)
26 if out == nil {
27 out = []SecretChoice{}
28 }
29 return out
30}
31
32func (s *Server) groupSecretChoices(r *http.Request, g store.Group) []SecretChoice {
33 out := s.choicesFrom(r, "group", g.ID, s.tr(r, "wh.src.group", g.Path))
34 if out == nil {
35 out = []SecretChoice{}
36 }
37 return out
38}
39
40func (s *Server) choicesFrom(r *http.Request, kind string, id int64, label string) []SecretChoice {
41 list, err := s.Store.ListSecrets(r.Context(), kind, id)
42 if err != nil || len(list) == 0 {
43 return nil
44 }
45 out := make([]SecretChoice, 0, len(list))
46 for _, m := range list {
47 out = append(out, SecretChoice{Name: m.Name, Source: kind, Label: label})
48 }
49 return out
50}
51
52// hookSecrets geeft de namen die een webhook mag invullen.
53// Een repository-webhook ziet eerst de groepsgeheimen en daarna de
54// eigen geheimen; dezelfde naam laat de repository winnen.
55func (s *Server) hookSecrets(ctx context.Context, kind string, scopeID int64, refProtected bool, repo store.Repo) (map[string]string, error) {
56 out := map[string]string{}
57 if kind == "repo" && repo.OwnerKind == "group" {
58 groupVals, err := s.Store.SecretValues(ctx, "group", repo.OwnerID, refProtected)
59 if err != nil {
60 return nil, err
61 }
62 for k, v := range groupVals {
63 out[k] = v
64 }
65 }
66 own, err := s.Store.SecretValues(ctx, kind, scopeID, refProtected)
67 if err != nil {
68 return nil, err
69 }
70 for k, v := range own {
71 out[k] = v
72 }
73 return out, nil
74}