| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | |
| 8 | "gitlab.com/Timfa/bluegit/internal/access" |
| 9 | "gitlab.com/Timfa/bluegit/internal/desctpl" |
| 10 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 11 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 12 | "gitlab.com/Timfa/bluegit/internal/store" |
| 13 | ) |
| 14 | |
| 15 | func (s *Server) guard(w http.ResponseWriter, r *http.Request, min access.Role) (store.User, store.Repo, access.Role, bool) { |
| 16 | owner := r.PathValue("owner") |
| 17 | name := strings.TrimSuffix(r.PathValue("repo"), ".git") |
| 18 | repo, err := s.Store.RepoByPath(r.Context(), owner, name) |
| 19 | if err != nil { |
| 20 | http.NotFound(w, r) |
| 21 | return store.User{}, store.Repo{}, "", false |
| 22 | } |
| 23 | p := s.who(r) |
| 24 | var role access.Role |
| 25 | if p.OK { |
| 26 | role, err = s.Store.EffectiveRole(r.Context(), p.User.ID, repo.ID) |
| 27 | if err != nil { |
| 28 | http.Error(w, s.tr(r, "err.rights"), http.StatusInternalServerError) |
| 29 | return store.User{}, store.Repo{}, "", false |
| 30 | } |
| 31 | } |
| 32 | if isCrawler(r) { |
| 33 | p = principal{} |
| 34 | role = access.RoleNone |
| 35 | } |
| 36 | readable := role.CanRead() || repo.Visibility == "public" |
| 37 | if !readable && releaseOnly(repo, role) && (r.Method == http.MethodGet || r.Method == http.MethodHead) && isRepoRootPath(r.URL.Path, repo) { |
| 38 | // Privé repo met publieke releases: alleen de Releases-pagina. |
| 39 | http.Redirect(w, r, "/"+repo.FullPath()+"/-/releases", http.StatusSeeOther) |
| 40 | return store.User{}, store.Repo{}, "", false |
| 41 | } |
| 42 | if !readable { |
| 43 | if isCrawler(r) { |
| 44 | s.writeLockedCard(w, r) |
| 45 | } else if p.OK { |
| 46 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 47 | } else { |
| 48 | http.NotFound(w, r) |
| 49 | } |
| 50 | return store.User{}, store.Repo{}, "", false |
| 51 | } |
| 52 | if min != access.RoleReadonly && !role.AtLeast(min) { |
| 53 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 54 | return store.User{}, store.Repo{}, "", false |
| 55 | } |
| 56 | return p.User, repo, role, true |
| 57 | } |
| 58 | |
| 59 | func (s *Server) repoView(w http.ResponseWriter, r *http.Request, repo store.Repo, role access.Role, title string) View { |
| 60 | v := s.view(w, r, title) |
| 61 | v.Repo = &repo |
| 62 | v.Role = role |
| 63 | v.CanWrite = role.CanWriteCode() |
| 64 | v.CanAdmin = role.CanManageSettings() |
| 65 | v.Rev = safeRev(r.URL.Query().Get("rev"), repo.DefaultBranch) |
| 66 | v.CloneHTTP = strings.TrimRight(s.Cfg.BaseURL, "/") + "/" + repo.FullPath() + ".git" |
| 67 | v.CloneSSH = s.Cfg.CloneSSH(repo.FullPath()) |
| 68 | v.ShowReleases = canSeeReleases(repo, role) |
| 69 | if releaseOnly(repo, role) { |
| 70 | card := releaseOnlyCard(repo) |
| 71 | v.Repo = &card |
| 72 | v.ReleaseOnly = true |
| 73 | v.CloneHTTP, v.CloneSSH, v.Rev = "", "", "" |
| 74 | } |
| 75 | return v |
| 76 | } |
| 77 | |
| 78 | func (s *Server) repoHome(w http.ResponseWriter, r *http.Request) { |
| 79 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 80 | if !ok { |
| 81 | return |
| 82 | } |
| 83 | v := s.repoView(w, r, repo, role, repo.Name) |
| 84 | v.Wide = true |
| 85 | s.fillOverview(r, &v, repo) |
| 86 | uid := int64(0) |
| 87 | if v.User != nil { |
| 88 | uid = v.User.ID |
| 89 | v.ForkTargets = s.forkTargets(r.Context(), *v.User) |
| 90 | } |
| 91 | v.Stars, v.Starred = s.starState(r.Context(), uid, repo.ID) |
| 92 | v.Fork = s.forkInfo(r.Context(), uid, repo) |
| 93 | v.Heat = s.heatmap(r.Context(), 0, []int64{repo.ID}, s.langOf(r)) |
| 94 | s.render(w, "repo", v) |
| 95 | } |
| 96 | |
| 97 | func (s *Server) treeRoot(w http.ResponseWriter, r *http.Request) { |
| 98 | r.SetPathValue("path", "") |
| 99 | s.tree(w, r) |
| 100 | } |
| 101 | |
| 102 | func (s *Server) tree(w http.ResponseWriter, r *http.Request) { |
| 103 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 104 | if !ok { |
| 105 | return |
| 106 | } |
| 107 | rel, err := cleanRel(r.PathValue("path")) |
| 108 | if err != nil { |
| 109 | http.NotFound(w, r) |
| 110 | return |
| 111 | } |
| 112 | v := s.repoView(w, r, repo, role, rel) |
| 113 | v.Wide = true |
| 114 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 115 | if _, err := gitrepo.Tree(r.Context(), s.Git, dir, v.Rev, rel); err != nil && rel != "" { |
| 116 | http.NotFound(w, r) |
| 117 | return |
| 118 | } |
| 119 | s.fillFiles(r, &v, repo, rel, false) |
| 120 | s.render(w, "files", v) |
| 121 | } |
| 122 | |
| 123 | func (s *Server) blob(w http.ResponseWriter, r *http.Request) { |
| 124 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 125 | if !ok { |
| 126 | return |
| 127 | } |
| 128 | rel, err := cleanRel(r.PathValue("path")) |
| 129 | if err != nil || rel == "" { |
| 130 | http.NotFound(w, r) |
| 131 | return |
| 132 | } |
| 133 | v := s.repoView(w, r, repo, role, rel) |
| 134 | v.Wide = true |
| 135 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 136 | if _, _, err := gitrepo.Blob(r.Context(), s.Git, dir, v.Rev, rel, 1); err != nil { |
| 137 | http.NotFound(w, r) |
| 138 | return |
| 139 | } |
| 140 | s.fillFiles(r, &v, repo, rel, true) |
| 141 | s.render(w, "files", v) |
| 142 | } |
| 143 | |
| 144 | func (s *Server) commits(w http.ResponseWriter, r *http.Request) { |
| 145 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 146 | if !ok { |
| 147 | return |
| 148 | } |
| 149 | v := s.repoView(w, r, repo, role, "title.commits") |
| 150 | v.Page = "commits" |
| 151 | v.Wide = true |
| 152 | v.Query = clipQuery(r.URL.Query().Get("q")) |
| 153 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 154 | s.branchList(r, &v, repo) |
| 155 | page := commitPageNum(r.URL.Query().Get("page")) |
| 156 | list, more := s.commitWindow(r.Context(), dir, v.Rev, v.Query, (page-1)*commitPageSize, commitPageSize) |
| 157 | lang := i18n.Parse(v.Lang) |
| 158 | rows := s.presentCommits(r.Context(), repo.ID, list, lang) |
| 159 | for i := range rows { |
| 160 | s.attachHeadPipeline(r.Context(), repo, &rows[i], lang) |
| 161 | } |
| 162 | v.Commits = rows |
| 163 | v.CommitDays = groupCommitDays(rows, lang) |
| 164 | if page > 1 { |
| 165 | v.CommitPrev = commitListURL(repo.FullPath(), v.Rev, v.Query, page-1) |
| 166 | } |
| 167 | if more { |
| 168 | v.CommitNext = commitListURL(repo.FullPath(), v.Rev, v.Query, page+1) |
| 169 | } |
| 170 | s.render(w, "commits", v) |
| 171 | } |
| 172 | |
| 173 | func (s *Server) commit(w http.ResponseWriter, r *http.Request) { |
| 174 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 175 | if !ok { |
| 176 | return |
| 177 | } |
| 178 | sha := r.PathValue("sha") |
| 179 | if !safeSHA(sha) { |
| 180 | http.NotFound(w, r) |
| 181 | return |
| 182 | } |
| 183 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 184 | c, err := gitrepo.Show(r.Context(), s.Git, dir, sha) |
| 185 | if err != nil { |
| 186 | http.NotFound(w, r) |
| 187 | return |
| 188 | } |
| 189 | v := s.repoView(w, r, repo, role, c.Subject) |
| 190 | v.Page = "commit" |
| 191 | v.Wide = true |
| 192 | lang := i18n.Parse(v.Lang) |
| 193 | rows := s.presentCommits(r.Context(), repo.ID, []gitrepo.Commit{c}, lang) |
| 194 | s.attachHeadPipeline(r.Context(), repo, &rows[0], lang) |
| 195 | v.Commit = &rows[0] |
| 196 | parents, files, err := gitrepo.CommitPatch(r.Context(), s.Git, dir, c.SHA) |
| 197 | if err != nil && s.Log != nil { |
| 198 | s.Log.Warn("commitdiff", "sha", c.SHA, "err", err) |
| 199 | } |
| 200 | v.CommitDiff = presentCommitDiff(repo.FullPath(), c.SHA, parents, files) |
| 201 | s.render(w, "commit", v) |
| 202 | } |
| 203 | |
| 204 | func (s *Server) mergeRequests(w http.ResponseWriter, r *http.Request) { |
| 205 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 206 | if !ok { |
| 207 | return |
| 208 | } |
| 209 | s.absorbMergedMRs(r.Context(), repo) |
| 210 | v := s.repoView(w, r, repo, role, "title.mrs") |
| 211 | v.Page = "mrs" |
| 212 | v.Wide = true |
| 213 | all, _ := s.Store.ListMergeRequests(r.Context(), repo.ID) |
| 214 | v.Flow = s.flowMRs(r, repo, role, all) |
| 215 | s.render(w, "mrs", v) |
| 216 | } |
| 217 | |
| 218 | func (s *Server) createMR(w http.ResponseWriter, r *http.Request) { |
| 219 | u, repo, _, ok := s.guard(w, r, access.RoleDeveloper) |
| 220 | if !ok || !s.csrfOK(w, r) { |
| 221 | return |
| 222 | } |
| 223 | source := safeRev(r.FormValue("source"), "") |
| 224 | target := safeRev(r.FormValue("target"), repo.DefaultBranch) |
| 225 | desc := desctpl.Expand(r.FormValue("description"), s.mrVars(r, repo, source, target)) |
| 226 | mr, err := s.opCreateMR(r.Context(), u, repo, r.FormValue("title"), desc, source, target, r.FormValue("draft") == "1") |
| 227 | if err == nil { |
| 228 | err = s.Store.SetMergeOptions(r.Context(), mr.ID, r.FormValue("remove_source_branch") == "1", r.FormValue("squash") == "1", r.FormValue("squash_commit_message")) |
| 229 | } |
| 230 | if !s.uiOK(w, r, repoURL(repo)+"/-/merge-requests", err) { |
| 231 | return |
| 232 | } |
| 233 | num := strconv.FormatInt(mr.Number, 10) |
| 234 | s.redirect(w, r, repoURL(repo)+"/-/merge-requests/"+num, s.tr(r, "flash.mrOpened"), "") |
| 235 | } |
| 236 | |
| 237 | func (s *Server) showMR(w http.ResponseWriter, r *http.Request) { |
| 238 | u, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 239 | if !ok { |
| 240 | return |
| 241 | } |
| 242 | s.absorbMergedMRs(r.Context(), repo) |
| 243 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 244 | if err != nil { |
| 245 | http.NotFound(w, r) |
| 246 | return |
| 247 | } |
| 248 | mr, err := s.Store.MergeRequestByNumber(r.Context(), repo.ID, n) |
| 249 | if err != nil { |
| 250 | http.NotFound(w, r) |
| 251 | return |
| 252 | } |
| 253 | v := s.repoView(w, r, repo, role, mr.Title) |
| 254 | v.Page = "mr" |
| 255 | v.MR = &mr |
| 256 | v.Wide = true |
| 257 | patterns, _ := s.Store.ProtectedPatterns(r.Context(), repo.ID) |
| 258 | v.CanComment = role.AtLeast(access.RoleReadonly) |
| 259 | v.CanMerge = mr.Status == "open" && role.CanMerge(access.BranchProtected(patterns, mr.TargetBranch)) |
| 260 | v.CanApply = mr.Status == "open" && role.CanPushBranch(access.BranchProtected(patterns, mr.SourceBranch)) |
| 261 | v.MergeBlocks = s.mergeBlocks(r, repo, mr) |
| 262 | v.CanForce = v.CanMerge && role.CanManageSettings() && len(v.MergeBlocks) > 0 |
| 263 | s.fillMRPipeline(r, &v, repo, mr) |
| 264 | s.fillReviews(r, &v, repo, mr, role) |
| 265 | if u.ID != 0 { |
| 266 | mine := u.ID == mr.AuthorID || role.CanManageSettings() |
| 267 | v.CanClose = mr.Status == "open" && mine |
| 268 | v.CanReopen = mr.Status == "closed" && mine |
| 269 | } |
| 270 | s.finishMR(r, &v, repo, mr, u) |
| 271 | s.render(w, "mr", v) |
| 272 | } |
| 273 | |
| 274 | func (s *Server) createNote(w http.ResponseWriter, r *http.Request) { |
| 275 | u, repo, _, ok := s.guard(w, r, access.RoleReadonly) |
| 276 | if !ok || !s.csrfOK(w, r) { |
| 277 | return |
| 278 | } |
| 279 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 280 | if err != nil { |
| 281 | http.NotFound(w, r) |
| 282 | return |
| 283 | } |
| 284 | back := repoURL(repo) + "/-/merge-requests/" + r.PathValue("number") |
| 285 | parent, _ := strconv.ParseInt(r.FormValue("parent"), 10, 64) |
| 286 | start, _ := strconv.Atoi(r.FormValue("line_start")) |
| 287 | end, _ := strconv.Atoi(r.FormValue("line_end")) |
| 288 | if _, err := s.opAddNote(r, u, repo, n, r.FormValue("body"), r.FormValue("path"), start, end, parent); !s.uiOK(w, r, back, err) { |
| 289 | return |
| 290 | } |
| 291 | s.redirect(w, r, back, s.tr(r, "flash.note"), "") |
| 292 | } |
| 293 | |
| 294 | func (s *Server) mergeMR(w http.ResponseWriter, r *http.Request) { |
| 295 | u, repo, _, ok := s.guard(w, r, access.RoleDeveloper) |
| 296 | if !ok || !s.csrfOK(w, r) { |
| 297 | return |
| 298 | } |
| 299 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 300 | if err != nil { |
| 301 | http.NotFound(w, r) |
| 302 | return |
| 303 | } |
| 304 | back := repoURL(repo) + "/-/merge-requests/" + r.PathValue("number") |
| 305 | out, err := s.opMergeMR(r, u, repo, n, formMergeChoice(r)) |
| 306 | if !s.uiOK(w, r, back, err) { |
| 307 | return |
| 308 | } |
| 309 | notice := s.tr(r, out.Notice) |
| 310 | if out.Arg != "" { |
| 311 | notice = s.tr(r, out.Notice, out.Arg) |
| 312 | } |
| 313 | s.redirect(w, r, back, notice, "") |
| 314 | } |
| 315 | |
| 316 | func formMergeChoice(r *http.Request) mergeChoice { |
| 317 | remove := r.FormValue("remove_source_branch") == "1" |
| 318 | squash := r.FormValue("squash") == "1" |
| 319 | message := r.FormValue("squash_commit_message") |
| 320 | return mergeChoice{ |
| 321 | FromForm: true, |
| 322 | Remove: &remove, |
| 323 | Squash: &squash, |
| 324 | Message: &message, |
| 325 | Force: r.FormValue("force") == "1", |
| 326 | Confirm: r.FormValue("confirm_force") == "1", |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | func (s *Server) closeMR(w http.ResponseWriter, r *http.Request) { |
| 331 | u, repo, _, ok := s.guard(w, r, access.RoleReadonly) |
| 332 | if !ok || !s.csrfOK(w, r) { |
| 333 | return |
| 334 | } |
| 335 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 336 | if err != nil { |
| 337 | http.NotFound(w, r) |
| 338 | return |
| 339 | } |
| 340 | back := repoURL(repo) + "/-/merge-requests/" + r.PathValue("number") |
| 341 | if !s.uiOK(w, r, back, s.opCloseMR(r.Context(), u, repo, n, false)) { |
| 342 | return |
| 343 | } |
| 344 | s.redirect(w, r, back, s.tr(r, "flash.closed"), "") |
| 345 | } |
| 346 | |
| 347 | func (s *Server) mrFromRequest(r *http.Request, repoID int64) (store.MergeRequest, error) { |
| 348 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 349 | if err != nil { |
| 350 | return store.MergeRequest{}, err |
| 351 | } |
| 352 | return s.Store.MergeRequestByNumber(r.Context(), repoID, n) |
| 353 | } |
| 354 | |
| 355 | func (s *Server) pipelines(w http.ResponseWriter, r *http.Request) { |
| 356 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 357 | if !ok { |
| 358 | return |
| 359 | } |
| 360 | if id := strings.TrimSpace(r.URL.Query().Get("show")); id != "" { |
| 361 | if n, err := strconv.ParseInt(id, 10, 64); err == nil && n > 0 { |
| 362 | http.Redirect(w, r, repoURL(repo)+"/-/pipelines/"+id, http.StatusSeeOther) |
| 363 | return |
| 364 | } |
| 365 | } |
| 366 | v := s.repoView(w, r, repo, role, "title.pipelines") |
| 367 | v.Wide = true |
| 368 | v.Page = "pipelines" |
| 369 | s.fillPipelines(r, &v, repo) |
| 370 | if wantsJSON(r) { |
| 371 | s.writePipeList(w, v) |
| 372 | return |
| 373 | } |
| 374 | s.render(w, "pipelines", v) |
| 375 | } |
| 376 | |
| 377 | func (s *Server) pipeline(w http.ResponseWriter, r *http.Request) { |
| 378 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 379 | if !ok { |
| 380 | return |
| 381 | } |
| 382 | id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) |
| 383 | if err != nil { |
| 384 | http.NotFound(w, r) |
| 385 | return |
| 386 | } |
| 387 | p, err := s.Store.PipelineByID(r.Context(), repo.ID, id) |
| 388 | if err != nil { |
| 389 | http.NotFound(w, r) |
| 390 | return |
| 391 | } |
| 392 | v := s.repoView(w, r, repo, role, "title.pipeline") |
| 393 | v.Wide = true |
| 394 | v.Page = "pipelines" |
| 395 | v.Pipe = &p |
| 396 | jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID) |
| 397 | v.Jobs = jobs |
| 398 | ranks := s.queueRanks(r.Context()) |
| 399 | tr := func(key string, args ...any) string { return s.tr(r, key, args...) } |
| 400 | v.PipeStatus = displayStatus(s.pipeStatusFor(p), jobs) |
| 401 | if blk, ok := s.protectedBlocker(p); ok { |
| 402 | v.PipeWait = tr("pipe.waitingFor", strconv.Quote(blk.Name), blk.PipelineID) |
| 403 | } |
| 404 | if role.CanWriteCode() { |
| 405 | v.PipeCancel = cancelView(tr, tr("pipe.cancelPipeline"), tr("pipe.cancelPipeAsk"), jobs) |
| 406 | } |
| 407 | v.PipeLabel = tr("pipe." + v.PipeStatus) |
| 408 | v.PipeSource = s.pipelineSourceText(r, repo, p) |
| 409 | v.PipeJobs = linksFor(jobs, ranks, tr) |
| 410 | v.PipeStages = stageViews(jobs, ranks, tr) |
| 411 | v.PipeFailed = jobsFailed(jobs) |
| 412 | v.Live = jobsLive(p.Status, jobs) |
| 413 | v.LiveURL = repoURL(repo) + "/-/pipelines/" + strconv.FormatInt(p.ID, 10) |
| 414 | if wantsJSON(r) { |
| 415 | s.writePipeDetail(w, v) |
| 416 | return |
| 417 | } |
| 418 | s.render(w, "pipeline", v) |
| 419 | } |
| 420 | |
| 421 | func (s *Server) runPipeline(w http.ResponseWriter, r *http.Request) { |
| 422 | u, repo, _, ok := s.guard(w, r, access.RoleDeveloper) |
| 423 | if !ok || !s.csrfOK(w, r) { |
| 424 | return |
| 425 | } |
| 426 | if _, err := s.opRunPipeline(r.Context(), u, repo, r.FormValue("ref"), "web"); !s.uiOK(w, r, repoURL(repo)+"/-/pipelines", err) { |
| 427 | return |
| 428 | } |
| 429 | s.redirect(w, r, repoURL(repo)+"/-/pipelines", s.tr(r, "flash.pipeline"), "") |
| 430 | } |
| 431 | |
| 432 | func (s *Server) releases(w http.ResponseWriter, r *http.Request) { |
| 433 | _, repo, role, ok := s.guardReleases(w, r) |
| 434 | if !ok { |
| 435 | return |
| 436 | } |
| 437 | v := s.repoView(w, r, repo, role, "title.releases") |
| 438 | v.Page = "releases" |
| 439 | v.Wide = true |
| 440 | v.Releases, _ = s.Store.ListReleases(r.Context(), repo.ID) |
| 441 | s.render(w, "releases", v) |
| 442 | } |
| 443 | |
| 444 | func (s *Server) createRepo(w http.ResponseWriter, r *http.Request) { |
| 445 | u, ok := s.loginRequired(w, r) |
| 446 | if !ok || !s.csrfOK(w, r) { |
| 447 | return |
| 448 | } |
| 449 | repo, err := s.opCreateRepo(r.Context(), u, r.FormValue("name"), r.FormValue("description"), r.FormValue("group"), r.FormValue("visibility"), false) |
| 450 | if err != nil { |
| 451 | if statusOf(err) == http.StatusForbidden { |
| 452 | http.Error(w, s.trErr(r, err), http.StatusForbidden) |
| 453 | return |
| 454 | } |
| 455 | s.failCreate(w, r, s.trErr(r, err)) |
| 456 | return |
| 457 | } |
| 458 | s.finishCreate(w, r, repoURL(repo), s.tr(r, "flash.repoCreated")) |
| 459 | } |
| 460 | |
| 461 | func (s *Server) createGroup(w http.ResponseWriter, r *http.Request) { |
| 462 | u, ok := s.loginRequired(w, r) |
| 463 | if !ok || !s.csrfOK(w, r) { |
| 464 | return |
| 465 | } |
| 466 | g, err := s.opCreateGroup(r.Context(), u, r.FormValue("name"), r.FormValue("description"), r.FormValue("default_role")) |
| 467 | if err != nil { |
| 468 | s.failCreate(w, r, s.trErr(r, err)) |
| 469 | return |
| 470 | } |
| 471 | s.finishCreate(w, r, "/groups/"+g.Path, s.tr(r, "flash.groupCreated")) |
| 472 | } |
| 473 | |
| 474 | func toEntries(base string, in []gitrepo.Entry) []EntryRow { |
| 475 | var out []EntryRow |
| 476 | for _, e := range in { |
| 477 | p := e.Name |
| 478 | if base != "" { |
| 479 | p = base + "/" + e.Name |
| 480 | } |
| 481 | out = append(out, EntryRow{Name: e.Name, Type: e.Type, Path: p}) |
| 482 | } |
| 483 | return out |
| 484 | } |
| 485 | |
| 486 | func cleanRel(p string) (string, error) { |
| 487 | p = strings.TrimPrefix(p, "/") |
| 488 | if p == "" || p == "." { |
| 489 | return "", nil |
| 490 | } |
| 491 | if strings.HasPrefix(p, "/") || strings.Contains(p, "\\") || strings.Contains(p, "..") { |
| 492 | return "", errBadID |
| 493 | } |
| 494 | return p, nil |
| 495 | } |
| 496 | |
| 497 | func safeRev(s, fallback string) string { |
| 498 | if s == "" { |
| 499 | return fallback |
| 500 | } |
| 501 | if strings.HasPrefix(s, "-") || strings.Contains(s, "..") || strings.ContainsAny(s, " \t\n~^:?*[\\") { |
| 502 | return fallback |
| 503 | } |
| 504 | return s |
| 505 | } |
| 506 | |
| 507 | func safeSHA(s string) bool { |
| 508 | if len(s) < 7 || len(s) > 64 { |
| 509 | return false |
| 510 | } |
| 511 | for _, c := range s { |
| 512 | if (c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F') { |
| 513 | return false |
| 514 | } |
| 515 | } |
| 516 | return true |
| 517 | } |
| 518 | |
| 519 | // isRepoRootPath: /{owner}/{repo}, met of zonder slash, of /{owner}/{repo}/-. |
| 520 | func isRepoRootPath(p string, repo store.Repo) bool { |
| 521 | t := strings.Trim(p, "/") |
| 522 | return t == repo.FullPath() || t == repo.FullPath()+"/-" |
| 523 | } |
| 524 | |
| 525 | // repoRootAlias: /{owner}/{repo}/ en /{owner}/{repo}/- gaan naar de projectpagina |
| 526 | // (of, release-only, naar Releases via guard). Geen toegang: 404 zoals niet-bestaand. |
| 527 | func (s *Server) repoRootAlias(w http.ResponseWriter, r *http.Request) { |
| 528 | _, repo, _, ok := s.guard(w, r, access.RoleReadonly) |
| 529 | if !ok { |
| 530 | return |
| 531 | } |
| 532 | http.Redirect(w, r, "/"+repo.FullPath(), http.StatusSeeOther) |
| 533 | } |