Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #25 - Add support for branches with '/' in them #72

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions src/app-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -138,20 +138,15 @@ export function useDocumentTitle(title) {
}

export function getUrlParams() {
const [
,
owner,
reponame,
action,
sha,
...paths
] = window.location.pathname.split("/");
const [, owner, reponame, action, ...paths] = window.location.pathname.split(
"/"
);

if (action !== "commits" && action !== "blob") {
return [];
}

return [owner + "/" + reponame, sha, "/" + paths.join("/")];
return [owner + "/" + reponame, "/" + paths.join("/")];
}

function login() {
Expand Down
4 changes: 2 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ export default function App() {
return <CliApp data={cli} />;
}

const [repo, sha, path] = getUrlParams();
const [repo, path] = getUrlParams();

if (!repo) {
return <Landing />;
} else {
return <GitHubApp repo={repo} sha={sha} path={path} />;
return <GitHubApp repo={repo} path={path} />;
}
}

Expand Down
34 changes: 31 additions & 3 deletions src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,37 @@ 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) {
Expand Down Expand Up @@ -55,7 +83,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;
})
Expand Down