This repository has been archived by the owner on Mar 12, 2022. It is now read-only.
/
github.go
255 lines (223 loc) · 7.63 KB
/
github.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2015 Unknwon
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package doc
import (
"bytes"
"errors"
"fmt"
"net/http"
"path"
"regexp"
"strings"
"time"
"github.com/unknwon/com"
log "gopkg.in/clog.v1"
"github.com/unknwon/gowalker/internal/base"
"github.com/unknwon/gowalker/internal/db"
"github.com/unknwon/gowalker/internal/httplib"
"github.com/unknwon/gowalker/internal/setting"
)
var (
githubRawHeader = http.Header{"Accept": {"application/vnd.github-blob.raw"}}
githubRevisionPattern = regexp.MustCompile(`value="[a-z0-9A-Z]+"`)
githubPattern = regexp.MustCompile(`^github\.com/(?P<owner>[a-z0-9A-Z_.\-]+)/(?P<repo>[a-z0-9A-Z_.\-]+)(?P<dir>/[a-z0-9A-Z_.\-/]*)?$`)
)
func getGithubRevision(importPath, tag string) (string, error) {
data, err := com.HttpGetBytes(Client, fmt.Sprintf("https://%s/commits/%s", importPath, tag), nil)
if err != nil {
return "", fmt.Errorf("fetch commits page for %q: %v", importPath, err)
}
i := bytes.Index(data, []byte(`class="BtnGroup"`))
if i == -1 {
return "", fmt.Errorf("find locater in page for %q", importPath)
}
data = data[i+1:]
m := githubRevisionPattern.FindSubmatch(data)
if m == nil {
return "", fmt.Errorf("find revision in page for %q", importPath)
}
return strings.TrimSuffix(strings.TrimPrefix(string(m[0]), `value="`), `"`), nil
}
type RepoInfo struct {
DefaultBranch string `json:"default_branch"`
Fork bool `json:"fork"`
Parent struct {
FullName string `json:"full_name"`
} `json:"parent"`
}
type RepoCommit struct {
Commit struct {
Committer struct {
Date time.Time `json:"date"`
} `json:"committer"`
} `json:"commit"`
}
func getGitHubDoc(match map[string]string, etag string) (*Package, error) {
httpGet := func(url string, v interface{}) error {
return httplib.Get(url).
SetBasicAuth(setting.GitHub.ClientID, setting.GitHub.ClientSecret).
ToJson(v)
}
repoInfo := new(RepoInfo)
err := httpGet(com.Expand("https://api.github.com/repos/{owner}/{repo}", match), repoInfo)
if err != nil {
return nil, fmt.Errorf("get repo default branch: %v", err)
}
// Set default branch if not presented.
if len(match["tag"]) == 0 {
match["tag"] = repoInfo.DefaultBranch
}
// Check if last commit time is behind upstream for fork repository.
if repoInfo.Fork {
url := com.Expand("https://api.github.com/repos/{owner}/{repo}/commits?per_page=1&{cred}", match)
forkCommits := make([]*RepoCommit, 0, 1)
if err := httpGet(url, &forkCommits); err != nil {
return nil, fmt.Errorf("get fork repository commits: %v", err)
}
if len(forkCommits) == 0 {
return nil, fmt.Errorf("unexpected zero number of fork repository commits: %s", url)
}
match["parent"] = repoInfo.Parent.FullName
url = com.Expand("https://api.github.com/repos/{parent}/commits?per_page=1&{cred}", match)
parentCommits := make([]*RepoCommit, 0, 1)
if err := httpGet(url, &parentCommits); err != nil {
return nil, fmt.Errorf("get parent repository commits: %v", err)
}
if len(parentCommits) == 0 {
return nil, fmt.Errorf("unexpected zero number of parent repository commits: %s", url)
}
if !forkCommits[0].Commit.Committer.Date.After(parentCommits[0].Commit.Committer.Date) {
return nil, fmt.Errorf("commits of this fork repository are behind or equal to its parent: %s", repoInfo.Parent.FullName)
}
}
// Check revision.
var commit string
if strings.HasPrefix(match["importPath"], "gopkg.in") {
// FIXME: get commit ID of gopkg.in indepdently.
var obj struct {
Sha string `json:"sha"`
}
if err := com.HttpGetJSON(Client,
com.Expand("https://gopm.io/api/v1/revision?pkgname={importPath}", match), &obj); err != nil {
return nil, fmt.Errorf("get gopkg.in revision: %v", err)
}
commit = obj.Sha
match["tag"] = commit
log.Trace("Import path %q found commit: %s", match["importPath"], commit)
} else {
commit, err = getGithubRevision(com.Expand("github.com/{owner}/{repo}", match), match["tag"])
if err != nil {
return nil, fmt.Errorf("get revision: %v", err)
}
if commit == etag {
return nil, ErrPackageNotModified
}
}
// Get files.
var tree struct {
Tree []struct {
Url string
Path string
Type string
}
Url string
}
if err := httpGet(com.Expand("https://api.github.com/repos/{owner}/{repo}/git/trees/{tag}?recursive=1", match), &tree); err != nil {
return nil, fmt.Errorf("get tree: %v", err)
}
// Because Github API URLs are case-insensitive, we need to check that the
// userRepo returned from Github matches the one that we are requesting.
if !strings.HasPrefix(tree.Url, com.Expand("https://api.github.com/repos/{owner}/{repo}/", match)) {
return nil, errors.New("GitHub import path has incorrect case")
}
// Get source file data and subdirectories.
dirPrefix := match["dir"]
if dirPrefix != "" {
dirPrefix = dirPrefix[1:] + "/"
}
dirLevel := len(strings.Split(dirPrefix, "/"))
dirLength := len(dirPrefix)
dirMap := make(map[string]bool)
files := make([]com.RawFile, 0, 10)
for _, node := range tree.Tree {
// Skip directories and files in wrong directories, get them later.
if node.Type != "blob" || !strings.HasPrefix(node.Path, dirPrefix) {
continue
}
// Get files and check if directories have acceptable files.
if d, f := path.Split(node.Path); base.IsDocFile(f) {
// Check if file is in the directory that is corresponding to import path.
if d == dirPrefix {
files = append(files, &Source{
SrcName: f,
BrowseUrl: com.Expand("github.com/{owner}/{repo}/blob/{tag}/{0}", match, node.Path),
RawSrcUrl: com.Expand("https://raw.github.com/{owner}/{repo}/{tag}/{0}", match, node.Path),
})
continue
}
// Otherwise, check if it's a direct sub-directory of import path.
if len(strings.Split(d, "/"))-dirLevel == 1 {
dirMap[d[dirLength:len(d)-1]] = true
continue
}
}
}
dirs := base.MapToSortedStrings(dirMap)
if len(files) == 0 && len(dirs) == 0 {
return nil, ErrPackageNoGoFile
} else if err := com.FetchFiles(Client, files, githubRawHeader); err != nil {
return nil, fmt.Errorf("fetch files: %v", err)
}
// Start generating data.
// IsGoSubrepo check has been placed to crawl.getDynamic.
w := &Walker{
LineFmt: "#L%d",
Pdoc: &Package{
PkgInfo: &db.PkgInfo{
ImportPath: match["importPath"],
ProjectPath: com.Expand("github.com/{owner}/{repo}", match),
ViewDirPath: com.Expand("github.com/{owner}/{repo}/tree/{tag}/{importPath}", match),
Etag: commit,
Subdirs: strings.Join(dirs, "|"),
},
},
}
srcs := make([]*Source, 0, len(files))
srcMap := make(map[string]*Source)
for _, f := range files {
s, _ := f.(*Source)
srcs = append(srcs, s)
if !strings.HasSuffix(f.Name(), "_test.go") {
srcMap[f.Name()] = s
}
}
pdoc, err := w.Build(&WalkRes{
WalkDepth: WD_All,
WalkType: WT_Memory,
WalkMode: WM_All,
Srcs: srcs,
})
if err != nil {
return nil, fmt.Errorf("error walking package: %v", err)
}
// Get stars.
var repoTree struct {
Stars int64 `json:"watchers"`
}
if err := httpGet(com.Expand("https://api.github.com/repos/{owner}/{repo}", match), &repoTree); err != nil {
return nil, fmt.Errorf("get repoTree: %v", err)
}
pdoc.Stars = repoTree.Stars
return pdoc, nil
}