Monorepo for Tangled
0
fork

Configure Feed

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

appview/pulls: remove dependencies to `oauth.MultiAccountUser`

In most helper methods, DID is enough. Don't pass entire session info.

Signed-off-by: Seongmin Lee <git@boltless.me>

authored by

Seongmin Lee and committed by
Tangled
531deaec 40e7df13

+55 -54
+55 -54
appview/pulls/pulls.go
··· 974 974 fromFork := r.FormValue("fork") 975 975 sourceBranch := r.FormValue("sourceBranch") 976 976 patch := r.FormValue("patch") 977 + userDid := syntax.DID(user.Active.Did) 977 978 978 979 if targetBranch == "" { 979 980 s.pages.Notice(w, "pull", "Target branch is required.") ··· 981 982 } 982 983 983 984 // Determine PR type based on input parameters 984 - roles := repoinfo.RolesInRepo{Roles: s.enforcer.GetPermissionsInRepo(user.Active.Did, f.Knot, f.RepoIdentifier())} 985 + roles := repoinfo.RolesInRepo{Roles: s.enforcer.GetPermissionsInRepo(userDid.String(), f.Knot, f.RepoIdentifier())} 985 986 isPushAllowed := roles.IsPushAllowed() 986 987 isBranchBased := isPushAllowed && sourceBranch != "" && fromFork == "" 987 988 isForkBased := fromFork != "" && sourceBranch != "" ··· 1059 1060 s.pages.Notice(w, "pull", "This knot doesn't support branch-based pull requests. Try another way?") 1060 1061 return 1061 1062 } 1062 - s.handleBranchBasedPull(w, r, f, user, title, body, targetBranch, sourceBranch, isStacked) 1063 + s.handleBranchBasedPull(w, r, f, userDid, title, body, targetBranch, sourceBranch, isStacked) 1063 1064 } else if isForkBased { 1064 1065 if !caps.PullRequests.ForkSubmissions { 1065 1066 s.pages.Notice(w, "pull", "This knot doesn't support fork-based pull requests. Try another way?") 1066 1067 return 1067 1068 } 1068 - s.handleForkBasedPull(w, r, f, user, fromFork, title, body, targetBranch, sourceBranch, isStacked) 1069 + s.handleForkBasedPull(w, r, f, userDid, fromFork, title, body, targetBranch, sourceBranch, isStacked) 1069 1070 } else if isPatchBased { 1070 1071 if !caps.PullRequests.PatchSubmissions { 1071 1072 s.pages.Notice(w, "pull", "This knot doesn't support patch-based pull requests. Send your patch over email.") 1072 1073 return 1073 1074 } 1074 - s.handlePatchBasedPull(w, r, f, user, title, body, targetBranch, patch, isStacked) 1075 + s.handlePatchBasedPull(w, r, f, userDid, title, body, targetBranch, patch, isStacked) 1075 1076 } 1076 1077 return 1077 1078 } ··· 1081 1082 w http.ResponseWriter, 1082 1083 r *http.Request, 1083 1084 repo *models.Repo, 1084 - user *oauth.MultiAccountUser, 1085 + userDid syntax.DID, 1085 1086 title, 1086 1087 body, 1087 1088 targetBranch, 1088 1089 sourceBranch string, 1089 1090 isStacked bool, 1090 1091 ) { 1091 - l := s.logger.With("handler", "handleBranchBasedPull", "user", user.Active.Did, "target_branch", targetBranch, "source_branch", sourceBranch, "is_stacked", isStacked) 1092 + l := s.logger.With("handler", "handleBranchBasedPull", "user", userDid, "target_branch", targetBranch, "source_branch", sourceBranch, "is_stacked", isStacked) 1092 1093 1093 1094 scheme := "http" 1094 1095 if !s.config.Core.Dev { ··· 1132 1133 Branch: sourceBranch, 1133 1134 } 1134 1135 1135 - s.createPullRequest(w, r, repo, user, title, body, targetBranch, patch, combined, sourceRev, pullSource, isStacked) 1136 + s.createPullRequest(w, r, repo, userDid, title, body, targetBranch, patch, combined, sourceRev, pullSource, isStacked) 1136 1137 } 1137 1138 1138 - func (s *Pulls) handlePatchBasedPull(w http.ResponseWriter, r *http.Request, repo *models.Repo, user *oauth.MultiAccountUser, title, body, targetBranch, patch string, isStacked bool) { 1139 + func (s *Pulls) handlePatchBasedPull(w http.ResponseWriter, r *http.Request, repo *models.Repo, userDid syntax.DID, title, body, targetBranch, patch string, isStacked bool) { 1139 1140 if err := s.validator.ValidatePatch(&patch); err != nil { 1140 1141 s.logger.Error("patch validation failed", "err", err) 1141 1142 s.pages.Notice(w, "pull", "Invalid patch format. Please provide a valid diff.") 1142 1143 return 1143 1144 } 1144 1145 1145 - s.createPullRequest(w, r, repo, user, title, body, targetBranch, patch, "", "", nil, isStacked) 1146 + s.createPullRequest(w, r, repo, userDid, title, body, targetBranch, patch, "", "", nil, isStacked) 1146 1147 } 1147 1148 1148 - func (s *Pulls) handleForkBasedPull(w http.ResponseWriter, r *http.Request, repo *models.Repo, user *oauth.MultiAccountUser, forkRepo string, title, body, targetBranch, sourceBranch string, isStacked bool) { 1149 - l := s.logger.With("handler", "handleForkBasedPull", "user", user.Active.Did, "fork_repo", forkRepo, "target_branch", targetBranch, "source_branch", sourceBranch, "is_stacked", isStacked) 1149 + func (s *Pulls) handleForkBasedPull(w http.ResponseWriter, r *http.Request, repo *models.Repo, userDid syntax.DID, forkRepo string, title, body, targetBranch, sourceBranch string, isStacked bool) { 1150 + l := s.logger.With("handler", "handleForkBasedPull", "user", userDid, "fork_repo", forkRepo, "target_branch", targetBranch, "source_branch", sourceBranch, "is_stacked", isStacked) 1150 1151 1151 1152 repoString := strings.SplitN(forkRepo, "/", 2) 1152 1153 forkOwnerDid := repoString[0] ··· 1249 1250 RepoDid: forkDid, 1250 1251 } 1251 1252 1252 - s.createPullRequest(w, r, repo, user, title, body, targetBranch, patch, combined, sourceRev, pullSource, isStacked) 1253 + s.createPullRequest(w, r, repo, userDid, title, body, targetBranch, patch, combined, sourceRev, pullSource, isStacked) 1253 1254 } 1254 1255 1255 1256 func (s *Pulls) createPullRequest( 1256 1257 w http.ResponseWriter, 1257 1258 r *http.Request, 1258 1259 repo *models.Repo, 1259 - user *oauth.MultiAccountUser, 1260 + userDid syntax.DID, 1260 1261 title, body, targetBranch string, 1261 1262 patch string, 1262 1263 combined string, ··· 1264 1265 pullSource *models.PullSource, 1265 1266 isStacked bool, 1266 1267 ) { 1267 - l := s.logger.With("handler", "createPullRequest", "user", user.Active.Did, "target_branch", targetBranch, "is_stacked", isStacked) 1268 + l := s.logger.With("handler", "createPullRequest", "user", userDid, "target_branch", targetBranch, "is_stacked", isStacked) 1268 1269 1269 1270 if isStacked { 1270 1271 // creates a series of PRs, each linking to the previous, identified by jj's change-id ··· 1272 1273 w, 1273 1274 r, 1274 1275 repo, 1275 - user, 1276 + userDid, 1276 1277 targetBranch, 1277 1278 patch, 1278 1279 sourceRev, ··· 1334 1335 Title: title, 1335 1336 Body: body, 1336 1337 TargetBranch: targetBranch, 1337 - OwnerDid: user.Active.Did, 1338 + OwnerDid: userDid.String(), 1338 1339 RepoAt: repo.RepoAt(), 1339 1340 Rkey: rkey, 1340 1341 Mentions: mentions, ··· 1356 1357 record := pull.AsRecord() 1357 1358 _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 1358 1359 Collection: tangled.RepoPullNSID, 1359 - Repo: user.Active.Did, 1360 + Repo: userDid.String(), 1360 1361 Rkey: rkey, 1361 1362 Record: &lexutil.LexiconTypeDecoder{ 1362 1363 Val: &record, ··· 1397 1398 w http.ResponseWriter, 1398 1399 r *http.Request, 1399 1400 repo *models.Repo, 1400 - user *oauth.MultiAccountUser, 1401 + userDid syntax.DID, 1401 1402 targetBranch string, 1402 1403 patch string, 1403 1404 sourceRev string, 1404 1405 pullSource *models.PullSource, 1405 1406 ) { 1406 - l := s.logger.With("handler", "createStackedPullRequest", "user", user.Active.Did, "target_branch", targetBranch, "source_rev", sourceRev) 1407 + l := s.logger.With("handler", "createStackedPullRequest", "user", userDid, "target_branch", targetBranch, "source_rev", sourceRev) 1407 1408 1408 1409 // run some necessary checks for stacked-prs first 1409 1410 ··· 1449 1450 } 1450 1451 1451 1452 // build a stack out of this patch 1452 - stack, err := s.newStack(r.Context(), repo, user, targetBranch, pullSource, formatPatches, blobs) 1453 + stack, err := s.newStack(r.Context(), repo, userDid, targetBranch, pullSource, formatPatches, blobs) 1453 1454 if err != nil { 1454 1455 l.Error("failed to create stack", "err", err) 1455 1456 s.pages.Notice(w, "pull", fmt.Sprintf("Failed to create stack: %v", err)) ··· 1471 1472 }) 1472 1473 } 1473 1474 _, err = comatproto.RepoApplyWrites(r.Context(), client, &comatproto.RepoApplyWrites_Input{ 1474 - Repo: user.Active.Did, 1475 + Repo: userDid.String(), 1475 1476 Writes: writes, 1476 1477 }) 1477 1478 if err != nil { ··· 1751 1752 } 1752 1753 l = l.With("pull_id", pull.PullId, "pull_owner", pull.OwnerDid) 1753 1754 1754 - f, err := s.repoResolver.Resolve(r) 1755 - if err != nil { 1756 - l.Error("failed to get repo and knot", "err", err) 1755 + if user == nil || user.Active.Did != pull.OwnerDid { 1756 + l.Warn("unauthorized user", "actual_user", user.Active.Did, "expected_owner", pull.OwnerDid) 1757 + w.WriteHeader(http.StatusUnauthorized) 1757 1758 return 1758 1759 } 1759 1760 1760 - if user.Active.Did != pull.OwnerDid { 1761 - l.Error("unauthorized user", "actual_user", user.Active.Did, "expected_owner", pull.OwnerDid) 1762 - w.WriteHeader(http.StatusUnauthorized) 1761 + f, err := s.repoResolver.Resolve(r) 1762 + if err != nil { 1763 + l.Error("failed to get repo and knot", "err", err) 1763 1764 return 1764 1765 } 1765 1766 1766 1767 patch := r.FormValue("patch") 1767 1768 1768 - s.resubmitPullHelper(w, r, f, user, pull, patch, "", "") 1769 + s.resubmitPullHelper(w, r, f, syntax.DID(user.Active.Did), pull, patch, "", "") 1769 1770 } 1770 1771 1771 1772 func (s *Pulls) resubmitBranch(w http.ResponseWriter, r *http.Request) { ··· 1784 1785 } 1785 1786 l = l.With("pull_id", pull.PullId, "pull_owner", pull.OwnerDid, "target_branch", pull.TargetBranch) 1786 1787 1787 - f, err := s.repoResolver.Resolve(r) 1788 - if err != nil { 1789 - l.Error("failed to get repo and knot", "err", err) 1788 + if user == nil || user.Active.Did != pull.OwnerDid { 1789 + l.Warn("unauthorized user", "actual_user", user.Active.Did, "expected_owner", pull.OwnerDid) 1790 + w.WriteHeader(http.StatusUnauthorized) 1790 1791 return 1791 1792 } 1792 1793 1793 - if user.Active.Did != pull.OwnerDid { 1794 - l.Error("unauthorized user", "actual_user", user.Active.Did, "expected_owner", pull.OwnerDid) 1795 - w.WriteHeader(http.StatusUnauthorized) 1794 + f, err := s.repoResolver.Resolve(r) 1795 + if err != nil { 1796 + l.Error("failed to get repo and knot", "err", err) 1796 1797 return 1797 1798 } 1798 1799 1799 1800 roles := repoinfo.RolesInRepo{Roles: s.enforcer.GetPermissionsInRepo(user.Active.Did, f.Knot, f.RepoIdentifier())} 1800 1801 if !roles.IsPushAllowed() { 1801 - l.Error("unauthorized user - no push permission") 1802 + l.Warn("unauthorized user - no push permission") 1802 1803 w.WriteHeader(http.StatusUnauthorized) 1803 1804 return 1804 1805 } ··· 1835 1836 patch := comparison.FormatPatchRaw 1836 1837 combined := comparison.CombinedPatchRaw 1837 1838 1838 - s.resubmitPullHelper(w, r, f, user, pull, patch, combined, sourceRev) 1839 + s.resubmitPullHelper(w, r, f, syntax.DID(user.Active.Did), pull, patch, combined, sourceRev) 1839 1840 } 1840 1841 1841 1842 func (s *Pulls) resubmitFork(w http.ResponseWriter, r *http.Request) { ··· 1854 1855 } 1855 1856 l = l.With("pull_id", pull.PullId, "pull_owner", pull.OwnerDid, "target_branch", pull.TargetBranch) 1856 1857 1857 - f, err := s.repoResolver.Resolve(r) 1858 - if err != nil { 1859 - l.Error("failed to get repo and knot", "err", err) 1858 + if user == nil || user.Active.Did != pull.OwnerDid { 1859 + l.Warn("unauthorized user", "actual_user", user.Active.Did, "expected_owner", pull.OwnerDid) 1860 + w.WriteHeader(http.StatusUnauthorized) 1860 1861 return 1861 1862 } 1862 1863 1863 - if user.Active.Did != pull.OwnerDid { 1864 - l.Error("unauthorized user", "actual_user", user.Active.Did, "expected_owner", pull.OwnerDid) 1865 - w.WriteHeader(http.StatusUnauthorized) 1864 + f, err := s.repoResolver.Resolve(r) 1865 + if err != nil { 1866 + l.Error("failed to get repo and knot", "err", err) 1866 1867 return 1867 1868 } 1868 1869 ··· 1938 1939 patch := comparison.FormatPatchRaw 1939 1940 combined := comparison.CombinedPatchRaw 1940 1941 1941 - s.resubmitPullHelper(w, r, f, user, pull, patch, combined, sourceRev) 1942 + s.resubmitPullHelper(w, r, f, syntax.DID(user.Active.Did), pull, patch, combined, sourceRev) 1942 1943 } 1943 1944 1944 1945 func (s *Pulls) resubmitPullHelper( 1945 1946 w http.ResponseWriter, 1946 1947 r *http.Request, 1947 1948 repo *models.Repo, 1948 - user *oauth.MultiAccountUser, 1949 + userDid syntax.DID, 1949 1950 pull *models.Pull, 1950 1951 patch string, 1951 1952 combined string, 1952 1953 sourceRev string, 1953 1954 ) { 1954 - l := s.logger.With("handler", "resubmitPullHelper", "user", user.Active.Did, "pull_id", pull.PullId, "target_branch", pull.TargetBranch) 1955 + l := s.logger.With("handler", "resubmitPullHelper", "user", userDid, "pull_id", pull.PullId, "target_branch", pull.TargetBranch) 1955 1956 1956 1957 stack := r.Context().Value("stack").(models.Stack) 1957 1958 if stack != nil && len(stack) != 1 { 1958 1959 l.Info("resubmitting stacked PR", "stack_size", len(stack)) 1959 - s.resubmitStackedPullHelper(w, r, repo, user, pull, patch) 1960 + s.resubmitStackedPullHelper(w, r, repo, userDid, pull, patch) 1960 1961 return 1961 1962 } 1962 1963 ··· 1991 1992 return 1992 1993 } 1993 1994 1994 - ex, err := comatproto.RepoGetRecord(r.Context(), client, "", tangled.RepoPullNSID, user.Active.Did, pull.Rkey) 1995 + ex, err := comatproto.RepoGetRecord(r.Context(), client, "", tangled.RepoPullNSID, userDid.String(), pull.Rkey) 1995 1996 if err != nil { 1996 1997 // failed to get record 1997 1998 l.Error("failed to get record from PDS", "err", err, "rkey", pull.Rkey) ··· 2013 2014 2014 2015 _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 2015 2016 Collection: tangled.RepoPullNSID, 2016 - Repo: user.Active.Did, 2017 + Repo: userDid.String(), 2017 2018 Rkey: pull.Rkey, 2018 2019 SwapRecord: ex.Cid, 2019 2020 Record: &lexutil.LexiconTypeDecoder{ ··· 2041 2042 w http.ResponseWriter, 2042 2043 r *http.Request, 2043 2044 repo *models.Repo, 2044 - user *oauth.MultiAccountUser, 2045 + userDid syntax.DID, 2045 2046 pull *models.Pull, 2046 2047 patch string, 2047 2048 ) { 2048 - l := s.logger.With("handler", "resubmitStackedPullHelper", "user", user.Active.Did, "pull_id", pull.PullId, "target_branch", pull.TargetBranch) 2049 + l := s.logger.With("handler", "resubmitStackedPullHelper", "user", userDid, "pull_id", pull.PullId, "target_branch", pull.TargetBranch) 2049 2050 2050 2051 targetBranch := pull.TargetBranch 2051 2052 ··· 2085 2086 blobs[i] = blob.Blob 2086 2087 } 2087 2088 2088 - newStack, err := s.newStack(r.Context(), repo, user, targetBranch, pull.PullSource, formatPatches, blobs) 2089 + newStack, err := s.newStack(r.Context(), repo, userDid, targetBranch, pull.PullSource, formatPatches, blobs) 2089 2090 if err != nil { 2090 2091 l.Error("failed to create resubmitted stack", "err", err) 2091 2092 s.pages.Notice(w, "pull-merge-error", "Failed to merge pull request. Try again later.") ··· 2275 2276 } 2276 2277 2277 2278 _, err = comatproto.RepoApplyWrites(r.Context(), client, &comatproto.RepoApplyWrites_Input{ 2278 - Repo: user.Active.Did, 2279 + Repo: userDid.String(), 2279 2280 Writes: writes, 2280 2281 }) 2281 2282 if err != nil { ··· 2576 2577 func (s *Pulls) newStack( 2577 2578 ctx context.Context, 2578 2579 repo *models.Repo, 2579 - user *oauth.MultiAccountUser, 2580 + userDid syntax.DID, 2580 2581 targetBranch string, 2581 2582 pullSource *models.PullSource, 2582 2583 formatPatches []types.FormatPatch, ··· 2603 2604 Title: title, 2604 2605 Body: body, 2605 2606 TargetBranch: targetBranch, 2606 - OwnerDid: user.Active.Did, 2607 + OwnerDid: userDid.String(), 2607 2608 RepoAt: repo.RepoAt(), 2608 2609 Rkey: rkey, 2609 2610 Mentions: mentions,