83 lines
1.8 KiB
Go
83 lines
1.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/google/go-github/v32/github"
|
|
"golang.org/x/oauth2"
|
|
|
|
"go.jolheiser.com/vanity/flags"
|
|
)
|
|
|
|
var _ Service = &GitHub{}
|
|
|
|
func NewGitHub() *GitHub {
|
|
ts := oauth2.StaticTokenSource(
|
|
&oauth2.Token{AccessToken: flags.Config.Token},
|
|
)
|
|
client := oauth2.NewClient(context.Background(), ts)
|
|
return &GitHub{
|
|
client: github.NewClient(client),
|
|
}
|
|
}
|
|
|
|
type GitHub struct {
|
|
client *github.Client
|
|
}
|
|
|
|
func (g GitHub) Packages() (map[string]*Package, error) {
|
|
packages := make(map[string]*Package)
|
|
page := 0
|
|
for {
|
|
opts := github.RepositoryListOptions{
|
|
ListOptions: github.ListOptions{
|
|
Page: page,
|
|
PerPage: 50,
|
|
},
|
|
}
|
|
|
|
repos, _, err := g.client.Repositories.List(context.Background(), flags.Config.Namespace, &opts)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, repo := range repos {
|
|
packages[repo.GetName()] = &Package{
|
|
Name: repo.GetName(),
|
|
Description: repo.GetDescription(),
|
|
Branch: repo.GetDefaultBranch(),
|
|
URL: repo.GetHTMLURL(),
|
|
HTTP: repo.GetCloneURL(),
|
|
SSH: repo.GetSSHURL(),
|
|
private: repo.GetPrivate(),
|
|
fork: repo.GetFork(),
|
|
mirror: false,
|
|
archive: repo.GetArchived(),
|
|
}
|
|
}
|
|
|
|
page++
|
|
if len(repos) == 0 {
|
|
break
|
|
}
|
|
}
|
|
|
|
return packages, nil
|
|
}
|
|
|
|
func (g GitHub) GoDir(pkg *Package) string {
|
|
return fmt.Sprintf("%s/tree/%s{/dir}", pkg.URL, pkg.Branch)
|
|
}
|
|
|
|
func (g GitHub) GoFile(pkg *Package) string {
|
|
return fmt.Sprintf("%s/blob/%s{/dir}/{file}#L{line}", pkg.URL, pkg.Branch)
|
|
}
|
|
|
|
func (g GitHub) GoMod(pkg *Package) bool {
|
|
_, _, _, err := g.client.Repositories.GetContents(context.Background(), flags.Config.Namespace, pkg.Name, "go.mod",
|
|
&github.RepositoryContentGetOptions{
|
|
Ref: pkg.Branch,
|
|
})
|
|
return err == nil
|
|
}
|