-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
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>
1 parent
ab94744
commit 9ebe39c
Showing
5 changed files
with
183 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
const ( | ||
protoGithub = "github://" | ||
protoGitlab = "gitlab://" | ||
protoHTTPS = "https://" | ||
) | ||
|
||
var ( | ||
githubURL *url.URL | ||
gitlabURL *url.URL | ||
urlsOnce sync.Once | ||
) | ||
|
||
func init() { | ||
urlsOnce.Do(func() { | ||
githubURL, _ = url.Parse("https://github.com") | ||
gitlabURL, _ = url.Parse("https://gitlab.com") | ||
}) | ||
} | ||
|
||
func readmeURL(path string) (*source, error) { | ||
switch { | ||
case strings.HasPrefix(path, protoGithub): | ||
if u := githubReadmeURL(path); u != nil { | ||
return readmeURL(u.String()) | ||
} | ||
return nil, nil | ||
case strings.HasPrefix(path, protoGitlab): | ||
if u := gitlabReadmeURL(path); u != nil { | ||
return readmeURL(u.String()) | ||
} | ||
return nil, nil | ||
} | ||
|
||
if !strings.HasPrefix(path, protoHTTPS) { | ||
path = protoHTTPS + path | ||
} | ||
u, err := url.Parse(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch { | ||
case u.Hostname() == githubURL.Hostname(): | ||
return findGitHubREADME(u) | ||
case u.Hostname() == gitlabURL.Hostname(): | ||
return findGitLabREADME(u) | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func githubReadmeURL(path string) *url.URL { | ||
path = strings.TrimPrefix(path, protoGithub) | ||
parts := strings.Split(path, "/") | ||
if len(parts) != 2 { | ||
// custom hostnames are not supported yet | ||
return nil | ||
} | ||
u, _ := url.Parse(githubURL.String()) | ||
return u.JoinPath(path) | ||
} | ||
|
||
func gitlabReadmeURL(path string) *url.URL { | ||
path = strings.TrimPrefix(path, protoGitlab) | ||
parts := strings.Split(path, "/") | ||
if len(parts) != 2 { | ||
// custom hostnames are not supported yet | ||
return nil | ||
} | ||
u, _ := url.Parse(gitlabURL.String()) | ||
return u.JoinPath(path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestURLParser(t *testing.T) { | ||
for path, url := range map[string]string{ | ||
"github.com/charmbracelet/glow": "https://raw.githubusercontent.com/charmbracelet/glow/master/README.md", | ||
"github://charmbracelet/glow": "https://raw.githubusercontent.com/charmbracelet/glow/master/README.md", | ||
"github://caarlos0/dotfiles.fish": "https://raw.githubusercontent.com/caarlos0/dotfiles.fish/main/README.md", | ||
"github://tj/git-extras": "https://raw.githubusercontent.com/tj/git-extras/main/Readme.md", | ||
"https://github.com/goreleaser/nfpm": "https://raw.githubusercontent.com/goreleaser/nfpm/main/README.md", | ||
"gitlab.com/caarlos0/test": "https://gitlab.com/caarlos0/test/-/raw/master/README.md", | ||
"gitlab://caarlos0/test": "https://gitlab.com/caarlos0/test/-/raw/master/README.md", | ||
"https://gitlab.com/terrakok/gitlab-client": "https://gitlab.com/terrakok/gitlab-client/-/raw/develop/Readme.md", | ||
} { | ||
t.Run(path, func(t *testing.T) { | ||
got, err := readmeURL(path) | ||
if err != nil { | ||
t.Fatalf("expected no error, got %v", err) | ||
} | ||
if got == nil { | ||
t.Fatalf("should not be nil") | ||
} | ||
if url != got.URL { | ||
t.Errorf("expected url for %s to be %s, was %s", path, url, got.URL) | ||
} | ||
}) | ||
} | ||
} |