BlueGit

timfa bluegit

Files

bluegit / internal / server / api_git.go

api_git.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
api_git.go 8.6 KiB Download Raw
1package server
2
3import (
4 "net/http"
5 "os/exec"
6 "strings"
7
8 "gitlab.com/Timfa/bluegit/internal/access"
9 "gitlab.com/Timfa/bluegit/internal/gitrepo"
10 "gitlab.com/Timfa/bluegit/internal/i18n"
11)
12
13func (s *Server) apiBranches(w http.ResponseWriter, r *http.Request) {
14 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
15 if !ok {
16 return
17 }
18 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
19 if r.Method == http.MethodGet {
20 tips, err := gitrepo.BranchTips(r.Context(), s.Git, dir)
21 if err != nil {
22 tips = nil
23 }
24 page, per := pageQuery(r)
25 rows := make([]map[string]any, 0, len(tips))
26 for _, t := range tips {
27 rows = append(rows, map[string]any{
28 "name": t.Name, "commit": t.SHA, "subject": t.Subject, "date": t.Date,
29 "default": t.Name == repo.DefaultBranch,
30 })
31 }
32 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
33 return
34 }
35 var body struct {
36 Name string `json:"name"`
37 From string `json:"from"`
38 }
39 if err := readJSON(r, &body); err != nil {
40 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
41 return
42 }
43 if !s.apiOK(w, r, s.opCreateBranch(r.Context(), apiUser(r), repo, body.Name, body.From)) {
44 return
45 }
46 writeJSON(w, http.StatusCreated, map[string]any{"name": body.Name})
47}
48
49func (s *Server) apiBranch(w http.ResponseWriter, r *http.Request) {
50 repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper)
51 if !ok {
52 return
53 }
54 name := r.PathValue("branch")
55 if r.Method == http.MethodDelete {
56 var body struct {
57 Confirm string `json:"confirm"`
58 }
59 if err := readJSON(r, &body); err != nil {
60 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
61 return
62 }
63 if !s.apiOK(w, r, s.opDeleteBranch(r.Context(), apiUser(r), repo, name, body.Confirm)) {
64 return
65 }
66 writeJSON(w, http.StatusNoContent, nil)
67 return
68 }
69 var body struct {
70 Name string `json:"name"`
71 Confirm string `json:"confirm"`
72 }
73 if err := readJSON(r, &body); err != nil {
74 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
75 return
76 }
77 if !s.apiOK(w, r, s.opRenameBranch(r.Context(), apiUser(r), repo, name, body.Name, body.Confirm)) {
78 return
79 }
80 writeJSON(w, http.StatusOK, map[string]any{"name": body.Name})
81}
82
83func (s *Server) apiTags(w http.ResponseWriter, r *http.Request) {
84 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
85 if !ok {
86 return
87 }
88 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
89 if r.Method == http.MethodGet {
90 names, err := gitrepo.Tags(r.Context(), s.Git, dir)
91 if err != nil {
92 names = nil
93 }
94 if names == nil {
95 names = []string{}
96 }
97 page, per := pageQuery(r)
98 writePage(w, r, slicePage(names, page, per), len(names), page, per)
99 return
100 }
101 var in struct {
102 Name string `json:"name"`
103 Target string `json:"target"`
104 Message string `json:"message"`
105 }
106 if err := readJSON(r, &in); err != nil {
107 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
108 return
109 }
110 if !s.apiOK(w, r, s.opCreateTag(r.Context(), apiUser(r), repo, in.Name, in.Target, in.Message)) {
111 return
112 }
113 writeJSON(w, http.StatusCreated, map[string]any{"name": in.Name})
114}
115
116func (s *Server) apiDeleteTag(w http.ResponseWriter, r *http.Request) {
117 repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper)
118 if !ok {
119 return
120 }
121 if !s.apiOK(w, r, s.opDeleteTag(r.Context(), apiUser(r), repo, r.PathValue("tag"))) {
122 return
123 }
124 writeJSON(w, http.StatusNoContent, nil)
125}
126
127func (s *Server) apiTagArchive(w http.ResponseWriter, r *http.Request) {
128 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
129 if !ok {
130 return
131 }
132 name := r.PathValue("tag")
133 format := r.URL.Query().Get("format")
134 if format == "" {
135 format = "tar.gz"
136 }
137 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
138 if !gitrepo.HasTag(r.Context(), s.Git, dir, name) {
139 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
140 return
141 }
142 filename := archiveFilename(repo.Name, name, format)
143 if format == "zip" {
144 w.Header().Set("Content-Type", "application/zip")
145 } else {
146 w.Header().Set("Content-Type", "application/gzip")
147 }
148 w.Header().Set("Content-Disposition", "attachment; filename=\""+filename+"\"")
149 if err := gitrepo.Archive(r.Context(), w, s.Git, dir, repo.Name+"-"+strings.ReplaceAll(name, "/", "-"), name, format); err != nil {
150 s.writeAPIErr(w, r, fail(400, err))
151 }
152}
153
154func (s *Server) apiTree(w http.ResponseWriter, r *http.Request) {
155 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
156 if !ok {
157 return
158 }
159 ref := safeRev(r.URL.Query().Get("ref"), repo.DefaultBranch)
160 rel := strings.Trim(r.URL.Query().Get("path"), "/")
161 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
162 entries, err := gitrepo.Tree(r.Context(), s.Git, dir, ref, rel)
163 if err != nil {
164 writeJSON(w, http.StatusOK, []any{})
165 return
166 }
167 rows := make([]map[string]any, 0, len(entries))
168 for _, e := range entries {
169 rows = append(rows, map[string]any{"name": e.Name, "type": e.Type, "mode": e.Mode, "id": e.SHA})
170 }
171 page, per := pageQuery(r)
172 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
173}
174
175func (s *Server) apiFile(w http.ResponseWriter, r *http.Request) {
176 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
177 if !ok {
178 return
179 }
180 path, pathOK := repoPath(r.PathValue("path"))
181 if !pathOK || path == "" {
182 s.writeAPIErr(w, r, fail(400, i18n.E("err.sugFile")))
183 return
184 }
185 ref := safeRev(r.URL.Query().Get("ref"), repo.DefaultBranch)
186 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
187 out, err := exec.CommandContext(r.Context(), s.Git, "-C", dir, "show", ref+":"+path).Output()
188 if err != nil {
189 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
190 return
191 }
192 if strings.Contains(string(out), "\x00") {
193 w.Header().Set("Content-Type", "application/octet-stream")
194 } else {
195 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
196 }
197 w.WriteHeader(http.StatusOK)
198 _, _ = w.Write(out)
199}
200
201func (s *Server) apiCommits(w http.ResponseWriter, r *http.Request) {
202 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
203 if !ok {
204 return
205 }
206 if r.Method == http.MethodPost {
207 var body struct {
208 Branch string `json:"branch"`
209 StartBranch string `json:"start_branch"`
210 CommitMessage string `json:"commit_message"`
211 Actions []fileAction `json:"actions"`
212 }
213 if err := readJSON(r, &body); err != nil {
214 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
215 return
216 }
217 sha, err := s.opCommitActions(r.Context(), apiUser(r), repo, body.Branch, body.StartBranch, body.CommitMessage, "", body.Actions)
218 if !s.apiOK(w, r, err) {
219 return
220 }
221 writeJSON(w, http.StatusCreated, map[string]any{"id": sha, "branch": body.Branch})
222 return
223 }
224 ref := safeRev(r.URL.Query().Get("ref"), repo.DefaultBranch)
225 page, per := pageQuery(r)
226 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
227 limit := page * per
228 if limit > 200 {
229 limit = 200
230 }
231 list, err := gitrepo.Log(r.Context(), s.Git, dir, ref, limit)
232 if err != nil || list == nil {
233 list = nil
234 }
235 rows := make([]map[string]any, 0, len(list))
236 for _, c := range list {
237 rows = append(rows, commitJSON(c))
238 }
239 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
240}
241
242func commitJSON(c gitrepo.Commit) map[string]any {
243 return map[string]any{
244 "id": c.SHA, "short_id": c.Short, "title": c.Subject, "message": c.Body,
245 "author_name": c.Author, "author_email": c.Email, "authored_date": c.Date,
246 }
247}
248
249func (s *Server) apiCommit(w http.ResponseWriter, r *http.Request) {
250 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
251 if !ok {
252 return
253 }
254 sha := r.PathValue("sha")
255 if !safeSHA(sha) {
256 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
257 return
258 }
259 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
260 c, err := gitrepo.Show(r.Context(), s.Git, dir, sha)
261 if err != nil {
262 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
263 return
264 }
265 writeJSON(w, http.StatusOK, commitJSON(c))
266}
267
268func (s *Server) apiCommitDiff(w http.ResponseWriter, r *http.Request) {
269 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
270 if !ok {
271 return
272 }
273 sha := r.PathValue("sha")
274 if !safeSHA(sha) {
275 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
276 return
277 }
278 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
279 parents, files, err := gitrepo.CommitPatch(r.Context(), s.Git, dir, sha)
280 if err != nil {
281 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
282 return
283 }
284 out := make([]map[string]any, 0, len(files))
285 for _, f := range files {
286 var b strings.Builder
287 for _, ln := range f.Lines {
288 b.WriteString(ln.Kind)
289 b.WriteString(ln.Text)
290 b.WriteByte('\n')
291 }
292 out = append(out, map[string]any{
293 "status": f.Status, "path": f.Path, "old_path": f.OldPath,
294 "added": f.Added, "deleted": f.Deleted, "binary": f.Binary, "diff": b.String(),
295 })
296 }
297 if parents == nil {
298 parents = []string{}
299 }
300 writeJSON(w, http.StatusOK, map[string]any{"id": sha, "parents": parents, "files": out})
301}