MCPcopy
hub / github.com/charmbracelet/glow / findGitLabREADME

Function findGitLabREADME

gitlab.go:14–61  ·  view source on GitHub ↗

findGitLabREADME tries to find the correct README filename in a repository using GitLab API.

(u *url.URL)

Source from the content-addressed store, hash-verified

12
13// findGitLabREADME tries to find the correct README filename in a repository using GitLab API.
14func findGitLabREADME(u *url.URL) (*source, error) {
15 owner, repo, ok := strings.Cut(strings.TrimPrefix(u.Path, "/"), "/")
16 if !ok {
17 return nil, fmt.Errorf("invalid url: %s", u.String())
18 }
19
20 projectPath := url.QueryEscape(owner + "/" + repo)
21
22 type readme struct {
23 ReadmeURL string `json:"readme_url"`
24 }
25
26 apiURL := fmt.Sprintf("https://%s/api/v4/projects/%s", u.Hostname(), projectPath)
27
28 //nolint:bodyclose
29 // it is closed on the caller
30 res, err := http.Get(apiURL) //nolint: gosec,noctx
31 if err != nil {
32 return nil, fmt.Errorf("unable to get url: %w", err)
33 }
34
35 body, err := io.ReadAll(res.Body)
36 if err != nil {
37 return nil, fmt.Errorf("unable to read http response body: %w", err)
38 }
39
40 var result readme
41 if err := json.Unmarshal(body, &result); err != nil {
42 return nil, fmt.Errorf("unable to parse json: %w", err)
43 }
44
45 readmeRawURL := strings.ReplaceAll(result.ReadmeURL, "blob", "raw")
46
47 if res.StatusCode == http.StatusOK {
48 //nolint:bodyclose
49 // it is closed on the caller
50 resp, err := http.Get(readmeRawURL) //nolint: gosec,noctx
51 if err != nil {
52 return nil, fmt.Errorf("unable to get url: %w", err)
53 }
54
55 if resp.StatusCode == http.StatusOK {
56 return &source{resp.Body, readmeRawURL}, nil
57 }
58 }
59
60 return nil, errors.New("can't find README in GitLab repository")
61}

Callers 1

readmeURLFunction · 0.85

Calls 1

StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…