From 6c1cf78dc2a903cd453a87b470eb52438e78d743 Mon Sep 17 00:00:00 2001 From: Amelia Wattenberger Date: Tue, 6 Dec 2022 12:46:22 -0500 Subject: [PATCH 1/5] fix: fetch full list of paginated branches --- components/repo-detail/index.tsx | 3 ++- ghapi/index.ts | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/components/repo-detail/index.tsx b/components/repo-detail/index.tsx index ad4f4817..8ad2c65d 100644 --- a/components/repo-detail/index.tsx +++ b/components/repo-detail/index.tsx @@ -344,7 +344,8 @@ export function RepoDetail() { path = branchPathString.slice(branch.name.length + 1); } else { branchName = repoInfo.data.default_branch; - path = ""; + // something's funky here, let's assume the first part of the path is a branch + path = branchPathString.slice(branchName.length + 1); } } diff --git a/ghapi/index.ts b/ghapi/index.ts index a270cda7..4304a2ea 100644 --- a/ghapi/index.ts +++ b/ghapi/index.ts @@ -466,11 +466,19 @@ export const getBranches: QueryFunction< let params = ctx.queryKey[1]; const { owner, repo } = params; let meta = ctx.meta as BlocksQueryMeta; - - const url = `repos/${owner}/${repo}/branches`; - - const res = await meta.ghapi(url); - return res.data; + const getBranchesPage = async (page: number) => { + const url = `repos/${owner}/${repo}/branches?page=${page}`; + const branchesRes = await meta.ghapi(url); + let branches = branchesRes.data; + const link = branchesRes.headers.link || ""; + const nextPage = link.match(/page=(\d+)>; rel="next"/)?.[1]; + if (nextPage) { + const nextBranches = await getBranchesPage(parseInt(nextPage)); + branches = [...branches, ...nextBranches]; + } + return branches; + }; + return getBranchesPage(1); }; export interface CreateBranchParams { From c7a5dbda4cf7dc98eb03fb3127fdeddd2f9c331b Mon Sep 17 00:00:00 2001 From: Amelia Wattenberger Date: Tue, 6 Dec 2022 12:57:35 -0500 Subject: [PATCH 2/5] fix: reroute unfound branch names to default --- components/repo-detail/index.tsx | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/components/repo-detail/index.tsx b/components/repo-detail/index.tsx index 8ad2c65d..2ebdf406 100644 --- a/components/repo-detail/index.tsx +++ b/components/repo-detail/index.tsx @@ -343,14 +343,23 @@ export function RepoDetail() { branchName = branch.name; path = branchPathString.slice(branch.name.length + 1); } else { + // branch not found, let's switch to the default branch branchName = repoInfo.data.default_branch; - // something's funky here, let's assume the first part of the path is a branch - path = branchPathString.slice(branchName.length + 1); + // let's assume the unfound branch name doesn't contain `/` + path = branchPath.slice(1).join("/"); + router.push({ + pathname: router.pathname, + query: { + ...router.query, + branchPath: makeBranchPath(branchName, path), + }, + }); } } useEffect(() => { if (branches.data) { + console.log(branchName); const currentBranchPath = makeBranchPath(branchName, path); if (branchPath.join("/") !== currentBranchPath.join("/")) { const query = { From 3c6ea0389924699c74e2c2c24069b15935bbc68b Mon Sep 17 00:00:00 2001 From: Amelia Wattenberger Date: Tue, 6 Dec 2022 12:59:50 -0500 Subject: [PATCH 3/5] kill log --- components/repo-detail/index.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/components/repo-detail/index.tsx b/components/repo-detail/index.tsx index 2ebdf406..a9483236 100644 --- a/components/repo-detail/index.tsx +++ b/components/repo-detail/index.tsx @@ -359,7 +359,6 @@ export function RepoDetail() { useEffect(() => { if (branches.data) { - console.log(branchName); const currentBranchPath = makeBranchPath(branchName, path); if (branchPath.join("/") !== currentBranchPath.join("/")) { const query = { From bb09a91fe70502e896ac7a522c1779ba7edf2a0e Mon Sep 17 00:00:00 2001 From: Amelia Wattenberger Date: Tue, 6 Dec 2022 13:04:44 -0500 Subject: [PATCH 4/5] use octokit in getBranches --- ghapi/index.ts | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/ghapi/index.ts b/ghapi/index.ts index 4304a2ea..e8b1b8a7 100644 --- a/ghapi/index.ts +++ b/ghapi/index.ts @@ -465,20 +465,16 @@ export const getBranches: QueryFunction< > = async (ctx) => { let params = ctx.queryKey[1]; const { owner, repo } = params; - let meta = ctx.meta as BlocksQueryMeta; - const getBranchesPage = async (page: number) => { - const url = `repos/${owner}/${repo}/branches?page=${page}`; - const branchesRes = await meta.ghapi(url); - let branches = branchesRes.data; - const link = branchesRes.headers.link || ""; - const nextPage = link.match(/page=(\d+)>; rel="next"/)?.[1]; - if (nextPage) { - const nextBranches = await getBranchesPage(parseInt(nextPage)); - branches = [...branches, ...nextBranches]; + let octokit = ctx.meta.octokit as Octokit; + const branchesRes = await octokit.paginate( + "GET /repos/{owner}/{repo}/branches", + { + owner, + repo, + per_page: 100, } - return branches; - }; - return getBranchesPage(1); + ); + return branchesRes; }; export interface CreateBranchParams { From 0f234745941ed55e5ae3723b48da37e8a19e41b8 Mon Sep 17 00:00:00 2001 From: Amelia Wattenberger Date: Tue, 6 Dec 2022 13:29:11 -0500 Subject: [PATCH 5/5] remove redundant redirect when branch name isn't found --- components/repo-detail/index.tsx | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/components/repo-detail/index.tsx b/components/repo-detail/index.tsx index a9483236..537fd409 100644 --- a/components/repo-detail/index.tsx +++ b/components/repo-detail/index.tsx @@ -343,17 +343,10 @@ export function RepoDetail() { branchName = branch.name; path = branchPathString.slice(branch.name.length + 1); } else { - // branch not found, let's switch to the default branch + // let's switch to the default branch and clear the path, + // since we don't know how to parse the path segments branchName = repoInfo.data.default_branch; - // let's assume the unfound branch name doesn't contain `/` - path = branchPath.slice(1).join("/"); - router.push({ - pathname: router.pathname, - query: { - ...router.query, - branchPath: makeBranchPath(branchName, path), - }, - }); + path = ""; } }