2020-09-12 15:23:25 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"go.jolheiser.com/vanity/api"
|
|
|
|
"go.jolheiser.com/vanity/flags"
|
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
2021-02-21 20:23:06 +00:00
|
|
|
"go.jolheiser.com/beaver"
|
2020-09-12 15:23:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ Service = &Gitea{}
|
|
|
|
|
|
|
|
func NewGitea() *Gitea {
|
2021-02-21 20:23:06 +00:00
|
|
|
client, err := gitea.NewClient(flags.BaseURL.String(), gitea.SetToken(flags.Token))
|
|
|
|
if err != nil {
|
|
|
|
beaver.Errorf("could not create Gitea client: %v", err)
|
|
|
|
}
|
2020-09-12 15:23:25 +00:00
|
|
|
return &Gitea{
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Gitea struct {
|
|
|
|
client *gitea.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g Gitea) Packages() (map[string]*api.Package, error) {
|
|
|
|
packages := make(map[string]*api.Package)
|
2021-02-21 20:23:06 +00:00
|
|
|
topicOpts := gitea.ListRepoTopicsOptions{
|
|
|
|
ListOptions: gitea.ListOptions{
|
|
|
|
Page: 1,
|
|
|
|
PageSize: 50,
|
|
|
|
},
|
|
|
|
}
|
2020-09-12 15:23:25 +00:00
|
|
|
page := 0
|
|
|
|
for {
|
|
|
|
opts := gitea.ListReposOptions{
|
|
|
|
ListOptions: gitea.ListOptions{
|
|
|
|
Page: page,
|
|
|
|
PageSize: 50,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-21 20:23:06 +00:00
|
|
|
repos, _, err := g.client.ListUserRepos(flags.Namespace, opts)
|
2020-09-12 15:23:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, repo := range repos {
|
2021-02-21 20:23:06 +00:00
|
|
|
pkg := &api.Package{
|
2020-09-12 15:23:25 +00:00
|
|
|
Name: repo.Name,
|
|
|
|
Description: repo.Description,
|
|
|
|
Branch: repo.DefaultBranch,
|
|
|
|
WebURL: repo.HTMLURL,
|
|
|
|
CloneHTTP: repo.CloneURL,
|
|
|
|
CloneSSH: repo.SSHURL,
|
|
|
|
Private: repo.Private,
|
|
|
|
Fork: repo.Fork,
|
|
|
|
Mirror: repo.Mirror,
|
|
|
|
Archive: repo.Archived,
|
|
|
|
}
|
2021-02-21 20:23:06 +00:00
|
|
|
|
|
|
|
// Get tags
|
|
|
|
topics, _, err := g.client.ListRepoTopics(flags.Namespace, repo.Name, topicOpts)
|
|
|
|
if err != nil {
|
|
|
|
beaver.Warnf("could not get topics for %s: %v", repo.Name, err)
|
|
|
|
}
|
|
|
|
pkg.Topics = topics
|
|
|
|
|
|
|
|
packages[repo.Name] = pkg
|
2020-09-12 15:23:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
page++
|
|
|
|
if len(repos) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return packages, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g Gitea) GoDir(pkg *api.Package) string {
|
|
|
|
return fmt.Sprintf("%s/src/branch/%s{/dir}", pkg.WebURL, pkg.Branch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g Gitea) GoFile(pkg *api.Package) string {
|
|
|
|
return fmt.Sprintf("%s/src/branch/%s{/dir}/{file}#L{line}", pkg.WebURL, pkg.Branch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g Gitea) GoMod(pkg *api.Package) (string, error) {
|
2021-02-21 20:23:06 +00:00
|
|
|
content, _, err := g.client.GetFile(flags.Namespace, pkg.Name, pkg.Branch, "go.mod")
|
2020-09-12 15:23:25 +00:00
|
|
|
return string(content), err
|
|
|
|
}
|