-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
fetch.go
159 lines (123 loc) · 4.03 KB
/
fetch.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
package bbc
import (
"archive/zip"
"bytes"
"fmt"
"os"
"strings"
"github.com/go-git/go-billy/v5"
"github.com/ktrysmt/go-bitbucket"
"github.com/parnurzeal/gorequest"
"github.com/hofstadter-io/hof/lib/yagu"
"github.com/hofstadter-io/hof/lib/yagu/repos/git"
)
func Fetch(FS billy.Filesystem, owner, repo, tag string, private bool) (error) {
fmt.Println("bbc.Fetch", owner, repo, tag, private)
// If private, and no token auth, try git protocol
// need to catch auth errors and suggest how to setup
if private && os.Getenv(TokenEnv) == "" {
fmt.Println("bitbucket git fallback")
return git.Fetch(FS, "bitbucket.org", owner, repo, tag, private)
}
client, err := NewClient()
if err != nil {
return err
}
var zReader *zip.Reader
if tag == "v0.0.0" {
r, err := GetRepo(client, owner, repo)
if err != nil {
return err
}
zReader, err = FetchBranchZip(owner, repo, r.Mainbranch.Name)
if err != nil {
return fmt.Errorf("While fetching branch zipfile for %s/%s@%s\n%w\n", owner, repo, r.Mainbranch.Name, err)
}
} else {
tags, err := GetTags(client, owner, repo)
if err != nil {
return err
}
// The tag we are looking for
var T *bitbucket.RepositoryTag
for _, t := range tags {
if tag != "" && tag == t.Name {
T = &t
// fmt.Printf("FOUND %v\n", *t.Name)
}
}
if T == nil {
return fmt.Errorf("Did not find tag %q for 'https://bitbucket.org/%s/%s' @%s", tag, owner, repo, tag)
}
zReader, err = FetchTagZip(owner, repo, T.Name)
if err != nil {
fmt.Printf("Error: %v\n", err)
return fmt.Errorf("While fetching tag zipfile\n%w\n", err)
}
}
if err != nil {
return fmt.Errorf("While fetching from bitbucket\n%w\n", err)
}
if err := yagu.BillyLoadFromZip(zReader, FS, true); err != nil {
return fmt.Errorf("While reading zipfile\n%w\n", err)
}
return nil
}
func FetchTagZip(owner, repo, ver string) (*zip.Reader, error) {
fmt.Println("bbc.FetchTagZip")
// url := *tag.ZipballURL
url := fmt.Sprintf("https://bitbucket.org/%s/%s/get/%s.zip", owner, repo, ver)
req := gorequest.New().Get(url)
// TODO, process auth logic here better, maybe find a way to DRY
if token := os.Getenv(TokenEnv); token != "" {
req.Header.Set("Authorization", "Bearer" + token)
} else if password := os.Getenv(PasswordEnv); password != "" {
username := os.Getenv(UsernameEnv)
req.SetBasicAuth(username, password)
}
resp, data, errs := req.EndBytes()
check := "http2: server sent GOAWAY and closed the connection"
if len(errs) != 0 && !strings.Contains(errs[0].Error(), check) {
fmt.Println("errs:", errs)
fmt.Println("resp:", resp)
fmt.Println("body:", len(data))
return nil, errs[0]
}
if len(errs) != 0 || resp.StatusCode >= 500 {
return nil, fmt.Errorf("Internal Error: " + string(resp.StatusCode))
}
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("Bad Request: %v\n%v\n", resp.StatusCode, errs)
}
r := bytes.NewReader(data)
zfile, err := zip.NewReader(r, int64(len(data)))
return zfile, err
}
func FetchBranchZip(owner, repo, branch string) (*zip.Reader, error) {
url := fmt.Sprintf("https://bitbucket.org/%s/%s/get/%s.zip", owner, repo, branch)
req := gorequest.New().Get(url)
// TODO, process auth logic here better, maybe find a way to DRY
if token := os.Getenv(TokenEnv); token != "" {
req.Header.Set("Authorization", "Bearer" + token)
} else if password := os.Getenv(PasswordEnv); password != "" {
username := os.Getenv(UsernameEnv)
req.SetBasicAuth(username, password)
}
resp, data, errs := req.EndBytes()
check := "http2: server sent GOAWAY and closed the connection"
if len(errs) != 0 && !strings.Contains(errs[0].Error(), check) {
fmt.Println("errs:", errs)
fmt.Println("resp:", resp)
fmt.Println("body:", len(data))
return nil, errs[0]
}
if len(errs) != 0 || resp.StatusCode >= 500 {
return nil, fmt.Errorf("Internal Error: " + string(resp.StatusCode))
}
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("Bad Request: " + string(resp.StatusCode))
}
r := bytes.NewReader(data)
zfile, err := zip.NewReader(r, int64(len(data)))
return zfile, err
}