| 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/store" |
| 13 | ) |
| 14 | |
| 15 | func (s *Server) repoRole(ctx context.Context, user store.User, repo store.Repo) (access.Role, error) { |
| 16 | role, err := s.Store.EffectiveRole(ctx, user.ID, repo.ID) |
| 17 | if err != nil { |
| 18 | return "", fail(500, err) |
| 19 | } |
| 20 | return role, nil |
| 21 | } |
| 22 | |
| 23 | func (s *Server) opCreateBranch(ctx context.Context, user store.User, repo store.Repo, name, from string) error { |
| 24 | role, err := s.repoRole(ctx, user, repo) |
| 25 | if err != nil { |
| 26 | return err |
| 27 | } |
| 28 | if !role.CanWriteCode() { |
| 29 | return fail(403, i18n.E("err.forbidden")) |
| 30 | } |
| 31 | name = strings.TrimSpace(name) |
| 32 | if err := names.ValidBranch(name); err != nil { |
| 33 | return fail(400, err) |
| 34 | } |
| 35 | from = safeRev(from, repo.DefaultBranch) |
| 36 | if from == "" { |
| 37 | return fail(400, i18n.E("err.branches")) |
| 38 | } |
| 39 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 40 | if gitOne(ctx, s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+name) != "" { |
| 41 | return fail(400, i18n.E("err.branchName", name)) |
| 42 | } |
| 43 | if err := s.postReceive(ctx, repo, user, dir, func() error { |
| 44 | return gitrepo.CreateBranch(ctx, s.Git, dir, name, from) |
| 45 | }); err != nil { |
| 46 | return fail(400, err) |
| 47 | } |
| 48 | return nil |
| 49 | } |
| 50 | |
| 51 | func (s *Server) branchLockError(ctx context.Context, repo store.Repo, name string) error { |
| 52 | if name == repo.DefaultBranch { |
| 53 | return i18n.E("err.branchLocked", name) |
| 54 | } |
| 55 | patterns, _ := s.Store.ProtectedPatterns(ctx, repo.ID) |
| 56 | if access.BranchProtected(patterns, name) { |
| 57 | return i18n.E("err.branchLocked", name) |
| 58 | } |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | func (s *Server) opDeleteBranch(ctx context.Context, user store.User, repo store.Repo, name, confirm string) error { |
| 63 | role, err := s.repoRole(ctx, user, repo) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | if !role.CanWriteCode() { |
| 68 | return fail(403, i18n.E("err.forbidden")) |
| 69 | } |
| 70 | name = strings.TrimSpace(name) |
| 71 | if confirm != name || name == "" { |
| 72 | return fail(400, i18n.E("err.branchConfirm")) |
| 73 | } |
| 74 | if err := s.branchLockError(ctx, repo, name); err != nil { |
| 75 | return fail(400, err) |
| 76 | } |
| 77 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 78 | if err := s.postReceive(ctx, repo, user, dir, func() error { |
| 79 | return gitrepo.DeleteBranch(ctx, s.Git, dir, name) |
| 80 | }); err != nil { |
| 81 | return fail(400, err) |
| 82 | } |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | func (s *Server) opRenameBranch(ctx context.Context, user store.User, repo store.Repo, name, newName, confirm string) error { |
| 87 | role, err := s.repoRole(ctx, user, repo) |
| 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | if !role.CanWriteCode() { |
| 92 | return fail(403, i18n.E("err.forbidden")) |
| 93 | } |
| 94 | name = strings.TrimSpace(name) |
| 95 | newName = strings.TrimSpace(newName) |
| 96 | if confirm != name || name == "" { |
| 97 | return fail(400, i18n.E("err.branchConfirm")) |
| 98 | } |
| 99 | if err := names.ValidBranch(newName); err != nil { |
| 100 | return fail(400, err) |
| 101 | } |
| 102 | if err := s.branchLockError(ctx, repo, name); err != nil { |
| 103 | return fail(400, err) |
| 104 | } |
| 105 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 106 | if gitOne(ctx, s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+newName) != "" { |
| 107 | return fail(400, i18n.E("err.branchName", newName)) |
| 108 | } |
| 109 | if err := s.postReceive(ctx, repo, user, dir, func() error { |
| 110 | if err := gitrepo.RenameBranch(ctx, s.Git, dir, name, newName); err != nil { |
| 111 | return err |
| 112 | } |
| 113 | return s.Store.RenameOpenMRBranches(ctx, repo.ID, name, newName) |
| 114 | }); err != nil { |
| 115 | return fail(400, err) |
| 116 | } |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | func (s *Server) opCreateTag(ctx context.Context, user store.User, repo store.Repo, name, target, message string) error { |
| 121 | role, err := s.repoRole(ctx, user, repo) |
| 122 | if err != nil { |
| 123 | return err |
| 124 | } |
| 125 | if !role.CanWriteCode() { |
| 126 | return fail(403, i18n.E("err.forbidden")) |
| 127 | } |
| 128 | name = strings.TrimSpace(name) |
| 129 | if err := names.ValidTag(name); err != nil { |
| 130 | return fail(400, err) |
| 131 | } |
| 132 | patterns, _ := s.Store.ProtectedTagPatterns(ctx, repo.ID) |
| 133 | if access.BranchProtected(patterns, name) && !role.CanManageSettings() { |
| 134 | return fail(403, i18n.E("err.tagProtected", name)) |
| 135 | } |
| 136 | if strings.TrimSpace(target) == "" { |
| 137 | target = repo.DefaultBranch |
| 138 | } |
| 139 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 140 | if err := s.postReceive(ctx, repo, user, dir, func() error { |
| 141 | return gitrepo.CreateTag(ctx, s.Git, dir, name, target, message, user.Username, userEmail(user.Username)) |
| 142 | }); err != nil { |
| 143 | return fail(400, err) |
| 144 | } |
| 145 | return nil |
| 146 | } |
| 147 | |
| 148 | func (s *Server) opDeleteTag(ctx context.Context, user store.User, repo store.Repo, name string) error { |
| 149 | role, err := s.repoRole(ctx, user, repo) |
| 150 | if err != nil { |
| 151 | return err |
| 152 | } |
| 153 | if !role.CanWriteCode() { |
| 154 | return fail(403, i18n.E("err.forbidden")) |
| 155 | } |
| 156 | name = strings.TrimSpace(name) |
| 157 | patterns, _ := s.Store.ProtectedTagPatterns(ctx, repo.ID) |
| 158 | if access.BranchProtected(patterns, name) && !role.CanManageSettings() { |
| 159 | return fail(403, i18n.E("err.tagProtected", name)) |
| 160 | } |
| 161 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 162 | if err := gitrepo.DeleteTag(ctx, s.Git, dir, name); err != nil { |
| 163 | return fail(400, err) |
| 164 | } |
| 165 | return nil |
| 166 | } |
| 167 | |
| 168 | type fileAction struct { |
| 169 | Action string `json:"action"` |
| 170 | Path string `json:"file_path"` |
| 171 | Content string `json:"content"` |
| 172 | } |
| 173 | |
| 174 | func (s *Server) opWriteFiles(ctx context.Context, user store.User, repo store.Repo, role access.Role, branch, base, title, description string, files map[string]string, deletes []string) (string, error) { |
| 175 | if role == "" { |
| 176 | var err error |
| 177 | role, err = s.repoRole(ctx, user, repo) |
| 178 | if err != nil { |
| 179 | return "", err |
| 180 | } |
| 181 | } |
| 182 | if !role.CanWriteCode() { |
| 183 | return "", fail(403, i18n.E("err.forbidden")) |
| 184 | } |
| 185 | branch = strings.TrimSpace(branch) |
| 186 | if !validBranchName(branch) { |
| 187 | return "", fail(400, i18n.E("edit.badBranch")) |
| 188 | } |
| 189 | title = strings.TrimSpace(title) |
| 190 | if title == "" || strings.ContainsAny(title, "\r\n") { |
| 191 | return "", fail(400, i18n.E("edit.needMessage")) |
| 192 | } |
| 193 | if base == "" { |
| 194 | base = repo.DefaultBranch |
| 195 | } |
| 196 | patterns, _ := s.Store.ProtectedPatterns(ctx, repo.ID) |
| 197 | if access.BranchProtected(patterns, branch) && !role.CanPushBranch(true) { |
| 198 | return "", fail(403, i18n.E("edit.protected")) |
| 199 | } |
| 200 | if s.StoragePath() == "" { |
| 201 | return "", fail(503, i18n.E("err.noStorage")) |
| 202 | } |
| 203 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 204 | work, err := s.scratchDir("api-commit") |
| 205 | if err != nil { |
| 206 | return "", fail(500, err) |
| 207 | } |
| 208 | defer os.RemoveAll(work) |
| 209 | if err := s.ensureBranch(ctx, dir, branch, base, work); err != nil { |
| 210 | return "", fail(400, err) |
| 211 | } |
| 212 | if err := s.postReceive(ctx, repo, user, dir, func() error { |
| 213 | return gitrepo.CommitChanges(ctx, s.Git, dir, work, branch, user.Username, userEmail(user.Username), title, description, files, deletes, role, patterns) |
| 214 | }); err != nil { |
| 215 | return "", fail(400, err) |
| 216 | } |
| 217 | return gitOne(ctx, s.Git, dir, "rev-parse", "--verify", "refs/heads/"+branch), nil |
| 218 | } |
| 219 | |
| 220 | func (s *Server) opCommitActions(ctx context.Context, user store.User, repo store.Repo, branch, base, title, description string, actions []fileAction) (string, error) { |
| 221 | files := map[string]string{} |
| 222 | var deletes []string |
| 223 | for _, a := range actions { |
| 224 | path, ok := repoPath(a.Path) |
| 225 | if !ok || path == "" { |
| 226 | return "", fail(400, i18n.E("err.sugFile")) |
| 227 | } |
| 228 | switch a.Action { |
| 229 | case "", "create", "update": |
| 230 | files[path] = a.Content |
| 231 | case "delete": |
| 232 | deletes = append(deletes, path) |
| 233 | default: |
| 234 | return "", fail(400, i18n.E("err.sugFile")) |
| 235 | } |
| 236 | } |
| 237 | return s.opWriteFiles(ctx, user, repo, "", branch, base, title, description, files, deletes) |
| 238 | } |