/
github.go
173 lines (148 loc) · 3.96 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
package external
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/Bananenpro/cli"
"github.com/adrg/xdg"
"github.com/code-game-project/go-utils/semver"
)
var (
ErrTagNotFound = errors.New("tag not found")
ErrHTTPStatus = errors.New("invalid http status")
)
func LatestGithubTag(owner, repo string) (string, error) {
type response []struct {
Name string `json:"name"`
}
res, err := githubRequest[response](fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", owner, repo))
if err != nil {
return "", fmt.Errorf("failed to access git tags from 'github.com/%s/%s'.", owner, repo)
}
return res[0].Name, nil
}
func GithubTagFromVersion(owner, repo, version string) (string, error) {
type response []struct {
Name string `json:"name"`
}
res, err := githubRequest[response](fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", owner, repo))
if err != nil {
return "", fmt.Errorf("Couldn't access git tags from 'github.com/%s/%s'.", owner, repo)
}
for _, tag := range res {
if strings.HasPrefix(tag.Name, "v"+version) {
return tag.Name, nil
}
}
return "", ErrTagNotFound
}
func LibraryVersionFromCGVersion(owner, repo, cgVersion string) string {
res, err := LoadVersionsJSON(owner, repo)
if err != nil {
cli.Warn("Couldn't fetch versions.json. Using latest client library version.")
return "latest"
}
var versions map[string]string
err = json.Unmarshal(res, &versions)
if err != nil {
cli.Warn("Invalid versions.json. Using latest client library version.")
return "latest"
}
return semver.CompatibleVersion(versions, cgVersion)
}
func LoadVersionsJSON(owner, repo string) (json.RawMessage, error) {
res, err := githubRequest[json.RawMessage](fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/main/versions.json", owner, repo))
if err != nil {
res, err = githubRequest[json.RawMessage](fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/master/versions.json", owner, repo))
}
return res, err
}
func githubRequest[T any](url string) (T, error) {
var data T
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return data, err
}
etag, err := githubETag(url)
if err == nil {
request.Header.Add("If-None-Match", etag)
}
resp, err := http.DefaultClient.Do(request)
if err != nil {
return data, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
err = json.NewDecoder(resp.Body).Decode(&data)
if err == nil {
saveGitHubETag(url, resp.Header.Get("etag"), data)
}
return data, err
}
if resp.StatusCode == http.StatusNotModified {
return loadGitHubCacheData[T](url)
}
return data, ErrHTTPStatus
}
var githubCacheDir = filepath.Join(xdg.CacheHome, "codegame", "github_requests")
func saveGitHubETag(reqURL, etag string, data any) error {
os.MkdirAll(githubCacheDir, 0o755)
file, err := os.Create(filepath.Join(githubCacheDir, url.PathEscape(reqURL)))
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(etag + "\n")
if err != nil {
return err
}
return json.NewEncoder(file).Encode(data)
}
func githubETag(reqURL string) (string, error) {
file, err := os.Open(filepath.Join(githubCacheDir, url.PathEscape(reqURL)))
if err != nil {
return "", err
}
defer file.Close()
scanner := bufio.NewScanner(file)
if !scanner.Scan() {
if scanner.Err() != nil {
return "", scanner.Err()
} else {
return "", io.EOF
}
}
return scanner.Text(), nil
}
func loadGitHubCacheData[T any](reqURL string) (T, error) {
var data T
file, err := os.Open(filepath.Join(githubCacheDir, url.PathEscape(reqURL)))
if err != nil {
return data, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
if !scanner.Scan() {
if scanner.Err() != nil {
return data, scanner.Err()
} else {
return data, io.EOF
}
}
if !scanner.Scan() {
if scanner.Err() != nil {
return data, scanner.Err()
} else {
return data, io.EOF
}
}
err = json.Unmarshal(scanner.Bytes(), &data)
return data, err
}