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

@ -18,6 +18,168 @@ type Forge struct {
client *github.Client
}
// UpdatePullRequest implements port.Forge.
func (f *Forge) UpdatePullRequest(ctx context.Context, rawProjectID string, rawPullRequestID string, title string, body string) (string, error) {
repo, err := f.getRepository(ctx, 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, res, err := f.client.PullRequests.Edit(ctx, *repo.Owner.Login, *repo.Name, int(pullRequestID), &github.PullRequest{
Title: &title,
Body: &body,
})
if err != nil {
if res != nil && (res.StatusCode == http.StatusForbidden || res.StatusCode == http.StatusUnauthorized) {
slog.ErrorContext(ctx, "could not edit pull request", slog.Any("error", errors.WithStack(err)))
return "", errors.WithStack(service.ErrForgeNotAvailable)
}
return "", errors.WithStack(err)
}
return *pr.HTMLURL, nil
}
// GetPullRequestTemplate implements port.Forge.
func (f *Forge) GetPullRequestTemplate(ctx context.Context, projectID string) (string, error) {
data, err := f.GetFile(ctx, projectID, ".github/PULL_REQUEST_TEMPLATE/pull_request_template.md")
if err != nil {
return "", errors.WithStack(err)
}
return string(data), nil
}
// GetPullRequestDiff implements port.Forge.
func (f *Forge) GetPullRequests(ctx context.Context, projectID string, pullRequestIDs ...string) ([]*model.PullRequest, error) {
repo, err := f.getRepository(ctx, projectID)
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, res, err := f.client.PullRequests.Get(ctx, *repo.Owner.Login, *repo.Name, int(id))
if err != nil {
if res != nil && (res.StatusCode == http.StatusForbidden || res.StatusCode == http.StatusUnauthorized) {
slog.ErrorContext(ctx, "could not retrieve repository pull requests", slog.Any("error", errors.WithStack(err)))
return nil, errors.WithStack(service.ErrForgeNotAvailable)
}
return nil, errors.WithStack(err)
}
var body string
if pr.Body != nil {
body = *pr.Body
}
var title string
if pr.Title != nil {
title = *pr.Title
}
pullRequests = append(pullRequests, &model.PullRequest{
ID: strconv.FormatInt(*pr.ID, 10),
Title: title,
Body: body,
})
}
return pullRequests, nil
}
// GetPullRequestDiff implements port.Forge.
func (f *Forge) GetPullRequestDiff(ctx context.Context, projectID string, rawPullRequestID string) (string, error) {
repo, err := f.getRepository(ctx, projectID)
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'", pullRequestID)
}
diff, res, err := f.client.PullRequests.GetRaw(ctx, *repo.Owner.Login, *repo.Name, int(pullRequestID), github.RawOptions{
Type: github.Diff,
})
if err != nil {
if res != nil && (res.StatusCode == http.StatusForbidden || res.StatusCode == http.StatusUnauthorized) {
slog.ErrorContext(ctx, "could not retrieve pull request diff", slog.Any("error", errors.WithStack(err)))
return "", errors.WithStack(service.ErrForgeNotAvailable)
}
return "", errors.WithStack(err)
}
return diff, nil
}
// ListOpenedPullRequests implements port.Forge.
func (f *Forge) ListOpenedPullRequests(ctx context.Context, projectID string) ([]*model.PullRequest, error) {
repo, err := f.getRepository(ctx, projectID)
if err != nil {
return nil, errors.WithStack(err)
}
pullRequests := make([]*model.PullRequest, 0)
page := 1
for {
repoPullRequests, res, err := f.client.PullRequests.List(ctx, *repo.Owner.Login, *repo.Name, &github.PullRequestListOptions{
State: "opened",
ListOptions: github.ListOptions{
Page: page,
PerPage: 100,
},
})
if err != nil {
if res != nil && (res.StatusCode == http.StatusForbidden || res.StatusCode == http.StatusUnauthorized) {
slog.ErrorContext(ctx, "could not retrieve repository pull requests", slog.Any("error", errors.WithStack(err)))
return nil, errors.WithStack(service.ErrForgeNotAvailable)
}
return nil, errors.WithStack(err)
}
for _, pr := range repoPullRequests {
var body string
if pr.Body != nil {
body = *pr.Body
}
var title string
if pr.Title != nil {
title = *pr.Title
}
pullRequests = append(pullRequests, &model.PullRequest{
ID: strconv.FormatInt(*pr.ID, 10),
Title: title,
Body: body,
})
}
if res.NextPage == 0 {
return pullRequests, nil
}
page = res.NextPage
}
}
// CreateIssue implements port.Forge.
func (f *Forge) CreateIssue(ctx context.Context, projectID string, title string, body string) (string, error) {
repo, err := f.getRepository(ctx, projectID)
@ -41,8 +203,8 @@ func (f *Forge) CreateIssue(ctx context.Context, projectID string, title string,
return *issue.HTMLURL, 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