| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strconv" |
| 6 | |
| 7 | "gitlab.com/Timfa/bluegit/internal/access" |
| 8 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 9 | "gitlab.com/Timfa/bluegit/internal/store" |
| 10 | "gitlab.com/Timfa/bluegit/internal/webhook" |
| 11 | ) |
| 12 | |
| 13 | func groupJSON(g store.Group) map[string]any { |
| 14 | return map[string]any{ |
| 15 | "id": g.ID, "name": g.Name, "path": g.Path, "description": g.Description, |
| 16 | "visibility": g.Visibility, "default_role": g.DefaultRole, |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | func projectJSON(repo store.Repo) map[string]any { |
| 21 | return map[string]any{ |
| 22 | "id": repo.ID, "path": repo.FullPath(), "name": repo.Name, "owner": repo.OwnerPath, |
| 23 | "description": repo.Description, "visibility": repo.Visibility, "default_branch": repo.DefaultBranch, |
| 24 | "release_visibility": repo.ReleaseVisibility, |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func memberJSON(m store.Member) map[string]any { |
| 29 | return map[string]any{"user_id": m.UserID, "username": m.Username, "role": m.Role, "pending": m.Pending} |
| 30 | } |
| 31 | |
| 32 | func (s *Server) apiListGroups(w http.ResponseWriter, r *http.Request) { |
| 33 | list, err := s.Store.ListGroups(r.Context(), apiUser(r).ID) |
| 34 | if !s.apiOK(w, r, err) { |
| 35 | return |
| 36 | } |
| 37 | page, per := pageQuery(r) |
| 38 | rows := make([]map[string]any, 0, len(list)) |
| 39 | for _, g := range list { |
| 40 | rows = append(rows, groupJSON(g)) |
| 41 | } |
| 42 | writePage(w, r, slicePage(rows, page, per), len(rows), page, per) |
| 43 | } |
| 44 | |
| 45 | func (s *Server) apiCreateGroup(w http.ResponseWriter, r *http.Request) { |
| 46 | var body struct { |
| 47 | Name string `json:"name"` |
| 48 | Description string `json:"description"` |
| 49 | DefaultRole string `json:"default_role"` |
| 50 | } |
| 51 | if err := readJSON(r, &body); err != nil { |
| 52 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 53 | return |
| 54 | } |
| 55 | g, err := s.opCreateGroup(r.Context(), apiUser(r), body.Name, body.Description, body.DefaultRole) |
| 56 | if !s.apiOK(w, r, err) { |
| 57 | return |
| 58 | } |
| 59 | writeJSON(w, http.StatusCreated, groupJSON(g)) |
| 60 | } |
| 61 | |
| 62 | func (s *Server) apiGroup(w http.ResponseWriter, r *http.Request, min access.Role) (store.Group, bool) { |
| 63 | g, err := s.Store.GroupByPath(r.Context(), r.PathValue("path")) |
| 64 | if err != nil { |
| 65 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 66 | return store.Group{}, false |
| 67 | } |
| 68 | role, err := s.Store.GroupRole(r.Context(), g.ID, apiUser(r).ID) |
| 69 | if err != nil { |
| 70 | s.writeAPIErr(w, r, fail(500, err)) |
| 71 | return store.Group{}, false |
| 72 | } |
| 73 | if !role.CanRead() && g.Visibility != "public" { |
| 74 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 75 | return store.Group{}, false |
| 76 | } |
| 77 | if min != access.RoleNone && min != access.RoleReadonly && !role.AtLeast(min) { |
| 78 | s.writeAPIErr(w, r, fail(403, i18n.E("err.forbidden"))) |
| 79 | return store.Group{}, false |
| 80 | } |
| 81 | if min == access.RoleReadonly && !role.CanRead() && g.Visibility != "public" { |
| 82 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 83 | return store.Group{}, false |
| 84 | } |
| 85 | return g, true |
| 86 | } |
| 87 | |
| 88 | func (s *Server) apiGetGroup(w http.ResponseWriter, r *http.Request) { |
| 89 | g, ok := s.apiGroup(w, r, access.RoleReadonly) |
| 90 | if !ok { |
| 91 | return |
| 92 | } |
| 93 | writeJSON(w, http.StatusOK, groupJSON(g)) |
| 94 | } |
| 95 | |
| 96 | func (s *Server) apiPutGroup(w http.ResponseWriter, r *http.Request) { |
| 97 | g, ok := s.apiGroup(w, r, access.RoleMaintainer) |
| 98 | if !ok { |
| 99 | return |
| 100 | } |
| 101 | var body struct { |
| 102 | Description string `json:"description"` |
| 103 | Visibility string `json:"visibility"` |
| 104 | DefaultRole string `json:"default_role"` |
| 105 | } |
| 106 | if err := readJSON(r, &body); err != nil { |
| 107 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 108 | return |
| 109 | } |
| 110 | if !s.apiOK(w, r, s.opUpdateGroup(r.Context(), apiUser(r), g, body.Description, body.Visibility, body.DefaultRole)) { |
| 111 | return |
| 112 | } |
| 113 | g, _ = s.Store.GroupByPath(r.Context(), g.Path) |
| 114 | writeJSON(w, http.StatusOK, groupJSON(g)) |
| 115 | } |
| 116 | |
| 117 | func (s *Server) apiTransferGroup(w http.ResponseWriter, r *http.Request) { |
| 118 | g, ok := s.apiGroup(w, r, access.RoleMaintainer) |
| 119 | if !ok { |
| 120 | return |
| 121 | } |
| 122 | var body struct { |
| 123 | Username string `json:"username"` |
| 124 | Confirm string `json:"confirm"` |
| 125 | } |
| 126 | if err := readJSON(r, &body); err != nil { |
| 127 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 128 | return |
| 129 | } |
| 130 | if !s.apiOK(w, r, s.opTransferGroup(r.Context(), apiUser(r), g, body.Username, body.Confirm)) { |
| 131 | return |
| 132 | } |
| 133 | writeJSON(w, http.StatusOK, map[string]any{"path": g.Path, "owner": body.Username}) |
| 134 | } |
| 135 | |
| 136 | func (s *Server) apiGroupProjects(w http.ResponseWriter, r *http.Request) { |
| 137 | g, ok := s.apiGroup(w, r, access.RoleReadonly) |
| 138 | if !ok { |
| 139 | return |
| 140 | } |
| 141 | list, err := s.Store.ReposByGroup(r.Context(), g.ID) |
| 142 | if !s.apiOK(w, r, err) { |
| 143 | return |
| 144 | } |
| 145 | s.writeProjects(w, r, list) |
| 146 | } |
| 147 | |
| 148 | func (s *Server) apiListProjects(w http.ResponseWriter, r *http.Request) { |
| 149 | ids, err := s.Store.ListRepoIDs(r.Context(), apiUser(r).ID) |
| 150 | if !s.apiOK(w, r, err) { |
| 151 | return |
| 152 | } |
| 153 | list, err := s.Store.ReposByIDs(r.Context(), ids) |
| 154 | if !s.apiOK(w, r, err) { |
| 155 | return |
| 156 | } |
| 157 | s.writeProjects(w, r, list) |
| 158 | } |
| 159 | |
| 160 | func (s *Server) writeProjects(w http.ResponseWriter, r *http.Request, list []store.Repo) { |
| 161 | page, per := pageQuery(r) |
| 162 | rows := make([]map[string]any, 0, len(list)) |
| 163 | for _, repo := range list { |
| 164 | rows = append(rows, projectJSON(repo)) |
| 165 | } |
| 166 | writePage(w, r, slicePage(rows, page, per), len(rows), page, per) |
| 167 | } |
| 168 | |
| 169 | func (s *Server) apiCreateProject(w http.ResponseWriter, r *http.Request) { |
| 170 | var body struct { |
| 171 | Name string `json:"name"` |
| 172 | Description string `json:"description"` |
| 173 | Group string `json:"group"` |
| 174 | Visibility string `json:"visibility"` |
| 175 | Readme bool `json:"initialize_with_readme"` |
| 176 | } |
| 177 | if err := readJSON(r, &body); err != nil { |
| 178 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 179 | return |
| 180 | } |
| 181 | repo, err := s.opCreateRepo(r.Context(), apiUser(r), body.Name, body.Description, body.Group, body.Visibility, body.Readme) |
| 182 | if !s.apiOK(w, r, err) { |
| 183 | return |
| 184 | } |
| 185 | writeJSON(w, http.StatusCreated, projectJSON(repo)) |
| 186 | } |
| 187 | |
| 188 | func (s *Server) apiRepo(w http.ResponseWriter, r *http.Request, min access.Role) (store.Repo, access.Role, bool) { |
| 189 | repo, err := s.Store.RepoByPath(r.Context(), r.PathValue("owner"), r.PathValue("repo")) |
| 190 | if err != nil { |
| 191 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 192 | return store.Repo{}, "", false |
| 193 | } |
| 194 | role, err := s.Store.EffectiveRole(r.Context(), apiUser(r).ID, repo.ID) |
| 195 | if err != nil { |
| 196 | s.writeAPIErr(w, r, fail(500, err)) |
| 197 | return store.Repo{}, "", false |
| 198 | } |
| 199 | readable := role.CanRead() || repo.Visibility == "public" |
| 200 | if !readable { |
| 201 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 202 | return store.Repo{}, "", false |
| 203 | } |
| 204 | if min.Rank() > access.RoleReadonly.Rank() && !role.AtLeast(min) { |
| 205 | s.writeAPIErr(w, r, fail(403, i18n.E("err.forbidden"))) |
| 206 | return store.Repo{}, "", false |
| 207 | } |
| 208 | return repo, role, true |
| 209 | } |
| 210 | |
| 211 | func (s *Server) apiGetProject(w http.ResponseWriter, r *http.Request) { |
| 212 | repo, _, ok := s.apiRepo(w, r, access.RoleReadonly) |
| 213 | if !ok { |
| 214 | return |
| 215 | } |
| 216 | writeJSON(w, http.StatusOK, projectJSON(repo)) |
| 217 | } |
| 218 | |
| 219 | func (s *Server) apiPutProject(w http.ResponseWriter, r *http.Request) { |
| 220 | repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer) |
| 221 | if !ok { |
| 222 | return |
| 223 | } |
| 224 | var body struct { |
| 225 | Description *string `json:"description"` |
| 226 | Visibility *string `json:"visibility"` |
| 227 | ReleaseVisibility *string `json:"release_visibility"` |
| 228 | } |
| 229 | if err := readJSON(r, &body); err != nil { |
| 230 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 231 | return |
| 232 | } |
| 233 | // Velden die ontbreken blijven zoals ze zijn. |
| 234 | desc, vis := repo.Description, repo.Visibility |
| 235 | if body.Description != nil { |
| 236 | desc = *body.Description |
| 237 | } |
| 238 | if body.Visibility != nil { |
| 239 | vis = *body.Visibility |
| 240 | } |
| 241 | if body.ReleaseVisibility != nil && !store.ValidReleaseVisibility(*body.ReleaseVisibility) { |
| 242 | s.writeAPIErr(w, r, fail(400, i18n.E("err.releaseVisibility"))) |
| 243 | return |
| 244 | } |
| 245 | if !s.apiOK(w, r, s.opUpdateRepo(r.Context(), apiUser(r), repo, desc, vis)) { |
| 246 | return |
| 247 | } |
| 248 | if body.ReleaseVisibility != nil { |
| 249 | if !s.apiOK(w, r, s.opSetReleaseVisibility(r.Context(), apiUser(r), repo, *body.ReleaseVisibility)) { |
| 250 | return |
| 251 | } |
| 252 | } |
| 253 | repo, _ = s.Store.RepoByPath(r.Context(), repo.OwnerPath, repo.Name) |
| 254 | writeJSON(w, http.StatusOK, projectJSON(repo)) |
| 255 | } |
| 256 | |
| 257 | func (s *Server) apiDeleteProject(w http.ResponseWriter, r *http.Request) { |
| 258 | repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer) |
| 259 | if !ok { |
| 260 | return |
| 261 | } |
| 262 | var body struct { |
| 263 | Confirm string `json:"confirm"` |
| 264 | } |
| 265 | if err := readJSON(r, &body); err != nil { |
| 266 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 267 | return |
| 268 | } |
| 269 | if !s.apiOK(w, r, s.opDeleteRepo(r.Context(), apiUser(r), repo, body.Confirm)) { |
| 270 | return |
| 271 | } |
| 272 | writeJSON(w, http.StatusNoContent, nil) |
| 273 | } |
| 274 | |
| 275 | func (s *Server) apiStar(w http.ResponseWriter, r *http.Request) { |
| 276 | repo, _, ok := s.apiRepo(w, r, access.RoleReadonly) |
| 277 | if !ok { |
| 278 | return |
| 279 | } |
| 280 | on, err := s.opToggleStar(r.Context(), apiUser(r), repo) |
| 281 | if !s.apiOK(w, r, err) { |
| 282 | return |
| 283 | } |
| 284 | writeJSON(w, http.StatusOK, map[string]any{"starred": on}) |
| 285 | } |
| 286 | |
| 287 | func (s *Server) apiMembers(w http.ResponseWriter, r *http.Request, kind string, id int64) { |
| 288 | if r.Method == http.MethodGet { |
| 289 | list, err := s.Store.ListMembers(r.Context(), kind, id) |
| 290 | if !s.apiOK(w, r, err) { |
| 291 | return |
| 292 | } |
| 293 | page, per := pageQuery(r) |
| 294 | rows := make([]map[string]any, 0, len(list)) |
| 295 | for _, m := range list { |
| 296 | rows = append(rows, memberJSON(m)) |
| 297 | } |
| 298 | writePage(w, r, slicePage(rows, page, per), len(rows), page, per) |
| 299 | return |
| 300 | } |
| 301 | var body struct { |
| 302 | Username string `json:"username"` |
| 303 | Role string `json:"role"` |
| 304 | } |
| 305 | if err := readJSON(r, &body); err != nil { |
| 306 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 307 | return |
| 308 | } |
| 309 | if !s.apiOK(w, r, s.opSetMember(r.Context(), apiUser(r), kind, id, body.Username, body.Role)) { |
| 310 | return |
| 311 | } |
| 312 | writeJSON(w, http.StatusOK, map[string]any{"username": body.Username, "role": body.Role}) |
| 313 | } |
| 314 | |
| 315 | func (s *Server) apiGroupMembers(w http.ResponseWriter, r *http.Request) { |
| 316 | min := access.RoleReadonly |
| 317 | if r.Method != http.MethodGet { |
| 318 | min = access.RoleMaintainer |
| 319 | } |
| 320 | g, ok := s.apiGroup(w, r, min) |
| 321 | if !ok { |
| 322 | return |
| 323 | } |
| 324 | s.apiMembers(w, r, "group", g.ID) |
| 325 | } |
| 326 | |
| 327 | func (s *Server) apiProjectMembers(w http.ResponseWriter, r *http.Request) { |
| 328 | min := access.RoleReadonly |
| 329 | if r.Method != http.MethodGet { |
| 330 | min = access.RoleMaintainer |
| 331 | } |
| 332 | repo, _, ok := s.apiRepo(w, r, min) |
| 333 | if !ok { |
| 334 | return |
| 335 | } |
| 336 | s.apiMembers(w, r, "repo", repo.ID) |
| 337 | } |
| 338 | |
| 339 | func (s *Server) apiDeleteMember(w http.ResponseWriter, r *http.Request, kind string, id int64) { |
| 340 | if !s.apiOK(w, r, s.opRemoveMember(r.Context(), apiUser(r), kind, id, r.PathValue("username"))) { |
| 341 | return |
| 342 | } |
| 343 | writeJSON(w, http.StatusNoContent, nil) |
| 344 | } |
| 345 | |
| 346 | func (s *Server) apiDeleteGroupMember(w http.ResponseWriter, r *http.Request) { |
| 347 | g, ok := s.apiGroup(w, r, access.RoleMaintainer) |
| 348 | if !ok { |
| 349 | return |
| 350 | } |
| 351 | s.apiDeleteMember(w, r, "group", g.ID) |
| 352 | } |
| 353 | |
| 354 | func (s *Server) apiDeleteProjectMember(w http.ResponseWriter, r *http.Request) { |
| 355 | repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer) |
| 356 | if !ok { |
| 357 | return |
| 358 | } |
| 359 | s.apiDeleteMember(w, r, "repo", repo.ID) |
| 360 | } |
| 361 | |
| 362 | func secretJSON(m store.SecretMeta) map[string]any { |
| 363 | return map[string]any{"name": m.Name, "protected": m.Protected, "created_at": rfcTime(m.CreatedAt)} |
| 364 | } |
| 365 | |
| 366 | func (s *Server) apiSecrets(w http.ResponseWriter, r *http.Request, kind string, id int64) { |
| 367 | if r.Method == http.MethodGet { |
| 368 | list, err := s.Store.ListSecrets(r.Context(), kind, id) |
| 369 | if !s.apiOK(w, r, err) { |
| 370 | return |
| 371 | } |
| 372 | rows := make([]map[string]any, 0, len(list)) |
| 373 | for _, m := range list { |
| 374 | rows = append(rows, secretJSON(m)) |
| 375 | } |
| 376 | page, per := pageQuery(r) |
| 377 | writePage(w, r, slicePage(rows, page, per), len(rows), page, per) |
| 378 | return |
| 379 | } |
| 380 | var body struct { |
| 381 | Name string `json:"name"` |
| 382 | Value string `json:"value"` |
| 383 | Protected bool `json:"protected"` |
| 384 | } |
| 385 | if err := readJSON(r, &body); err != nil { |
| 386 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 387 | return |
| 388 | } |
| 389 | if !s.apiOK(w, r, s.opPutSecret(r.Context(), apiUser(r), kind, id, body.Name, body.Value, body.Protected)) { |
| 390 | return |
| 391 | } |
| 392 | writeJSON(w, http.StatusOK, map[string]any{"name": body.Name}) |
| 393 | } |
| 394 | |
| 395 | func (s *Server) apiGroupSecrets(w http.ResponseWriter, r *http.Request) { |
| 396 | min := access.RoleReadonly |
| 397 | if r.Method != http.MethodGet { |
| 398 | min = access.RoleMaintainer |
| 399 | } |
| 400 | g, ok := s.apiGroup(w, r, min) |
| 401 | if !ok { |
| 402 | return |
| 403 | } |
| 404 | if r.Method == http.MethodGet { |
| 405 | if err := s.manageScope(r.Context(), apiUser(r), "group", g.ID); err != nil { |
| 406 | s.writeAPIErr(w, r, err) |
| 407 | return |
| 408 | } |
| 409 | } |
| 410 | s.apiSecrets(w, r, "group", g.ID) |
| 411 | } |
| 412 | |
| 413 | func (s *Server) apiProjectSecrets(w http.ResponseWriter, r *http.Request) { |
| 414 | min := access.RoleReadonly |
| 415 | if r.Method != http.MethodGet { |
| 416 | min = access.RoleMaintainer |
| 417 | } |
| 418 | repo, _, ok := s.apiRepo(w, r, min) |
| 419 | if !ok { |
| 420 | return |
| 421 | } |
| 422 | if r.Method == http.MethodGet { |
| 423 | if err := s.manageScope(r.Context(), apiUser(r), "repo", repo.ID); err != nil { |
| 424 | s.writeAPIErr(w, r, err) |
| 425 | return |
| 426 | } |
| 427 | } |
| 428 | s.apiSecrets(w, r, "repo", repo.ID) |
| 429 | } |
| 430 | |
| 431 | func (s *Server) apiDeleteSecret(w http.ResponseWriter, r *http.Request, kind string, id int64) { |
| 432 | if !s.apiOK(w, r, s.opDeleteSecret(r.Context(), apiUser(r), kind, id, r.PathValue("name"))) { |
| 433 | return |
| 434 | } |
| 435 | writeJSON(w, http.StatusNoContent, nil) |
| 436 | } |
| 437 | |
| 438 | func (s *Server) apiDeleteGroupSecret(w http.ResponseWriter, r *http.Request) { |
| 439 | g, ok := s.apiGroup(w, r, access.RoleMaintainer) |
| 440 | if !ok { |
| 441 | return |
| 442 | } |
| 443 | s.apiDeleteSecret(w, r, "group", g.ID) |
| 444 | } |
| 445 | |
| 446 | func (s *Server) apiDeleteProjectSecret(w http.ResponseWriter, r *http.Request) { |
| 447 | repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer) |
| 448 | if !ok { |
| 449 | return |
| 450 | } |
| 451 | s.apiDeleteSecret(w, r, "repo", repo.ID) |
| 452 | } |
| 453 | |
| 454 | func hookJSON(h store.Webhook) map[string]any { |
| 455 | return map[string]any{ |
| 456 | "id": h.ID, "name": h.Name, "preset": h.Preset, "url": h.URLTemplate, |
| 457 | "method": h.Method, "events": h.Events, "branch_filter": h.BranchFilter, "active": h.Active, |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | func (s *Server) apiHooks(w http.ResponseWriter, r *http.Request, kind string, id int64) { |
| 462 | if r.Method == http.MethodGet { |
| 463 | list, err := s.Store.ListWebhooks(r.Context(), kind, id) |
| 464 | if !s.apiOK(w, r, err) { |
| 465 | return |
| 466 | } |
| 467 | rows := make([]map[string]any, 0, len(list)) |
| 468 | for _, h := range list { |
| 469 | rows = append(rows, hookJSON(h)) |
| 470 | } |
| 471 | page, per := pageQuery(r) |
| 472 | writePage(w, r, slicePage(rows, page, per), len(rows), page, per) |
| 473 | return |
| 474 | } |
| 475 | var body struct { |
| 476 | Name string `json:"name"` |
| 477 | Preset string `json:"preset"` |
| 478 | BranchFilter string `json:"branch_filter"` |
| 479 | Events []string `json:"events"` |
| 480 | URL string `json:"url"` |
| 481 | Username string `json:"username"` |
| 482 | Avatar string `json:"avatar"` |
| 483 | Icon string `json:"icon"` |
| 484 | Alias string `json:"alias"` |
| 485 | Token string `json:"token"` |
| 486 | Chat string `json:"chat"` |
| 487 | Server string `json:"server"` |
| 488 | Topic string `json:"topic"` |
| 489 | Homeserver string `json:"homeserver"` |
| 490 | Room string `json:"room"` |
| 491 | Method string `json:"method"` |
| 492 | ContentType string `json:"content_type"` |
| 493 | Headers string `json:"headers"` |
| 494 | Body string `json:"body"` |
| 495 | } |
| 496 | if err := readJSON(r, &body); err != nil { |
| 497 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 498 | return |
| 499 | } |
| 500 | wid, err := s.opCreateWebhook(r.Context(), apiUser(r), kind, id, hookIn{ |
| 501 | Name: body.Name, Preset: body.Preset, Filter: body.BranchFilter, Events: body.Events, |
| 502 | Fields: webhook.Input{ |
| 503 | URL: body.URL, Username: body.Username, Avatar: body.Avatar, Icon: body.Icon, Alias: body.Alias, |
| 504 | Token: body.Token, Chat: body.Chat, Server: body.Server, Topic: body.Topic, |
| 505 | Homeserver: body.Homeserver, Room: body.Room, Method: body.Method, ContentType: body.ContentType, |
| 506 | Headers: body.Headers, Body: body.Body, |
| 507 | }, |
| 508 | }) |
| 509 | if !s.apiOK(w, r, err) { |
| 510 | return |
| 511 | } |
| 512 | hook, err := s.Store.Webhook(r.Context(), kind, id, wid) |
| 513 | if err != nil { |
| 514 | writeJSON(w, http.StatusCreated, map[string]any{"id": wid}) |
| 515 | return |
| 516 | } |
| 517 | writeJSON(w, http.StatusCreated, hookJSON(hook)) |
| 518 | } |
| 519 | |
| 520 | func (s *Server) apiGroupHooks(w http.ResponseWriter, r *http.Request) { |
| 521 | g, ok := s.apiGroup(w, r, access.RoleMaintainer) |
| 522 | if !ok { |
| 523 | return |
| 524 | } |
| 525 | s.apiHooks(w, r, "group", g.ID) |
| 526 | } |
| 527 | |
| 528 | func (s *Server) apiProjectHooks(w http.ResponseWriter, r *http.Request) { |
| 529 | repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer) |
| 530 | if !ok { |
| 531 | return |
| 532 | } |
| 533 | s.apiHooks(w, r, "repo", repo.ID) |
| 534 | } |
| 535 | |
| 536 | func (s *Server) apiDeleteHook(w http.ResponseWriter, r *http.Request, kind string, id int64) { |
| 537 | hid, err := strconv.ParseInt(r.PathValue("id"), 10, 64) |
| 538 | if err != nil { |
| 539 | s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound"))) |
| 540 | return |
| 541 | } |
| 542 | if !s.apiOK(w, r, s.opDeleteWebhook(r.Context(), apiUser(r), kind, id, hid)) { |
| 543 | return |
| 544 | } |
| 545 | writeJSON(w, http.StatusNoContent, nil) |
| 546 | } |
| 547 | |
| 548 | func (s *Server) apiDeleteGroupHook(w http.ResponseWriter, r *http.Request) { |
| 549 | g, ok := s.apiGroup(w, r, access.RoleMaintainer) |
| 550 | if !ok { |
| 551 | return |
| 552 | } |
| 553 | s.apiDeleteHook(w, r, "group", g.ID) |
| 554 | } |
| 555 | |
| 556 | func (s *Server) apiDeleteProjectHook(w http.ResponseWriter, r *http.Request) { |
| 557 | repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer) |
| 558 | if !ok { |
| 559 | return |
| 560 | } |
| 561 | s.apiDeleteHook(w, r, "repo", repo.ID) |
| 562 | } |
| 563 | |
| 564 | func approvalJSON(p store.ApprovalPolicy, locked string) map[string]any { |
| 565 | return map[string]any{ |
| 566 | "configured": p.Configured, "maintainer_required": p.MaintainerRequired, |
| 567 | "required_count": p.RequiredCount, "developers_approve": p.DevelopersApprove, |
| 568 | "locked_by": locked, |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | func approvalBody(r *http.Request) (store.ApprovalPolicy, error) { |
| 573 | var body struct { |
| 574 | MaintainerRequired bool `json:"maintainer_required"` |
| 575 | RequiredCount int `json:"required_count"` |
| 576 | DevelopersApprove bool `json:"developers_approve"` |
| 577 | } |
| 578 | if err := readJSON(r, &body); err != nil { |
| 579 | return store.ApprovalPolicy{}, fail(400, i18n.E("err.json")) |
| 580 | } |
| 581 | return store.ApprovalPolicy{ |
| 582 | MaintainerRequired: body.MaintainerRequired, |
| 583 | RequiredCount: body.RequiredCount, |
| 584 | DevelopersApprove: body.DevelopersApprove, |
| 585 | }, nil |
| 586 | } |
| 587 | |
| 588 | func (s *Server) apiGroupApproval(w http.ResponseWriter, r *http.Request) { |
| 589 | min := access.RoleReadonly |
| 590 | if r.Method != http.MethodGet { |
| 591 | min = access.RoleMaintainer |
| 592 | } |
| 593 | g, ok := s.apiGroup(w, r, min) |
| 594 | if !ok { |
| 595 | return |
| 596 | } |
| 597 | if r.Method == http.MethodGet { |
| 598 | p, err := s.Store.GetApproval(r.Context(), "group", g.ID) |
| 599 | if !s.apiOK(w, r, err) { |
| 600 | return |
| 601 | } |
| 602 | writeJSON(w, http.StatusOK, approvalJSON(p, "")) |
| 603 | return |
| 604 | } |
| 605 | pol, err := approvalBody(r) |
| 606 | if !s.apiOK(w, r, err) { |
| 607 | return |
| 608 | } |
| 609 | if !s.apiOK(w, r, s.opSaveGroupApproval(r.Context(), apiUser(r), g, pol)) { |
| 610 | return |
| 611 | } |
| 612 | writeJSON(w, http.StatusOK, approvalJSON(pol, "")) |
| 613 | } |
| 614 | |
| 615 | func (s *Server) apiProjectApproval(w http.ResponseWriter, r *http.Request) { |
| 616 | min := access.RoleReadonly |
| 617 | if r.Method != http.MethodGet { |
| 618 | min = access.RoleMaintainer |
| 619 | } |
| 620 | repo, _, ok := s.apiRepo(w, r, min) |
| 621 | if !ok { |
| 622 | return |
| 623 | } |
| 624 | pol, locked, err := s.Store.EffectiveApproval(r.Context(), repo) |
| 625 | if !s.apiOK(w, r, err) { |
| 626 | return |
| 627 | } |
| 628 | if r.Method == http.MethodGet { |
| 629 | writeJSON(w, http.StatusOK, approvalJSON(pol, locked)) |
| 630 | return |
| 631 | } |
| 632 | next, err := approvalBody(r) |
| 633 | if !s.apiOK(w, r, err) { |
| 634 | return |
| 635 | } |
| 636 | if !s.apiOK(w, r, s.opSaveRepoApproval(r.Context(), apiUser(r), repo, next)) { |
| 637 | return |
| 638 | } |
| 639 | writeJSON(w, http.StatusOK, approvalJSON(next, "")) |
| 640 | } |
| 641 | |
| 642 | func (s *Server) apiLogo(w http.ResponseWriter, r *http.Request, kind string, id int64, mime string) { |
| 643 | if r.Method == http.MethodGet { |
| 644 | s.serveLogo(w, r, kind, id, mime) |
| 645 | return |
| 646 | } |
| 647 | if err := r.ParseMultipartForm(2 << 20); err != nil { |
| 648 | s.writeAPIErr(w, r, fail(400, i18n.E("err.badForm"))) |
| 649 | return |
| 650 | } |
| 651 | if err := s.storeLogo(r, kind, id); err != nil { |
| 652 | s.writeAPIErr(w, r, fail(400, err)) |
| 653 | return |
| 654 | } |
| 655 | writeJSON(w, http.StatusOK, map[string]any{"logo": true}) |
| 656 | } |
| 657 | |
| 658 | func (s *Server) apiGroupLogo(w http.ResponseWriter, r *http.Request) { |
| 659 | min := access.RoleReadonly |
| 660 | if r.Method != http.MethodGet { |
| 661 | min = access.RoleMaintainer |
| 662 | } |
| 663 | g, ok := s.apiGroup(w, r, min) |
| 664 | if !ok { |
| 665 | return |
| 666 | } |
| 667 | s.apiLogo(w, r, "group", g.ID, g.LogoMIME) |
| 668 | } |
| 669 | |
| 670 | func (s *Server) apiProjectLogo(w http.ResponseWriter, r *http.Request) { |
| 671 | min := access.RoleReadonly |
| 672 | if r.Method != http.MethodGet { |
| 673 | min = access.RoleMaintainer |
| 674 | } |
| 675 | repo, _, ok := s.apiRepo(w, r, min) |
| 676 | if !ok { |
| 677 | return |
| 678 | } |
| 679 | s.apiLogo(w, r, "repo", repo.ID, repo.LogoMIME) |
| 680 | } |
| 681 | |
| 682 | func (s *Server) apiPatterns(w http.ResponseWriter, r *http.Request, tag bool) { |
| 683 | repo, _, ok := s.apiRepo(w, r, access.RoleReadonly) |
| 684 | if !ok { |
| 685 | return |
| 686 | } |
| 687 | if r.Method == http.MethodGet { |
| 688 | var list []string |
| 689 | var err error |
| 690 | if tag { |
| 691 | list, err = s.Store.ProtectedTagPatterns(r.Context(), repo.ID) |
| 692 | } else { |
| 693 | list, err = s.Store.ProtectedPatterns(r.Context(), repo.ID) |
| 694 | } |
| 695 | if !s.apiOK(w, r, err) { |
| 696 | return |
| 697 | } |
| 698 | if list == nil { |
| 699 | list = []string{} |
| 700 | } |
| 701 | writeJSON(w, http.StatusOK, list) |
| 702 | return |
| 703 | } |
| 704 | if _, _, ok := s.apiRepo(w, r, access.RoleMaintainer); !ok { |
| 705 | return |
| 706 | } |
| 707 | var body struct { |
| 708 | Pattern string `json:"pattern"` |
| 709 | } |
| 710 | if err := readJSON(r, &body); err != nil { |
| 711 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 712 | return |
| 713 | } |
| 714 | if !s.apiOK(w, r, s.opProtect(r.Context(), apiUser(r), repo, body.Pattern, tag)) { |
| 715 | return |
| 716 | } |
| 717 | writeJSON(w, http.StatusCreated, map[string]any{"pattern": body.Pattern}) |
| 718 | } |
| 719 | |
| 720 | func (s *Server) apiProtectBranches(w http.ResponseWriter, r *http.Request) { |
| 721 | s.apiPatterns(w, r, false) |
| 722 | } |
| 723 | |
| 724 | func (s *Server) apiProtectTags(w http.ResponseWriter, r *http.Request) { |
| 725 | s.apiPatterns(w, r, true) |
| 726 | } |
| 727 | |
| 728 | func (s *Server) apiUnprotect(w http.ResponseWriter, r *http.Request, tag bool) { |
| 729 | repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer) |
| 730 | if !ok { |
| 731 | return |
| 732 | } |
| 733 | name := "pattern" |
| 734 | if tag { |
| 735 | name = "pattern" |
| 736 | } |
| 737 | pattern := r.PathValue(name) |
| 738 | if !s.apiOK(w, r, s.opUnprotect(r.Context(), apiUser(r), repo, pattern, tag)) { |
| 739 | return |
| 740 | } |
| 741 | writeJSON(w, http.StatusNoContent, nil) |
| 742 | } |
| 743 | |
| 744 | func (s *Server) apiUnprotectBranch(w http.ResponseWriter, r *http.Request) { |
| 745 | s.apiUnprotect(w, r, false) |
| 746 | } |
| 747 | |
| 748 | func (s *Server) apiUnprotectTag(w http.ResponseWriter, r *http.Request) { |
| 749 | s.apiUnprotect(w, r, true) |
| 750 | } |