-
Notifications
You must be signed in to change notification settings - Fork 18
/
github.go
188 lines (152 loc) · 4.21 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
package github
import (
"context"
"encoding/base64"
"errors"
"fmt"
"github.com/cirruslabs/cirrus-cli/pkg/larker/fs"
"github.com/google/go-github/v59/github"
lru "github.com/hashicorp/golang-lru"
"net/http"
"os"
"path"
"syscall"
"time"
)
var ErrAPI = errors.New("failed to communicate with the GitHub API")
var defaultGitHubClient = github.NewClient(&http.Client{
Transport: &http.Transport{
MaxIdleConns: 1024,
MaxIdleConnsPerHost: 1024, // default is 2 which is too small and we mostly access the same host
IdleConnTimeout: time.Minute, // let's put something big but not infinite like the default
},
Timeout: 11 * time.Second, // GitHub has a 10-second timeout for API requests
})
type GitHub struct {
token string
owner string
repo string
reference string
contentsCache *lru.Cache
fileInfosCache *lru.Cache
apiCallCount uint64
}
type Contents struct {
File *github.RepositoryContent
Directory []*github.RepositoryContent
}
func New(owner, repo, reference, token string) (*GitHub, error) {
contentsCache, err := lru.New(16)
if err != nil {
return nil, err
}
fileInfosCache, err := lru.New(1024)
if err != nil {
return nil, err
}
return &GitHub{
token: token,
owner: owner,
repo: repo,
reference: reference,
contentsCache: contentsCache,
fileInfosCache: fileInfosCache,
}, nil
}
func (gh *GitHub) APICallCount() uint64 {
return gh.apiCallCount
}
func (gh *GitHub) Stat(ctx context.Context, path string) (*fs.FileInfo, error) {
cachedFileInfo, ok := gh.fileInfosCache.Get(path)
if ok {
return cachedFileInfo.(*fs.FileInfo), nil
}
_, directoryContent, err := gh.getContentsWrapper(ctx, path)
if err != nil {
return nil, err
}
if directoryContent != nil {
return &fs.FileInfo{IsDir: true}, nil
}
return &fs.FileInfo{IsDir: false}, nil
}
func (gh *GitHub) Get(ctx context.Context, path string) ([]byte, error) {
fileContent, _, err := gh.getContentsWrapper(ctx, path)
if err != nil {
return nil, err
}
// Simulate os.Read() behavior in case the supplied path points to a directory
if fileContent == nil {
return nil, fs.ErrNormalizedIsADirectory
}
fileBytes, err := base64.StdEncoding.DecodeString(*fileContent.Content)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrAPI, err)
}
return fileBytes, nil
}
func (gh *GitHub) ReadDir(ctx context.Context, path string) ([]string, error) {
_, directoryContent, err := gh.getContentsWrapper(ctx, path)
if err != nil {
return nil, err
}
// Simulate ioutil.ReadDir() behavior in case the supplied path points to a file
if directoryContent == nil {
return nil, syscall.ENOTDIR
}
var entries []string
for _, fileContent := range directoryContent {
entries = append(entries, *fileContent.Name)
}
return entries, nil
}
func (gh *GitHub) Join(elem ...string) string {
return path.Join(elem...)
}
func (gh *GitHub) client() *github.Client {
if gh.token != "" {
return defaultGitHubClient.WithAuthToken(gh.token)
}
return defaultGitHubClient
}
func (gh *GitHub) getContentsWrapper(
ctx context.Context,
path string,
) (*github.RepositoryContent, []*github.RepositoryContent, error) {
contents, ok := gh.contentsCache.Get(path)
if ok {
return contents.(*Contents).File, contents.(*Contents).Directory, nil
}
gh.apiCallCount++
fileContent, directoryContent, resp, err := gh.client().Repositories.GetContents(ctx, gh.owner, gh.repo, path,
&github.RepositoryContentGetOptions{
Ref: gh.reference,
},
)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil, nil, os.ErrNotExist
}
return nil, nil, fmt.Errorf("%w: %v", ErrAPI, err)
}
if fileContent != nil {
gh.fileInfosCache.ContainsOrAdd(path, &fs.FileInfo{IsDir: false})
}
for _, directoryEntry := range directoryContent {
if directoryEntry.Type == nil || directoryEntry.Path == nil {
continue
}
var fileInfo fs.FileInfo
switch *directoryEntry.Type {
case "file":
fileInfo.IsDir = false
case "dir":
fileInfo.IsDir = true
default:
continue
}
gh.fileInfosCache.ContainsOrAdd(*directoryEntry.Path, &fileInfo)
}
gh.contentsCache.Add(path, &Contents{File: fileContent, Directory: directoryContent})
return fileContent, directoryContent, nil
}