BlueGit

timfa bluegit

Files

bluegit / internal / server / branches.go

branches.go

feat: bied dezelfde acties als de UI aan via /api/v1
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
d89195e9 History
branches.go 3.4 KiB Download Raw
1package server
2
3import (
4 "net/http"
5 "sort"
6
7 "gitlab.com/Timfa/bluegit/internal/access"
8 "gitlab.com/Timfa/bluegit/internal/gitrepo"
9 "gitlab.com/Timfa/bluegit/internal/store"
10)
11
12func (s *Server) branches(w http.ResponseWriter, r *http.Request) {
13 _, repo, role, ok := s.guard(w, r, access.RoleReadonly)
14 if !ok {
15 return
16 }
17 v := s.repoView(w, r, repo, role, "title.branches")
18 v.Page = "branches"
19 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
20 tips, _ := gitrepo.BranchTips(r.Context(), s.Git, dir)
21 patterns, _ := s.Store.ProtectedPatterns(r.Context(), repo.ID)
22 for _, tip := range tips {
23 row := BranchRow{
24 Name: tip.Name, SHA: tip.SHA, Short: shortSHA(tip.SHA),
25 Subject: tip.Subject, When: tip.Date,
26 Protected: access.BranchProtected(patterns, tip.Name),
27 Default: tip.Name == repo.DefaultBranch,
28 }
29 if !row.Default && repo.DefaultBranch != "" {
30 ahead, behind, err := gitrepo.AheadBehind(r.Context(), s.Git, dir, repo.DefaultBranch, tip.Name)
31 if err == nil {
32 row.Ahead, row.Behind = ahead, behind
33 }
34 }
35 if p, err := s.Store.LatestPipelineByCommit(r.Context(), repo.ID, tip.SHA); err == nil {
36 jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
37 row.PipeID = p.ID
38 row.PipeStatus = displayStatus(p.Status, jobs)
39 row.PipeLabel = s.tr(r, "pipe."+row.PipeStatus)
40 }
41 if mrs, err := s.Store.OpenMergeRequestsBySource(r.Context(), repo.ID, tip.Name); err == nil && len(mrs) > 0 {
42 row.MRNumber = mrs[0].Number
43 }
44 v.Branches = append(v.Branches, row)
45 }
46 sort.Slice(v.Branches, func(i, j int) bool {
47 if v.Branches[i].Default != v.Branches[j].Default {
48 return v.Branches[i].Default
49 }
50 return v.Branches[i].Name < v.Branches[j].Name
51 })
52 tags, _ := gitrepo.Tags(r.Context(), s.Git, dir)
53 for _, name := range tags {
54 v.Tags = append(v.Tags, TagRow{Name: name})
55 }
56 s.render(w, "branches", v)
57}
58
59func (s *Server) createBranch(w http.ResponseWriter, r *http.Request) {
60 u, repo, _, ok := s.guard(w, r, access.RoleDeveloper)
61 if !ok || !s.csrfOK(w, r) {
62 return
63 }
64 back := repoURL(repo) + "/-/branches"
65 if !s.uiOK(w, r, back, s.opCreateBranch(r.Context(), u, repo, r.FormValue("name"), r.FormValue("from"))) {
66 return
67 }
68 s.redirect(w, r, back, s.tr(r, "flash.branchCreated"), "")
69}
70
71func (s *Server) deleteBranch(w http.ResponseWriter, r *http.Request) {
72 u, repo, _, ok := s.guard(w, r, access.RoleDeveloper)
73 if !ok || !s.csrfOK(w, r) {
74 return
75 }
76 back := repoURL(repo) + "/-/branches"
77 if !s.uiOK(w, r, back, s.opDeleteBranch(r.Context(), u, repo, r.FormValue("name"), r.FormValue("confirm"))) {
78 return
79 }
80 s.redirect(w, r, back, s.tr(r, "flash.branchDeleted"), "")
81}
82
83func (s *Server) renameBranch(w http.ResponseWriter, r *http.Request) {
84 u, repo, _, ok := s.guard(w, r, access.RoleDeveloper)
85 if !ok || !s.csrfOK(w, r) {
86 return
87 }
88 back := repoURL(repo) + "/-/branches"
89 if !s.uiOK(w, r, back, s.opRenameBranch(r.Context(), u, repo, r.FormValue("name"), r.FormValue("new_name"), r.FormValue("confirm"))) {
90 return
91 }
92 s.redirect(w, r, back, s.tr(r, "flash.branchRenamed"), "")
93}
94
95// branchLocked weigert de standaardbranch en elke beschermde branch, ook voor een beheerder.
96func (s *Server) branchLocked(r *http.Request, repo store.Repo, name string) (bool, string) {
97 if name == repo.DefaultBranch {
98 return true, s.tr(r, "err.branchLocked", name)
99 }
100 patterns, _ := s.Store.ProtectedPatterns(r.Context(), repo.ID)
101 if access.BranchProtected(patterns, name) {
102 return true, s.tr(r, "err.branchLocked", name)
103 }
104 return false, ""
105}