Prepare GitLab integration

This commit is contained in:
Rodrigo Pombo 2019-02-19 17:03:58 -03:00
parent becf99b611
commit 9f8755676d
3 changed files with 135 additions and 2 deletions

View File

@ -48,7 +48,7 @@ export function Error({ error, gitProvider }) {
{gitProvider.isLoggedIn && !gitProvider.isLoggedIn() && (
<React.Fragment>
<p>Is it from a private repo? Sign in with GitHub:</p>
<GitHubButton onClick={gitProvider.login} />
<GitHubButton onClick={gitProvider.logIn} />
</React.Fragment>
)}
</Center>

View File

@ -0,0 +1,127 @@
import netlify from "netlify-auth-providers";
import { Base64 } from "js-base64";
const TOKEN_KEY = "gitlab-token";
function getHeaders() {
const token = window.localStorage.getItem(TOKEN_KEY);
return token ? { "PRIVATE-TOKEN": `${token}` } : {};
}
function isLoggedIn() {
return !!window.localStorage.getItem(TOKEN_KEY);
}
// async function getContent(repo, sha, path) {
// const contentResponse = await fetch(
// `https://gitlab.com/api/v4/projects/${encodeURIComponent(
// repo
// )}/repository/commits?path=${encodeURIComponent(path)}&ref_name=${sha}`,
// { headers: getHeaders() }
// );
// if (!contentResponse.ok) {
// throw contentResponse;
// }
// const contentJson = await contentResponse.json();
// const content = Base64.decode(contentJson.content);
// return { content, url: contentJson.html_url };
// }
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]) {
const commitsResponse = await fetch(
`https://gitlab.com/api/v4/projects/${encodeURIComponent(
"6509971" //TODO fix
)}/repository/commits?path=${encodeURIComponent(path)}&ref_name=${sha}`,
{ headers: getHeaders() }
);
if (!commitsResponse.ok) {
throw commitsResponse;
}
const commitsJson = await commitsResponse.json();
cache[path] = commitsJson.map(commit => ({
sha: commit.id,
date: new Date(commit.authored_date),
author: {
login: commit.author_name
},
// commitUrl: commit.html_url,
message: commit.title,
content: "foo"
}));
}
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;
// commit.fileUrl = info.url;
// }
// })
// );
return commits;
}
function logIn() {
console.log("login");
// return new Promise((resolve, reject) => {
var authenticator = new netlify({
site_id: "ccf3a0e2-ac06-4f37-9b17-df1dd41fb1a6"
});
authenticator.authenticate({ provider: "gitlab", scope: "api" }, function(
err,
data
) {
if (err) {
console.error(err);
return;
}
window.localStorage.setItem(TOKEN_KEY, data.token);
window.location.reload(false);
});
// });
}
export default {
showLanding,
getPath,
getCommits,
logIn,
isLoggedIn
};

View File

@ -1,6 +1,7 @@
import cliProvider from "./cli-provider";
import githubProvider from "./github-provider";
import vscodeProvider from "./vscode-provider";
import gitlabProvider from "./gitlab-provider";
export default function getGitProvider() {
switch (process.env.REACT_APP_GIT_PROVIDER) {
@ -8,7 +9,12 @@ export default function getGitProvider() {
return cliProvider;
case "vscode":
return vscodeProvider;
default:
default: {
const [cloud] = window.location.host.split(".");
if (cloud === "gitlab") {
return gitlabProvider;
}
return githubProvider;
}
}
}