BlueGit

timfa bluegit

Files

bluegit / internal / server / webhook_api.go

webhook_api.go

feat: toon webhooks in een tabel en maak secrets bewerkbaar
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
89b83b18 History
webhook_api.go 5.3 KiB Download Raw
1package server
2
3import (
4 "encoding/json"
5 "io"
6 "net/http"
7 "strconv"
8 "strings"
9
10 "gitlab.com/Timfa/bluegit/internal/store"
11 "gitlab.com/Timfa/bluegit/internal/webhook"
12)
13
14func (s *Server) repoWebhookSecrets(w http.ResponseWriter, r *http.Request) {
15 repo, _, ok := s.webhookAPIRepo(w, r)
16 if !ok {
17 return
18 }
19 s.writeSecretChoices(w, s.repoSecretChoices(r, repo))
20}
21
22func (s *Server) groupWebhookSecrets(w http.ResponseWriter, r *http.Request) {
23 g, _, ok := s.webhookAPIGroup(w, r)
24 if !ok {
25 return
26 }
27 s.writeSecretChoices(w, s.groupSecretChoices(r, g))
28}
29
30func (s *Server) repoWebhookAPI(w http.ResponseWriter, r *http.Request) {
31 repo, _, ok := s.webhookAPIRepo(w, r)
32 if !ok {
33 return
34 }
35 s.writeWebhookSave(w, r, "repo", repo.ID, 0)
36}
37
38func (s *Server) groupWebhookAPI(w http.ResponseWriter, r *http.Request) {
39 g, _, ok := s.webhookAPIGroup(w, r)
40 if !ok {
41 return
42 }
43 s.writeWebhookSave(w, r, "group", g.ID, 0)
44}
45
46func (s *Server) repoWebhookUpdate(w http.ResponseWriter, r *http.Request) {
47 repo, _, ok := s.webhookAPIRepo(w, r)
48 if !ok {
49 return
50 }
51 id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
52 if err != nil || id <= 0 {
53 http.Error(w, s.tr(r, "err.notFound"), http.StatusNotFound)
54 return
55 }
56 s.writeWebhookSave(w, r, "repo", repo.ID, id)
57}
58
59func (s *Server) groupWebhookUpdate(w http.ResponseWriter, r *http.Request) {
60 g, _, ok := s.webhookAPIGroup(w, r)
61 if !ok {
62 return
63 }
64 id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
65 if err != nil || id <= 0 {
66 http.Error(w, s.tr(r, "err.notFound"), http.StatusNotFound)
67 return
68 }
69 s.writeWebhookSave(w, r, "group", g.ID, id)
70}
71
72func (s *Server) writeSecretChoices(w http.ResponseWriter, choices []SecretChoice) {
73 if choices == nil {
74 choices = []SecretChoice{}
75 }
76 w.Header().Set("Content-Type", "application/json")
77 _ = json.NewEncoder(w).Encode(map[string]any{"secrets": choices})
78}
79
80func (s *Server) writeWebhookSave(w http.ResponseWriter, r *http.Request, kind string, scopeID, id int64) {
81 var body struct {
82 Name string `json:"name"`
83 Preset string `json:"preset"`
84 BranchFilter string `json:"branch_filter"`
85 Events []string `json:"events"`
86 URL string `json:"url"`
87 Username string `json:"username"`
88 Avatar string `json:"avatar"`
89 Icon string `json:"icon"`
90 Alias string `json:"alias"`
91 Token string `json:"token"`
92 Chat string `json:"chat"`
93 Server string `json:"server"`
94 Topic string `json:"topic"`
95 Homeserver string `json:"homeserver"`
96 Room string `json:"room"`
97 Method string `json:"method"`
98 ContentType string `json:"content_type"`
99 Headers string `json:"headers"`
100 Body string `json:"body"`
101 }
102 if err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&body); err != nil {
103 http.Error(w, s.tr(r, "err.json"), http.StatusBadRequest)
104 return
105 }
106 saved, err := s.saveWebhookRecord(r.Context(), kind, scopeID, id, strings.TrimSpace(body.Name), body.Preset, body.BranchFilter, body.Events, webhook.Input{
107 URL: body.URL, Username: body.Username, Avatar: body.Avatar, Icon: body.Icon, Alias: body.Alias,
108 Token: body.Token, Chat: body.Chat, Server: body.Server, Topic: body.Topic,
109 Homeserver: body.Homeserver, Room: body.Room, Method: body.Method, ContentType: body.ContentType,
110 Headers: body.Headers, Body: body.Body,
111 })
112 if err != nil {
113 status := http.StatusBadRequest
114 if store.Missing(err) {
115 status = http.StatusNotFound
116 }
117 http.Error(w, s.trErr(r, err), status)
118 return
119 }
120 w.Header().Set("Content-Type", "application/json")
121 if id == 0 {
122 w.WriteHeader(http.StatusCreated)
123 }
124 _ = json.NewEncoder(w).Encode(map[string]any{"id": saved})
125}
126
127func (s *Server) webhookAPIRepo(w http.ResponseWriter, r *http.Request) (store.Repo, store.User, bool) {
128 u, ok := s.webhookAPIUser(w, r)
129 if !ok {
130 return store.Repo{}, store.User{}, false
131 }
132 repo, err := s.Store.RepoByPath(r.Context(), r.PathValue("owner"), r.PathValue("repo"))
133 if err != nil {
134 http.NotFound(w, r)
135 return store.Repo{}, store.User{}, false
136 }
137 role, err := s.Store.EffectiveRole(r.Context(), u.ID, repo.ID)
138 if err != nil || !role.CanManageSettings() {
139 http.Error(w, s.tr(r, "err.onlyAdmin"), http.StatusForbidden)
140 return store.Repo{}, store.User{}, false
141 }
142 return repo, u, true
143}
144
145func (s *Server) webhookAPIGroup(w http.ResponseWriter, r *http.Request) (store.Group, store.User, bool) {
146 u, ok := s.webhookAPIUser(w, r)
147 if !ok {
148 return store.Group{}, store.User{}, false
149 }
150 g, err := s.Store.GroupByPath(r.Context(), r.PathValue("path"))
151 if err != nil {
152 http.NotFound(w, r)
153 return store.Group{}, store.User{}, false
154 }
155 role, err := s.Store.GroupRole(r.Context(), g.ID, u.ID)
156 if err != nil || !role.CanManageSettings() {
157 http.Error(w, s.tr(r, "err.onlyAdmin"), http.StatusForbidden)
158 return store.Group{}, store.User{}, false
159 }
160 return g, u, true
161}
162
163func (s *Server) webhookAPIUser(w http.ResponseWriter, r *http.Request) (store.User, bool) {
164 token := bearer(r)
165 if token == "" {
166 token = strings.TrimSpace(r.Header.Get("PRIVATE-TOKEN"))
167 }
168 if !strings.HasPrefix(token, "bg_") {
169 http.Error(w, s.tr(r, "err.tokenAbsent"), http.StatusUnauthorized)
170 return store.User{}, false
171 }
172 u, err := s.Store.UserByToken(r.Context(), token)
173 if err != nil {
174 http.Error(w, s.tr(r, "err.tokenBad"), http.StatusUnauthorized)
175 return store.User{}, false
176 }
177 return u, true
178}