From 356c7269adc6c48c77a5aadec1a33e332619b48e Mon Sep 17 00:00:00 2001 From: Rodrigo Pombo Date: Fri, 22 Feb 2019 16:29:45 -0300 Subject: [PATCH] Add private bitbucket repos --- src/app-helpers.js | 1 + src/git-providers/bitbucket-provider.js | 39 ++++++++++++------------- src/git-providers/providers.js | 3 +- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/app-helpers.js b/src/app-helpers.js index 6832d79..d972037 100644 --- a/src/app-helpers.js +++ b/src/app-helpers.js @@ -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 (

diff --git a/src/git-providers/bitbucket-provider.js b/src/git-providers/bitbucket-provider.js index 2b6eded..b7f253c 100644 --- a/src/git-providers/bitbucket-provider.js +++ b/src/git-providers/bitbucket-provider.js @@ -14,17 +14,20 @@ function isLoggedIn() { async function getContent(repo, sha, path) { const contentResponse = await fetch( - `https://gitlab.com/api/v4/projects/${encodeURIComponent( - repo - )}/repository/files/${encodeURIComponent(path)}?ref=${sha}`, + `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 contentJson = await contentResponse.json(); - const content = Base64.decode(contentJson.content); + + const content = await contentResponse.text(); + // const content = Base64.decode(contentJson.content); return { content }; } @@ -83,21 +86,20 @@ async function getCommits(path, last) { avatar: commit.author.user && commit.author.user.links.avatar.href }, commitUrl: commit.links.html.href, - message: commit.message, - content: "foo" + 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; - // } - // }) - // ); + await Promise.all( + commits.map(async commit => { + if (!commit.content) { + const info = await getContent(repo, commit.sha, path); + commit.content = info.content; + } + }) + ); return commits; } @@ -107,10 +109,7 @@ function logIn() { var authenticator = new netlify({ site_id: "ccf3a0e2-ac06-4f37-9b17-df1dd41fb1a6" }); - authenticator.authenticate({ provider: "gitlab", scope: "api" }, function( - err, - data - ) { + authenticator.authenticate({ provider: "bitbucket" }, function(err, data) { if (err) { console.error(err); return; @@ -127,7 +126,7 @@ function LogInButton() { onClick={logIn} style={{ fontWeight: 600, padding: "0.5em 0.7em", cursor: "pointer" }} > -

Sign in with GitLab
+
Sign in with Bitbucket
); } diff --git a/src/git-providers/providers.js b/src/git-providers/providers.js index 5ed5d78..02e5a4b 100644 --- a/src/git-providers/providers.js +++ b/src/git-providers/providers.js @@ -14,8 +14,9 @@ export default function getGitProvider() { const [cloud] = window.location.host.split("."); if (cloud === "gitlab") { return gitlabProvider; + } else if (cloud === "bitbucket") { + return bitbucketProvider; } - return bitbucketProvider; return githubProvider; } }