91 lines
2.0 KiB
Go

package gitea
import (
"context"
"slices"
"strconv"
"strings"
"code.gitea.io/sdk/gitea"
"forge.cadoles.com/wpetit/clearcase/internal/core/model"
"forge.cadoles.com/wpetit/clearcase/internal/core/port"
"github.com/pkg/errors"
)
type Forge struct {
client *gitea.Client
}
// CreateIssue implements port.Forge.
func (f *Forge) CreateIssue(ctx context.Context, projectID string, title string, content string) error {
return nil
}
// GetIssueTemplate implements port.Forge.
func (f *Forge) GetIssueTemplate(ctx context.Context, rawProjectID string) (string, error) {
projectID, err := strconv.ParseInt(rawProjectID, 10, 64)
if err != nil {
return "", errors.WithStack(err)
}
project, _, err := f.client.GetRepoByID(projectID)
if err != nil {
return "", errors.WithStack(err)
}
data, _, err := f.client.GetFile(project.Owner.UserName, project.Name, project.DefaultBranch, ".gitea/issue_template.md")
if err != nil {
return "", errors.WithStack(err)
}
return string(data), nil
}
// GetIssues implements port.Forge.
func (f *Forge) GetIssues(ctx context.Context, projectID string, issueIDs ...string) ([]*model.Issue, error) {
panic("unimplemented")
}
// ListProjects implements port.Forge.
func (f *Forge) GetProjects(ctx context.Context) ([]*model.Project, error) {
projects := make([]*model.Project, 0)
page := 1
for {
repositories, res, err := f.client.ListMyRepos(gitea.ListReposOptions{
ListOptions: gitea.ListOptions{
Page: page,
PageSize: 100,
},
})
if err != nil {
return nil, errors.WithStack(err)
}
page = res.NextPage
if res.NextPage == 0 {
break
}
for _, r := range repositories {
projects = append(projects, &model.Project{
ID: strconv.FormatInt(r.ID, 10),
Label: r.Owner.UserName + "/" + r.Name,
})
}
}
slices.SortFunc(projects, func(p1 *model.Project, p2 *model.Project) int {
return strings.Compare(p1.Label, p2.Label)
})
return projects, nil
}
func NewForge(client *gitea.Client) *Forge {
return &Forge{client: client}
}
var _ port.Forge = &Forge{}