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