vanity/service/gitea.go

77 lines
1.5 KiB
Go
Raw Normal View History

package service
import (
"fmt"
"go.jolheiser.com/vanity/api"
"go.jolheiser.com/vanity/flags"
"code.gitea.io/sdk/gitea"
)
var _ Service = &Gitea{}
func NewGitea() *Gitea {
client := gitea.NewClient(flags.BaseURL.String(), flags.Token)
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)
page := 0
for {
opts := gitea.ListReposOptions{
ListOptions: gitea.ListOptions{
Page: page,
PageSize: 50,
},
}
repos, err := g.client.ListUserRepos(flags.Namespace, opts)
if err != nil {
return nil, err
}
for _, repo := range repos {
packages[repo.Name] = &api.Package{
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,
}
}
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) {
content, err := g.client.GetFile(flags.Namespace, pkg.Name, pkg.Branch, "go.mod")
return string(content), err
}