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