| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "strings" |
| 8 | |
| 9 | "gitlab.com/Timfa/bluegit/internal/access" |
| 10 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 11 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 12 | "gitlab.com/Timfa/bluegit/internal/names" |
| 13 | "gitlab.com/Timfa/bluegit/internal/state" |
| 14 | "gitlab.com/Timfa/bluegit/internal/store" |
| 15 | "gitlab.com/Timfa/bluegit/internal/webhook" |
| 16 | ) |
| 17 | |
| 18 | func (s *Server) opCreateGroup(ctx context.Context, user store.User, display, description, defaultRole string) (store.Group, error) { |
| 19 | display = strings.TrimSpace(display) |
| 20 | path, err := slugKind("group", display) |
| 21 | if err != nil { |
| 22 | return store.Group{}, fail(400, err) |
| 23 | } |
| 24 | role := access.Role("") |
| 25 | if strings.TrimSpace(defaultRole) != "" { |
| 26 | role, err = access.ParseRole(defaultRole) |
| 27 | if err != nil { |
| 28 | return store.Group{}, fail(400, err) |
| 29 | } |
| 30 | } |
| 31 | g, err := s.Store.CreateGroup(ctx, user.ID, display, path, description, role) |
| 32 | if err != nil { |
| 33 | return store.Group{}, fail(400, err) |
| 34 | } |
| 35 | return g, nil |
| 36 | } |
| 37 | |
| 38 | func slugKind(kind, raw string) (string, error) { |
| 39 | path, err := names.Slug(kind, raw) |
| 40 | if err != nil { |
| 41 | return "", i18n.E("slug.bad") |
| 42 | } |
| 43 | return path, nil |
| 44 | } |
| 45 | |
| 46 | func (s *Server) opUpdateGroup(ctx context.Context, user store.User, g store.Group, description, visibility, defaultRole string) error { |
| 47 | role, err := s.Store.GroupRole(ctx, g.ID, user.ID) |
| 48 | if err != nil || !role.AtLeast(access.RoleMaintainer) { |
| 49 | return fail(403, i18n.E("err.forbidden")) |
| 50 | } |
| 51 | if visibility != "public" { |
| 52 | visibility = "private" |
| 53 | } |
| 54 | next := g.DefaultRole |
| 55 | if strings.TrimSpace(defaultRole) != "" { |
| 56 | next, err = access.ParseRole(defaultRole) |
| 57 | if err != nil { |
| 58 | return fail(400, err) |
| 59 | } |
| 60 | } |
| 61 | if err := s.Store.UpdateGroup(ctx, g.ID, description, visibility, next); err != nil { |
| 62 | return fail(400, err) |
| 63 | } |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | func (s *Server) opTransferGroup(ctx context.Context, user store.User, g store.Group, username, confirm string) error { |
| 68 | if user.ID != g.OwnerID { |
| 69 | return fail(403, i18n.E("err.ownerOnly")) |
| 70 | } |
| 71 | username = strings.ToLower(strings.TrimSpace(username)) |
| 72 | if username == "" || confirm != username { |
| 73 | return fail(400, i18n.E("err.ownerConfirm")) |
| 74 | } |
| 75 | to, err := s.Store.UserByUsername(ctx, username) |
| 76 | if err != nil { |
| 77 | return fail(400, i18n.E("err.ownerMember")) |
| 78 | } |
| 79 | if err := s.Store.TransferGroupOwner(ctx, g.ID, user.ID, to.ID); err != nil { |
| 80 | return fail(400, err) |
| 81 | } |
| 82 | return nil |
| 83 | } |
| 84 | |
| 85 | func (s *Server) opCreateRepo(ctx context.Context, user store.User, rawName, description, groupPath, visibility string, readme bool) (store.Repo, error) { |
| 86 | if s.StoragePath() == "" { |
| 87 | return store.Repo{}, fail(503, i18n.E("err.noStorage")) |
| 88 | } |
| 89 | name, err := slugKind("repo", rawName) |
| 90 | if err != nil { |
| 91 | return store.Repo{}, fail(400, err) |
| 92 | } |
| 93 | ownerKind := "user" |
| 94 | ownerID := user.ID |
| 95 | if gpath := strings.TrimSpace(groupPath); gpath != "" { |
| 96 | g, err := s.Store.GroupByPath(ctx, gpath) |
| 97 | if err != nil { |
| 98 | return store.Repo{}, fail(404, i18n.E("err.groupMissing")) |
| 99 | } |
| 100 | role, err := s.Store.GroupRole(ctx, g.ID, user.ID) |
| 101 | if err != nil || !role.CanManageSettings() { |
| 102 | return store.Repo{}, fail(403, i18n.E("err.forbidden")) |
| 103 | } |
| 104 | ownerKind, ownerID = "group", g.ID |
| 105 | } |
| 106 | if visibility != "public" { |
| 107 | visibility = "private" |
| 108 | } |
| 109 | repo, err := s.Store.CreateRepo(ctx, ownerKind, ownerID, name, description, s.Cfg.Git.DefaultBranch, visibility) |
| 110 | if err != nil { |
| 111 | return store.Repo{}, fail(400, err) |
| 112 | } |
| 113 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 114 | if err := gitrepo.Init(s.Git, dir, repo.DefaultBranch, s.HookBin); err != nil { |
| 115 | _ = s.Store.DeleteRepo(ctx, repo.ID) |
| 116 | return store.Repo{}, fail(500, i18n.E("err.gitInit", err.Error())) |
| 117 | } |
| 118 | if readme { |
| 119 | role, _ := s.Store.EffectiveRole(ctx, user.ID, repo.ID) |
| 120 | if _, err := s.opWriteFiles(ctx, user, repo, role, repo.DefaultBranch, "", "Initial commit", "", map[string]string{ |
| 121 | "README.md": "# " + repo.Name + "\n", |
| 122 | }, nil); err != nil { |
| 123 | _ = s.Store.DeleteRepo(ctx, repo.ID) |
| 124 | _ = os.RemoveAll(dir) |
| 125 | return store.Repo{}, err |
| 126 | } |
| 127 | } |
| 128 | return repo, nil |
| 129 | } |
| 130 | |
| 131 | func (s *Server) opUpdateRepo(ctx context.Context, user store.User, repo store.Repo, description, visibility string) error { |
| 132 | role, err := s.Store.EffectiveRole(ctx, user.ID, repo.ID) |
| 133 | if err != nil || !role.CanManageSettings() { |
| 134 | return fail(403, i18n.E("err.forbidden")) |
| 135 | } |
| 136 | if visibility != "public" { |
| 137 | visibility = "private" |
| 138 | } |
| 139 | if visibility == "public" && s.forkMustStayPrivate(ctx, repo) { |
| 140 | return fail(400, i18n.E("err.forkPrivate")) |
| 141 | } |
| 142 | if err := s.Store.UpdateRepo(ctx, repo.ID, description, visibility); err != nil { |
| 143 | return fail(400, err) |
| 144 | } |
| 145 | if visibility != repo.Visibility { |
| 146 | s.ReadCache().DropRepo(repo.ID) |
| 147 | } |
| 148 | return nil |
| 149 | } |
| 150 | |
| 151 | // opSetDefaultBranch zet HEAD en de metadata naar een bestaande branch. |
| 152 | func (s *Server) opSetDefaultBranch(ctx context.Context, user store.User, repo store.Repo, branch string) error { |
| 153 | role, err := s.Store.EffectiveRole(ctx, user.ID, repo.ID) |
| 154 | if err != nil || !role.CanManageSettings() { |
| 155 | return fail(403, i18n.E("err.forbidden")) |
| 156 | } |
| 157 | branch = strings.TrimSpace(branch) |
| 158 | if err := names.ValidBranch(branch); err != nil { |
| 159 | return fail(400, err) |
| 160 | } |
| 161 | if branch == repo.DefaultBranch { |
| 162 | return nil |
| 163 | } |
| 164 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 165 | if !gitrepo.HasBranch(ctx, s.Git, dir, branch) { |
| 166 | return fail(400, i18n.E("err.branchMissing")) |
| 167 | } |
| 168 | if err := gitrepo.SetHEAD(ctx, s.Git, dir, branch); err != nil { |
| 169 | return err |
| 170 | } |
| 171 | return s.Store.SetDefaultBranch(ctx, repo.ID, branch) |
| 172 | } |
| 173 | |
| 174 | // opSetRepoCIPaused zet of nieuwe pipelines mogen worden ingepland. |
| 175 | func (s *Server) opSetRepoCIPaused(ctx context.Context, user store.User, repo store.Repo, paused bool) error { |
| 176 | role, err := s.Store.EffectiveRole(ctx, user.ID, repo.ID) |
| 177 | if err != nil || !role.CanManageSettings() { |
| 178 | return fail(403, i18n.E("err.forbidden")) |
| 179 | } |
| 180 | return s.Store.SetRepoCIPaused(ctx, repo.ID, paused) |
| 181 | } |
| 182 | |
| 183 | // opSetReleaseVisibility zet inherit, private of public (alleen beheerders). |
| 184 | func (s *Server) opSetReleaseVisibility(ctx context.Context, user store.User, repo store.Repo, v string) error { |
| 185 | role, err := s.Store.EffectiveRole(ctx, user.ID, repo.ID) |
| 186 | if err != nil || !role.CanManageSettings() { |
| 187 | return fail(403, i18n.E("err.forbidden")) |
| 188 | } |
| 189 | if err := s.Store.SetReleaseVisibility(ctx, repo.ID, v); err != nil { |
| 190 | return fail(400, err) |
| 191 | } |
| 192 | return nil |
| 193 | } |
| 194 | |
| 195 | func (s *Server) opDeleteRepo(ctx context.Context, user store.User, repo store.Repo, confirm string, force bool) error { |
| 196 | role, err := s.Store.EffectiveRole(ctx, user.ID, repo.ID) |
| 197 | if err != nil || !role.CanManageSettings() { |
| 198 | return fail(403, i18n.E("err.forbidden")) |
| 199 | } |
| 200 | if confirm != repo.Name { |
| 201 | return fail(400, i18n.E("err.confirmDelete")) |
| 202 | } |
| 203 | if prot, _ := s.Store.RunningProtectedJobs(ctx, repo.ID, 0); len(prot) > 0 && !force { |
| 204 | return fail(409, i18n.E("err.dniRepoDelete", jobNames(prot))) |
| 205 | } |
| 206 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 207 | if err := s.Store.DeleteRepo(ctx, repo.ID); err != nil { |
| 208 | return fail(400, err) |
| 209 | } |
| 210 | _ = os.RemoveAll(dir) |
| 211 | s.ReadCache().DropRepo(repo.ID) |
| 212 | return nil |
| 213 | } |
| 214 | |
| 215 | // opDeleteGroup verwijdert een lege groep. Alleen groepsbeheerders en |
| 216 | // admins; confirm is de groepsnaam of het pad. Een groep met repositories |
| 217 | // geeft 409 met het aantal; er is geen force. |
| 218 | func (s *Server) opDeleteGroup(ctx context.Context, user store.User, g store.Group, confirm string) error { |
| 219 | if err := s.manageScope(ctx, user, "group", g.ID); err != nil { |
| 220 | return err |
| 221 | } |
| 222 | if c := strings.TrimSpace(confirm); c == "" || (c != g.Name && c != g.Path) { |
| 223 | return fail(400, i18n.E("err.confirmGroupDelete")) |
| 224 | } |
| 225 | if err := s.Store.DeleteGroup(ctx, g.ID); err != nil { |
| 226 | var ne store.ErrGroupNotEmpty |
| 227 | if errors.As(err, &ne) { |
| 228 | return fail(409, i18n.E("err.groupNotEmpty", ne.Repos)) |
| 229 | } |
| 230 | return fail(500, err) |
| 231 | } |
| 232 | return nil |
| 233 | } |
| 234 | |
| 235 | func (s *Server) manageScope(ctx context.Context, user store.User, kind string, id int64) error { |
| 236 | if kind == "group" { |
| 237 | role, err := s.Store.GroupRole(ctx, id, user.ID) |
| 238 | if err != nil || !role.AtLeast(access.RoleMaintainer) { |
| 239 | return fail(403, i18n.E("err.forbidden")) |
| 240 | } |
| 241 | return nil |
| 242 | } |
| 243 | role, err := s.Store.EffectiveRole(ctx, user.ID, id) |
| 244 | if err != nil || !role.CanManageSettings() { |
| 245 | return fail(403, i18n.E("err.forbidden")) |
| 246 | } |
| 247 | return nil |
| 248 | } |
| 249 | |
| 250 | func (s *Server) opSetMember(ctx context.Context, user store.User, kind string, id int64, username, roleName string) error { |
| 251 | if err := s.manageScope(ctx, user, kind, id); err != nil { |
| 252 | return err |
| 253 | } |
| 254 | role, err := access.ParseRole(roleName) |
| 255 | if err != nil { |
| 256 | return fail(400, err) |
| 257 | } |
| 258 | if err := s.Store.SetMember(ctx, kind, id, username, role); err != nil { |
| 259 | return fail(400, err) |
| 260 | } |
| 261 | return nil |
| 262 | } |
| 263 | |
| 264 | func (s *Server) opRemoveMember(ctx context.Context, user store.User, kind string, id int64, username string) error { |
| 265 | if err := s.manageScope(ctx, user, kind, id); err != nil { |
| 266 | return err |
| 267 | } |
| 268 | if err := s.Store.RemoveMember(ctx, kind, id, username); err != nil { |
| 269 | return fail(400, err) |
| 270 | } |
| 271 | return nil |
| 272 | } |
| 273 | |
| 274 | func (s *Server) opPutSecret(ctx context.Context, user store.User, kind string, id int64, name, value string, protected bool) error { |
| 275 | if err := s.manageScope(ctx, user, kind, id); err != nil { |
| 276 | return err |
| 277 | } |
| 278 | if err := s.Store.PutSecret(ctx, kind, id, strings.TrimSpace(name), value, protected); err != nil { |
| 279 | return fail(400, err) |
| 280 | } |
| 281 | return nil |
| 282 | } |
| 283 | |
| 284 | func (s *Server) opDeleteSecret(ctx context.Context, user store.User, kind string, id int64, name string) error { |
| 285 | if err := s.manageScope(ctx, user, kind, id); err != nil { |
| 286 | return err |
| 287 | } |
| 288 | _ = s.Store.DeleteSecret(ctx, kind, id, name) |
| 289 | return nil |
| 290 | } |
| 291 | |
| 292 | type hookIn struct { |
| 293 | Name string |
| 294 | Preset string |
| 295 | Filter string |
| 296 | Events []string |
| 297 | Fields webhook.Input |
| 298 | } |
| 299 | |
| 300 | func (s *Server) opCreateWebhook(ctx context.Context, user store.User, kind string, id int64, in hookIn) (int64, error) { |
| 301 | if err := s.manageScope(ctx, user, kind, id); err != nil { |
| 302 | return 0, err |
| 303 | } |
| 304 | if !webhook.KnownPreset(in.Preset) { |
| 305 | return 0, fail(400, i18n.E("err.preset")) |
| 306 | } |
| 307 | if _, err := webhook.MatchBranch(strings.TrimSpace(in.Filter), "main"); err != nil { |
| 308 | return 0, fail(400, err) |
| 309 | } |
| 310 | if len(in.Events) == 0 { |
| 311 | return 0, fail(400, i18n.E("err.pickEvent")) |
| 312 | } |
| 313 | spec, err := webhook.Prepare(in.Preset, in.Fields) |
| 314 | if err != nil { |
| 315 | return 0, fail(400, err) |
| 316 | } |
| 317 | wid, err := s.Store.CreateWebhook(ctx, store.Webhook{ |
| 318 | ScopeKind: kind, ScopeID: id, Name: strings.TrimSpace(in.Name), |
| 319 | Preset: in.Preset, URLTemplate: spec.URL, Headers: spec.Headers, |
| 320 | BodyTemplate: spec.Body, Events: in.Events, BranchFilter: strings.TrimSpace(in.Filter), Method: spec.Method, |
| 321 | }) |
| 322 | if err != nil { |
| 323 | return 0, fail(400, err) |
| 324 | } |
| 325 | return wid, nil |
| 326 | } |
| 327 | |
| 328 | func (s *Server) opDeleteWebhook(ctx context.Context, user store.User, kind string, scopeID, id int64) error { |
| 329 | if err := s.manageScope(ctx, user, kind, scopeID); err != nil { |
| 330 | return err |
| 331 | } |
| 332 | _ = s.Store.DeleteWebhook(ctx, kind, scopeID, id) |
| 333 | return nil |
| 334 | } |
| 335 | |
| 336 | func (s *Server) opSaveRepoApproval(ctx context.Context, user store.User, repo store.Repo, pol store.ApprovalPolicy) error { |
| 337 | if err := s.manageScope(ctx, user, "repo", repo.ID); err != nil { |
| 338 | return err |
| 339 | } |
| 340 | _, groupPath, err := s.Store.EffectiveApproval(ctx, repo) |
| 341 | if err != nil { |
| 342 | return fail(400, err) |
| 343 | } |
| 344 | if groupPath != "" { |
| 345 | return fail(400, i18n.E("appr.locked")) |
| 346 | } |
| 347 | if err := s.Store.SaveApproval(ctx, "repo", repo.ID, pol); err != nil { |
| 348 | return fail(400, err) |
| 349 | } |
| 350 | return nil |
| 351 | } |
| 352 | |
| 353 | func (s *Server) opSaveGroupApproval(ctx context.Context, user store.User, g store.Group, pol store.ApprovalPolicy) error { |
| 354 | if err := s.manageScope(ctx, user, "group", g.ID); err != nil { |
| 355 | return err |
| 356 | } |
| 357 | if err := s.Store.SaveApproval(ctx, "group", g.ID, pol); err != nil { |
| 358 | return fail(400, err) |
| 359 | } |
| 360 | return nil |
| 361 | } |
| 362 | |
| 363 | func (s *Server) opProtect(ctx context.Context, user store.User, repo store.Repo, pattern string, tag bool) error { |
| 364 | if err := s.manageScope(ctx, user, "repo", repo.ID); err != nil { |
| 365 | return err |
| 366 | } |
| 367 | var err error |
| 368 | if tag { |
| 369 | err = s.Store.AddProtectedTag(ctx, repo.ID, pattern) |
| 370 | } else { |
| 371 | err = s.Store.AddProtected(ctx, repo.ID, pattern) |
| 372 | } |
| 373 | if err != nil { |
| 374 | return fail(400, err) |
| 375 | } |
| 376 | return nil |
| 377 | } |
| 378 | |
| 379 | func (s *Server) opUnprotect(ctx context.Context, user store.User, repo store.Repo, pattern string, tag bool) error { |
| 380 | if err := s.manageScope(ctx, user, "repo", repo.ID); err != nil { |
| 381 | return err |
| 382 | } |
| 383 | if tag { |
| 384 | _ = s.Store.RemoveProtectedTag(ctx, repo.ID, pattern) |
| 385 | } else { |
| 386 | _ = s.Store.RemoveProtected(ctx, repo.ID, pattern) |
| 387 | } |
| 388 | return nil |
| 389 | } |
| 390 | |
| 391 | func (s *Server) opToggleStar(ctx context.Context, user store.User, repo store.Repo) (bool, error) { |
| 392 | role, err := s.Store.EffectiveRole(ctx, user.ID, repo.ID) |
| 393 | if err != nil { |
| 394 | return false, fail(500, err) |
| 395 | } |
| 396 | if user.ID == 0 || (!role.CanRead() && repo.Visibility != "public") { |
| 397 | return false, fail(403, i18n.E("err.forbidden")) |
| 398 | } |
| 399 | _, mine, err := s.Store.StarInfo(ctx, user.ID, []int64{repo.ID}) |
| 400 | if err != nil { |
| 401 | return false, fail(400, err) |
| 402 | } |
| 403 | on := !mine[repo.ID] |
| 404 | if err := s.Store.SetStar(ctx, user.ID, repo.ID, on); err != nil { |
| 405 | return false, fail(400, err) |
| 406 | } |
| 407 | return on, nil |
| 408 | } |
| 409 | |
| 410 | func (s *Server) opSetStorage(path string) (state.Info, bool, error) { |
| 411 | if s.StateDir == "" { |
| 412 | return state.Info{}, false, fail(500, i18n.E("err.storageSave")) |
| 413 | } |
| 414 | info, err := state.Inspect(path) |
| 415 | if err != nil { |
| 416 | return state.Info{}, false, fail(400, err) |
| 417 | } |
| 418 | same, err := samePath(s.StateDir, info.Path) |
| 419 | if err != nil { |
| 420 | return state.Info{}, false, fail(400, err) |
| 421 | } |
| 422 | if same { |
| 423 | return info, false, fail(400, i18n.E("err.storageSame")) |
| 424 | } |
| 425 | previous := s.StoragePath() |
| 426 | if err := state.Write(s.StateDir, info.Path); err != nil { |
| 427 | return state.Info{}, false, fail(500, err) |
| 428 | } |
| 429 | s.AdoptStorage(info.Path) |
| 430 | s.RefreshHooks() |
| 431 | return info, previous != "" && previous != info.Path, nil |
| 432 | } |