forked from hofstadter-io/hof
/
fetch.go
148 lines (112 loc) · 3.49 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
package github
import (
"archive/zip"
"bytes"
"fmt"
"os"
"strings"
"github.com/go-git/go-billy/v5"
"github.com/google/go-github/v38/github"
"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 {
// 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("github git fallback")
return git.Fetch(FS, "github.com", 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.DefaultBranch)
if err != nil {
return fmt.Errorf("While fetching branch zipfile for %s/%s@%s\n%w\n", owner, repo, *r.DefaultBranch, err)
}
} else {
tags, err := GetTags(client, owner, repo)
if err != nil {
return err
}
// The tag we are looking for
var T *github.RepositoryTag
for _, t := range tags {
if tag != "" && tag == *t.Name {
T = t
}
}
if T == nil {
return fmt.Errorf("Did not find tag %q for 'https://github.com/%s/%s' @%s", tag, owner, repo, tag)
}
zReader, err = FetchTagZip(T)
if err != nil {
return fmt.Errorf("While fetching tag zipfile\n%w\n", err)
}
}
if err != nil {
return fmt.Errorf("While fetching from github\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(tag *github.RepositoryTag) (*zip.Reader, error) {
url := *tag.ZipballURL
req := gorequest.New().Get(url)
// TODO, process auth logic here better, maybe find a way to DRY
if token := os.Getenv(TokenEnv); token != "" {
req.SetBasicAuth("github-token", token)
}
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://github.com/%s/%s/archive/refs/heads/%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.SetBasicAuth("github-token", token)
}
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
}