2020-09-10 18:43:38 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html"
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
"go.jolheiser.com/vanity/api"
|
2020-09-10 18:43:38 +00:00
|
|
|
"go.jolheiser.com/vanity/flags"
|
|
|
|
|
|
|
|
"github.com/xanzy/go-gitlab"
|
|
|
|
"go.jolheiser.com/beaver"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ Service = &GitLab{}
|
|
|
|
|
|
|
|
func NewGitLab() *GitLab {
|
2020-09-12 01:38:18 +00:00
|
|
|
client, err := gitlab.NewClient(flags.Token, gitlab.WithBaseURL(flags.BaseURL.String()))
|
2020-09-10 18:43:38 +00:00
|
|
|
if err != nil {
|
|
|
|
beaver.Errorf("could not create GitLab client: %v", err)
|
|
|
|
}
|
|
|
|
return &GitLab{
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type GitLab struct {
|
|
|
|
client *gitlab.Client
|
|
|
|
}
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
func (g GitLab) Packages() (map[string]*api.Package, error) {
|
|
|
|
packages := make(map[string]*api.Package)
|
2020-09-10 18:43:38 +00:00
|
|
|
page := 0
|
|
|
|
for {
|
|
|
|
opts := gitlab.ListProjectsOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
Page: page,
|
|
|
|
PerPage: 50,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
repos, _, err := g.client.Projects.ListUserProjects(flags.Namespace, &opts)
|
2020-09-10 18:43:38 +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 18:43:38 +00:00
|
|
|
Name: repo.Name,
|
|
|
|
Description: repo.Description,
|
|
|
|
Branch: repo.DefaultBranch,
|
2020-09-12 01:38:18 +00:00
|
|
|
WebURL: repo.WebURL,
|
|
|
|
CloneHTTP: repo.HTTPURLToRepo,
|
|
|
|
CloneSSH: repo.SSHURLToRepo,
|
|
|
|
Private: repo.Visibility != gitlab.PublicVisibility,
|
|
|
|
Fork: repo.ForkedFromProject != nil,
|
|
|
|
Mirror: repo.Mirror,
|
|
|
|
Archive: repo.Archived,
|
2020-09-10 18:43:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
page++
|
|
|
|
if len(repos) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return packages, nil
|
|
|
|
}
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
func (g GitLab) GoDir(pkg *api.Package) string {
|
|
|
|
return fmt.Sprintf("%s/-/tree/%s{/dir}", pkg.WebURL, pkg.Branch)
|
2020-09-10 18:43:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
func (g GitLab) GoFile(pkg *api.Package) string {
|
|
|
|
return fmt.Sprintf("%s/-/blob/%s{/dir}/{file}#L{line}", pkg.WebURL, pkg.Branch)
|
2020-09-10 18:43:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
func (g GitLab) GoMod(pkg *api.Package) (string, error) {
|
|
|
|
id := fmt.Sprintf("%s/%s", flags.Namespace, pkg.Name)
|
|
|
|
content, _, err := g.client.RepositoryFiles.GetRawFile(html.EscapeString(id), "go.mod", &gitlab.GetRawFileOptions{
|
2020-09-10 18:43:38 +00:00
|
|
|
Ref: &pkg.Branch,
|
|
|
|
})
|
2020-09-12 01:38:18 +00:00
|
|
|
return string(content), err
|
2020-09-10 18:43:38 +00:00
|
|
|
}
|