BlueGit

timfa bluegit

Files

bluegit / internal / server / branches.go

branches.go

fix: toon mergeblokkades en markeer een al gepushte merge
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
81edde89 History
branches.go 3.9 KiB Download Raw
1package server
2
3import (
4 "net/http"
5 "sort"
6 "strconv"
7 "strings"
8 "time"
9
10 "gitlab.com/Timfa/bluegit/internal/access"
11 "gitlab.com/Timfa/bluegit/internal/gitrepo"
12 "gitlab.com/Timfa/bluegit/internal/i18n"
13 "gitlab.com/Timfa/bluegit/internal/store"
14)
15
16func (s *Server) branches(w http.ResponseWriter, r *http.Request) {
17 _, repo, role, ok := s.guard(w, r, access.RoleReadonly)
18 if !ok {
19 return
20 }
21 s.absorbMergedMRs(r.Context(), repo)
22 v := s.repoView(w, r, repo, role, "title.branches")
23 v.Page = "branches"
24 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
25 tips, _ := gitrepo.BranchTips(r.Context(), s.Git, dir)
26 patterns, _ := s.Store.ProtectedPatterns(r.Context(), repo.ID)
27 lang := s.langOf(r)
28 for _, tip := range tips {
29 when, title := branchWhen(lang, tip.Date)
30 row := BranchRow{
31 Name: tip.Name, SHA: tip.SHA, Short: shortSHA(tip.SHA),
32 Subject: tip.Subject, When: when, WhenTitle: title,
33 Protected: access.BranchProtected(patterns, tip.Name),
34 Default: tip.Name == repo.DefaultBranch,
35 }
36 if !row.Default && repo.DefaultBranch != "" {
37 ahead, behind, err := gitrepo.AheadBehind(r.Context(), s.Git, dir, repo.DefaultBranch, tip.Name)
38 if err == nil {
39 row.Ahead, row.Behind = ahead, behind
40 }
41 }
42 if p, err := s.Store.LatestPipelineByCommit(r.Context(), repo.ID, tip.SHA); err == nil {
43 jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
44 row.PipeID = p.ID
45 row.PipeStatus = displayStatus(p.Status, jobs)
46 row.PipeLabel = s.tr(r, "pipe."+row.PipeStatus)
47 }
48 if mrs, err := s.Store.OpenMergeRequestsBySource(r.Context(), repo.ID, tip.Name); err == nil {
49 for _, mr := range mrs {
50 row.OpenMRs = append(row.OpenMRs, mr.Number)
51 }
52 }
53 v.Branches = append(v.Branches, row)
54 }
55 sort.Slice(v.Branches, func(i, j int) bool {
56 if v.Branches[i].Default != v.Branches[j].Default {
57 return v.Branches[i].Default
58 }
59 return v.Branches[i].Name < v.Branches[j].Name
60 })
61 tags, _ := gitrepo.Tags(r.Context(), s.Git, dir)
62 for _, name := range tags {
63 v.Tags = append(v.Tags, TagRow{Name: name})
64 }
65 s.render(w, "branches", v)
66}
67
68func (s *Server) createBranch(w http.ResponseWriter, r *http.Request) {
69 u, repo, _, ok := s.guard(w, r, access.RoleDeveloper)
70 if !ok || !s.csrfOK(w, r) {
71 return
72 }
73 back := repoURL(repo) + "/-/branches"
74 if !s.uiOK(w, r, back, s.opCreateBranch(r.Context(), u, repo, r.FormValue("name"), r.FormValue("from"))) {
75 return
76 }
77 s.redirect(w, r, back, s.tr(r, "flash.branchCreated"), "")
78}
79
80func (s *Server) deleteBranch(w http.ResponseWriter, r *http.Request) {
81 u, repo, _, ok := s.guard(w, r, access.RoleDeveloper)
82 if !ok || !s.csrfOK(w, r) {
83 return
84 }
85 back := repoURL(repo) + "/-/branches"
86 if !s.uiOK(w, r, back, s.opDeleteBranch(r.Context(), u, repo, r.FormValue("name"), r.FormValue("confirm"))) {
87 return
88 }
89 s.redirect(w, r, back, s.tr(r, "flash.branchDeleted"), "")
90}
91
92func (s *Server) renameBranch(w http.ResponseWriter, r *http.Request) {
93 u, repo, _, ok := s.guard(w, r, access.RoleDeveloper)
94 if !ok || !s.csrfOK(w, r) {
95 return
96 }
97 back := repoURL(repo) + "/-/branches"
98 if !s.uiOK(w, r, back, s.opRenameBranch(r.Context(), u, repo, r.FormValue("name"), r.FormValue("new_name"), r.FormValue("confirm"))) {
99 return
100 }
101 s.redirect(w, r, back, s.tr(r, "flash.branchRenamed"), "")
102}
103
104func branchWhen(lang i18n.Lang, unixText string) (short, title string) {
105 unix, err := strconv.ParseInt(strings.TrimSpace(unixText), 10, 64)
106 if err != nil || unix <= 0 {
107 return "", ""
108 }
109 at := time.Unix(unix, 0).UTC()
110 return i18n.ShortAgo(lang, unix), at.Format("2006-01-02 15:04 UTC")
111}
112
113// branchLocked weigert de standaardbranch en elke beschermde branch, ook voor een beheerder.
114func (s *Server) branchLocked(r *http.Request, repo store.Repo, name string) (bool, string) {
115 if name == repo.DefaultBranch {
116 return true, s.tr(r, "err.branchLocked", name)
117 }
118 patterns, _ := s.Store.ProtectedPatterns(r.Context(), repo.ID)
119 if access.BranchProtected(patterns, name) {
120 return true, s.tr(r, "err.branchLocked", name)
121 }
122 return false, ""
123}