Monorepo for Tangled
tangled.org
1package pulls
2
3import (
4 "fmt"
5 "net/http"
6 "slices"
7 "strconv"
8
9 "tangled.org/core/api/tangled"
10 "tangled.org/core/appview/db"
11 "tangled.org/core/appview/models"
12 "tangled.org/core/appview/pages"
13 "tangled.org/core/appview/xrpcclient"
14 "tangled.org/core/orm"
15 "tangled.org/core/patchutil"
16 "tangled.org/core/types"
17
18 "github.com/bluesky-social/indigo/atproto/syntax"
19 indigoxrpc "github.com/bluesky-social/indigo/xrpc"
20 "github.com/go-chi/chi/v5"
21)
22
23// htmx fragment
24func (s *Pulls) PullActions(w http.ResponseWriter, r *http.Request) {
25 l := s.logger.With("handler", "PullActions")
26
27 switch r.Method {
28 case http.MethodGet:
29 user := s.oauth.GetMultiAccountUser(r)
30 if user != nil {
31 l = l.With("user", user.Did)
32 }
33
34 f, err := s.repoResolver.Resolve(r)
35 if err != nil {
36 l.Error("failed to get repo and knot", "err", err)
37 return
38 }
39
40 pull, ok := r.Context().Value("pull").(*models.Pull)
41 if !ok {
42 l.Error("failed to get pull")
43 s.pages.Notice(w, "pull-error", "Failed to edit patch. Try again later.")
44 return
45 }
46 l = l.With("pull_id", pull.PullId, "pull_owner", pull.OwnerDid)
47
48 // can be nil if this pull is not stacked
49 stack, _ := r.Context().Value("stack").(models.Stack)
50
51 roundNumberStr := chi.URLParam(r, "round")
52 roundNumber, err := strconv.Atoi(roundNumberStr)
53 if err != nil {
54 roundNumber = pull.LastRoundNumber()
55 }
56 if roundNumber >= len(pull.Submissions) {
57 http.Error(w, "bad round id", http.StatusBadRequest)
58 l.Error("failed to parse round id", "err", err, "round_number", roundNumber)
59 return
60 }
61
62 mergeCheckResponse := s.mergeCheck(r, f, pull, stack)
63 branchDeleteStatus := s.branchDeleteStatus(r, f, pull)
64 resubmitResult := pages.Unknown
65 if user.Did == pull.OwnerDid {
66 resubmitResult = s.resubmitCheck(r, f, pull, stack)
67 }
68
69 s.pages.PullActionsFragment(w, pages.PullActionsParams{
70 LoggedInUser: user,
71 RepoInfo: s.repoResolver.GetRepoInfo(r, user),
72 Pull: pull,
73 RoundNumber: roundNumber,
74 MergeCheck: mergeCheckResponse,
75 ResubmitCheck: resubmitResult,
76 BranchDeleteStatus: branchDeleteStatus,
77 Stack: stack,
78 })
79 return
80 }
81}
82
83func (s *Pulls) repoPullHelper(w http.ResponseWriter, r *http.Request, interdiff bool) {
84 l := s.logger.With("handler", "repoPullHelper", "interdiff", interdiff)
85
86 user := s.oauth.GetMultiAccountUser(r)
87 if user != nil {
88 l = l.With("user", user.Did)
89 }
90
91 f, err := s.repoResolver.Resolve(r)
92 if err != nil {
93 l.Error("failed to get repo and knot", "err", err)
94 return
95 }
96
97 pull, ok := r.Context().Value("pull").(*models.Pull)
98 if !ok {
99 l.Error("failed to get pull")
100 s.pages.Notice(w, "pull-error", "Failed to edit patch. Try again later.")
101 return
102 }
103 l = l.With("pull_id", pull.PullId, "pull_owner", pull.OwnerDid)
104
105 backlinks, err := db.GetBacklinks(s.db, pull.AtUri())
106 if err != nil {
107 l.Error("failed to get pull backlinks", "err", err)
108 s.pages.Notice(w, "pull-error", "Failed to get pull. Try again later.")
109 return
110 }
111
112 roundId := chi.URLParam(r, "round")
113 roundIdInt := pull.LastRoundNumber()
114 if r, err := strconv.Atoi(roundId); err == nil {
115 roundIdInt = r
116 }
117 if roundIdInt >= len(pull.Submissions) {
118 http.Error(w, "bad round id", http.StatusBadRequest)
119 l.Error("failed to parse round id", "err", err, "round_number", roundIdInt)
120 return
121 }
122
123 var diffOpts types.DiffOpts
124 if d := r.URL.Query().Get("diff"); d == "split" {
125 diffOpts.Split = true
126 }
127
128 // can be nil if this pull is not stacked
129 stack, _ := r.Context().Value("stack").(models.Stack)
130
131 mergeCheckResponse := s.mergeCheck(r, f, pull, stack)
132 branchDeleteStatus := s.branchDeleteStatus(r, f, pull)
133 resubmitResult := pages.Unknown
134 if user != nil && user.Did == pull.OwnerDid {
135 resubmitResult = s.resubmitCheck(r, f, pull, stack)
136 }
137
138 m := make(map[string]models.Pipeline)
139
140 var shas []string
141 for _, s := range pull.Submissions {
142 shas = append(shas, s.SourceRev)
143 }
144 for _, p := range stack {
145 shas = append(shas, p.LatestSha())
146 }
147
148 ps, err := db.GetPipelineStatuses(
149 s.db,
150 len(shas),
151 orm.FilterEq("p.repo_owner", f.Did),
152 orm.FilterEq("p.repo_name", f.Name),
153 orm.FilterEq("p.knot", f.Knot),
154 orm.FilterIn("p.sha", shas),
155 )
156 if err != nil {
157 l.Error("failed to fetch pipeline statuses", "err", err)
158 // non-fatal
159 }
160
161 for _, p := range ps {
162 m[p.Sha] = p
163 }
164
165 reactionMap, err := db.GetReactionMap(s.db, 20, pull.AtUri())
166 if err != nil {
167 l.Error("failed to get pull reactions", "err", err)
168 }
169
170 userReactions := map[models.ReactionKind]bool{}
171 if user != nil {
172 userReactions = db.GetReactionStatusMap(s.db, user.Did, pull.AtUri())
173 }
174
175 labelDefs, err := db.GetLabelDefinitions(
176 s.db,
177 orm.FilterIn("at_uri", f.Labels),
178 orm.FilterContains("scope", tangled.RepoPullNSID),
179 )
180 if err != nil {
181 l.Error("failed to fetch labels", "err", err)
182 s.pages.Error503(w)
183 return
184 }
185
186 defs := make(map[string]*models.LabelDefinition)
187 for _, l := range labelDefs {
188 defs[l.AtUri().String()] = &l
189 }
190
191 vouchRelationships := make(map[syntax.DID]*models.VouchRelationship)
192 if user != nil {
193 participants := pull.Participants()
194 vouchRelationships, err = db.GetVouchRelationshipsBatch(s.db, syntax.DID(user.Did), participants)
195 if err != nil {
196 l.Error("failed to fetch vouch relationships", "err", err)
197 }
198 }
199
200 patch := pull.Submissions[roundIdInt].CombinedPatch()
201 var diff types.DiffRenderer
202 diff = patchutil.AsNiceDiff(patch, pull.TargetBranch)
203
204 if interdiff {
205 currentPatch, err := patchutil.AsDiff(pull.Submissions[roundIdInt].CombinedPatch())
206 if err != nil {
207 l.Error("failed to interdiff; current patch malformed", "err", err, "round_number", roundIdInt)
208 s.pages.Notice(w, fmt.Sprintf("interdiff-error-%d", roundIdInt), "Failed to calculate interdiff; current patch is invalid.")
209 return
210 }
211
212 previousPatch, err := patchutil.AsDiff(pull.Submissions[roundIdInt-1].CombinedPatch())
213 if err != nil {
214 l.Error("failed to interdiff; previous patch malformed", "err", err, "round_number", roundIdInt)
215 s.pages.Notice(w, fmt.Sprintf("interdiff-error-%d", roundIdInt), "Failed to calculate interdiff; previous patch is invalid.")
216 return
217 }
218
219 diff = patchutil.Interdiff(previousPatch, currentPatch)
220 }
221
222 err = s.pages.RepoSinglePull(w, pages.RepoSinglePullParams{
223 LoggedInUser: user,
224 RepoInfo: s.repoResolver.GetRepoInfo(r, user),
225 Pull: pull,
226 Stack: stack,
227 Backlinks: backlinks,
228 BranchDeleteStatus: branchDeleteStatus,
229 MergeCheck: mergeCheckResponse,
230 ResubmitCheck: resubmitResult,
231 Pipelines: m,
232 Diff: diff,
233 DiffOpts: diffOpts,
234 ActiveRound: roundIdInt,
235 IsInterdiff: interdiff,
236
237 Reactions: reactionMap,
238 UserReacted: userReactions,
239
240 LabelDefs: defs,
241 VouchRelationships: vouchRelationships,
242 })
243 if err != nil {
244 l.Error("failed to render page", "err", err)
245 }
246}
247
248func (s *Pulls) RepoSinglePull(w http.ResponseWriter, r *http.Request) {
249 l := s.logger.With("handler", "RepoSinglePull")
250
251 pull, ok := r.Context().Value("pull").(*models.Pull)
252 if !ok {
253 l.Error("failed to get pull")
254 s.pages.Notice(w, "pull-error", "Failed to edit patch. Try again later.")
255 return
256 }
257
258 http.Redirect(w, r, r.URL.String()+fmt.Sprintf("/round/%d", pull.LastRoundNumber()), http.StatusFound)
259}
260
261func (s *Pulls) mergeCheck(r *http.Request, f *models.Repo, pull *models.Pull, stack models.Stack) types.MergeCheckResponse {
262 if pull.State == models.PullMerged {
263 return types.MergeCheckResponse{}
264 }
265
266 xrpcc := s.knotClient(f.Knot)
267
268 // combine patches of substack
269 subStack := stack.Below(pull)
270 // collect the portion of the stack that is mergeable
271 mergeable := subStack.Mergeable()
272 // combine each patch
273 patch := mergeable.CombinedPatch()
274
275 resp, err := tangled.RepoMergeCheck(
276 r.Context(),
277 xrpcc,
278 &tangled.RepoMergeCheck_Input{
279 Did: f.Did,
280 Name: f.Name,
281 Branch: pull.TargetBranch,
282 Patch: patch,
283 },
284 )
285 if xrpcerr := xrpcclient.HandleXrpcErr(err); xrpcerr != nil {
286 s.logger.Error("failed to check for mergeability", "xrpcerr", xrpcerr, "err", err, "pull_id", pull.PullId, "target_branch", pull.TargetBranch)
287 return types.MergeCheckResponse{
288 Error: fmt.Sprintf("failed to check merge status: %s", xrpcerr.Error()),
289 }
290 }
291
292 return mergeCheckResponseFrom(resp)
293}
294
295func mergeCheckResponseFrom(resp *tangled.RepoMergeCheck_Output) types.MergeCheckResponse {
296 conflicts := make([]types.ConflictInfo, len(resp.Conflicts))
297 for i, c := range resp.Conflicts {
298 conflicts[i] = types.ConflictInfo{Filename: c.Filename, Reason: c.Reason}
299 }
300 out := types.MergeCheckResponse{
301 IsConflicted: resp.Is_conflicted,
302 Conflicts: conflicts,
303 }
304 if resp.Message != nil {
305 out.Message = *resp.Message
306 }
307 if resp.Error != nil {
308 out.Error = *resp.Error
309 }
310 return out
311}
312
313func (s *Pulls) branchDeleteStatus(r *http.Request, repo *models.Repo, pull *models.Pull) *models.BranchDeleteStatus {
314 if pull.State != models.PullMerged {
315 return nil
316 }
317
318 user := s.oauth.GetMultiAccountUser(r)
319 if user == nil {
320 return nil
321 }
322
323 var branch string
324 // check if the branch exists
325 // NOTE: appview could cache branches/tags etc. for every repo by listening for gitRefUpdates
326 if pull.IsBranchBased() {
327 branch = pull.PullSource.Branch
328 } else if pull.IsForkBased() {
329 branch = pull.PullSource.Branch
330 repo = pull.PullSource.Repo
331 } else {
332 return nil
333 }
334
335 // deleted fork
336 if repo == nil {
337 return nil
338 }
339
340 // user can only delete branch if they are a collaborator in the repo that the branch belongs to
341 perms := s.enforcer.GetPermissionsInRepo(user.Did, repo.Knot, repo.RepoIdentifier())
342 if !slices.Contains(perms, "repo:push") {
343 return nil
344 }
345
346 xrpcc := &indigoxrpc.Client{Host: s.config.KnotMirror.Url}
347 resp, err := tangled.GitTempGetBranch(r.Context(), xrpcc, branch, repo.RepoAt().String())
348 if xrpcerr := xrpcclient.HandleXrpcErr(err); xrpcerr != nil {
349 s.logger.Error("failed to get branch", "xrpcerr", xrpcerr, "err", err)
350 return nil
351 }
352
353 return &models.BranchDeleteStatus{
354 Repo: repo,
355 Branch: resp.Name,
356 }
357}
358
359func (s *Pulls) resubmitCheck(r *http.Request, repo *models.Repo, pull *models.Pull, stack models.Stack) pages.ResubmitResult {
360 if pull.State == models.PullMerged || pull.State == models.PullAbandoned || pull.PullSource == nil {
361 return pages.Unknown
362 }
363
364 var sourceRepo syntax.ATURI
365 if pull.PullSource.RepoAt != nil {
366 sourceRepo = *pull.PullSource.RepoAt
367 } else {
368 sourceRepo = repo.RepoAt()
369 }
370
371 xrpcc := &indigoxrpc.Client{Host: s.config.KnotMirror.Url}
372 branchResp, err := tangled.GitTempGetBranch(r.Context(), xrpcc, pull.PullSource.Branch, sourceRepo.String())
373 if err != nil {
374 if xrpcerr := xrpcclient.HandleXrpcErr(err); xrpcerr != nil {
375 s.logger.Error("failed to call XRPC repo.branches", "xrpcerr", xrpcerr, "err", err, "pull_id", pull.PullId, "branch", pull.PullSource.Branch)
376 return pages.Unknown
377 }
378 s.logger.Error("failed to reach knotserver", "err", err, "pull_id", pull.PullId)
379 return pages.Unknown
380 }
381
382 targetBranch := branchResp
383
384 top := stack[0]
385 latestSourceRev := top.LatestSha()
386
387 if latestSourceRev != targetBranch.Hash {
388 return pages.ShouldResubmit
389 }
390
391 return pages.ShouldNotResubmit
392}
393
394func (s *Pulls) RepoPullPatch(w http.ResponseWriter, r *http.Request) {
395 s.repoPullHelper(w, r, false)
396}
397
398func (s *Pulls) RepoPullInterdiff(w http.ResponseWriter, r *http.Request) {
399 s.repoPullHelper(w, r, true)
400}
401
402func (s *Pulls) RepoPullPatchRaw(w http.ResponseWriter, r *http.Request) {
403 l := s.logger.With("handler", "RepoPullPatchRaw")
404
405 pull, ok := r.Context().Value("pull").(*models.Pull)
406 if !ok {
407 l.Error("failed to get pull")
408 s.pages.Notice(w, "pull-error", "Failed to edit patch. Try again later.")
409 return
410 }
411 l = l.With("pull_id", pull.PullId)
412
413 roundId := chi.URLParam(r, "round")
414 roundIdInt, err := strconv.Atoi(roundId)
415 if err != nil || roundIdInt >= len(pull.Submissions) {
416 http.Error(w, "bad round id", http.StatusBadRequest)
417 l.Error("failed to parse round id", "err", err, "round_id_str", roundId)
418 return
419 }
420
421 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
422 w.Write([]byte(pull.Submissions[roundIdInt].Patch))
423}