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 7f6debe9 00205b2a

verified
+48 -47
+48 -47
appview/pulls/pulls.go
··· 849 fromFork := r.FormValue("fork") 850 sourceBranch := r.FormValue("sourceBranch") 851 patch := r.FormValue("patch") 852 853 if targetBranch == "" { 854 s.pages.Notice(w, "pull", "Target branch is required.") ··· 856 } 857 858 // Determine PR type based on input parameters 859 - roles := repoinfo.RolesInRepo{Roles: s.enforcer.GetPermissionsInRepo(user.Active.Did, f.Knot, f.DidSlashRepo())} 860 isPushAllowed := roles.IsPushAllowed() 861 isBranchBased := isPushAllowed && sourceBranch != "" && fromFork == "" 862 isForkBased := fromFork != "" && sourceBranch != "" ··· 934 s.pages.Notice(w, "pull", "This knot doesn't support branch-based pull requests. Try another way?") 935 return 936 } 937 - s.handleBranchBasedPull(w, r, f, user, title, body, targetBranch, sourceBranch, isStacked) 938 } else if isForkBased { 939 if !caps.PullRequests.ForkSubmissions { 940 s.pages.Notice(w, "pull", "This knot doesn't support fork-based pull requests. Try another way?") 941 return 942 } 943 - s.handleForkBasedPull(w, r, f, user, fromFork, title, body, targetBranch, sourceBranch, isStacked) 944 } else if isPatchBased { 945 if !caps.PullRequests.PatchSubmissions { 946 s.pages.Notice(w, "pull", "This knot doesn't support patch-based pull requests. Send your patch over email.") 947 return 948 } 949 - s.handlePatchBasedPull(w, r, f, user, title, body, targetBranch, patch, isStacked) 950 } 951 return 952 } ··· 956 w http.ResponseWriter, 957 r *http.Request, 958 repo *models.Repo, 959 - user *oauth.MultiAccountUser, 960 title, 961 body, 962 targetBranch, ··· 1010 Sha: comparison.Rev2, 1011 } 1012 1013 - s.createPullRequest(w, r, repo, user, title, body, targetBranch, patch, combined, sourceRev, pullSource, recordPullSource, isStacked) 1014 } 1015 1016 - func (s *Pulls) handlePatchBasedPull(w http.ResponseWriter, r *http.Request, repo *models.Repo, user *oauth.MultiAccountUser, title, body, targetBranch, patch string, isStacked bool) { 1017 if err := s.validator.ValidatePatch(&patch); err != nil { 1018 s.logger.Error("patch validation failed", "err", err) 1019 s.pages.Notice(w, "pull", "Invalid patch format. Please provide a valid diff.") 1020 return 1021 } 1022 1023 - s.createPullRequest(w, r, repo, user, title, body, targetBranch, patch, "", "", nil, nil, isStacked) 1024 } 1025 1026 - 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) { 1027 repoString := strings.SplitN(forkRepo, "/", 2) 1028 forkOwnerDid := repoString[0] 1029 repoName := repoString[1] ··· 1125 Sha: sourceRev, 1126 } 1127 1128 - s.createPullRequest(w, r, repo, user, title, body, targetBranch, patch, combined, sourceRev, pullSource, recordPullSource, isStacked) 1129 } 1130 1131 func (s *Pulls) createPullRequest( 1132 w http.ResponseWriter, 1133 r *http.Request, 1134 repo *models.Repo, 1135 - user *oauth.MultiAccountUser, 1136 title, body, targetBranch string, 1137 patch string, 1138 combined string, ··· 1147 w, 1148 r, 1149 repo, 1150 - user, 1151 targetBranch, 1152 patch, 1153 sourceRev, ··· 1204 Title: title, 1205 Body: body, 1206 TargetBranch: targetBranch, 1207 - OwnerDid: user.Active.Did, 1208 RepoAt: repo.RepoAt(), 1209 Rkey: rkey, 1210 Mentions: mentions, ··· 1236 1237 _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 1238 Collection: tangled.RepoPullNSID, 1239 - Repo: user.Active.Did, 1240 Rkey: rkey, 1241 Record: &lexutil.LexiconTypeDecoder{ 1242 Val: &tangled.RepoPull{ ··· 1273 w http.ResponseWriter, 1274 r *http.Request, 1275 repo *models.Repo, 1276 - user *oauth.MultiAccountUser, 1277 targetBranch string, 1278 patch string, 1279 sourceRev string, ··· 1304 1305 // build a stack out of this patch 1306 stackId := uuid.New() 1307 - stack, err := s.newStack(r.Context(), repo, user, targetBranch, patch, pullSource, stackId.String()) 1308 if err != nil { 1309 log.Println("failed to create stack", err) 1310 s.pages.Notice(w, "pull", fmt.Sprintf("Failed to create stack: %v", err)) ··· 1341 }) 1342 } 1343 _, err = comatproto.RepoApplyWrites(r.Context(), client, &comatproto.RepoApplyWrites_Input{ 1344 - Repo: user.Active.Did, 1345 Writes: writes, 1346 }) 1347 if err != nil { ··· 1625 return 1626 } 1627 1628 f, err := s.repoResolver.Resolve(r) 1629 if err != nil { 1630 log.Println("failed to get repo and knot", err) 1631 return 1632 } 1633 1634 - if user.Active.Did != pull.OwnerDid { 1635 - log.Println("unauthorized user") 1636 - w.WriteHeader(http.StatusUnauthorized) 1637 - return 1638 - } 1639 - 1640 patch := r.FormValue("patch") 1641 1642 - s.resubmitPullHelper(w, r, f, user, pull, patch, "", "") 1643 } 1644 1645 func (s *Pulls) resubmitBranch(w http.ResponseWriter, r *http.Request) { ··· 1652 return 1653 } 1654 1655 - f, err := s.repoResolver.Resolve(r) 1656 - if err != nil { 1657 - log.Println("failed to get repo and knot", err) 1658 return 1659 } 1660 1661 - if user.Active.Did != pull.OwnerDid { 1662 - log.Println("unauthorized user") 1663 - w.WriteHeader(http.StatusUnauthorized) 1664 return 1665 } 1666 ··· 1704 patch := comparison.FormatPatchRaw 1705 combined := comparison.CombinedPatchRaw 1706 1707 - s.resubmitPullHelper(w, r, f, user, pull, patch, combined, sourceRev) 1708 } 1709 1710 func (s *Pulls) resubmitFork(w http.ResponseWriter, r *http.Request) { ··· 1717 return 1718 } 1719 1720 f, err := s.repoResolver.Resolve(r) 1721 if err != nil { 1722 log.Println("failed to get repo and knot", err) 1723 return 1724 } 1725 1726 - if user.Active.Did != pull.OwnerDid { 1727 - log.Println("unauthorized user") 1728 - w.WriteHeader(http.StatusUnauthorized) 1729 - return 1730 - } 1731 - 1732 forkRepo, err := db.GetRepoByAtUri(s.db, pull.PullSource.RepoAt.String()) 1733 if err != nil { 1734 log.Println("failed to get source repo", err) ··· 1801 patch := comparison.FormatPatchRaw 1802 combined := comparison.CombinedPatchRaw 1803 1804 - s.resubmitPullHelper(w, r, f, user, pull, patch, combined, sourceRev) 1805 } 1806 1807 func (s *Pulls) resubmitPullHelper( 1808 w http.ResponseWriter, 1809 r *http.Request, 1810 repo *models.Repo, 1811 - user *oauth.MultiAccountUser, 1812 pull *models.Pull, 1813 patch string, 1814 combined string, ··· 1816 ) { 1817 if pull.IsStacked() { 1818 log.Println("resubmitting stacked PR") 1819 - s.resubmitStackedPullHelper(w, r, repo, user, pull, patch, pull.StackId) 1820 return 1821 } 1822 ··· 1864 return 1865 } 1866 1867 - ex, err := comatproto.RepoGetRecord(r.Context(), client, "", tangled.RepoPullNSID, user.Active.Did, pull.Rkey) 1868 if err != nil { 1869 // failed to get record 1870 s.pages.Notice(w, "resubmit-error", "Failed to update pull, no record found on PDS.") ··· 1884 1885 _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 1886 Collection: tangled.RepoPullNSID, 1887 - Repo: user.Active.Did, 1888 Rkey: pull.Rkey, 1889 SwapRecord: ex.Cid, 1890 Record: &lexutil.LexiconTypeDecoder{ ··· 1911 w http.ResponseWriter, 1912 r *http.Request, 1913 repo *models.Repo, 1914 - user *oauth.MultiAccountUser, 1915 pull *models.Pull, 1916 patch string, 1917 stackId string, ··· 1919 targetBranch := pull.TargetBranch 1920 1921 origStack, _ := r.Context().Value("stack").(models.Stack) 1922 - newStack, err := s.newStack(r.Context(), repo, user, targetBranch, patch, pull.PullSource, stackId) 1923 if err != nil { 1924 log.Println("failed to create resubmitted stack", err) 1925 s.pages.Notice(w, "pull-merge-error", "Failed to merge pull request. Try again later.") ··· 2101 } 2102 2103 _, err = comatproto.RepoApplyWrites(r.Context(), client, &comatproto.RepoApplyWrites_Input{ 2104 - Repo: user.Active.Did, 2105 Writes: writes, 2106 }) 2107 if err != nil { ··· 2380 s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d", ownerSlashRepo, pull.PullId)) 2381 } 2382 2383 - func (s *Pulls) newStack(ctx context.Context, repo *models.Repo, user *oauth.MultiAccountUser, targetBranch, patch string, pullSource *models.PullSource, stackId string) (models.Stack, error) { 2384 formatPatches, err := patchutil.ExtractPatches(patch) 2385 if err != nil { 2386 return nil, fmt.Errorf("Failed to extract patches: %v", err) ··· 2416 Title: title, 2417 Body: body, 2418 TargetBranch: targetBranch, 2419 - OwnerDid: user.Active.Did, 2420 RepoAt: repo.RepoAt(), 2421 Rkey: rkey, 2422 Mentions: mentions,
··· 849 fromFork := r.FormValue("fork") 850 sourceBranch := r.FormValue("sourceBranch") 851 patch := r.FormValue("patch") 852 + userDid := syntax.DID(user.Active.Did) 853 854 if targetBranch == "" { 855 s.pages.Notice(w, "pull", "Target branch is required.") ··· 857 } 858 859 // Determine PR type based on input parameters 860 + roles := repoinfo.RolesInRepo{Roles: s.enforcer.GetPermissionsInRepo(userDid.String(), f.Knot, f.DidSlashRepo())} 861 isPushAllowed := roles.IsPushAllowed() 862 isBranchBased := isPushAllowed && sourceBranch != "" && fromFork == "" 863 isForkBased := fromFork != "" && sourceBranch != "" ··· 935 s.pages.Notice(w, "pull", "This knot doesn't support branch-based pull requests. Try another way?") 936 return 937 } 938 + s.handleBranchBasedPull(w, r, f, userDid, title, body, targetBranch, sourceBranch, isStacked) 939 } else if isForkBased { 940 if !caps.PullRequests.ForkSubmissions { 941 s.pages.Notice(w, "pull", "This knot doesn't support fork-based pull requests. Try another way?") 942 return 943 } 944 + s.handleForkBasedPull(w, r, f, userDid, fromFork, title, body, targetBranch, sourceBranch, isStacked) 945 } else if isPatchBased { 946 if !caps.PullRequests.PatchSubmissions { 947 s.pages.Notice(w, "pull", "This knot doesn't support patch-based pull requests. Send your patch over email.") 948 return 949 } 950 + s.handlePatchBasedPull(w, r, f, userDid, title, body, targetBranch, patch, isStacked) 951 } 952 return 953 } ··· 957 w http.ResponseWriter, 958 r *http.Request, 959 repo *models.Repo, 960 + userDid syntax.DID, 961 title, 962 body, 963 targetBranch, ··· 1011 Sha: comparison.Rev2, 1012 } 1013 1014 + s.createPullRequest(w, r, repo, userDid, title, body, targetBranch, patch, combined, sourceRev, pullSource, recordPullSource, isStacked) 1015 } 1016 1017 + func (s *Pulls) handlePatchBasedPull(w http.ResponseWriter, r *http.Request, repo *models.Repo, userDid syntax.DID, title, body, targetBranch, patch string, isStacked bool) { 1018 if err := s.validator.ValidatePatch(&patch); err != nil { 1019 s.logger.Error("patch validation failed", "err", err) 1020 s.pages.Notice(w, "pull", "Invalid patch format. Please provide a valid diff.") 1021 return 1022 } 1023 1024 + s.createPullRequest(w, r, repo, userDid, title, body, targetBranch, patch, "", "", nil, nil, isStacked) 1025 } 1026 1027 + 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) { 1028 repoString := strings.SplitN(forkRepo, "/", 2) 1029 forkOwnerDid := repoString[0] 1030 repoName := repoString[1] ··· 1126 Sha: sourceRev, 1127 } 1128 1129 + s.createPullRequest(w, r, repo, userDid, title, body, targetBranch, patch, combined, sourceRev, pullSource, recordPullSource, isStacked) 1130 } 1131 1132 func (s *Pulls) createPullRequest( 1133 w http.ResponseWriter, 1134 r *http.Request, 1135 repo *models.Repo, 1136 + userDid syntax.DID, 1137 title, body, targetBranch string, 1138 patch string, 1139 combined string, ··· 1148 w, 1149 r, 1150 repo, 1151 + userDid, 1152 targetBranch, 1153 patch, 1154 sourceRev, ··· 1205 Title: title, 1206 Body: body, 1207 TargetBranch: targetBranch, 1208 + OwnerDid: userDid.String(), 1209 RepoAt: repo.RepoAt(), 1210 Rkey: rkey, 1211 Mentions: mentions, ··· 1237 1238 _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 1239 Collection: tangled.RepoPullNSID, 1240 + Repo: userDid.String(), 1241 Rkey: rkey, 1242 Record: &lexutil.LexiconTypeDecoder{ 1243 Val: &tangled.RepoPull{ ··· 1274 w http.ResponseWriter, 1275 r *http.Request, 1276 repo *models.Repo, 1277 + userDid syntax.DID, 1278 targetBranch string, 1279 patch string, 1280 sourceRev string, ··· 1305 1306 // build a stack out of this patch 1307 stackId := uuid.New() 1308 + stack, err := s.newStack(r.Context(), repo, userDid, targetBranch, patch, pullSource, stackId.String()) 1309 if err != nil { 1310 log.Println("failed to create stack", err) 1311 s.pages.Notice(w, "pull", fmt.Sprintf("Failed to create stack: %v", err)) ··· 1342 }) 1343 } 1344 _, err = comatproto.RepoApplyWrites(r.Context(), client, &comatproto.RepoApplyWrites_Input{ 1345 + Repo: userDid.String(), 1346 Writes: writes, 1347 }) 1348 if err != nil { ··· 1626 return 1627 } 1628 1629 + if user == nil || user.Active.Did != pull.OwnerDid { 1630 + log.Println("unauthorized user") 1631 + w.WriteHeader(http.StatusUnauthorized) 1632 + return 1633 + } 1634 + 1635 f, err := s.repoResolver.Resolve(r) 1636 if err != nil { 1637 log.Println("failed to get repo and knot", err) 1638 return 1639 } 1640 1641 patch := r.FormValue("patch") 1642 1643 + s.resubmitPullHelper(w, r, f, syntax.DID(user.Active.Did), pull, patch, "", "") 1644 } 1645 1646 func (s *Pulls) resubmitBranch(w http.ResponseWriter, r *http.Request) { ··· 1653 return 1654 } 1655 1656 + if user == nil || user.Active.Did != pull.OwnerDid { 1657 + log.Println("unauthorized user") 1658 + w.WriteHeader(http.StatusUnauthorized) 1659 return 1660 } 1661 1662 + f, err := s.repoResolver.Resolve(r) 1663 + if err != nil { 1664 + log.Println("failed to get repo and knot", err) 1665 return 1666 } 1667 ··· 1705 patch := comparison.FormatPatchRaw 1706 combined := comparison.CombinedPatchRaw 1707 1708 + s.resubmitPullHelper(w, r, f, syntax.DID(user.Active.Did), pull, patch, combined, sourceRev) 1709 } 1710 1711 func (s *Pulls) resubmitFork(w http.ResponseWriter, r *http.Request) { ··· 1718 return 1719 } 1720 1721 + if user == nil || user.Active.Did != pull.OwnerDid { 1722 + log.Println("unauthorized user") 1723 + w.WriteHeader(http.StatusUnauthorized) 1724 + return 1725 + } 1726 + 1727 f, err := s.repoResolver.Resolve(r) 1728 if err != nil { 1729 log.Println("failed to get repo and knot", err) 1730 return 1731 } 1732 1733 forkRepo, err := db.GetRepoByAtUri(s.db, pull.PullSource.RepoAt.String()) 1734 if err != nil { 1735 log.Println("failed to get source repo", err) ··· 1802 patch := comparison.FormatPatchRaw 1803 combined := comparison.CombinedPatchRaw 1804 1805 + s.resubmitPullHelper(w, r, f, syntax.DID(user.Active.Did), pull, patch, combined, sourceRev) 1806 } 1807 1808 func (s *Pulls) resubmitPullHelper( 1809 w http.ResponseWriter, 1810 r *http.Request, 1811 repo *models.Repo, 1812 + userDid syntax.DID, 1813 pull *models.Pull, 1814 patch string, 1815 combined string, ··· 1817 ) { 1818 if pull.IsStacked() { 1819 log.Println("resubmitting stacked PR") 1820 + s.resubmitStackedPullHelper(w, r, repo, userDid, pull, patch, pull.StackId) 1821 return 1822 } 1823 ··· 1865 return 1866 } 1867 1868 + ex, err := comatproto.RepoGetRecord(r.Context(), client, "", tangled.RepoPullNSID, userDid.String(), pull.Rkey) 1869 if err != nil { 1870 // failed to get record 1871 s.pages.Notice(w, "resubmit-error", "Failed to update pull, no record found on PDS.") ··· 1885 1886 _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 1887 Collection: tangled.RepoPullNSID, 1888 + Repo: userDid.String(), 1889 Rkey: pull.Rkey, 1890 SwapRecord: ex.Cid, 1891 Record: &lexutil.LexiconTypeDecoder{ ··· 1912 w http.ResponseWriter, 1913 r *http.Request, 1914 repo *models.Repo, 1915 + userDid syntax.DID, 1916 pull *models.Pull, 1917 patch string, 1918 stackId string, ··· 1920 targetBranch := pull.TargetBranch 1921 1922 origStack, _ := r.Context().Value("stack").(models.Stack) 1923 + newStack, err := s.newStack(r.Context(), repo, userDid, targetBranch, patch, pull.PullSource, stackId) 1924 if err != nil { 1925 log.Println("failed to create resubmitted stack", err) 1926 s.pages.Notice(w, "pull-merge-error", "Failed to merge pull request. Try again later.") ··· 2102 } 2103 2104 _, err = comatproto.RepoApplyWrites(r.Context(), client, &comatproto.RepoApplyWrites_Input{ 2105 + Repo: userDid.String(), 2106 Writes: writes, 2107 }) 2108 if err != nil { ··· 2381 s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d", ownerSlashRepo, pull.PullId)) 2382 } 2383 2384 + func (s *Pulls) newStack(ctx context.Context, repo *models.Repo, userDid syntax.DID, targetBranch, patch string, pullSource *models.PullSource, stackId string) (models.Stack, error) { 2385 formatPatches, err := patchutil.ExtractPatches(patch) 2386 if err != nil { 2387 return nil, fmt.Errorf("Failed to extract patches: %v", err) ··· 2417 Title: title, 2418 Body: body, 2419 TargetBranch: targetBranch, 2420 + OwnerDid: userDid.String(), 2421 RepoAt: repo.RepoAt(), 2422 Rkey: rkey, 2423 Mentions: mentions,