2020-09-10 17:27:46 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
"go.jolheiser.com/vanity/api"
|
2020-09-10 17:27:46 +00:00
|
|
|
"go.jolheiser.com/vanity/flags"
|
2020-09-10 18:43:38 +00:00
|
|
|
|
|
|
|
"github.com/google/go-github/v32/github"
|
|
|
|
"golang.org/x/oauth2"
|
2020-09-10 17:27:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ Service = &GitHub{}
|
|
|
|
|
|
|
|
func NewGitHub() *GitHub {
|
|
|
|
ts := oauth2.StaticTokenSource(
|
2020-09-12 01:38:18 +00:00
|
|
|
&oauth2.Token{AccessToken: flags.Token},
|
2020-09-10 17:27:46 +00:00
|
|
|
)
|
|
|
|
client := oauth2.NewClient(context.Background(), ts)
|
2020-09-10 18:43:38 +00:00
|
|
|
ghClient := github.NewClient(client)
|
2020-09-12 01:38:18 +00:00
|
|
|
ghClient.BaseURL = flags.BaseURL
|
2020-09-10 17:27:46 +00:00
|
|
|
return &GitHub{
|
2020-09-10 18:43:38 +00:00
|
|
|
client: ghClient,
|
2020-09-10 17:27:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type GitHub struct {
|
|
|
|
client *github.Client
|
|
|
|
}
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
func (g GitHub) Packages() (map[string]*api.Package, error) {
|
|
|
|
packages := make(map[string]*api.Package)
|
2020-09-10 17:27:46 +00:00
|
|
|
page := 0
|
|
|
|
for {
|
|
|
|
opts := github.RepositoryListOptions{
|
|
|
|
ListOptions: github.ListOptions{
|
|
|
|
Page: page,
|
|
|
|
PerPage: 50,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
repos, _, err := g.client.Repositories.List(context.Background(), flags.Namespace, &opts)
|
2020-09-10 17:27:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, repo := range repos {
|
2020-09-12 01:38:18 +00:00
|
|
|
packages[repo.GetName()] = &api.Package{
|
2020-09-10 17:27:46 +00:00
|
|
|
Name: repo.GetName(),
|
|
|
|
Description: repo.GetDescription(),
|
|
|
|
Branch: repo.GetDefaultBranch(),
|
2020-09-12 01:38:18 +00:00
|
|
|
WebURL: repo.GetHTMLURL(),
|
|
|
|
CloneHTTP: repo.GetCloneURL(),
|
|
|
|
CloneSSH: repo.GetSSHURL(),
|
|
|
|
Private: repo.GetPrivate(),
|
|
|
|
Fork: repo.GetFork(),
|
|
|
|
Mirror: false,
|
|
|
|
Archive: repo.GetArchived(),
|
2020-09-10 17:27:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
page++
|
|
|
|
if len(repos) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return packages, nil
|
|
|
|
}
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
func (g GitHub) GoDir(pkg *api.Package) string {
|
|
|
|
return fmt.Sprintf("%s/tree/%s{/dir}", pkg.WebURL, pkg.Branch)
|
2020-09-10 17:27:46 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
func (g GitHub) GoFile(pkg *api.Package) string {
|
|
|
|
return fmt.Sprintf("%s/blob/%s{/dir}/{file}#L{line}", pkg.WebURL, pkg.Branch)
|
2020-09-10 17:27:46 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
func (g GitHub) GoMod(pkg *api.Package) (string, error) {
|
|
|
|
content, _, _, err := g.client.Repositories.GetContents(context.Background(), flags.Namespace, pkg.Name, "go.mod",
|
2020-09-10 17:27:46 +00:00
|
|
|
&github.RepositoryContentGetOptions{
|
|
|
|
Ref: pkg.Branch,
|
|
|
|
})
|
2020-09-12 01:38:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return content.GetContent()
|
2020-09-10 17:27:46 +00:00
|
|
|
}
|