| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "os" |
| 7 | "strings" |
| 8 | "syscall" |
| 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/state" |
| 14 | "gitlab.com/Timfa/bluegit/internal/store" |
| 15 | ) |
| 16 | |
| 17 | // forkReserveBytes: zoveel moet er naast de upstream-grootte vrij blijven. |
| 18 | const forkReserveBytes = 512 << 20 |
| 19 | |
| 20 | // canReadRepo: openbaar, of de gebruiker heeft minstens leesrechten. |
| 21 | func (s *Server) canReadRepo(ctx context.Context, userID int64, repo store.Repo) bool { |
| 22 | if repo.Visibility == "public" { |
| 23 | return true |
| 24 | } |
| 25 | if userID == 0 { |
| 26 | return false |
| 27 | } |
| 28 | role, err := s.Store.EffectiveRole(ctx, userID, repo.ID) |
| 29 | return err == nil && role.CanRead() |
| 30 | } |
| 31 | |
| 32 | // ForkInfo is wat de repo-pagina over de fork-relatie toont. |
| 33 | type ForkInfo struct { |
| 34 | IsFork bool |
| 35 | Upstream *store.Repo // nil als de kijker upstream niet mag zien |
| 36 | Forks int // zichtbare forks van deze repo |
| 37 | } |
| 38 | |
| 39 | func (s *Server) forkInfo(ctx context.Context, userID int64, repo store.Repo) ForkInfo { |
| 40 | var fi ForkInfo |
| 41 | if up, _ := s.Store.ForkOf(ctx, repo.ID); up != 0 { |
| 42 | fi.IsFork = true |
| 43 | if u, err := s.Store.RepoByID(ctx, up); err == nil && s.canReadRepo(ctx, userID, u) { |
| 44 | fi.Upstream = &u |
| 45 | } |
| 46 | } |
| 47 | fi.Forks = len(s.visibleForks(ctx, userID, repo)) |
| 48 | return fi |
| 49 | } |
| 50 | |
| 51 | func (s *Server) visibleForks(ctx context.Context, userID int64, repo store.Repo) []store.Repo { |
| 52 | ids, _ := s.Store.ForkIDs(ctx, repo.ID) |
| 53 | if len(ids) == 0 { |
| 54 | return nil |
| 55 | } |
| 56 | list, _ := s.Store.ReposByIDs(ctx, ids) |
| 57 | var out []store.Repo |
| 58 | for _, f := range list { |
| 59 | if s.canReadRepo(ctx, userID, f) { |
| 60 | out = append(out, f) |
| 61 | } |
| 62 | } |
| 63 | return out |
| 64 | } |
| 65 | |
| 66 | // ForkTarget is een plek waar de gebruiker een fork mag maken. |
| 67 | type ForkTarget struct { |
| 68 | Value string // "" = eigen account, anders groepspad |
| 69 | Label string |
| 70 | } |
| 71 | |
| 72 | func (s *Server) forkTargets(ctx context.Context, user store.User) []ForkTarget { |
| 73 | out := []ForkTarget{{Value: "", Label: user.Username}} |
| 74 | groups, _ := s.Store.ListGroups(ctx, user.ID) |
| 75 | for _, g := range groups { |
| 76 | if role, err := s.Store.GroupRole(ctx, g.ID, user.ID); err == nil && role.CanWriteCode() { |
| 77 | out = append(out, ForkTarget{Value: g.Path, Label: g.Path}) |
| 78 | } |
| 79 | } |
| 80 | return out |
| 81 | } |
| 82 | |
| 83 | // opForkRepo maakt een fork met alle branches en tags. Doel: eigen account |
| 84 | // of een groep waar de gebruiker minstens developer is. Een fork van een |
| 85 | // privé repo is privé. |
| 86 | func (s *Server) opForkRepo(ctx context.Context, user store.User, upstream store.Repo, groupPath, rawName string) (store.Repo, error) { |
| 87 | if s.StoragePath() == "" { |
| 88 | return store.Repo{}, fail(503, i18n.E("err.noStorage")) |
| 89 | } |
| 90 | if !s.canReadRepo(ctx, user.ID, upstream) { |
| 91 | return store.Repo{}, fail(404, i18n.E("err.notFound")) |
| 92 | } |
| 93 | if strings.TrimSpace(rawName) == "" { |
| 94 | rawName = upstream.Name |
| 95 | } |
| 96 | name, err := slugKind("repo", rawName) |
| 97 | if err != nil { |
| 98 | return store.Repo{}, fail(400, err) |
| 99 | } |
| 100 | ownerKind, ownerID, ownerPath := "user", user.ID, user.Username |
| 101 | if gp := strings.TrimSpace(groupPath); gp != "" { |
| 102 | g, err := s.Store.GroupByPath(ctx, gp) |
| 103 | if err != nil { |
| 104 | return store.Repo{}, fail(404, i18n.E("err.groupMissing")) |
| 105 | } |
| 106 | role, err := s.Store.GroupRole(ctx, g.ID, user.ID) |
| 107 | if err != nil || !role.AtLeast(access.RoleDeveloper) { |
| 108 | return store.Repo{}, fail(403, i18n.E("err.forbidden")) |
| 109 | } |
| 110 | ownerKind, ownerID, ownerPath = "group", g.ID, g.Path |
| 111 | } |
| 112 | if _, err := s.Store.RepoByPath(ctx, ownerPath, name); err == nil { |
| 113 | return store.Repo{}, fail(409, i18n.E("err.forkExists", ownerPath+"/"+name)) |
| 114 | } |
| 115 | src := gitrepo.Dir(s.StoragePath(), upstream.OwnerPath, upstream.Name) |
| 116 | if size, err := state.DirSize(ctx, src); err == nil { |
| 117 | var st syscall.Statfs_t |
| 118 | if syscall.Statfs(s.StoragePath(), &st) == nil && st.Bavail*uint64(st.Bsize) < size+forkReserveBytes { |
| 119 | return store.Repo{}, fail(507, i18n.E("err.forkDisk")) |
| 120 | } |
| 121 | } |
| 122 | repo, err := s.Store.CreateRepo(ctx, ownerKind, ownerID, name, upstream.Description, upstream.DefaultBranch, upstream.Visibility) |
| 123 | if err != nil { |
| 124 | return store.Repo{}, fail(400, err) |
| 125 | } |
| 126 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 127 | if err := gitrepo.Fork(s.Git, src, dir, s.HookBin); err != nil { |
| 128 | _ = s.Store.DeleteRepo(ctx, repo.ID) |
| 129 | _ = os.RemoveAll(dir) |
| 130 | return store.Repo{}, fail(500, i18n.E("err.forkGit", err.Error())) |
| 131 | } |
| 132 | if err := s.Store.SetForkOf(ctx, repo.ID, upstream.ID); err != nil { |
| 133 | return store.Repo{}, fail(500, err) |
| 134 | } |
| 135 | s.syncRepos(ctx, []int64{repo.ID}) |
| 136 | return repo, nil |
| 137 | } |
| 138 | |
| 139 | // forkMustStayPrivate: een fork van een privé upstream mag niet openbaar. |
| 140 | func (s *Server) forkMustStayPrivate(ctx context.Context, repo store.Repo) bool { |
| 141 | up, _ := s.Store.ForkOf(ctx, repo.ID) |
| 142 | if up == 0 { |
| 143 | return false |
| 144 | } |
| 145 | u, err := s.Store.RepoByID(ctx, up) |
| 146 | return err == nil && u.Visibility != "public" |
| 147 | } |
| 148 | |
| 149 | func (s *Server) forkPost(w http.ResponseWriter, r *http.Request) { |
| 150 | u, repo, _, ok := s.guard(w, r, access.RoleReadonly) |
| 151 | if !ok || !s.csrfOK(w, r) { |
| 152 | return |
| 153 | } |
| 154 | if u.ID == 0 { |
| 155 | http.Redirect(w, r, "/login", http.StatusSeeOther) |
| 156 | return |
| 157 | } |
| 158 | fork, err := s.opForkRepo(r.Context(), u, repo, r.FormValue("namespace"), r.FormValue("name")) |
| 159 | if !s.uiOK(w, r, repoURL(repo), err) { |
| 160 | return |
| 161 | } |
| 162 | s.redirect(w, r, repoURL(fork), s.tr(r, "flash.forked"), "") |
| 163 | } |
| 164 | |
| 165 | func (s *Server) forksPage(w http.ResponseWriter, r *http.Request) { |
| 166 | u, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 167 | if !ok { |
| 168 | return |
| 169 | } |
| 170 | v := s.repoView(w, r, repo, role, s.tr(r, "fork.listTitle")) |
| 171 | v.ForkList = s.visibleForks(r.Context(), u.ID, repo) |
| 172 | v.Fork = s.forkInfo(r.Context(), u.ID, repo) |
| 173 | s.render(w, "forks", v) |
| 174 | } |
| 175 | |
| 176 | func forkJSON(fork store.Repo, upstream store.Repo) map[string]any { |
| 177 | out := projectJSON(fork) |
| 178 | out["forked_from"] = upstream.FullPath() |
| 179 | return out |
| 180 | } |
| 181 | |
| 182 | func (s *Server) apiForkProject(w http.ResponseWriter, r *http.Request) { |
| 183 | repo, _, ok := s.apiRepo(w, r, access.RoleReadonly) |
| 184 | if !ok { |
| 185 | return |
| 186 | } |
| 187 | var body struct { |
| 188 | Namespace string `json:"namespace"` |
| 189 | Name string `json:"name"` |
| 190 | } |
| 191 | if err := readJSON(r, &body); err != nil { |
| 192 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 193 | return |
| 194 | } |
| 195 | fork, err := s.opForkRepo(r.Context(), apiUser(r), repo, body.Namespace, body.Name) |
| 196 | if !s.apiOK(w, r, err) { |
| 197 | return |
| 198 | } |
| 199 | writeJSON(w, http.StatusCreated, forkJSON(fork, repo)) |
| 200 | } |
| 201 | |
| 202 | func (s *Server) apiListForks(w http.ResponseWriter, r *http.Request) { |
| 203 | repo, _, ok := s.apiRepo(w, r, access.RoleReadonly) |
| 204 | if !ok { |
| 205 | return |
| 206 | } |
| 207 | s.writeProjects(w, r, s.visibleForks(r.Context(), apiUser(r).ID, repo)) |
| 208 | } |