feat: pull request generation

This commit is contained in:
2025-03-06 22:47:02 +01:00
parent 4d6459fae5
commit 367f9f9e70
28 changed files with 1918 additions and 181 deletions

View File

@ -16,6 +16,128 @@ type Forge struct {
client *gitea.Client
}
// UpdatePullRequest implements port.Forge.
func (f *Forge) UpdatePullRequest(ctx context.Context, rawProjectID string, rawPullRequestID string, title string, body string) (string, error) {
project, err := f.getProject(rawProjectID)
if err != nil {
return "", errors.WithStack(err)
}
pullRequestID, err := strconv.ParseInt(rawPullRequestID, 10, 64)
if err != nil {
return "", errors.Wrapf(err, "could not parse pull request id '%v'", rawPullRequestID)
}
pr, _, err := f.client.EditPullRequest(project.Owner.UserName, project.Name, pullRequestID, gitea.EditPullRequestOption{
Title: title,
Body: body,
})
if err != nil {
return "", errors.WithStack(err)
}
return pr.HTMLURL, nil
}
// GetPullRequestTemplate implements port.Forge.
func (f *Forge) GetPullRequestTemplate(ctx context.Context, rawProjectID string) (string, error) {
data, err := f.GetFile(ctx, rawProjectID, ".gitea/pull_request_template.md")
if err != nil {
return "", errors.WithStack(err)
}
return string(data), nil
}
// GetPullRequests implements port.Forge.
func (f *Forge) GetPullRequests(ctx context.Context, rawProjectID string, pullRequestIDs ...string) ([]*model.PullRequest, error) {
project, err := f.getProject(rawProjectID)
if err != nil {
return nil, errors.WithStack(err)
}
pullRequests := make([]*model.PullRequest, 0)
for _, rawID := range pullRequestIDs {
id, err := strconv.ParseInt(rawID, 10, 64)
if err != nil {
return nil, errors.Wrapf(err, "could not parse pull request id '%v'", rawID)
}
pr, _, err := f.client.GetPullRequest(project.Owner.UserName, project.Name, id)
if err != nil {
return nil, errors.WithStack(err)
}
pullRequests = append(pullRequests, &model.PullRequest{
ID: strconv.FormatInt(pr.ID, 10),
Title: pr.Title,
Body: pr.Body,
})
}
return pullRequests, nil
}
// GetPullRequestDiff implements port.Forge.
func (f *Forge) GetPullRequestDiff(ctx context.Context, rawProjectID string, rawPullRequestID string) (string, error) {
project, err := f.getProject(rawProjectID)
if err != nil {
return "", errors.WithStack(err)
}
pullRequestID, err := strconv.ParseInt(rawPullRequestID, 10, 64)
if err != nil {
return "", errors.Wrapf(err, "could not parse pull request id '%v'", rawPullRequestID)
}
diff, _, err := f.client.GetPullRequestDiff(project.Owner.UserName, project.Name, pullRequestID, gitea.PullRequestDiffOptions{
Binary: false,
})
if err != nil {
return "", errors.WithStack(err)
}
return string(diff), nil
}
// ListOpenedPullRequests implements port.Forge.
func (f *Forge) ListOpenedPullRequests(ctx context.Context, rawProjectID string) ([]*model.PullRequest, error) {
project, err := f.getProject(rawProjectID)
if err != nil {
return nil, errors.WithStack(err)
}
pullRequests := make([]*model.PullRequest, 0)
page := 1
for {
repoPullRequests, res, err := f.client.ListRepoPullRequests(project.Owner.UserName, project.Name, gitea.ListPullRequestsOptions{
State: gitea.StateOpen,
ListOptions: gitea.ListOptions{
Page: page,
PageSize: 100,
},
})
if err != nil {
return nil, errors.WithStack(err)
}
for _, pr := range repoPullRequests {
pullRequests = append(pullRequests, &model.PullRequest{
ID: strconv.FormatInt(pr.Index, 10),
Title: pr.Title,
Body: pr.Body,
})
}
if res.NextPage == 0 {
return pullRequests, nil
}
page = res.NextPage
}
}
// GetFile implements port.Forge.
func (f *Forge) GetFile(ctx context.Context, rawProjectID string, path string) ([]byte, error) {
project, err := f.getProject(rawProjectID)
@ -132,8 +254,8 @@ func (f *Forge) GetIssues(ctx context.Context, rawProjectID string, issueIDs ...
return issues, nil
}
// GetAllProjects implements port.Forge.
func (f *Forge) GetAllProjects(ctx context.Context) ([]*model.Project, error) {
// ListProjects implements port.Forge.
func (f *Forge) ListProjects(ctx context.Context) ([]*model.Project, error) {
projects := make([]*model.Project, 0)
page := 1