Skip to content

Commit

Permalink
Merge pull request pomber#105 from pomber/bitbucket
Browse files Browse the repository at this point in the history
Add Bitbucket support
  • Loading branch information
pomber authored Feb 22, 2019
2 parents f2d6bc4 + 356c726 commit 7bd2a0f
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/app-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function Loading({ repo, path }) {
export function Error({ error, gitProvider }) {
const { LogInButton } = gitProvider;
if (error.status === 403) {
// FIX bitbucket uses 403 for private repos
return (
<Center>
<p>
Expand Down
141 changes: 141 additions & 0 deletions src/git-providers/bitbucket-provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import netlify from "netlify-auth-providers";
import { Base64 } from "js-base64";
import React from "react";
const TOKEN_KEY = "bitbucket-token";

function getHeaders() {
const token = window.localStorage.getItem(TOKEN_KEY);
return token ? { Authorization: `Bearer ${token}` } : {};
}

function isLoggedIn() {
return !!window.localStorage.getItem(TOKEN_KEY);
}

async function getContent(repo, sha, path) {
const contentResponse = await fetch(
`https://api.bitbucket.org/2.0/repositories/${repo}/src/${sha}/${path}`,
{ headers: getHeaders() }
);

if (contentResponse.status === 404) {
return { content: "" };
}

if (!contentResponse.ok) {
throw contentResponse;
}

const content = await contentResponse.text();
// const content = Base64.decode(contentJson.content);
return { content };
}

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

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

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

function getPath() {
const [, , path] = getUrlParams();
return path;
}

function showLanding() {
const [repo, ,] = getUrlParams();
return !repo;
}

const cache = {};

async function getCommits(path, last) {
const [repo, sha] = getUrlParams();

if (!cache[path]) {
let fields =
"values.path,values.commit.date,values.commit.message,values.commit.hash,values.commit.author.*,values.commit.links.html, values.commit.author.user.nickname, values.commit.author.user.links.avatar.href, values.commit.links.html.href";
// fields = "*.*.*.*.*";
const commitsResponse = await fetch(
`https://api.bitbucket.org/2.0/repositories/${repo}/filehistory/${sha}/${path}?fields=${fields}`,
{ headers: getHeaders() }
);
if (!commitsResponse.ok) {
throw commitsResponse;
}
const commitsJson = await commitsResponse.json();

cache[path] = commitsJson.values.map(({ commit }) => ({
sha: commit.hash,
date: new Date(commit.date),
author: {
login: commit.author.user
? commit.author.user.nickname
: commit.author.raw,
avatar: commit.author.user && commit.author.user.links.avatar.href
},
commitUrl: commit.links.html.href,
message: commit.message
}));
}

const commits = cache[path].slice(0, last);

await Promise.all(
commits.map(async commit => {
if (!commit.content) {
const info = await getContent(repo, commit.sha, path);
commit.content = info.content;
}
})
);

return commits;
}

function logIn() {
// return new Promise((resolve, reject) => {
var authenticator = new netlify({
site_id: "ccf3a0e2-ac06-4f37-9b17-df1dd41fb1a6"
});
authenticator.authenticate({ provider: "bitbucket" }, function(err, data) {
if (err) {
console.error(err);
return;
}
window.localStorage.setItem(TOKEN_KEY, data.token);
window.location.reload(false);
});
// });
}

function LogInButton() {
return (
<button
onClick={logIn}
style={{ fontWeight: 600, padding: "0.5em 0.7em", cursor: "pointer" }}
>
<div>Sign in with Bitbucket</div>
</button>
);
}

export default {
showLanding,
getPath,
getCommits,
logIn,
isLoggedIn,
LogInButton
};
3 changes: 3 additions & 0 deletions src/git-providers/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import cliProvider from "./cli-provider";
import githubProvider from "./github-provider";
import vscodeProvider from "./vscode-provider";
import gitlabProvider from "./gitlab-provider";
import bitbucketProvider from "./bitbucket-provider";

export default function getGitProvider() {
switch (process.env.REACT_APP_GIT_PROVIDER) {
Expand All @@ -13,6 +14,8 @@ export default function getGitProvider() {
const [cloud] = window.location.host.split(".");
if (cloud === "gitlab") {
return gitlabProvider;
} else if (cloud === "bitbucket") {
return bitbucketProvider;
}
return githubProvider;
}
Expand Down

0 comments on commit 7bd2a0f

Please sign in to comment.