-
Notifications
You must be signed in to change notification settings - Fork 358
/
github.go
57 lines (47 loc) · 1.17 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
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
// findGitHubREADME tries to find the correct README filename in a repository using GitHub API.
func findGitHubREADME(u *url.URL) (*source, error) {
owner, repo, ok := strings.Cut(strings.TrimPrefix(u.Path, "/"), "/")
if !ok {
return nil, fmt.Errorf("invalid url: %s", u.String())
}
type readme struct {
DownloadURL string `json:"download_url"`
}
apiURL := fmt.Sprintf("https://api.%s/repos/%s/%s/readme", u.Hostname(), owner, repo)
// nolint:bodyclose
// it is closed on the caller
res, err := http.Get(apiURL) // nolint: gosec
if err != nil {
return nil, err
}
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
var result readme
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
if res.StatusCode == http.StatusOK {
// nolint:bodyclose
// it is closed on the caller
resp, err := http.Get(result.DownloadURL)
if err != nil {
return nil, err
}
if resp.StatusCode == http.StatusOK {
return &source{resp.Body, result.DownloadURL}, nil
}
}
return nil, errors.New("can't find README in GitHub repository")
}