BlueGit

timfa bluegit

Files

bluegit / internal / server / project.go

project.go

feat: laat een pipeline opnieuw draaien
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
19059084 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 row.PipeFailed = jobsFailed(jobs)
127}
128
129func fileRows(entries []gitrepo.Entry, base string, touch map[string]gitrepo.Commit, lang i18n.Lang) []FileRow {
130 var out []FileRow
131 for _, e := range entries {
132 p := e.Name
133 if base != "" {
134 p = base + "/" + e.Name
135 }
136 row := FileRow{Name: e.Name, Path: p, Dir: e.Type == "tree", Icon: fileIcon(e.Name, e.Type)}
137 if c, ok := touch[p]; ok {
138 row.Subject = c.Subject
139 row.When = i18n.Ago(lang, c.Unix)
140 row.SHA = c.SHA
141 }
142 out = append(out, row)
143 }
144 sort.Slice(out, func(i, j int) bool {
145 if out[i].Dir != out[j].Dir {
146 return out[i].Dir
147 }
148 return strings.ToLower(out[i].Name) < strings.ToLower(out[j].Name)
149 })
150 return out
151}
152
153func treeRows(entries []gitrepo.Entry, current string) []FileRow {
154 var out []FileRow
155 for _, e := range entries {
156 base := e.Name
157 if i := strings.LastIndex(e.Name, "/"); i >= 0 {
158 base = e.Name[i+1:]
159 }
160 depth := strings.Count(e.Name, "/")
161 if depth > 8 {
162 depth = 8
163 }
164 dir := e.Type == "tree"
165 inside := current == e.Name || strings.HasPrefix(current, e.Name+"/")
166 active := e.Name == current
167 if dir && inside {
168 active = true
169 }
170 out = append(out, FileRow{
171 Name: base, Path: e.Name, Dir: dir, Icon: fileIcon(base, e.Type),
172 Depth: depth, Active: active, Open: dir && inside,
173 })
174 }
175 return out
176}
177
178func findDocs(entries []gitrepo.Entry) (readme, license string) {
179 readmeRank := 9
180 for _, e := range entries {
181 if e.Type != "blob" {
182 continue
183 }
184 low := strings.ToLower(e.Name)
185 rank := 9
186 switch low {
187 case "readme.md":
188 rank = 0
189 case "readme.markdown":
190 rank = 1
191 case "readme":
192 rank = 2
193 }
194 if rank < readmeRank {
195 readmeRank = rank
196 readme = e.Name
197 }
198 switch low {
199 case "license", "license.md", "license.txt", "licence", "copying":
200 if license == "" || low == "license" {
201 license = e.Name
202 }
203 }
204 }
205 return readme, license
206}
207
208func fileIcon(name, typ string) string {
209 if typ == "tree" {
210 return "dir"
211 }
212 switch strings.ToLower(path.Ext(name)) {
213 case ".md", ".markdown":
214 return "md"
215 case ".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp":
216 return "img"
217 case ".go", ".js", ".ts", ".py", ".rs", ".c", ".h", ".rb", ".sh":
218 return "code"
219 case ".yml", ".yaml", ".toml", ".json", ".lock":
220 return "cfg"
221 }
222 base := strings.ToLower(name)
223 if base == "license" || base == "licence" || base == "copying" || strings.HasPrefix(base, "license.") {
224 return "lic"
225 }
226 if strings.HasPrefix(base, "readme") {
227 return "md"
228 }
229 return "file"
230}
231
232func formatSize(n int64) string {
233 if n < 1024 {
234 return fmt.Sprintf("%d B", n)
235 }
236 if n < 1024*1024 {
237 return fmt.Sprintf("%.1f KiB", float64(n)/1024)
238 }
239 return fmt.Sprintf("%.1f MiB", float64(n)/(1024*1024))
240}