2023-04-22 16:23:56 -04:00
|
|
|
package vc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
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-04 20:01:46 -04:00
|
|
|
func (gc *GithubClient) OpenCodeChangeRequest(req llm.CodeChangeRequest, res llm.CodeChangeResponse, fromBranch, toBranch 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
|
|
|
|
body += fmt.Sprintf("\n\nResolves #%s", req.IssueID)
|
|
|
|
|
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,
|
|
|
|
Base: &toBranch,
|
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-04-22 21:41:28 -04:00
|
|
|
ID: strconv.Itoa(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-04-24 20:28:27 -04:00
|
|
|
toReturn := []Comment{}
|
|
|
|
prNumber, err := strconv.Atoi(options.ChangeID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
comments, _, err := gc.client.PullRequests.ListComments(gc.ctx, gc.repo.Owner.Handle, gc.repo.Name, prNumber, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: filter out comments that "self" has already replied to
|
|
|
|
// TODO: ignore resolved comments
|
|
|
|
for _, c := range comments {
|
|
|
|
commentUser := c.GetUser().GetLogin()
|
|
|
|
allowedUser := false
|
|
|
|
for _, u := range options.Handles {
|
|
|
|
if commentUser == u {
|
|
|
|
allowedUser = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !allowedUser {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
nextComment := Comment{
|
|
|
|
ID: strconv.FormatInt(c.GetID(), 10),
|
|
|
|
ChangeID: options.ChangeID,
|
|
|
|
URL: c.GetHTMLURL(),
|
|
|
|
Author: Author{
|
|
|
|
Email: c.GetUser().GetEmail(),
|
|
|
|
Handle: c.GetUser().GetLogin(),
|
|
|
|
},
|
|
|
|
Body: c.GetBody(),
|
|
|
|
Position: c.GetPosition(),
|
|
|
|
DiffHunk: c.GetDiffHunk(),
|
|
|
|
}
|
|
|
|
toReturn = append(toReturn, nextComment)
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn, nil
|
2023-04-24 19:49:10 -04:00
|
|
|
}
|