1
1
import netlify from "netlify-auth-providers" ;
2
2
import React from "react" ;
3
- const TOKEN_KEY = "bitbucket-token" ;
4
3
5
- function getHeaders ( ) {
6
- const token = window . localStorage . getItem ( TOKEN_KEY ) ;
7
- return token ? { Authorization : `Bearer ${ token } ` } : { } ;
8
- }
4
+ import versioner from "./versioner" ;
5
+ import { SOURCE } from "./sources" ;
6
+
7
+ const TOKEN_KEY = "bitbucket-token" ;
9
8
10
9
function isLoggedIn ( ) {
11
10
return ! ! window . localStorage . getItem ( TOKEN_KEY ) ;
12
11
}
13
12
14
- async function getContent ( repo , sha , path ) {
15
- const contentResponse = await fetch (
16
- `https://api.bitbucket.org/2.0/repositories/${ repo } /src/${ sha } /${ path } ` ,
17
- { headers : getHeaders ( ) }
18
- ) ;
19
-
20
- if ( contentResponse . status === 404 ) {
21
- return { content : "" } ;
22
- }
23
-
24
- if ( ! contentResponse . ok ) {
25
- throw contentResponse ;
26
- }
27
-
28
- const content = await contentResponse . text ( ) ;
29
-
30
- return { content } ;
31
- }
32
-
33
13
function getUrlParams ( ) {
34
14
const [ , owner , reponame , , sha , ...paths ] = window . location . pathname . split (
35
15
"/"
@@ -52,52 +32,6 @@ function showLanding() {
52
32
return ! repo ;
53
33
}
54
34
55
- const cache = { } ;
56
-
57
- async function getCommits ( path , last ) {
58
- const [ repo , sha ] = getUrlParams ( ) ;
59
-
60
- if ( ! cache [ path ] ) {
61
- let fields =
62
- "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" ;
63
- // fields = "*.*.*.*.*";
64
- const commitsResponse = await fetch (
65
- `https://api.bitbucket.org/2.0/repositories/${ repo } /filehistory/${ sha } /${ path } ?fields=${ fields } ` ,
66
- { headers : getHeaders ( ) }
67
- ) ;
68
- if ( ! commitsResponse . ok ) {
69
- throw commitsResponse ;
70
- }
71
- const commitsJson = await commitsResponse . json ( ) ;
72
-
73
- cache [ path ] = commitsJson . values . map ( ( { commit } ) => ( {
74
- sha : commit . hash ,
75
- date : new Date ( commit . date ) ,
76
- author : {
77
- login : commit . author . user
78
- ? commit . author . user . nickname
79
- : commit . author . raw ,
80
- avatar : commit . author . user && commit . author . user . links . avatar . href
81
- } ,
82
- commitUrl : commit . links . html . href ,
83
- message : commit . message
84
- } ) ) ;
85
- }
86
-
87
- const commits = cache [ path ] . slice ( 0 , last ) ;
88
-
89
- await Promise . all (
90
- commits . map ( async commit => {
91
- if ( ! commit . content ) {
92
- const info = await getContent ( repo , commit . sha , path ) ;
93
- commit . content = info . content ;
94
- }
95
- } )
96
- ) ;
97
-
98
- return commits ;
99
- }
100
-
101
35
function logIn ( ) {
102
36
// return new Promise((resolve, reject) => {
103
37
var authenticator = new netlify ( {
@@ -125,10 +59,21 @@ function LogInButton() {
125
59
) ;
126
60
}
127
61
62
+ function getParams ( ) {
63
+ const [ repo , sha , path ] = getUrlParams ( ) ;
64
+ const token = window . localStorage . getItem ( TOKEN_KEY ) ;
65
+ return { repo, sha, path, token } ;
66
+ }
67
+
68
+ async function getVersions ( last ) {
69
+ const params = { ...getParams ( ) , last } ;
70
+ return await versioner . getVersions ( SOURCE . BITBUCKET , params ) ;
71
+ }
72
+
128
73
export default {
129
74
showLanding,
130
75
getPath,
131
- getCommits ,
76
+ getVersions ,
132
77
logIn,
133
78
isLoggedIn,
134
79
LogInButton
0 commit comments