2023-04-22 16:23:56 -04:00
|
|
|
package vc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2023-05-08 20:14:19 -04:00
|
|
|
"strings"
|
2023-04-22 16:59:59 -04:00
|
|
|
|
|
|
|
"github.com/mobyvb/pull-pal/llm"
|
2023-04-22 16:23:56 -04:00
|
|
|
|
|
|
|
"github.com/google/go-github/github"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GithubClient implements the VCClient interface.
|
|
|
|
type GithubClient struct {
|
|
|
|
ctx context.Context
|
|
|
|
log *zap.Logger
|
|
|
|
|
|
|
|
client *github.Client
|
|
|
|
self Author
|
|
|
|
repo Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGithubClient initializes a Github client and checks out a repository locally.
|
|
|
|
func NewGithubClient(ctx context.Context, log *zap.Logger, self Author, repo Repository) (*GithubClient, error) {
|
2023-04-22 21:41:28 -04:00
|
|
|
log.Info("Creating new Github client...")
|
2023-04-22 16:23:56 -04:00
|
|
|
if self.Token == "" {
|
|
|
|
return nil, errors.New("Github access token not provided")
|
|
|
|
}
|
|
|
|
ts := oauth2.StaticTokenSource(
|
|
|
|
&oauth2.Token{AccessToken: self.Token},
|
|
|
|
)
|
|
|
|
// oauth client is used to list issues, open pull requests, etc...
|
|
|
|
tc := oauth2.NewClient(ctx, ts)
|
|
|
|
|
2023-04-22 21:41:28 -04:00
|
|
|
log.Info("Success. Github client set up.")
|
2023-04-22 16:23:56 -04:00
|
|
|
|
|
|
|
return &GithubClient{
|
|
|
|
ctx: ctx,
|
|
|
|
log: log,
|
|
|
|
client: github.NewClient(tc),
|
|
|
|
self: self,
|
|
|
|
repo: repo,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-22 16:59:59 -04:00
|
|
|
// OpenCodeChangeRequest pushes to a new remote branch and opens a PR on Github.
|
2023-05-12 00:59:21 -04:00
|
|
|
func (gc *GithubClient) OpenCodeChangeRequest(req llm.CodeChangeRequest, res llm.CodeChangeResponse, fromBranch string) (id, url string, err error) {
|
2023-04-22 16:23:56 -04:00
|
|
|
// TODO handle gc.ctx canceled
|
|
|
|
|
|
|
|
title := req.Subject
|
2023-04-22 22:54:00 -04:00
|
|
|
if title == "" {
|
|
|
|
title = "update files"
|
|
|
|
}
|
2023-05-04 20:01:46 -04:00
|
|
|
|
2023-04-22 16:23:56 -04:00
|
|
|
body := res.Notes
|
2023-05-12 00:59:21 -04:00
|
|
|
body += fmt.Sprintf("\n\nResolves #%d", req.IssueNumber)
|
2023-04-22 16:23:56 -04:00
|
|
|
|
2023-04-22 16:59:59 -04:00
|
|
|
// Finally, open a pull request from the new branch.
|
2023-04-22 16:23:56 -04:00
|
|
|
pr, _, err := gc.client.PullRequests.Create(gc.ctx, gc.repo.Owner.Handle, gc.repo.Name, &github.NewPullRequest{
|
|
|
|
Title: &title,
|
2023-05-04 20:01:46 -04:00
|
|
|
Head: &fromBranch,
|
2023-05-12 00:59:21 -04:00
|
|
|
Base: &req.BaseBranch,
|
2023-04-22 16:23:56 -04:00
|
|
|
Body: &body,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
url = pr.GetHTMLURL()
|
|
|
|
id = strconv.Itoa(int(pr.GetID()))
|
|
|
|
|
|
|
|
return id, url, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListOpenIssues lists unresolved issues in the Github repository.
|
2023-04-24 19:49:10 -04:00
|
|
|
func (gc *GithubClient) ListOpenIssues(options ListIssueOptions) ([]Issue, error) {
|
2023-04-22 16:23:56 -04:00
|
|
|
// List and parse GitHub issues
|
2023-04-24 19:49:10 -04:00
|
|
|
opt := &github.IssueListByRepoOptions{
|
|
|
|
Labels: options.Labels,
|
|
|
|
}
|
|
|
|
issues, _, err := gc.client.Issues.ListByRepo(gc.ctx, gc.repo.Owner.Handle, gc.repo.Name, opt)
|
2023-04-22 16:23:56 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-22 17:50:57 -04:00
|
|
|
toReturn := []Issue{}
|
2023-04-22 16:23:56 -04:00
|
|
|
for _, issue := range issues {
|
2023-04-24 19:49:10 -04:00
|
|
|
issueUser := issue.GetUser().GetLogin()
|
|
|
|
allowedUser := false
|
|
|
|
for _, u := range options.Handles {
|
|
|
|
if issueUser == u {
|
|
|
|
allowedUser = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !allowedUser {
|
2023-04-22 17:50:57 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-04-22 16:23:56 -04:00
|
|
|
nextIssue := Issue{
|
2023-05-12 00:59:21 -04:00
|
|
|
Number: issue.GetNumber(),
|
2023-04-22 16:23:56 -04:00
|
|
|
Subject: issue.GetTitle(),
|
|
|
|
Body: issue.GetBody(),
|
|
|
|
URL: issue.GetHTMLURL(),
|
|
|
|
Author: Author{
|
|
|
|
Email: issue.GetUser().GetEmail(),
|
|
|
|
Handle: issue.GetUser().GetLogin(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
toReturn = append(toReturn, nextIssue)
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn, nil
|
|
|
|
}
|
2023-04-22 16:59:59 -04:00
|
|
|
|
2023-05-04 20:01:46 -04:00
|
|
|
// CommentOnIssue adds a comment to the issue provided.
|
|
|
|
func (gc *GithubClient) CommentOnIssue(issueNumber int, comment string) error {
|
|
|
|
ghComment := &github.IssueComment{
|
|
|
|
Body: github.String(comment),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err := gc.client.Issues.CreateComment(gc.ctx, gc.repo.Owner.Handle, gc.repo.Name, issueNumber, ghComment)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveLabelFromIssue removes the provided label from an issue if that label is applied.
|
|
|
|
func (gc *GithubClient) RemoveLabelFromIssue(issueNumber int, label string) error {
|
|
|
|
hasLabel := false
|
|
|
|
labels, _, err := gc.client.Issues.ListLabelsByIssue(gc.ctx, gc.repo.Owner.Handle, gc.repo.Name, issueNumber, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, l := range labels {
|
|
|
|
if l.GetName() == label {
|
|
|
|
hasLabel = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasLabel {
|
|
|
|
_, err = gc.client.Issues.RemoveLabelForIssue(gc.ctx, gc.repo.Owner.Handle, gc.repo.Name, issueNumber, label)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-24 19:49:10 -04:00
|
|
|
// ListOpenComments lists unresolved comments in the Github repository.
|
|
|
|
func (gc *GithubClient) ListOpenComments(options ListCommentOptions) ([]Comment, error) {
|
2023-05-08 20:14:19 -04:00
|
|
|
prs, _, err := gc.client.PullRequests.List(gc.ctx, gc.repo.Owner.Handle, gc.repo.Name, nil)
|
2023-04-24 20:28:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-08 20:14:19 -04:00
|
|
|
allComments := []Comment{}
|
|
|
|
repliedTo := make(map[int64]bool)
|
|
|
|
|
|
|
|
for _, pr := range prs {
|
|
|
|
if pr.GetUser().GetLogin() != gc.self.Handle {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
branch := ""
|
|
|
|
if pr.Head != nil {
|
|
|
|
branch = pr.Head.GetLabel()
|
|
|
|
if strings.Contains(branch, ":") {
|
|
|
|
branch = strings.Split(branch, ":")[1]
|
2023-04-24 20:28:27 -04:00
|
|
|
}
|
|
|
|
}
|
2023-05-08 20:14:19 -04:00
|
|
|
|
|
|
|
comments, _, err := gc.client.PullRequests.ListComments(gc.ctx, gc.repo.Owner.Handle, gc.repo.Name, pr.GetNumber(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-04-24 20:28:27 -04:00
|
|
|
}
|
|
|
|
|
2023-05-08 20:14:19 -04:00
|
|
|
for _, c := range comments {
|
|
|
|
commentUser := c.GetUser().GetLogin()
|
|
|
|
if commentUser == gc.self.Handle {
|
|
|
|
repliedTo[c.GetInReplyTo()] = true
|
|
|
|
}
|
|
|
|
allowedUser := false
|
|
|
|
for _, u := range options.Handles {
|
|
|
|
if commentUser == u {
|
|
|
|
allowedUser = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !allowedUser {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
nextComment := Comment{
|
|
|
|
ID: c.GetID(),
|
|
|
|
ChangeID: strconv.Itoa(pr.GetNumber()),
|
|
|
|
URL: c.GetHTMLURL(),
|
|
|
|
Author: Author{
|
|
|
|
Email: c.GetUser().GetEmail(),
|
|
|
|
Handle: c.GetUser().GetLogin(),
|
|
|
|
},
|
|
|
|
Body: c.GetBody(),
|
|
|
|
FilePath: c.GetPath(),
|
|
|
|
Position: c.GetPosition(),
|
|
|
|
DiffHunk: c.GetDiffHunk(),
|
|
|
|
Branch: branch,
|
|
|
|
PRNumber: pr.GetNumber(),
|
|
|
|
}
|
|
|
|
allComments = append(allComments, nextComment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove any comments that bot has replied to already from the list
|
|
|
|
toReturn := []Comment{}
|
|
|
|
for _, c := range allComments {
|
|
|
|
if !repliedTo[c.ID] {
|
|
|
|
toReturn = append(toReturn, c)
|
2023-04-24 20:28:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn, nil
|
2023-04-24 19:49:10 -04:00
|
|
|
}
|
2023-05-08 20:14:19 -04:00
|
|
|
|
|
|
|
// RespondToComment adds a comment to the provided thread.
|
|
|
|
func (gc *GithubClient) RespondToComment(prNumber int, commentID int64, comment string) error {
|
|
|
|
_, _, err := gc.client.PullRequests.CreateCommentInReplyTo(gc.ctx, gc.repo.Owner.Handle, gc.repo.Name, prNumber, comment, commentID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|