• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

charmbracelet / glow / 9862794127

09 Jul 2024 07:07PM UTC coverage: 12.832% (+2.7%) from 10.105%
9862794127

push

github

web-flow
feat: improve gitlab/github readme url (#456)

* Use GitHub API to find readme filename

* Fix lint errors and typos

* Bring back "tries to find" instead of "finds"

* Rename `readmeURL` to `apiURL`

* Don't close body

* Use GitLab API to find readme filename

* feat: improve gitlab/github readme url

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

---------

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Co-authored-by: danielwerg <35052399+danielwerg@users.noreply.github.com>

87 of 111 new or added lines in 4 files covered. (78.38%)

2 existing lines in 2 files now uncovered.

251 of 1956 relevant lines covered (12.83%)

4.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.44
/github.go
1
package main
2

3
import (
4
        "encoding/json"
5
        "errors"
6
        "fmt"
7
        "io"
8
        "net/http"
9
        "net/url"
10
        "strings"
11
)
12

13
// findGitHubREADME tries to find the correct README filename in a repository using GitHub API.
14
func findGitHubREADME(u *url.URL) (*source, error) {
7✔
15
        owner, repo, ok := strings.Cut(strings.TrimPrefix(u.Path, "/"), "/")
7✔
16
        if !ok {
7✔
NEW
17
                return nil, fmt.Errorf("invalid url: %s", u.String())
×
UNCOV
18
        }
×
19

20
        type readme struct {
7✔
21
                DownloadURL string `json:"download_url"`
7✔
22
        }
7✔
23

7✔
24
        apiURL := fmt.Sprintf("https://api.%s/repos/%s/%s/readme", u.Hostname(), owner, repo)
7✔
25

7✔
26
        // nolint:bodyclose
7✔
27
        // it is closed on the caller
7✔
28
        res, err := http.Get(apiURL) // nolint: gosec
7✔
29
        if err != nil {
7✔
NEW
30
                return nil, err
×
NEW
31
        }
×
32

33
        body, err := io.ReadAll(res.Body)
7✔
34
        if err != nil {
7✔
35
                return nil, err
×
36
        }
×
37

38
        var result readme
7✔
39
        if err := json.Unmarshal(body, &result); err != nil {
7✔
NEW
40
                return nil, err
×
NEW
41
        }
×
42

43
        if res.StatusCode == http.StatusOK {
14✔
44
                // nolint:bodyclose
7✔
45
                // it is closed on the caller
7✔
46
                resp, err := http.Get(result.DownloadURL)
7✔
47
                if err != nil {
7✔
NEW
48
                        return nil, err
×
NEW
49
                }
×
50

51
                if resp.StatusCode == http.StatusOK {
14✔
52
                        return &source{resp.Body, result.DownloadURL}, nil
7✔
53
                }
7✔
54
        }
55

56
        return nil, errors.New("can't find README in GitHub repository")
×
57
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc