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