2023-04-25 20:32:08 -04:00
|
|
|
package vc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"go/format"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2023-09-04 16:44:59 -04:00
|
|
|
"path"
|
2023-04-25 20:32:08 -04:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mobyvb/pull-pal/llm"
|
2023-05-04 20:01:46 -04:00
|
|
|
"go.uber.org/zap"
|
2023-04-25 20:32:08 -04:00
|
|
|
|
|
|
|
"github.com/go-git/go-git/v5"
|
|
|
|
"github.com/go-git/go-git/v5/config"
|
2023-05-08 20:14:19 -04:00
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
2023-04-25 20:32:08 -04:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LocalGitClient represents a service that interacts with a local git repository.
|
|
|
|
type LocalGitClient struct {
|
2023-05-12 01:22:10 -04:00
|
|
|
log *zap.Logger
|
2023-04-25 20:32:08 -04:00
|
|
|
self Author
|
|
|
|
repo Repository
|
|
|
|
|
|
|
|
worktree *git.Worktree
|
2023-09-04 16:44:59 -04:00
|
|
|
debugDir string
|
2023-04-25 20:32:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLocalGitClient initializes a local git client by checking out a repository locally.
|
2023-09-04 16:44:59 -04:00
|
|
|
func NewLocalGitClient(log *zap.Logger, self Author, repo Repository, debugDir string) (*LocalGitClient, error) {
|
2023-05-04 20:01:46 -04:00
|
|
|
log.Info("checking out local github repo", zap.String("repo name", repo.Name), zap.String("local path", repo.LocalPath))
|
2023-04-25 20:32:08 -04:00
|
|
|
// clone provided repository to local path
|
|
|
|
if repo.LocalPath == "" {
|
|
|
|
return nil, errors.New("local path to clone repository not provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove local repo if it exists already
|
|
|
|
err := os.RemoveAll(repo.LocalPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
localRepo, err := git.PlainClone(repo.LocalPath, false, &git.CloneOptions{
|
|
|
|
URL: repo.SSH(),
|
|
|
|
// URL: repo.HTTPS(),
|
|
|
|
Auth: &http.BasicAuth{
|
|
|
|
Username: self.Handle,
|
|
|
|
Password: self.Token,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
repo.localRepo = localRepo
|
|
|
|
|
|
|
|
return &LocalGitClient{
|
2023-09-04 16:44:59 -04:00
|
|
|
log: log,
|
|
|
|
self: self,
|
|
|
|
repo: repo,
|
|
|
|
debugDir: debugDir,
|
2023-04-25 20:32:08 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-05-08 20:14:19 -04:00
|
|
|
func (gc *LocalGitClient) CheckoutRemoteBranch(branchName string) (err error) {
|
2023-05-04 20:01:46 -04:00
|
|
|
if gc.worktree == nil {
|
|
|
|
return errors.New("worktree is nil - cannot check out a branch")
|
|
|
|
}
|
|
|
|
|
2023-05-08 20:14:19 -04:00
|
|
|
// TODO configurable remote
|
|
|
|
branchRefName := plumbing.NewRemoteReferenceName("origin", branchName)
|
|
|
|
branchCoOpts := git.CheckoutOptions{
|
|
|
|
Branch: plumbing.ReferenceName(branchRefName),
|
|
|
|
Force: true,
|
|
|
|
}
|
|
|
|
err = gc.worktree.Checkout(&branchCoOpts)
|
2023-05-04 20:01:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-25 20:32:08 -04:00
|
|
|
|
2023-05-08 20:14:19 -04:00
|
|
|
// Pull the latest changes from the remote branch
|
2023-05-12 01:22:10 -04:00
|
|
|
/*
|
|
|
|
err = gc.worktree.Pull(&git.PullOptions{
|
|
|
|
RemoteName: "origin",
|
|
|
|
Auth: &http.BasicAuth{
|
|
|
|
Username: gc.self.Handle,
|
|
|
|
Password: gc.self.Token,
|
|
|
|
},
|
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
if err != nil && err != git.NoErrAlreadyUpToDate {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*/
|
2023-04-25 20:32:08 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gc *LocalGitClient) PushBranch(branchName string) (err error) {
|
2023-05-04 20:01:46 -04:00
|
|
|
//branchRefName := plumbing.NewBranchReferenceName(branchName)
|
2023-04-25 20:32:08 -04:00
|
|
|
remoteName := "origin"
|
|
|
|
|
2023-05-08 20:14:19 -04:00
|
|
|
headRef, err := gc.repo.localRepo.Head()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new branch at current HEAD
|
|
|
|
branchRef := plumbing.NewHashReference(plumbing.NewBranchReferenceName(branchName), headRef.Hash())
|
|
|
|
err = gc.repo.localRepo.Storer.SetReference(branchRef)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-25 20:32:08 -04:00
|
|
|
// Push the new branch to the remote repository
|
|
|
|
remote, err := gc.repo.localRepo.Remote(remoteName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = remote.Push(&git.PushOptions{
|
2023-05-04 20:01:46 -04:00
|
|
|
RemoteName: remoteName,
|
|
|
|
// TODO remove hardcoded "main"
|
2023-05-08 20:14:19 -04:00
|
|
|
RefSpecs: []config.RefSpec{config.RefSpec(fmt.Sprintf("+refs/heads/%s:refs/heads/%s", branchName, branchName))},
|
2023-04-25 20:32:08 -04:00
|
|
|
Auth: &http.BasicAuth{
|
|
|
|
Username: gc.self.Handle,
|
|
|
|
Password: gc.self.Token,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gc *LocalGitClient) GetLocalFile(path string) (llm.File, error) {
|
|
|
|
fullPath := filepath.Join(gc.repo.LocalPath, path)
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
// if file doesn't exist, just return an empty file
|
|
|
|
// this means we want to prompt the llm to populate it for the first time
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
return llm.File{
|
|
|
|
Path: path,
|
|
|
|
Contents: "",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return llm.File{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return llm.File{
|
|
|
|
Path: path,
|
|
|
|
Contents: string(data),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gc *LocalGitClient) StartCommit() error {
|
|
|
|
if gc.worktree != nil {
|
|
|
|
return errors.New("worktree is not nil - cannot start a new commit")
|
|
|
|
}
|
|
|
|
|
|
|
|
worktree, err := gc.repo.localRepo.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
gc.worktree = worktree
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReplaceOrAddLocalFile updates or adds a file in the locally cloned repo, and applies these changes to the current git worktree.
|
|
|
|
func (gc *LocalGitClient) ReplaceOrAddLocalFile(newFile llm.File) error {
|
|
|
|
if gc.worktree == nil {
|
|
|
|
return errors.New("worktree is nil - StartCommit must be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO format non-go files as well
|
|
|
|
if strings.HasSuffix(newFile.Path, ".go") {
|
|
|
|
newContents, err := format.Source([]byte(newFile.Contents))
|
|
|
|
if err != nil {
|
2023-05-04 20:01:46 -04:00
|
|
|
// TODO also make logger accessible
|
|
|
|
fmt.Println("go format error")
|
|
|
|
// TODO handle this error
|
|
|
|
// return err
|
|
|
|
} else {
|
|
|
|
newFile.Contents = string(newContents)
|
2023-04-25 20:32:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fullPath := filepath.Join(gc.repo.LocalPath, newFile.Path)
|
2023-05-15 19:23:36 -04:00
|
|
|
dirPath := filepath.Dir(fullPath)
|
|
|
|
err := os.MkdirAll(dirPath, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-25 20:32:08 -04:00
|
|
|
|
2023-05-15 19:23:36 -04:00
|
|
|
err = ioutil.WriteFile(fullPath, []byte(newFile.Contents), 0644)
|
2023-04-25 20:32:08 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = gc.worktree.Add(newFile.Path)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FinishCommit completes a commit, after which a code change request can be opened or updated.
|
|
|
|
func (gc *LocalGitClient) FinishCommit(message string) error {
|
|
|
|
if gc.worktree == nil {
|
|
|
|
return errors.New("worktree is nil - StartCommit must be called")
|
|
|
|
}
|
|
|
|
_, err := gc.worktree.Commit(message, &git.CommitOptions{
|
|
|
|
Author: &object.Signature{
|
|
|
|
Name: gc.self.Handle,
|
|
|
|
Email: gc.self.Email,
|
|
|
|
When: time.Now(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// set worktree to nil so a new commit can be started
|
|
|
|
gc.worktree = nil
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-12 00:59:21 -04:00
|
|
|
|
|
|
|
// ParseIssueAndStartCommit parses the information provided in the issue to check out the appropriate branch,
|
|
|
|
// get the contents of the files mentioned in the issue, and initialize the worktree.
|
|
|
|
func (gc *LocalGitClient) ParseIssueAndStartCommit(issue Issue) (llm.CodeChangeRequest, error) {
|
|
|
|
var changeRequest llm.CodeChangeRequest
|
|
|
|
|
|
|
|
if gc.worktree != nil {
|
|
|
|
return changeRequest, errors.New("worktree is active - some other work is incomplete")
|
|
|
|
}
|
|
|
|
|
|
|
|
issueBody := ParseIssueBody(issue.Body)
|
2023-09-04 16:44:59 -04:00
|
|
|
gc.log.Info("issue body info", zap.Any("files", issueBody.FilePaths))
|
2023-05-12 00:59:21 -04:00
|
|
|
|
|
|
|
// start a worktree
|
|
|
|
err := gc.StartCommit()
|
|
|
|
if err != nil {
|
2023-05-12 01:22:10 -04:00
|
|
|
gc.log.Error("error starting commit", zap.Error(err))
|
2023-05-12 00:59:21 -04:00
|
|
|
return changeRequest, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = gc.CheckoutRemoteBranch(issueBody.BaseBranch)
|
|
|
|
if err != nil {
|
2023-05-12 01:22:10 -04:00
|
|
|
gc.log.Error("error checking out remote branch", zap.Error(err))
|
2023-05-12 00:59:21 -04:00
|
|
|
return changeRequest, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// get file contents from local git repository
|
|
|
|
files := []llm.File{}
|
|
|
|
for _, path := range issueBody.FilePaths {
|
|
|
|
nextFile, err := gc.GetLocalFile(path)
|
|
|
|
if err != nil {
|
2023-05-12 01:22:10 -04:00
|
|
|
gc.log.Error("error getting local file", zap.Error(err))
|
2023-05-12 00:59:21 -04:00
|
|
|
return changeRequest, err
|
|
|
|
}
|
|
|
|
files = append(files, nextFile)
|
|
|
|
}
|
|
|
|
|
2023-09-04 16:44:59 -04:00
|
|
|
req := llm.CodeChangeRequest{
|
2023-05-12 00:59:21 -04:00
|
|
|
Subject: issue.Subject,
|
|
|
|
Body: issueBody.PromptBody,
|
|
|
|
IssueNumber: issue.Number,
|
|
|
|
Files: files,
|
|
|
|
BaseBranch: issueBody.BaseBranch,
|
2023-09-04 16:44:59 -04:00
|
|
|
}
|
|
|
|
debugFileNamePrefix := fmt.Sprintf("issue-%d-%d", issue.Number, time.Now().Unix())
|
|
|
|
gc.writeDebug("issues", debugFileNamePrefix+"-originalbody.txt", issue.Body)
|
|
|
|
gc.writeDebug("issues", debugFileNamePrefix+"-parsed-req.txt", req.String())
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gc *LocalGitClient) writeDebug(subdir, filename, contents string) {
|
|
|
|
if gc.debugDir == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fullFolderPath := path.Join(gc.debugDir, subdir)
|
|
|
|
|
|
|
|
err := os.MkdirAll(fullFolderPath, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
gc.log.Error("failed to ensure debug directory existed", zap.String("folderpath", fullFolderPath), zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fullPath := path.Join(fullFolderPath, filename)
|
|
|
|
err = ioutil.WriteFile(fullPath, []byte(contents), 0644)
|
|
|
|
if err != nil {
|
|
|
|
gc.log.Error("failed to write response to debug file", zap.String("filepath", fullPath), zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
gc.log.Info("response written to debug file", zap.String("filepath", fullPath))
|
2023-05-12 00:59:21 -04:00
|
|
|
}
|