Monorepo for Tangled tangled.org

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>

boltless.me c5db1ce1 78526427

verified
+48 -47
+48 -47
appview/pulls/pulls.go
··· 956 956 fromFork := r.FormValue("fork") 957 957 sourceBranch := r.FormValue("sourceBranch") 958 958 patch := r.FormValue("patch") 959 + userDid := syntax.DID(user.Active.Did) 959 960 960 961 if targetBranch == "" { 961 962 s.pages.Notice(w, "pull", "Target branch is required.") ··· 963 964 } 964 965 965 966 // Determine PR type based on input parameters 966 - roles := repoinfo.RolesInRepo{Roles: s.enforcer.GetPermissionsInRepo(user.Active.Did, f.Knot, f.DidSlashRepo())} 967 + roles := repoinfo.RolesInRepo{Roles: s.enforcer.GetPermissionsInRepo(userDid.String(), f.Knot, f.DidSlashRepo())} 967 968 isPushAllowed := roles.IsPushAllowed() 968 969 isBranchBased := isPushAllowed && sourceBranch != "" && fromFork == "" 969 970 isForkBased := fromFork != "" && sourceBranch != "" ··· 1041 1042 s.pages.Notice(w, "pull", "This knot doesn't support branch-based pull requests. Try another way?") 1042 1043 return 1043 1044 } 1044 - s.handleBranchBasedPull(w, r, f, user, title, body, targetBranch, sourceBranch, isStacked) 1045 + s.handleBranchBasedPull(w, r, f, userDid, title, body, targetBranch, sourceBranch, isStacked) 1045 1046 } else if isForkBased { 1046 1047 if !caps.PullRequests.ForkSubmissions { 1047 1048 s.pages.Notice(w, "pull", "This knot doesn't support fork-based pull requests. Try another way?") 1048 1049 return 1049 1050 } 1050 - s.handleForkBasedPull(w, r, f, user, fromFork, title, body, targetBranch, sourceBranch, isStacked) 1051 + s.handleForkBasedPull(w, r, f, userDid, fromFork, title, body, targetBranch, sourceBranch, isStacked) 1051 1052 } else if isPatchBased { 1052 1053 if !caps.PullRequests.PatchSubmissions { 1053 1054 s.pages.Notice(w, "pull", "This knot doesn't support patch-based pull requests. Send your patch over email.") 1054 1055 return 1055 1056 } 1056 - s.handlePatchBasedPull(w, r, f, user, title, body, targetBranch, patch, isStacked) 1057 + s.handlePatchBasedPull(w, r, f, userDid, title, body, targetBranch, patch, isStacked) 1057 1058 } 1058 1059 return 1059 1060 } ··· 1063 1064 w http.ResponseWriter, 1064 1065 r *http.Request, 1065 1066 repo *models.Repo, 1066 - user *oauth.MultiAccountUser, 1067 + userDid syntax.DID, 1067 1068 title, 1068 1069 body, 1069 1070 targetBranch, ··· 1117 1118 Sha: comparison.Rev2, 1118 1119 } 1119 1120 1120 - s.createPullRequest(w, r, repo, user, title, body, targetBranch, patch, combined, sourceRev, pullSource, recordPullSource, isStacked) 1121 + s.createPullRequest(w, r, repo, userDid, title, body, targetBranch, patch, combined, sourceRev, pullSource, recordPullSource, isStacked) 1121 1122 } 1122 1123 1123 - func (s *Pulls) handlePatchBasedPull(w http.ResponseWriter, r *http.Request, repo *models.Repo, user *oauth.MultiAccountUser, title, body, targetBranch, patch string, isStacked bool) { 1124 + func (s *Pulls) handlePatchBasedPull(w http.ResponseWriter, r *http.Request, repo *models.Repo, userDid syntax.DID, title, body, targetBranch, patch string, isStacked bool) { 1124 1125 if err := s.validator.ValidatePatch(&patch); err != nil { 1125 1126 s.logger.Error("patch validation failed", "err", err) 1126 1127 s.pages.Notice(w, "pull", "Invalid patch format. Please provide a valid diff.") 1127 1128 return 1128 1129 } 1129 1130 1130 - s.createPullRequest(w, r, repo, user, title, body, targetBranch, patch, "", "", nil, nil, isStacked) 1131 + s.createPullRequest(w, r, repo, userDid, title, body, targetBranch, patch, "", "", nil, nil, isStacked) 1131 1132 } 1132 1133 1133 - 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) { 1134 + 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) { 1134 1135 repoString := strings.SplitN(forkRepo, "/", 2) 1135 1136 forkOwnerDid := repoString[0] 1136 1137 repoName := repoString[1] ··· 1232 1233 Sha: sourceRev, 1233 1234 } 1234 1235 1235 - s.createPullRequest(w, r, repo, user, title, body, targetBranch, patch, combined, sourceRev, pullSource, recordPullSource, isStacked) 1236 + s.createPullRequest(w, r, repo, userDid, title, body, targetBranch, patch, combined, sourceRev, pullSource, recordPullSource, isStacked) 1236 1237 } 1237 1238 1238 1239 func (s *Pulls) createPullRequest( 1239 1240 w http.ResponseWriter, 1240 1241 r *http.Request, 1241 1242 repo *models.Repo, 1242 - user *oauth.MultiAccountUser, 1243 + userDid syntax.DID, 1243 1244 title, body, targetBranch string, 1244 1245 patch string, 1245 1246 combined string, ··· 1254 1255 w, 1255 1256 r, 1256 1257 repo, 1257 - user, 1258 + userDid, 1258 1259 targetBranch, 1259 1260 patch, 1260 1261 sourceRev, ··· 1311 1312 Title: title, 1312 1313 Body: body, 1313 1314 TargetBranch: targetBranch, 1314 - OwnerDid: user.Active.Did, 1315 + OwnerDid: userDid.String(), 1315 1316 RepoAt: repo.RepoAt(), 1316 1317 Rkey: rkey, 1317 1318 Mentions: mentions, ··· 1343 1344 1344 1345 _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 1345 1346 Collection: tangled.RepoPullNSID, 1346 - Repo: user.Active.Did, 1347 + Repo: userDid.String(), 1347 1348 Rkey: rkey, 1348 1349 Record: &lexutil.LexiconTypeDecoder{ 1349 1350 Val: &tangled.RepoPull{ ··· 1380 1381 w http.ResponseWriter, 1381 1382 r *http.Request, 1382 1383 repo *models.Repo, 1383 - user *oauth.MultiAccountUser, 1384 + userDid syntax.DID, 1384 1385 targetBranch string, 1385 1386 patch string, 1386 1387 sourceRev string, ··· 1411 1412 1412 1413 // build a stack out of this patch 1413 1414 stackId := uuid.New() 1414 - stack, err := s.newStack(r.Context(), repo, user, targetBranch, patch, pullSource, stackId.String()) 1415 + stack, err := s.newStack(r.Context(), repo, userDid, targetBranch, patch, pullSource, stackId.String()) 1415 1416 if err != nil { 1416 1417 log.Println("failed to create stack", err) 1417 1418 s.pages.Notice(w, "pull", fmt.Sprintf("Failed to create stack: %v", err)) ··· 1448 1449 }) 1449 1450 } 1450 1451 _, err = comatproto.RepoApplyWrites(r.Context(), client, &comatproto.RepoApplyWrites_Input{ 1451 - Repo: user.Active.Did, 1452 + Repo: userDid.String(), 1452 1453 Writes: writes, 1453 1454 }) 1454 1455 if err != nil { ··· 1732 1733 return 1733 1734 } 1734 1735 1736 + if user == nil || user.Active.Did != pull.OwnerDid { 1737 + log.Println("unauthorized user") 1738 + w.WriteHeader(http.StatusUnauthorized) 1739 + return 1740 + } 1741 + 1735 1742 f, err := s.repoResolver.Resolve(r) 1736 1743 if err != nil { 1737 1744 log.Println("failed to get repo and knot", err) 1738 1745 return 1739 1746 } 1740 1747 1741 - if user.Active.Did != pull.OwnerDid { 1742 - log.Println("unauthorized user") 1743 - w.WriteHeader(http.StatusUnauthorized) 1744 - return 1745 - } 1746 - 1747 1748 patch := r.FormValue("patch") 1748 1749 1749 - s.resubmitPullHelper(w, r, f, user, pull, patch, "", "") 1750 + s.resubmitPullHelper(w, r, f, syntax.DID(user.Active.Did), pull, patch, "", "") 1750 1751 } 1751 1752 1752 1753 func (s *Pulls) resubmitBranch(w http.ResponseWriter, r *http.Request) { ··· 1759 1760 return 1760 1761 } 1761 1762 1762 - f, err := s.repoResolver.Resolve(r) 1763 - if err != nil { 1764 - log.Println("failed to get repo and knot", err) 1763 + if user == nil || user.Active.Did != pull.OwnerDid { 1764 + log.Println("unauthorized user") 1765 + w.WriteHeader(http.StatusUnauthorized) 1765 1766 return 1766 1767 } 1767 1768 1768 - if user.Active.Did != pull.OwnerDid { 1769 - log.Println("unauthorized user") 1770 - w.WriteHeader(http.StatusUnauthorized) 1769 + f, err := s.repoResolver.Resolve(r) 1770 + if err != nil { 1771 + log.Println("failed to get repo and knot", err) 1771 1772 return 1772 1773 } 1773 1774 ··· 1811 1812 patch := comparison.FormatPatchRaw 1812 1813 combined := comparison.CombinedPatchRaw 1813 1814 1814 - s.resubmitPullHelper(w, r, f, user, pull, patch, combined, sourceRev) 1815 + s.resubmitPullHelper(w, r, f, syntax.DID(user.Active.Did), pull, patch, combined, sourceRev) 1815 1816 } 1816 1817 1817 1818 func (s *Pulls) resubmitFork(w http.ResponseWriter, r *http.Request) { ··· 1824 1825 return 1825 1826 } 1826 1827 1828 + if user == nil || user.Active.Did != pull.OwnerDid { 1829 + log.Println("unauthorized user") 1830 + w.WriteHeader(http.StatusUnauthorized) 1831 + return 1832 + } 1833 + 1827 1834 f, err := s.repoResolver.Resolve(r) 1828 1835 if err != nil { 1829 1836 log.Println("failed to get repo and knot", err) 1830 1837 return 1831 1838 } 1832 1839 1833 - if user.Active.Did != pull.OwnerDid { 1834 - log.Println("unauthorized user") 1835 - w.WriteHeader(http.StatusUnauthorized) 1836 - return 1837 - } 1838 - 1839 1840 forkRepo, err := db.GetRepoByAtUri(s.db, pull.PullSource.RepoAt.String()) 1840 1841 if err != nil { 1841 1842 log.Println("failed to get source repo", err) ··· 1908 1909 patch := comparison.FormatPatchRaw 1909 1910 combined := comparison.CombinedPatchRaw 1910 1911 1911 - s.resubmitPullHelper(w, r, f, user, pull, patch, combined, sourceRev) 1912 + s.resubmitPullHelper(w, r, f, syntax.DID(user.Active.Did), pull, patch, combined, sourceRev) 1912 1913 } 1913 1914 1914 1915 func (s *Pulls) resubmitPullHelper( 1915 1916 w http.ResponseWriter, 1916 1917 r *http.Request, 1917 1918 repo *models.Repo, 1918 - user *oauth.MultiAccountUser, 1919 + userDid syntax.DID, 1919 1920 pull *models.Pull, 1920 1921 patch string, 1921 1922 combined string, ··· 1923 1924 ) { 1924 1925 if pull.IsStacked() { 1925 1926 log.Println("resubmitting stacked PR") 1926 - s.resubmitStackedPullHelper(w, r, repo, user, pull, patch, pull.StackId) 1927 + s.resubmitStackedPullHelper(w, r, repo, userDid, pull, patch, pull.StackId) 1927 1928 return 1928 1929 } 1929 1930 ··· 1971 1972 return 1972 1973 } 1973 1974 1974 - ex, err := comatproto.RepoGetRecord(r.Context(), client, "", tangled.RepoPullNSID, user.Active.Did, pull.Rkey) 1975 + ex, err := comatproto.RepoGetRecord(r.Context(), client, "", tangled.RepoPullNSID, userDid.String(), pull.Rkey) 1975 1976 if err != nil { 1976 1977 // failed to get record 1977 1978 s.pages.Notice(w, "resubmit-error", "Failed to update pull, no record found on PDS.") ··· 1994 1995 1995 1996 _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 1996 1997 Collection: tangled.RepoPullNSID, 1997 - Repo: user.Active.Did, 1998 + Repo: userDid.String(), 1998 1999 Rkey: pull.Rkey, 1999 2000 SwapRecord: ex.Cid, 2000 2001 Record: &lexutil.LexiconTypeDecoder{ ··· 2021 2022 w http.ResponseWriter, 2022 2023 r *http.Request, 2023 2024 repo *models.Repo, 2024 - user *oauth.MultiAccountUser, 2025 + userDid syntax.DID, 2025 2026 pull *models.Pull, 2026 2027 patch string, 2027 2028 stackId string, ··· 2029 2030 targetBranch := pull.TargetBranch 2030 2031 2031 2032 origStack, _ := r.Context().Value("stack").(models.Stack) 2032 - newStack, err := s.newStack(r.Context(), repo, user, targetBranch, patch, pull.PullSource, stackId) 2033 + newStack, err := s.newStack(r.Context(), repo, userDid, targetBranch, patch, pull.PullSource, stackId) 2033 2034 if err != nil { 2034 2035 log.Println("failed to create resubmitted stack", err) 2035 2036 s.pages.Notice(w, "pull-merge-error", "Failed to merge pull request. Try again later.") ··· 2211 2212 } 2212 2213 2213 2214 _, err = comatproto.RepoApplyWrites(r.Context(), client, &comatproto.RepoApplyWrites_Input{ 2214 - Repo: user.Active.Did, 2215 + Repo: userDid.String(), 2215 2216 Writes: writes, 2216 2217 }) 2217 2218 if err != nil { ··· 2490 2491 s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d", ownerSlashRepo, pull.PullId)) 2491 2492 } 2492 2493 2493 - func (s *Pulls) newStack(ctx context.Context, repo *models.Repo, user *oauth.MultiAccountUser, targetBranch, patch string, pullSource *models.PullSource, stackId string) (models.Stack, error) { 2494 + func (s *Pulls) newStack(ctx context.Context, repo *models.Repo, userDid syntax.DID, targetBranch, patch string, pullSource *models.PullSource, stackId string) (models.Stack, error) { 2494 2495 formatPatches, err := patchutil.ExtractPatches(patch) 2495 2496 if err != nil { 2496 2497 return nil, fmt.Errorf("Failed to extract patches: %v", err) ··· 2526 2527 Title: title, 2527 2528 Body: body, 2528 2529 TargetBranch: targetBranch, 2529 - OwnerDid: user.Active.Did, 2530 + OwnerDid: userDid.String(), 2530 2531 RepoAt: repo.RepoAt(), 2531 2532 Rkey: rkey, 2532 2533 Mentions: mentions,