glow/gitlab.go

62 lines
1.3 KiB
Go
Raw Normal View History

2019-11-25 05:55:50 +00:00
package main
import (
"encoding/json"
2019-11-25 05:55:50 +00:00
"errors"
"fmt"
"io"
2019-11-25 05:55:50 +00:00
"net/http"
"net/url"
"strings"
)
// findGitLabREADME tries to find the correct README filename in a repository using GitLab API.
func findGitLabREADME(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())
2019-11-25 05:55:50 +00:00
}
projectPath := url.QueryEscape(owner + "/" + repo)
type readme struct {
ReadmeURL string `json:"readme_url"`
2019-11-25 05:55:50 +00:00
}
apiURL := fmt.Sprintf("https://%s/api/v4/projects/%s", u.Hostname(), projectPath)
// nolint:bodyclose
// it is closed on the caller
res, err := http.Get(apiURL) // nolint: gosec
if err != nil {
return nil, err
}
2019-11-25 05:55:50 +00:00
body, err := io.ReadAll(res.Body)
2019-11-25 05:55:50 +00:00
if err != nil {
return nil, err
}
var result readme
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
readmeRawURL := strings.Replace(result.ReadmeURL, "blob", "raw", -1)
if res.StatusCode == http.StatusOK {
// nolint:bodyclose
// it is closed on the caller
resp, err := http.Get(readmeRawURL) // nolint: gosec
if err != nil {
return nil, err
}
if resp.StatusCode == http.StatusOK {
return &source{resp.Body, readmeRawURL}, nil
2019-11-25 05:55:50 +00:00
}
}
return nil, errors.New("can't find README in GitLab repository")
}