BlueGit

timfa bluegit

Files

bluegit / internal / server / project.go

project.go

feat: ververs de pipeline op het projectoverzicht
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Passed 42fd7379 History
project.go 6.8 KiB Download Raw
1package server
2
3import (
4 "context"
5 "fmt"
6 "html/template"
7 "net/http"
8 "path"
9 "sort"
10 "strings"
11
12 "gitlab.com/Timfa/bluegit/internal/access"
13 "gitlab.com/Timfa/bluegit/internal/gitrepo"
14 "gitlab.com/Timfa/bluegit/internal/highlight"
15 "gitlab.com/Timfa/bluegit/internal/i18n"
16 "gitlab.com/Timfa/bluegit/internal/markdown"
17 "gitlab.com/Timfa/bluegit/internal/store"
18)
19
20func (s *Server) fillOverview(r *http.Request, v *View, repo store.Repo) {
21 v.Page = "overview"
22 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
23 s.branchList(r, v, dir)
24 lang := i18n.Parse(v.Lang)
25 if c, err := gitrepo.LastCommit(r.Context(), s.Git, dir, v.Rev, ""); err == nil && c.SHA != "" {
26 if rows := s.presentCommits(r.Context(), repo.ID, []gitrepo.Commit{c}, lang); len(rows) > 0 {
27 s.attachHeadPipeline(r.Context(), repo, &rows[0], lang)
28 v.Head = &rows[0]
29 }
30 }
31 entries, err := gitrepo.Tree(r.Context(), s.Git, dir, v.Rev, "")
32 touch, _ := gitrepo.Touch(r.Context(), s.Git, dir, v.Rev)
33 if err == nil {
34 v.Files = fileRows(entries, "", touch, lang)
35 }
36 readme, license := findDocs(entries)
37 v.LicensePath = license
38 if readme != "" {
39 text, binary, err := gitrepo.Blob(r.Context(), s.Git, dir, v.Rev, readme, 200_000)
40 if err == nil && !binary {
41 v.ReadmeName = readme
42 base := "/" + repo.FullPath() + "/-/blob/"
43 v.Readme = template.HTML(markdown.RenderTasks(text, base, v.Rev, "/"+repo.FullPath(), markdown.Tasks{Count: s.tr(r, "tpl.tasks")}))
44 }
45 }
46 commits, branches, tags, _ := gitrepo.Stats(r.Context(), s.Git, dir, v.Rev)
47 v.NCommits, v.NBranches, v.NTags = commits, branches, tags
48 if rels, err := s.Store.ListReleases(r.Context(), repo.ID); err == nil {
49 v.NReleases = len(rels)
50 }
51 v.Created = i18n.FormatDate(lang, repo.CreatedAt)
52}
53
54func (s *Server) fillFiles(r *http.Request, v *View, repo store.Repo, rel string, isFile bool) {
55 v.Page = "files"
56 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
57 lang := i18n.Parse(v.Lang)
58 s.branchList(r, v, dir)
59 all, _ := gitrepo.TreeAll(r.Context(), s.Git, dir, v.Rev)
60 v.Tree = treeRows(all, rel)
61 if !isFile {
62 v.TreePath = rel
63 entries, err := gitrepo.Tree(r.Context(), s.Git, dir, v.Rev, rel)
64 touch, _ := gitrepo.Touch(r.Context(), s.Git, dir, v.Rev)
65 if err == nil {
66 v.Files = fileRows(entries, rel, touch, lang)
67 }
68 return
69 }
70 v.BlobPath = rel
71 v.FileName = path.Base(rel)
72 text, binary, err := gitrepo.Blob(r.Context(), s.Git, dir, v.Rev, rel, 200_000)
73 if err != nil {
74 return
75 }
76 v.Binary = binary
77 if !binary {
78 for i, line := range highlight.Lines(rel, text) {
79 v.Lines = append(v.Lines, CodeLine{N: i + 1, HTML: template.HTML(line)})
80 }
81 }
82 if n, err := gitrepo.BlobSize(r.Context(), s.Git, dir, v.Rev, rel); err == nil {
83 v.BlobSize = formatSize(n)
84 }
85 if c, err := gitrepo.LastCommit(r.Context(), s.Git, dir, v.Rev, rel); err == nil && c.SHA != "" {
86 if rows := s.presentCommits(r.Context(), repo.ID, []gitrepo.Commit{c}, lang); len(rows) > 0 {
87 s.attachHeadPipeline(r.Context(), repo, &rows[0], lang)
88 v.Head = &rows[0]
89 }
90 }
91}
92
93func (s *Server) branchList(r *http.Request, v *View, dir string) {
94 patterns, _ := s.Store.ProtectedPatterns(r.Context(), v.Repo.ID)
95 v.RevProtected = access.BranchProtected(patterns, v.Rev)
96 names, _ := gitrepo.Branches(r.Context(), s.Git, dir)
97 seen := false
98 for _, name := range names {
99 if name == v.Rev {
100 seen = true
101 }
102 v.Branches = append(v.Branches, BranchRow{Name: name, Protected: access.BranchProtected(patterns, name)})
103 }
104 if v.Rev != "" && !seen {
105 v.Branches = append([]BranchRow{{Name: v.Rev, Protected: v.RevProtected}}, v.Branches...)
106 }
107}
108
109func (s *Server) attachHeadPipeline(ctx context.Context, repo store.Repo, row *CommitRow, lang i18n.Lang) {
110 if row == nil || row.SHA == "" || s.Store == nil {
111 return
112 }
113 p, err := s.Store.LatestPipelineByCommit(ctx, repo.ID, row.SHA)
114 if err != nil {
115 return
116 }
117 jobs, _ := s.Store.JobsByPipeline(ctx, p.ID)
118 status := displayStatus(p.Status, jobs)
119 row.PipeID = p.ID
120 row.PipeStatus = status
121 row.PipeLabel = i18n.T(lang, "pipe."+status)
122 row.PipeDuration = pipelineClock(p)
123 tr := func(key string, args ...any) string { return i18n.T(lang, key, args...) }
124 row.PipeStages = stageViews(jobs, s.queueRanks(ctx), tr)
125 row.PipeLive = jobsLive(p.Status, jobs)
126}
127
128func fileRows(entries []gitrepo.Entry, base string, touch map[string]gitrepo.Commit, lang i18n.Lang) []FileRow {
129 var out []FileRow
130 for _, e := range entries {
131 p := e.Name
132 if base != "" {
133 p = base + "/" + e.Name
134 }
135 row := FileRow{Name: e.Name, Path: p, Dir: e.Type == "tree", Icon: fileIcon(e.Name, e.Type)}
136 if c, ok := touch[p]; ok {
137 row.Subject = c.Subject
138 row.When = i18n.Ago(lang, c.Unix)
139 row.SHA = c.SHA
140 }
141 out = append(out, row)
142 }
143 sort.Slice(out, func(i, j int) bool {
144 if out[i].Dir != out[j].Dir {
145 return out[i].Dir
146 }
147 return strings.ToLower(out[i].Name) < strings.ToLower(out[j].Name)
148 })
149 return out
150}
151
152func treeRows(entries []gitrepo.Entry, current string) []FileRow {
153 var out []FileRow
154 for _, e := range entries {
155 base := e.Name
156 if i := strings.LastIndex(e.Name, "/"); i >= 0 {
157 base = e.Name[i+1:]
158 }
159 depth := strings.Count(e.Name, "/")
160 if depth > 8 {
161 depth = 8
162 }
163 active := e.Name == current
164 if e.Type == "tree" && (current == e.Name || strings.HasPrefix(current, e.Name+"/")) {
165 active = true
166 }
167 out = append(out, FileRow{
168 Name: base, Path: e.Name, Dir: e.Type == "tree", Icon: fileIcon(base, e.Type),
169 Depth: depth, Active: active,
170 })
171 }
172 return out
173}
174
175func findDocs(entries []gitrepo.Entry) (readme, license string) {
176 readmeRank := 9
177 for _, e := range entries {
178 if e.Type != "blob" {
179 continue
180 }
181 low := strings.ToLower(e.Name)
182 rank := 9
183 switch low {
184 case "readme.md":
185 rank = 0
186 case "readme.markdown":
187 rank = 1
188 case "readme":
189 rank = 2
190 }
191 if rank < readmeRank {
192 readmeRank = rank
193 readme = e.Name
194 }
195 switch low {
196 case "license", "license.md", "license.txt", "licence", "copying":
197 if license == "" || low == "license" {
198 license = e.Name
199 }
200 }
201 }
202 return readme, license
203}
204
205func fileIcon(name, typ string) string {
206 if typ == "tree" {
207 return "dir"
208 }
209 switch strings.ToLower(path.Ext(name)) {
210 case ".md", ".markdown":
211 return "md"
212 case ".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp":
213 return "img"
214 case ".go", ".js", ".ts", ".py", ".rs", ".c", ".h", ".rb", ".sh":
215 return "code"
216 case ".yml", ".yaml", ".toml", ".json", ".lock":
217 return "cfg"
218 }
219 base := strings.ToLower(name)
220 if base == "license" || base == "licence" || base == "copying" || strings.HasPrefix(base, "license.") {
221 return "lic"
222 }
223 if strings.HasPrefix(base, "readme") {
224 return "md"
225 }
226 return "file"
227}
228
229func formatSize(n int64) string {
230 if n < 1024 {
231 return fmt.Sprintf("%d B", n)
232 }
233 if n < 1024*1024 {
234 return fmt.Sprintf("%.1f KiB", float64(n)/1024)
235 }
236 return fmt.Sprintf("%.1f MiB", float64(n)/(1024*1024))
237}