From bdc09dd473e02325020e5aeb6007e58e8adbf522 Mon Sep 17 00:00:00 2001 From: Matthew Watson Date: Mon, 11 Feb 2019 18:59:14 +0000 Subject: [PATCH 1/2] Fix for #25 - Support branches with '/' in them Use the branches API to figure out all the branches present in the repo and then split the path in to branch and fileName. The contents API needs the branchname in the 'ref' field and the commits api uses it in the 'sha' field. --- src/app-helpers.js | 7 +++---- src/app.js | 4 ++-- src/github.js | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/app-helpers.js b/src/app-helpers.js index 8531de8..ab9b488 100644 --- a/src/app-helpers.js +++ b/src/app-helpers.js @@ -127,8 +127,8 @@ export function useLanguageLoader(path) { }, [path]); } -export function useCommitsFetcher({ repo, sha, path }) { - return useLoader(async () => getCommits(repo, sha, path), [repo, sha, path]); +export function useCommitsFetcher({ repo, path }) { + return useLoader(async () => getCommits(repo, path), [repo, path]); } export function useDocumentTitle(title) { @@ -143,7 +143,6 @@ export function getUrlParams() { owner, reponame, action, - sha, ...paths ] = window.location.pathname.split("/"); @@ -151,7 +150,7 @@ export function getUrlParams() { return []; } - return [owner + "/" + reponame, sha, "/" + paths.join("/")]; + return [owner + "/" + reponame, "/" + paths.join("/")]; } function login() { diff --git a/src/app.js b/src/app.js index 3407bf9..78d6984 100644 --- a/src/app.js +++ b/src/app.js @@ -17,12 +17,12 @@ export default function App() { return ; } - const [repo, sha, path] = getUrlParams(); + const [repo, path] = getUrlParams(); if (!repo) { return ; } else { - return ; + return ; } } diff --git a/src/github.js b/src/github.js index ba4f486..f3f7ada 100644 --- a/src/github.js +++ b/src/github.js @@ -25,9 +25,35 @@ async function getContent(repo, sha, path) { return { content, url: contentJson.html_url }; } -export async function getCommits(repo, sha, path, top = 10) { +async function splitBranchAndFilePath(repo, path) { + const branchesResponse = await fetch( + `https://api.github.com/repos/${repo}/branches`, + { headers: getHeaders() } + ); + + const branchesJson = await branchesResponse.json() + const branchNames = branchesJson.map(branch => branch.name).sort(function(a, b) { + // Sort by length so that the longest string is always tried first. + // That way we won't remove any substrings by accident. + return b.length - a.length; + }) + + let branchName = "master"; + let filePath = path + branchNames.forEach(branch => { + if(path.startsWith("/" + branch)) { + branchName = branch + filePath = path.replace("/" + branch + "/", "") + } + }) + return {branchName, filePath} +} + +export async function getCommits(repo, path, top = 10) { + const {branchName, filePath} = await splitBranchAndFilePath(repo, path) + const commitsResponse = await fetch( - `https://api.github.com/repos/${repo}/commits?sha=${sha}&path=${path}`, + `https://api.github.com/repos/${repo}/commits?sha=${branchName}&path=${filePath}`, { headers: getHeaders() } ); if (!commitsResponse.ok) { @@ -35,6 +61,7 @@ export async function getCommits(repo, sha, path, top = 10) { } const commitsJson = await commitsResponse.json(); + const commits = commitsJson .slice(0, top) .map(commit => ({ @@ -55,7 +82,7 @@ export async function getCommits(repo, sha, path, top = 10) { await Promise.all( commits.map(async commit => { - const info = await getContent(repo, commit.sha, path); + const info = await getContent(repo, commit.sha, filePath); commit.content = info.content; commit.fileUrl = info.url; }) From fad87ad1afee01ac7836e682c624eecafbffa64b Mon Sep 17 00:00:00 2001 From: Matthew Watson Date: Mon, 11 Feb 2019 19:03:52 +0000 Subject: [PATCH 2/2] Run prettier on files changed during fix for #25 --- src/app-helpers.js | 14 +++++--------- src/github.js | 31 ++++++++++++++++--------------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/app-helpers.js b/src/app-helpers.js index ab9b488..9cebad2 100644 --- a/src/app-helpers.js +++ b/src/app-helpers.js @@ -128,7 +128,7 @@ export function useLanguageLoader(path) { } export function useCommitsFetcher({ repo, path }) { - return useLoader(async () => getCommits(repo, path), [repo, path]); + return useLoader(async () => getCommits(repo, path), [repo, path]); } export function useDocumentTitle(title) { @@ -138,19 +138,15 @@ export function useDocumentTitle(title) { } export function getUrlParams() { - const [ - , - owner, - reponame, - action, - ...paths - ] = window.location.pathname.split("/"); + const [, owner, reponame, action, ...paths] = window.location.pathname.split( + "/" + ); if (action !== "commits" && action !== "blob") { return []; } - return [owner + "/" + reponame, "/" + paths.join("/")]; + return [owner + "/" + reponame, "/" + paths.join("/")]; } function login() { diff --git a/src/github.js b/src/github.js index f3f7ada..2353064 100644 --- a/src/github.js +++ b/src/github.js @@ -31,27 +31,29 @@ async function splitBranchAndFilePath(repo, path) { { headers: getHeaders() } ); - const branchesJson = await branchesResponse.json() - const branchNames = branchesJson.map(branch => branch.name).sort(function(a, b) { - // Sort by length so that the longest string is always tried first. - // That way we won't remove any substrings by accident. - return b.length - a.length; - }) + const branchesJson = await branchesResponse.json(); + const branchNames = branchesJson + .map(branch => branch.name) + .sort(function(a, b) { + // Sort by length so that the longest string is always tried first. + // That way we won't remove any substrings by accident. + return b.length - a.length; + }); let branchName = "master"; - let filePath = path + let filePath = path; branchNames.forEach(branch => { - if(path.startsWith("/" + branch)) { - branchName = branch - filePath = path.replace("/" + branch + "/", "") + if (path.startsWith("/" + branch)) { + branchName = branch; + filePath = path.replace("/" + branch + "/", ""); } - }) - return {branchName, filePath} + }); + return { branchName, filePath }; } export async function getCommits(repo, path, top = 10) { - const {branchName, filePath} = await splitBranchAndFilePath(repo, path) - + const { branchName, filePath } = await splitBranchAndFilePath(repo, path); + const commitsResponse = await fetch( `https://api.github.com/repos/${repo}/commits?sha=${branchName}&path=${filePath}`, { headers: getHeaders() } @@ -61,7 +63,6 @@ export async function getCommits(repo, path, top = 10) { } const commitsJson = await commitsResponse.json(); - const commits = commitsJson .slice(0, top) .map(commit => ({