feat: parse and include referenced issues as prompt context (#12)
This commit is contained in:
@ -2,6 +2,7 @@ package gitea
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strconv"
|
||||
@ -17,6 +18,51 @@ type Forge struct {
|
||||
client *gitea.Client
|
||||
}
|
||||
|
||||
// GetProject implements port.Forge.
|
||||
func (f *Forge) GetProject(ctx context.Context, rawProjectID string) (*model.Project, error) {
|
||||
projectID, err := strconv.ParseInt(rawProjectID, 10, 64)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
project, _, err := f.client.GetRepoByID(projectID)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return &model.Project{
|
||||
ID: rawProjectID,
|
||||
Name: project.FullName,
|
||||
Description: project.Description,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetProjectLanguages implements port.Forge.
|
||||
func (f *Forge) GetProjectLanguages(ctx context.Context, rawProjectID string) ([]string, error) {
|
||||
projectID, err := strconv.ParseInt(rawProjectID, 10, 64)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
project, _, err := f.client.GetRepoByID(projectID)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
mappedLanguages, _, err := f.client.GetRepoLanguages(project.Owner.UserName, project.Name)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
languages := make([]string, 0, len(mappedLanguages))
|
||||
|
||||
for l := range mappedLanguages {
|
||||
languages = append(languages, l)
|
||||
}
|
||||
|
||||
return languages, nil
|
||||
}
|
||||
|
||||
// CreateIssue implements port.Forge.
|
||||
func (f *Forge) CreateIssue(ctx context.Context, rawProjectID string, title string, body string) (string, error) {
|
||||
projectID, err := strconv.ParseInt(rawProjectID, 10, 64)
|
||||
@ -65,12 +111,46 @@ func (f *Forge) GetIssueTemplate(ctx context.Context, rawProjectID string) (stri
|
||||
}
|
||||
|
||||
// GetIssues implements port.Forge.
|
||||
func (f *Forge) GetIssues(ctx context.Context, projectID string, issueIDs ...string) ([]*model.Issue, error) {
|
||||
panic("unimplemented")
|
||||
func (f *Forge) GetIssues(ctx context.Context, rawProjectID string, issueIDs ...string) ([]*model.Issue, error) {
|
||||
projectID, err := strconv.ParseInt(rawProjectID, 10, 64)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
project, _, err := f.client.GetRepoByID(projectID)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
issues := make([]*model.Issue, 0)
|
||||
|
||||
for _, rawIssueID := range issueIDs {
|
||||
issueID, err := strconv.ParseInt(rawIssueID, 10, 64)
|
||||
if err != nil {
|
||||
slog.ErrorContext(ctx, "could not parse issue id", slog.Any("error", errors.WithStack(err)))
|
||||
issues = append(issues, nil)
|
||||
continue
|
||||
}
|
||||
|
||||
issue, _, err := f.client.GetIssue(project.Owner.UserName, project.Name, issueID)
|
||||
if err != nil {
|
||||
slog.ErrorContext(ctx, "could not get issue", slog.String("project", project.FullName), slog.Int64("issueID", issueID), slog.Any("error", errors.WithStack(err)))
|
||||
issues = append(issues, nil)
|
||||
continue
|
||||
}
|
||||
|
||||
issues = append(issues, &model.Issue{
|
||||
ID: rawIssueID,
|
||||
Title: issue.Title,
|
||||
Body: issue.Body,
|
||||
})
|
||||
}
|
||||
|
||||
return issues, nil
|
||||
}
|
||||
|
||||
// ListProjects implements port.Forge.
|
||||
func (f *Forge) GetProjects(ctx context.Context) ([]*model.Project, error) {
|
||||
// GetAllProjects implements port.Forge.
|
||||
func (f *Forge) GetAllProjects(ctx context.Context) ([]*model.Project, error) {
|
||||
projects := make([]*model.Project, 0)
|
||||
|
||||
page := 1
|
||||
@ -93,14 +173,14 @@ func (f *Forge) GetProjects(ctx context.Context) ([]*model.Project, error) {
|
||||
|
||||
for _, r := range repositories {
|
||||
projects = append(projects, &model.Project{
|
||||
ID: strconv.FormatInt(r.ID, 10),
|
||||
Label: r.Owner.UserName + "/" + r.Name,
|
||||
ID: strconv.FormatInt(r.ID, 10),
|
||||
Name: r.FullName,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
slices.SortFunc(projects, func(p1 *model.Project, p2 *model.Project) int {
|
||||
return strings.Compare(p1.Label, p2.Label)
|
||||
return strings.Compare(p1.Name, p2.Name)
|
||||
})
|
||||
|
||||
return projects, nil
|
||||
|
Reference in New Issue
Block a user