Monorepo for Tangled tangled.org
8

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