Monorepo for Tangled tangled.org
5

Configure Feed

Select the types of activity you want to include in your feed.

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_did", f.RepoDid), 152 orm.FilterIn("p.sha", shas), 153 ) 154 if err != nil { 155 l.Error("failed to fetch pipeline statuses", "err", err) 156 // non-fatal 157 } 158 159 for _, p := range ps { 160 m[p.Sha] = p 161 } 162 163 entities := []syntax.ATURI{pull.AtUri()} 164 for _, s := range pull.Submissions { 165 for _, c := range s.Comments { 166 entities = append(entities, c.FeedCommentAtUri()) 167 } 168 } 169 reactions, err := db.ListReactionDisplayDataMap(s.db, entities, 20) 170 if err != nil { 171 l.Error("failed to get pull reactions", "err", err) 172 } 173 174 var userReactions map[syntax.ATURI]map[models.ReactionKind]bool 175 if user != nil { 176 userReactions, err = db.ListReactionStatusMap(s.db, entities, syntax.DID(user.Did)) 177 if err != nil { 178 s.logger.Error("failed to get user reactions", "err", err) 179 } 180 } 181 182 labelDefs, err := db.GetLabelDefinitions( 183 s.db, 184 orm.FilterIn("at_uri", f.Labels), 185 orm.FilterContains("scope", tangled.RepoPullNSID), 186 ) 187 if err != nil { 188 l.Error("failed to fetch labels", "err", err) 189 s.pages.Error503(w) 190 return 191 } 192 193 defs := make(map[string]*models.LabelDefinition) 194 for _, l := range labelDefs { 195 defs[l.AtUri().String()] = &l 196 } 197 198 vouchRelationships := make(map[syntax.DID]*models.VouchRelationship) 199 vouchSkips := make(map[syntax.DID]bool) 200 if user != nil { 201 participants := pull.Participants() 202 vouchRelationships, err = db.GetVouchRelationshipsBatch(s.db, syntax.DID(user.Did), participants) 203 if err != nil { 204 l.Error("failed to fetch vouch relationships", "err", err) 205 } 206 ownerDid := syntax.DID(pull.OwnerDid) 207 skipped, err := db.IsVouchSkipped(s.db, user.Did, pull.OwnerDid) 208 if err != nil { 209 l.Error("failed to check vouch skip", "err", err) 210 } 211 vouchSkips[ownerDid] = skipped 212 } 213 214 patch := pull.Submissions[roundIdInt].CombinedPatch() 215 var diff types.DiffRenderer 216 diff = patchutil.AsNiceDiff(patch, pull.TargetBranch) 217 218 if interdiff { 219 currentPatch, err := patchutil.AsDiff(pull.Submissions[roundIdInt].CombinedPatch()) 220 if err != nil { 221 l.Error("failed to interdiff; current patch malformed", "err", err, "round_number", roundIdInt) 222 s.pages.Notice(w, fmt.Sprintf("interdiff-error-%d", roundIdInt), "Failed to calculate interdiff; current patch is invalid.") 223 return 224 } 225 226 previousPatch, err := patchutil.AsDiff(pull.Submissions[roundIdInt-1].CombinedPatch()) 227 if err != nil { 228 l.Error("failed to interdiff; previous patch malformed", "err", err, "round_number", roundIdInt) 229 s.pages.Notice(w, fmt.Sprintf("interdiff-error-%d", roundIdInt), "Failed to calculate interdiff; previous patch is invalid.") 230 return 231 } 232 233 diff = patchutil.Interdiff(previousPatch, currentPatch) 234 } 235 236 err = s.pages.RepoSinglePull(w, pages.RepoSinglePullParams{ 237 LoggedInUser: user, 238 RepoInfo: s.repoResolver.GetRepoInfo(r, user), 239 Pull: pull, 240 Stack: stack, 241 Backlinks: backlinks, 242 BranchDeleteStatus: branchDeleteStatus, 243 MergeCheck: mergeCheckResponse, 244 ResubmitCheck: resubmitResult, 245 Pipelines: m, 246 Diff: diff, 247 DiffOpts: diffOpts, 248 ActiveRound: roundIdInt, 249 IsInterdiff: interdiff, 250 251 Reactions: reactions, 252 UserReacted: userReactions, 253 254 LabelDefs: defs, 255 VouchRelationships: vouchRelationships, 256 VouchSkips: vouchSkips, 257 }) 258 if err != nil { 259 l.Error("failed to render page", "err", err) 260 } 261} 262 263func (s *Pulls) RepoSinglePull(w http.ResponseWriter, r *http.Request) { 264 l := s.logger.With("handler", "RepoSinglePull") 265 266 pull, ok := r.Context().Value("pull").(*models.Pull) 267 if !ok { 268 l.Error("failed to get pull") 269 s.pages.Notice(w, "pull-error", "Failed to edit patch. Try again later.") 270 return 271 } 272 273 http.Redirect(w, r, r.URL.String()+fmt.Sprintf("/round/%d", pull.LastRoundNumber()), http.StatusFound) 274} 275 276func (s *Pulls) mergeCheck(r *http.Request, f *models.Repo, pull *models.Pull, stack models.Stack) types.MergeCheckResponse { 277 if pull.State == models.PullMerged { 278 return types.MergeCheckResponse{} 279 } 280 281 xrpcc := s.knotClient(f.Knot) 282 283 // combine patches of substack 284 subStack := stack.Below(pull) 285 // collect the portion of the stack that is mergeable 286 mergeable := subStack.Mergeable() 287 // combine each patch 288 patch := mergeable.CombinedPatch() 289 290 resp, err := tangled.RepoMergeCheck( 291 r.Context(), 292 xrpcc, 293 &tangled.RepoMergeCheck_Input{ 294 Did: f.Did, 295 Name: f.Name, 296 Branch: pull.TargetBranch, 297 Patch: patch, 298 }, 299 ) 300 if xrpcerr := xrpcclient.HandleXrpcErr(err); xrpcerr != nil { 301 s.logger.Error("failed to check for mergeability", "xrpcerr", xrpcerr, "err", err, "pull_id", pull.PullId, "target_branch", pull.TargetBranch) 302 return types.MergeCheckResponse{ 303 Error: fmt.Sprintf("failed to check merge status: %s", xrpcerr.Error()), 304 } 305 } 306 307 return mergeCheckResponseFrom(resp) 308} 309 310func mergeCheckResponseFrom(resp *tangled.RepoMergeCheck_Output) types.MergeCheckResponse { 311 conflicts := make([]types.ConflictInfo, len(resp.Conflicts)) 312 for i, c := range resp.Conflicts { 313 conflicts[i] = types.ConflictInfo{Filename: c.Filename, Reason: c.Reason} 314 } 315 out := types.MergeCheckResponse{ 316 IsConflicted: resp.Is_conflicted, 317 Conflicts: conflicts, 318 } 319 if resp.Message != nil { 320 out.Message = *resp.Message 321 } 322 if resp.Error != nil { 323 out.Error = *resp.Error 324 } 325 return out 326} 327 328func (s *Pulls) branchDeleteStatus(r *http.Request, repo *models.Repo, pull *models.Pull) *models.BranchDeleteStatus { 329 if pull.State != models.PullMerged { 330 return nil 331 } 332 333 user := s.oauth.GetMultiAccountUser(r) 334 if user == nil { 335 return nil 336 } 337 338 var branch string 339 // check if the branch exists 340 // NOTE: appview could cache branches/tags etc. for every repo by listening for gitRefUpdates 341 if pull.IsBranchBased() { 342 branch = pull.PullSource.Branch 343 } else if pull.IsForkBased() { 344 branch = pull.PullSource.Branch 345 repo = pull.PullSource.Repo 346 } else { 347 return nil 348 } 349 350 // deleted fork 351 if repo == nil { 352 return nil 353 } 354 355 // user can only delete branch if they are a collaborator in the repo that the branch belongs to 356 perms := s.enforcer.GetPermissionsInRepo(user.Did, repo.Knot, repo.RepoIdentifier()) 357 if !slices.Contains(perms, "repo:push") { 358 return nil 359 } 360 361 xrpcc := &indigoxrpc.Client{Host: s.config.KnotMirror.Url} 362 resp, err := tangled.GitTempGetBranch(r.Context(), xrpcc, branch, repo.RepoDid) 363 if xrpcerr := xrpcclient.HandleXrpcErr(err); xrpcerr != nil { 364 s.logger.Error("failed to get branch", "xrpcerr", xrpcerr, "err", err) 365 return nil 366 } 367 368 return &models.BranchDeleteStatus{ 369 Repo: repo, 370 Branch: resp.Name, 371 } 372} 373 374func (s *Pulls) resubmitCheck(r *http.Request, repo *models.Repo, pull *models.Pull, stack models.Stack) pages.ResubmitResult { 375 if pull.State == models.PullMerged || pull.State == models.PullAbandoned || pull.PullSource == nil { 376 return pages.Unknown 377 } 378 379 var sourceRepoDid string 380 if pull.PullSource.RepoDid != nil { 381 sourceRepoDid = string(*pull.PullSource.RepoDid) 382 } else { 383 sourceRepoDid = repo.RepoDid 384 } 385 386 xrpcc := &indigoxrpc.Client{Host: s.config.KnotMirror.Url} 387 branchResp, err := tangled.GitTempGetBranch(r.Context(), xrpcc, pull.PullSource.Branch, sourceRepoDid) 388 if err != nil { 389 if xrpcerr := xrpcclient.HandleXrpcErr(err); xrpcerr != nil { 390 s.logger.Error("failed to call XRPC repo.branches", "xrpcerr", xrpcerr, "err", err, "pull_id", pull.PullId, "branch", pull.PullSource.Branch) 391 return pages.Unknown 392 } 393 s.logger.Error("failed to reach knotserver", "err", err, "pull_id", pull.PullId) 394 return pages.Unknown 395 } 396 397 targetBranch := branchResp 398 399 top := stack[0] 400 latestSourceRev := top.LatestSha() 401 402 if latestSourceRev != targetBranch.Hash { 403 return pages.ShouldResubmit 404 } 405 406 return pages.ShouldNotResubmit 407} 408 409func (s *Pulls) RepoPullPatch(w http.ResponseWriter, r *http.Request) { 410 s.repoPullHelper(w, r, false) 411} 412 413func (s *Pulls) RepoPullInterdiff(w http.ResponseWriter, r *http.Request) { 414 s.repoPullHelper(w, r, true) 415} 416 417func (s *Pulls) RepoPullPatchRaw(w http.ResponseWriter, r *http.Request) { 418 l := s.logger.With("handler", "RepoPullPatchRaw") 419 420 pull, ok := r.Context().Value("pull").(*models.Pull) 421 if !ok { 422 l.Error("failed to get pull") 423 s.pages.Notice(w, "pull-error", "Failed to edit patch. Try again later.") 424 return 425 } 426 l = l.With("pull_id", pull.PullId) 427 428 roundId := chi.URLParam(r, "round") 429 roundIdInt, err := strconv.Atoi(roundId) 430 if err != nil || roundIdInt >= len(pull.Submissions) { 431 http.Error(w, "bad round id", http.StatusBadRequest) 432 l.Error("failed to parse round id", "err", err, "round_id_str", roundId) 433 return 434 } 435 436 w.Header().Set("Content-Type", "text/plain; charset=utf-8") 437 w.Write([]byte(pull.Submissions[roundIdInt].Patch)) 438}