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>
This commit is contained in:
Carlos Alexandro Becker 2024-07-09 16:07:39 -03:00 committed by GitHub
commit 9ebe39cd09
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 183 additions and 69 deletions

View file

@ -1,49 +1,59 @@
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
// isGitLabURL tests a string to determine if it is a well-structured GitLab URL.
func isGitLabURL(s string) (string, bool) {
if strings.HasPrefix(s, "gitlab.com/") {
s = "https://" + s
// 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())
}
u, err := url.ParseRequestURI(s)
if err != nil {
return "", false
projectPath := url.QueryEscape(owner + "/" + repo)
type readme struct {
ReadmeURL string `json:"readme_url"`
}
return u.String(), strings.ToLower(u.Host) == "gitlab.com"
}
apiURL := fmt.Sprintf("https://%s/api/v4/projects/%s", u.Hostname(), projectPath)
// findGitLabREADME tries to find the correct README filename in a repository.
func findGitLabREADME(s string) (*source, error) {
u, err := url.ParseRequestURI(s)
// nolint:bodyclose
// it is closed on the caller
res, err := http.Get(apiURL) // nolint: gosec
if err != nil {
return nil, err
}
for _, b := range readmeBranches {
for _, r := range readmeNames {
v := *u
v.Path += fmt.Sprintf("/raw/%s/%s", b, r)
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
// nolint:bodyclose
// it is closed on the caller
resp, err := http.Get(v.String())
if err != nil {
return nil, err
}
var result readme
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
if resp.StatusCode == http.StatusOK {
return &source{resp.Body, v.String()}, nil
}
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
}
}