forked from golang/gddo
/
bitbucket.go
148 lines (124 loc) · 3.66 KB
/
bitbucket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2013 The Go Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd.
package gosrc
import (
"net/http"
"path"
"regexp"
"time"
)
func init() {
addService(&service{
pattern: regexp.MustCompile(`^bitbucket\.org/(?P<owner>[a-z0-9A-Z_.\-]+)/(?P<repo>[a-z0-9A-Z_.\-]+)(?P<dir>/[a-z0-9A-Z_.\-/]*)?$`),
prefix: "bitbucket.org/",
get: getBitbucketDir,
})
}
var bitbucketEtagRe = regexp.MustCompile(`^(hg|git)-`)
type bitbucketRepo struct {
Scm string
CreatedOn string `json:"created_on"`
LastUpdated string `json:"last_updated"`
ForkOf struct {
Scm string
} `json:"fork_of"`
}
func getBitbucketDir(client *http.Client, match map[string]string, savedEtag string) (*Directory, error) {
var repo *bitbucketRepo
c := &httpClient{client: client}
if m := bitbucketEtagRe.FindStringSubmatch(savedEtag); m != nil {
match["vcs"] = m[1]
} else {
repo, err := getBitbucketRepo(c, match)
if err != nil {
return nil, err
}
match["vcs"] = repo.Scm
}
tags := make(map[string]string)
for _, nodeType := range []string{"branches", "tags"} {
var nodes map[string]struct {
Node string
}
if _, err := c.getJSON(expand("https://api.bitbucket.org/1.0/repositories/{owner}/{repo}/{0}", match, nodeType), &nodes); err != nil {
return nil, err
}
for t, n := range nodes {
tags[t] = n.Node
}
}
var err error
match["tag"], match["commit"], err = bestTag(tags, defaultTags[match["vcs"]])
if err != nil {
return nil, err
}
etag := expand("{vcs}-{commit}", match)
if etag == savedEtag {
return nil, ErrNotModified
}
if repo == nil {
repo, err = getBitbucketRepo(c, match)
if err != nil {
return nil, err
}
}
var contents struct {
Directories []string
Files []struct {
Path string
}
}
if _, err := c.getJSON(expand("https://api.bitbucket.org/1.0/repositories/{owner}/{repo}/src/{tag}{dir}/", match), &contents); err != nil {
return nil, err
}
var files []*File
var dataURLs []string
for _, f := range contents.Files {
_, name := path.Split(f.Path)
if isDocFile(name) {
files = append(files, &File{Name: name, BrowseURL: expand("https://bitbucket.org/{owner}/{repo}/src/{tag}/{0}", match, f.Path)})
dataURLs = append(dataURLs, expand("https://api.bitbucket.org/1.0/repositories/{owner}/{repo}/raw/{tag}/{0}", match, f.Path))
}
}
if err := c.getFiles(dataURLs, files); err != nil {
return nil, err
}
return &Directory{
BrowseURL: expand("https://bitbucket.org/{owner}/{repo}/src/{tag}{dir}", match),
Etag: etag,
Files: files,
LineFmt: "%s#cl-%d",
ProjectName: match["repo"],
ProjectRoot: expand("bitbucket.org/{owner}/{repo}", match),
ProjectURL: expand("https://bitbucket.org/{owner}/{repo}/", match),
Subdirectories: contents.Directories,
VCS: match["vcs"],
DeadEndFork: isBitbucketDeadEndFork(repo),
}, nil
}
func getBitbucketRepo(c *httpClient, match map[string]string) (*bitbucketRepo, error) {
var repo bitbucketRepo
if _, err := c.getJSON(expand("https://api.bitbucket.org/1.0/repositories/{owner}/{repo}", match), &repo); err != nil {
return nil, err
}
return &repo, nil
}
func isBitbucketDeadEndFork(repo *bitbucketRepo) bool {
l := "2006-01-02T15:04:05.999999999"
created, err := time.Parse(l, repo.CreatedOn)
if err != nil {
return false
}
updated, err := time.Parse(l, repo.LastUpdated)
if err != nil {
return false
}
isDeadEndFork := false
if repo.ForkOf.Scm != "" && created.Unix() >= updated.Unix() {
isDeadEndFork = true
}
return isDeadEndFork
}