BlueGit

timfa bluegit

Files

bluegit / internal / server / setup.go

setup.go

feat: stuur volledige details mee in webhook-payloads
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
3ca4d219 History
setup.go 4.4 KiB Download Raw
1package server
2
3import (
4 "context"
5 "net/http"
6 "os"
7 "strings"
8
9 "gitlab.com/Timfa/bluegit/internal/gitrepo"
10 "gitlab.com/Timfa/bluegit/internal/state"
11)
12
13func (s *Server) noStorageText(r *http.Request) string {
14 return s.tr(r, "err.noStorage") + "\n"
15}
16
17func allowsWithoutStorage(r *http.Request) bool {
18 switch r.URL.Path {
19 case "/healthz", "/login", "/auth/callback", "/logout", "/setup", "/-/info", "/api/v1/ci/spec", "/api/v1/ci/presets", "/api/v1/openapi.json", "/api/v1/webhooks/openapi.json", "/api/v1/admin/storage", "/api/v1/admin/self-update":
20 return true
21 }
22 return strings.HasPrefix(r.URL.Path, "/assets/")
23}
24
25func isGitPath(path string) bool {
26 return strings.HasSuffix(path, "/info/refs") ||
27 strings.HasSuffix(path, "/git-upload-pack") ||
28 strings.HasSuffix(path, "/git-receive-pack")
29}
30
31func (s *Server) gate(next http.Handler) http.Handler {
32 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
33 if allowsWithoutStorage(r) || s.StoragePath() != "" {
34 next.ServeHTTP(w, r)
35 return
36 }
37 if isGitPath(r.URL.Path) || strings.HasPrefix(r.URL.Path, "/api/") {
38 http.Error(w, s.noStorageText(r), http.StatusServiceUnavailable)
39 return
40 }
41 if p := s.who(r); p.OK && p.User.InstanceAdmin {
42 http.Redirect(w, r, "/setup", http.StatusFound)
43 return
44 }
45 next.ServeHTTP(w, r)
46 })
47}
48
49// StoragePath is de datadisk. Leeg zolang de beheerder er geen heeft gekozen.
50func (s *Server) StoragePath() string {
51 s.storageMu.RLock()
52 set, path := s.storageSet, s.storage
53 s.storageMu.RUnlock()
54 if set {
55 return path
56 }
57 s.storageMu.Lock()
58 defer s.storageMu.Unlock()
59 if !s.storageSet {
60 s.storage = s.Cfg.StoragePath
61 s.storageSet = true
62 }
63 return s.storage
64}
65
66// AdoptStorage zet het pad dat bij het starten al geldig was.
67func (s *Server) AdoptStorage(path string) {
68 s.storageMu.Lock()
69 s.storage = path
70 s.storageSet = true
71 s.storageMu.Unlock()
72 if path != "" && s.AfterStorage != nil {
73 s.AfterStorage(path)
74 }
75}
76
77func (s *Server) setupPage(w http.ResponseWriter, r *http.Request) {
78 if _, ok := s.adminOnly(w, r); !ok {
79 return
80 }
81 s.renderSetup(w, r, "", "")
82}
83
84func (s *Server) setupSave(w http.ResponseWriter, r *http.Request) {
85 if _, ok := s.adminOnly(w, r); !ok || !s.csrfOK(w, r) {
86 return
87 }
88 info, moved, err := s.opSetStorage(r.FormValue("path"))
89 if err != nil {
90 s.renderSetup(w, r, s.trErr(r, err), strings.TrimSpace(r.FormValue("path")))
91 return
92 }
93 notice := s.tr(r, "err.storageSet", info.Path, state.Format(info.Free))
94 if moved {
95 notice = s.tr(r, "err.storageMoved", info.Path, state.Format(info.Free))
96 }
97 s.redirect(w, r, "/", notice, "")
98}
99
100func samePath(stateDir, path string) (bool, error) {
101 if stateDir == "" {
102 return false, nil
103 }
104 return strings.TrimRight(stateDir, "/") == strings.TrimRight(path, "/"), nil
105}
106
107func (s *Server) adminOnly(w http.ResponseWriter, r *http.Request) (bool, bool) {
108 u, ok := s.loginRequired(w, r)
109 if !ok {
110 return false, false
111 }
112 if !u.InstanceAdmin {
113 http.Error(w, s.tr(r, "err.noStorageAdmin"), http.StatusForbidden)
114 return false, false
115 }
116 return true, true
117}
118
119func (s *Server) renderSetup(w http.ResponseWriter, r *http.Request, problem, prefill string) {
120 v := s.view(w, r, "title.storage")
121 v.Page = "setup"
122 if problem != "" {
123 v.Err = problem
124 }
125 v.MinFree = state.Format(state.MinFreeBytes)
126 v.StoragePath = strings.TrimSpace(prefill)
127 if v.StoragePath == "" {
128 v.StoragePath = s.StoragePath()
129 }
130 if v.StoragePath == "" && s.StateDir != "" {
131 if saved, err := state.Read(s.StateDir); err == nil {
132 v.StoragePath = saved
133 }
134 }
135 if v.StoragePath != "" {
136 info, err := state.Inspect(v.StoragePath)
137 if info.Total > 0 {
138 v.StorageFree = state.Format(info.Free)
139 v.StorageTotal = state.Format(info.Total)
140 }
141 if err != nil && v.Err == "" {
142 v.Err = s.trErr(r, err)
143 }
144 }
145 s.attachUpdate(&v, s.langOf(r))
146 s.render(w, "setup", v)
147}
148
149// RefreshHooks zet de pre-receive-hook opnieuw in elke repository die op de datadisk staat.
150func (s *Server) RefreshHooks() {
151 root := s.StoragePath()
152 if root == "" || s.HookBin == "" || s.Store == nil {
153 return
154 }
155 repos, err := s.Store.AllRepos(context.Background())
156 if err != nil {
157 return
158 }
159 for _, repo := range repos {
160 dir := gitrepo.Dir(root, repo.OwnerPath, repo.Name)
161 if _, err := os.Stat(dir); err != nil {
162 continue
163 }
164 if err := gitrepo.InstallHook(dir, s.HookBin); err != nil && s.Log != nil {
165 s.Log.Error("hook", "repo", repo.FullPath(), "err", err)
166 }
167 }
168}