2020-09-10 17:27:46 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ Service = &Gitea{}
|
|
|
|
|
|
|
|
func NewGitea() *Gitea {
|
2020-09-12 01:38:18 +00:00
|
|
|
client := gitea.NewClient(flags.BaseURL.String(), flags.Token)
|
2020-09-10 17:27:46 +00:00
|
|
|
return &Gitea{
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Gitea struct {
|
|
|
|
client *gitea.Client
|
|
|
|
}
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
func (g Gitea) Packages() (map[string]*api.Package, error) {
|
|
|
|
packages := make(map[string]*api.Package)
|
2020-09-10 17:27:46 +00:00
|
|
|
page := 0
|
|
|
|
for {
|
|
|
|
opts := gitea.ListReposOptions{
|
|
|
|
ListOptions: gitea.ListOptions{
|
|
|
|
Page: page,
|
|
|
|
PageSize: 50,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
repos, err := g.client.ListUserRepos(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.Name] = &api.Package{
|
2020-09-10 17:27:46 +00:00
|
|
|
Name: repo.Name,
|
|
|
|
Description: repo.Description,
|
|
|
|
Branch: repo.DefaultBranch,
|
2020-09-12 01:38:18 +00:00
|
|
|
WebURL: repo.HTMLURL,
|
|
|
|
CloneHTTP: repo.CloneURL,
|
|
|
|
CloneSSH: repo.SSHURL,
|
|
|
|
Private: repo.Private,
|
|
|
|
Fork: repo.Fork,
|
|
|
|
Mirror: repo.Mirror,
|
|
|
|
Archive: repo.Archived,
|
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 Gitea) GoDir(pkg *api.Package) string {
|
|
|
|
return fmt.Sprintf("%s/src/branch/%s{/dir}", pkg.WebURL, pkg.Branch)
|
2020-09-10 17:27:46 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
func (g Gitea) GoFile(pkg *api.Package) string {
|
|
|
|
return fmt.Sprintf("%s/src/branch/%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 Gitea) GoMod(pkg *api.Package) (string, error) {
|
|
|
|
content, err := g.client.GetFile(flags.Namespace, pkg.Name, pkg.Branch, "go.mod")
|
|
|
|
return string(content), err
|
2020-09-10 17:27:46 +00:00
|
|
|
}
|