84 lines
1.8 KiB
Go
84 lines
1.8 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"html"
|
|
|
|
"go.jolheiser.com/vanity/flags"
|
|
|
|
"github.com/xanzy/go-gitlab"
|
|
"go.jolheiser.com/beaver"
|
|
)
|
|
|
|
var _ Service = &GitLab{}
|
|
|
|
func NewGitLab() *GitLab {
|
|
client, err := gitlab.NewClient(flags.Config.Token, gitlab.WithBaseURL(flags.Config.BaseURL))
|
|
if err != nil {
|
|
beaver.Errorf("could not create GitLab client: %v", err)
|
|
}
|
|
return &GitLab{
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
type GitLab struct {
|
|
client *gitlab.Client
|
|
}
|
|
|
|
func (g GitLab) Packages() (map[string]*Package, error) {
|
|
packages := make(map[string]*Package)
|
|
page := 0
|
|
for {
|
|
opts := gitlab.ListProjectsOptions{
|
|
ListOptions: gitlab.ListOptions{
|
|
Page: page,
|
|
PerPage: 50,
|
|
},
|
|
}
|
|
|
|
repos, _, err := g.client.Projects.ListUserProjects(flags.Config.Namespace, &opts)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, repo := range repos {
|
|
packages[repo.Name] = &Package{
|
|
Name: repo.Name,
|
|
Description: repo.Description,
|
|
Branch: repo.DefaultBranch,
|
|
URL: repo.WebURL,
|
|
HTTP: repo.HTTPURLToRepo,
|
|
SSH: repo.SSHURLToRepo,
|
|
private: repo.Visibility != gitlab.PublicVisibility,
|
|
fork: repo.ForkedFromProject != nil,
|
|
mirror: repo.Mirror,
|
|
archive: repo.Archived,
|
|
}
|
|
}
|
|
|
|
page++
|
|
if len(repos) == 0 {
|
|
break
|
|
}
|
|
}
|
|
|
|
return packages, nil
|
|
}
|
|
|
|
func (g GitLab) GoDir(pkg *Package) string {
|
|
return fmt.Sprintf("%s/-/tree/%s{/dir}", pkg.URL, pkg.Branch)
|
|
}
|
|
|
|
func (g GitLab) GoFile(pkg *Package) string {
|
|
return fmt.Sprintf("%s/-/blob/%s{/dir}/{file}#L{line}", pkg.URL, pkg.Branch)
|
|
}
|
|
|
|
func (g GitLab) GoMod(pkg *Package) bool {
|
|
id := fmt.Sprintf("%s/%s", flags.Config.Namespace, pkg.Name)
|
|
_, _, err := g.client.RepositoryFiles.GetRawFile(html.EscapeString(id), "go.mod", &gitlab.GetRawFileOptions{
|
|
Ref: &pkg.Branch,
|
|
})
|
|
return err == nil
|
|
}
|