2019-11-22 02:52:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-07-03 16:09:20 +00:00
|
|
|
"fmt"
|
2019-11-22 02:52:21 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-12-01 03:12:05 +00:00
|
|
|
// isGitHubURL tests a string to determine if it is a well-structured GitHub URL.
|
2019-11-25 03:55:09 +00:00
|
|
|
func isGitHubURL(s string) (string, bool) {
|
|
|
|
if strings.HasPrefix(s, "github.com/") {
|
|
|
|
s = "https://" + s
|
|
|
|
}
|
|
|
|
|
2019-11-22 02:52:21 +00:00
|
|
|
u, err := url.ParseRequestURI(s)
|
|
|
|
if err != nil {
|
2019-11-25 03:55:09 +00:00
|
|
|
return "", false
|
2019-11-22 02:52:21 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 03:55:09 +00:00
|
|
|
return u.String(), strings.ToLower(u.Host) == "github.com"
|
2019-11-22 02:52:21 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 03:12:05 +00:00
|
|
|
// findGitHubREADME tries to find the correct README filename in a repository.
|
2020-03-31 06:53:35 +00:00
|
|
|
func findGitHubREADME(s string) (*source, error) {
|
2019-11-22 02:52:21 +00:00
|
|
|
u, err := url.ParseRequestURI(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
u.Host = "raw.githubusercontent.com"
|
|
|
|
|
2024-07-03 16:09:20 +00:00
|
|
|
for _, b := range readmeBranches {
|
|
|
|
for _, r := range readmeNames {
|
|
|
|
v := *u
|
|
|
|
v.Path += fmt.Sprintf("/%s/%s", b, r)
|
|
|
|
|
|
|
|
// nolint:bodyclose
|
|
|
|
// it is closed on the caller
|
|
|
|
resp, err := http.Get(v.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusOK {
|
|
|
|
return &source{resp.Body, v.String()}, nil
|
|
|
|
}
|
2019-11-22 02:52:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("can't find README in GitHub repository")
|
|
|
|
}
|