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 "context" 5 "fmt" 6 "net/http" 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 // only the last round's buttons and banners use merge/resubmit checks 63 isLastRound := roundNumber == pull.LastRoundNumber() 64 branchDeleteStatus := s.branchDeleteStatus(r, f, pull) 65 mergeCheckResponse := types.MergeCheckResponse{} 66 resubmitResult := pages.Unknown 67 if isLastRound { 68 mergeCheckResponse = s.mergeCheck(r, f, pull, stack) 69 if user != nil && user.Did == pull.OwnerDid { 70 resubmitResult = s.resubmitCheck(r, f, pull, stack) 71 } 72 } 73 74 s.pages.PullActionsFragment(w, pages.PullActionsParams{ 75 BaseParams: pages.BaseParamsFromContext(r.Context()), 76 RepoInfo: s.repoResolver.GetRepoInfo(r, user), 77 Pull: pull, 78 RoundNumber: roundNumber, 79 MergeCheck: mergeCheckResponse, 80 ResubmitCheck: resubmitResult, 81 BranchDeleteStatus: branchDeleteStatus, 82 Stack: stack, 83 }) 84 return 85 } 86} 87 88func (s *Pulls) repoPullHelper(w http.ResponseWriter, r *http.Request, interdiff bool) { 89 l := s.logger.With("handler", "repoPullHelper", "interdiff", interdiff) 90 91 user := s.oauth.GetMultiAccountUser(r) 92 if user != nil { 93 l = l.With("user", user.Did) 94 } 95 96 f, err := s.repoResolver.Resolve(r) 97 if err != nil { 98 l.Error("failed to get repo and knot", "err", err) 99 return 100 } 101 102 pull, ok := r.Context().Value("pull").(*models.Pull) 103 if !ok { 104 l.Error("failed to get pull") 105 s.pages.Notice(w, "pull-error", "Failed to edit patch. Try again later.") 106 return 107 } 108 l = l.With("pull_id", pull.PullId, "pull_owner", pull.OwnerDid) 109 110 if user != nil { 111 userDid := user.Did 112 repoDid := f.RepoDid 113 pullId := pull.PullId 114 atUri := pull.AtUri().String() 115 focusing := pages.BaseParamsFromContext(r.Context()).FocusParams.Focusing 116 go func() { 117 if !focusing { 118 if err := db.MarkNotificationsReadForPull(s.db, userDid, repoDid, pullId); err != nil { 119 l.Error("failed to mark pull notifications as read", "err", err) 120 } 121 } 122 if err := db.UpsertRecentLink(s.db, userDid, models.RecentLinkTypePull, atUri); err != nil { 123 l.Error("failed to upsert recent link", "err", err) 124 } 125 }() 126 } 127 128 backlinks, err := db.GetBacklinks(s.db, pull.AtUri()) 129 if err != nil { 130 l.Error("failed to get pull backlinks", "err", err) 131 s.pages.Notice(w, "pull-error", "Failed to get pull. Try again later.") 132 return 133 } 134 135 roundId := chi.URLParam(r, "round") 136 roundIdInt := pull.LastRoundNumber() 137 if r, err := strconv.Atoi(roundId); err == nil { 138 roundIdInt = r 139 } 140 if roundIdInt >= len(pull.Submissions) { 141 http.Error(w, "bad round id", http.StatusBadRequest) 142 l.Error("failed to parse round id", "err", err, "round_number", roundIdInt) 143 return 144 } 145 146 var diffOpts types.DiffOpts 147 if d := r.URL.Query().Get("diff"); d == "split" { 148 diffOpts.Split = true 149 } 150 151 // can be nil if this pull is not stacked 152 stack, _ := r.Context().Value("stack").(models.Stack) 153 154 var shas []string 155 for _, s := range pull.Submissions { 156 shas = append(shas, s.SourceRev) 157 } 158 for _, p := range stack { 159 shas = append(shas, p.LatestSha()) 160 } 161 162 // commitId -> latest pipeline 163 pipelines := func(ctx context.Context) map[string]tangled.CiDefs_Pipeline { 164 xrpcc := &indigoxrpc.Client{Host: f.Spindle} 165 out, err := tangled.CiQueryPipelines(ctx, xrpcc, shas, "", 0, f.RepoDid) 166 if err != nil { 167 l.Error("failed to fetch pipelines", "err", err) 168 } 169 170 m := make(map[string]tangled.CiDefs_Pipeline) 171 172 for _, pipeline := range out.Pipelines { 173 if pipeline == nil { 174 continue 175 } 176 m[pipeline.Commit] = *pipeline 177 } 178 return m 179 }(r.Context()) 180 181 entities := []syntax.ATURI{pull.AtUri()} 182 for _, s := range pull.Submissions { 183 for _, c := range s.Comments { 184 entities = append(entities, c.FeedCommentAtUri()) 185 } 186 } 187 reactions, err := db.ListReactionDisplayDataMap(s.db, entities, 20) 188 if err != nil { 189 l.Error("failed to get pull reactions", "err", err) 190 } 191 192 var userReactions map[syntax.ATURI]map[models.ReactionKind]bool 193 if user != nil { 194 userReactions, err = db.ListReactionStatusMap(s.db, entities, syntax.DID(user.Did)) 195 if err != nil { 196 s.logger.Error("failed to get user reactions", "err", err) 197 } 198 } 199 200 labelDefs, err := db.GetLabelDefinitions( 201 s.db, 202 orm.FilterIn("at_uri", f.Labels), 203 orm.FilterContains("scope", tangled.RepoPullNSID), 204 ) 205 if err != nil { 206 l.Error("failed to fetch labels", "err", err) 207 s.pages.Error503(w) 208 return 209 } 210 211 defs := make(map[string]*models.LabelDefinition) 212 for _, l := range labelDefs { 213 defs[l.AtUri().String()] = &l 214 } 215 216 vouchRelationships := make(map[syntax.DID]*models.VouchRelationship) 217 vouchSkips := make(map[syntax.DID]bool) 218 if user != nil { 219 participants := pull.Participants() 220 vouchRelationships, err = db.GetVouchRelationshipsBatch(s.db, syntax.DID(user.Did), participants) 221 if err != nil { 222 l.Error("failed to fetch vouch relationships", "err", err) 223 } 224 ownerDid := syntax.DID(pull.OwnerDid) 225 skipped, err := db.IsVouchSkipped(s.db, user.Did, pull.OwnerDid) 226 if err != nil { 227 l.Error("failed to check vouch skip", "err", err) 228 } 229 vouchSkips[ownerDid] = skipped 230 } 231 232 patch := pull.Submissions[roundIdInt].CombinedPatch() 233 var diff types.DiffRenderer 234 diff = patchutil.AsNiceDiff(patch, pull.TargetBranch) 235 236 if interdiff { 237 currentPatch, err := patchutil.AsDiff(pull.Submissions[roundIdInt].CombinedPatch()) 238 if err != nil { 239 l.Error("failed to interdiff; current patch malformed", "err", err, "round_number", roundIdInt) 240 s.pages.Notice(w, fmt.Sprintf("interdiff-error-%d", roundIdInt), "Failed to calculate interdiff; current patch is invalid.") 241 return 242 } 243 244 previousPatch, err := patchutil.AsDiff(pull.Submissions[roundIdInt-1].CombinedPatch()) 245 if err != nil { 246 l.Error("failed to interdiff; previous patch malformed", "err", err, "round_number", roundIdInt) 247 s.pages.Notice(w, fmt.Sprintf("interdiff-error-%d", roundIdInt), "Failed to calculate interdiff; previous patch is invalid.") 248 return 249 } 250 251 diff = patchutil.Interdiff(previousPatch, currentPatch) 252 } 253 254 err = s.pages.RepoSinglePull(w, pages.RepoSinglePullParams{ 255 BaseParams: pages.BaseParamsFromContext(r.Context()), 256 RepoInfo: s.repoResolver.GetRepoInfo(r, user), 257 Pull: pull, 258 Stack: stack, 259 Backlinks: backlinks, 260 BranchDeleteStatus: nil, 261 MergeCheck: types.MergeCheckResponse{}, 262 ResubmitCheck: pages.Unknown, 263 Pipelines: pipelines, 264 Diff: diff, 265 DiffOpts: diffOpts, 266 ActiveRound: roundIdInt, 267 IsInterdiff: interdiff, 268 269 Reactions: reactions, 270 UserReacted: userReactions, 271 272 LabelDefs: defs, 273 VouchRelationships: vouchRelationships, 274 VouchSkips: vouchSkips, 275 }) 276 if err != nil { 277 l.Error("failed to render page", "err", err) 278 } 279} 280 281func (s *Pulls) RepoSinglePull(w http.ResponseWriter, r *http.Request) { 282 l := s.logger.With("handler", "RepoSinglePull") 283 284 pull, ok := r.Context().Value("pull").(*models.Pull) 285 if !ok { 286 l.Error("failed to get pull") 287 s.pages.Notice(w, "pull-error", "Failed to edit patch. Try again later.") 288 return 289 } 290 291 http.Redirect(w, r, r.URL.String()+fmt.Sprintf("/round/%d", pull.LastRoundNumber()), http.StatusFound) 292} 293 294func (s *Pulls) mergeCheck(r *http.Request, f *models.Repo, pull *models.Pull, stack models.Stack) types.MergeCheckResponse { 295 if pull.State == models.PullMerged { 296 return types.MergeCheckResponse{} 297 } 298 299 xrpcc := s.knotClient(f.Knot) 300 301 // combine patches of substack 302 subStack := stack.Below(pull) 303 // collect the portion of the stack that is mergeable 304 mergeable := subStack.Mergeable() 305 // combine each patch 306 patch := mergeable.CombinedPatch() 307 308 resp, err := tangled.RepoMergeCheck( 309 r.Context(), 310 xrpcc, 311 &tangled.RepoMergeCheck_Input{ 312 Did: f.Did, 313 Name: f.Name, 314 Branch: pull.TargetBranch, 315 Patch: patch, 316 }, 317 ) 318 if xrpcerr := xrpcclient.HandleXrpcErr(err); xrpcerr != nil { 319 s.logger.Error("failed to check for mergeability", "xrpcerr", xrpcerr, "err", err, "pull_id", pull.PullId, "target_branch", pull.TargetBranch) 320 return types.MergeCheckResponse{ 321 Error: fmt.Sprintf("failed to check merge status: %s", xrpcerr.Error()), 322 } 323 } 324 325 return mergeCheckResponseFrom(resp) 326} 327 328func mergeCheckResponseFrom(resp *tangled.RepoMergeCheck_Output) types.MergeCheckResponse { 329 conflicts := make([]types.ConflictInfo, len(resp.Conflicts)) 330 for i, c := range resp.Conflicts { 331 conflicts[i] = types.ConflictInfo{Filename: c.Filename, Reason: c.Reason} 332 } 333 out := types.MergeCheckResponse{ 334 IsConflicted: resp.Is_conflicted, 335 Conflicts: conflicts, 336 } 337 if resp.Message != nil { 338 out.Message = *resp.Message 339 } 340 if resp.Error != nil { 341 out.Error = *resp.Error 342 } 343 return out 344} 345 346func (s *Pulls) branchDeleteStatus(r *http.Request, repo *models.Repo, pull *models.Pull) *models.BranchDeleteStatus { 347 if pull.State != models.PullMerged { 348 return nil 349 } 350 351 user := s.oauth.GetMultiAccountUser(r) 352 if user == nil { 353 return nil 354 } 355 356 var branch string 357 // check if the branch exists 358 // NOTE: appview could cache branches/tags etc. for every repo by listening for gitRefUpdates 359 if pull.IsBranchBased() { 360 branch = pull.PullSource.Branch 361 } else if pull.IsForkBased() { 362 branch = pull.PullSource.Branch 363 repo = pull.PullSource.Repo 364 } else { 365 return nil 366 } 367 368 // deleted fork 369 if repo == nil { 370 return nil 371 } 372 373 // user can only delete branch if they are a collaborator in the repo that the branch belongs to 374 if !s.acl.HasRepoPermission(r.Context(), repo, user.Did, "repo:push") { 375 return nil 376 } 377 378 xrpcc := &indigoxrpc.Client{Host: s.config.KnotMirror.Url} 379 resp, err := tangled.GitTempGetBranch(r.Context(), xrpcc, branch, repo.RepoDid) 380 if xrpcerr := xrpcclient.HandleXrpcErr(err); xrpcerr != nil { 381 s.logger.Error("failed to get branch", "xrpcerr", xrpcerr, "err", err) 382 return nil 383 } 384 385 return &models.BranchDeleteStatus{ 386 Repo: repo, 387 Branch: resp.Name, 388 } 389} 390 391func (s *Pulls) resubmitCheck(r *http.Request, repo *models.Repo, pull *models.Pull, stack models.Stack) pages.ResubmitResult { 392 if pull.State == models.PullMerged || pull.State == models.PullAbandoned || pull.PullSource == nil { 393 return pages.Unknown 394 } 395 396 var sourceRepoDid string 397 if pull.PullSource.RepoDid != nil { 398 sourceRepoDid = string(*pull.PullSource.RepoDid) 399 } else { 400 sourceRepoDid = repo.RepoDid 401 } 402 403 xrpcc := &indigoxrpc.Client{Host: s.config.KnotMirror.Url} 404 branchResp, err := tangled.GitTempGetBranch(r.Context(), xrpcc, pull.PullSource.Branch, sourceRepoDid) 405 if err != nil { 406 if xrpcerr := xrpcclient.HandleXrpcErr(err); xrpcerr != nil { 407 s.logger.Error("failed to call XRPC repo.branches", "xrpcerr", xrpcerr, "err", err, "pull_id", pull.PullId, "branch", pull.PullSource.Branch) 408 return pages.Unknown 409 } 410 s.logger.Error("failed to reach knotserver", "err", err, "pull_id", pull.PullId) 411 return pages.Unknown 412 } 413 414 targetBranch := branchResp 415 416 top := stack[0] 417 latestSourceRev := top.LatestSha() 418 419 if latestSourceRev != targetBranch.Hash { 420 return pages.ShouldResubmit 421 } 422 423 return pages.ShouldNotResubmit 424} 425 426func (s *Pulls) RepoPullPatch(w http.ResponseWriter, r *http.Request) { 427 s.repoPullHelper(w, r, false) 428} 429 430func (s *Pulls) RepoPullInterdiff(w http.ResponseWriter, r *http.Request) { 431 s.repoPullHelper(w, r, true) 432} 433 434func (s *Pulls) RepoPullPatchRaw(w http.ResponseWriter, r *http.Request) { 435 l := s.logger.With("handler", "RepoPullPatchRaw") 436 437 pull, ok := r.Context().Value("pull").(*models.Pull) 438 if !ok { 439 l.Error("failed to get pull") 440 s.pages.Notice(w, "pull-error", "Failed to edit patch. Try again later.") 441 return 442 } 443 l = l.With("pull_id", pull.PullId) 444 445 roundId := chi.URLParam(r, "round") 446 roundIdInt, err := strconv.Atoi(roundId) 447 if err != nil || roundIdInt >= len(pull.Submissions) { 448 http.Error(w, "bad round id", http.StatusBadRequest) 449 l.Error("failed to parse round id", "err", err, "round_id_str", roundId) 450 return 451 } 452 453 w.Header().Set("Content-Type", "text/plain; charset=utf-8") 454 w.Write([]byte(pull.Submissions[roundIdInt].Patch)) 455}