From e4918fcb36413c08a71c3a54850d41661b932a68 Mon Sep 17 00:00:00 2001 From: Moby von Briesen Date: Tue, 2 May 2023 22:46:47 -0400 Subject: [PATCH] wip start refactor --- cmd/root.go | 15 +++++---- pullpal/common.go | 51 ++++++++++++++++++---------- vc/common.go | 4 +-- vc/github.go | 84 ++++++++++++++++++++++++----------------------- 4 files changed, 88 insertions(+), 66 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 9bda540..4b5a6ed 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -159,13 +159,16 @@ It can be used to: continue } - prURL, err := p.ProcessResponseFromFile(changeRequest, cfg.responsePath) - if err != nil { - fmt.Println("error parsing LLM response and/or making version control changes", err) - return - } + /* + prURL, err := p.ProcessResponseFromFile(changeRequest, cfg.responsePath) + if err != nil { + fmt.Println("error parsing LLM response and/or making version control changes", err) + return + } - fmt.Printf("Successfully opened a code change request. Link: %s\n", prURL) + fmt.Printf("Successfully opened a code change request. Link: %s\n", prURL) + */ + _ = changeRequest } fmt.Println("Done. Thank you!") diff --git a/pullpal/common.go b/pullpal/common.go index d7cb267..ebbb720 100644 --- a/pullpal/common.go +++ b/pullpal/common.go @@ -28,7 +28,7 @@ type PullPal struct { log *zap.Logger listIssueOptions vc.ListIssueOptions - vcClient vc.VCClient + ghClient *vc.GithubClient localGitClient *vc.LocalGitClient openAIClient *llm.OpenAIClient } @@ -49,7 +49,7 @@ func NewPullPal(ctx context.Context, log *zap.Logger, listIssueOptions vc.ListIs log: log, listIssueOptions: listIssueOptions, - vcClient: ghClient, + ghClient: ghClient, localGitClient: localGitClient, openAIClient: llm.NewOpenAIClient(log.Named("openaiClient"), openAIToken), }, nil @@ -60,7 +60,7 @@ func (p *PullPal) Run() error { p.log.Info("Starting Pull Pal") // TODO gracefully handle context cancelation for { - issues, err := p.vcClient.ListOpenIssues(p.listIssueOptions) + issues, err := p.ghClient.ListOpenIssues(p.listIssueOptions) if err != nil { p.log.Error("error listing issues", zap.Error(err)) continue @@ -89,7 +89,7 @@ func (p *PullPal) Run() error { files := []llm.File{} for _, path := range fileList { path = strings.TrimSpace(path) - nextFile, err := p.vcClient.GetLocalFile(path) + nextFile, err := p.ghClient.GetLocalFile(path) if err != nil { p.log.Error("error getting file from vcclient", zap.Error(err)) continue @@ -115,13 +115,21 @@ func (p *PullPal) Run() error { //codeChangeResponse := llm.ParseCodeChangeResponse(llmResponse) // create commit with file changes - err = p.vcClient.StartCommit() + newBranchName := fmt.Sprintf("fix-%s", changeRequest.IssueID) + err = p.localGitClient.SwitchBranch(newBranchName) + if err != nil { + p.log.Error("error switching branch", zap.Error(err)) + continue + } + err = p.localGitClient.StartCommit() + //err = p.ghClient.StartCommit() if err != nil { p.log.Error("error starting commit", zap.Error(err)) continue } for _, f := range changeResponse.Files { - err = p.vcClient.ReplaceOrAddLocalFile(f) + err = p.localGitClient.ReplaceOrAddLocalFile(f) + // err = p.ghClient.ReplaceOrAddLocalFile(f) if err != nil { p.log.Error("error replacing or adding file", zap.Error(err)) continue @@ -129,14 +137,21 @@ func (p *PullPal) Run() error { } commitMessage := changeRequest.Subject + "\n\n" + changeResponse.Notes + "\n\nResolves: #" + changeRequest.IssueID - err = p.vcClient.FinishCommit(commitMessage) + err = p.localGitClient.FinishCommit(commitMessage) + if err != nil { + p.log.Error("error finshing commit", zap.Error(err)) + continue + } + + err = p.localGitClient.PushBranch(newBranchName) if err != nil { p.log.Error("error finshing commit", zap.Error(err)) continue } // open code change request - _, url, err := p.vcClient.OpenCodeChangeRequest(changeRequest, changeResponse) + // TODO don't hardcode main branch, make configurable + _, url, err := p.ghClient.OpenCodeChangeRequest(changeRequest, changeResponse, newBranchName, "main") if err != nil { p.log.Error("error opening PR", zap.Error(err)) } @@ -184,7 +199,7 @@ func (p *PullPal) PickIssueToClipboard() (issue vc.Issue, changeRequest llm.Code // PickIssue selects an issue from the version control server and returns the selected issue, as well as the LLM prompt needed to fulfill the request. func (p *PullPal) PickIssue() (issue vc.Issue, changeRequest llm.CodeChangeRequest, err error) { // TODO I should be able to pass in settings for listing issues from here - issues, err := p.vcClient.ListOpenIssues(p.listIssueOptions) + issues, err := p.ghClient.ListOpenIssues(p.listIssueOptions) if err != nil { return issue, changeRequest, err } @@ -209,7 +224,7 @@ func (p *PullPal) PickIssue() (issue vc.Issue, changeRequest llm.CodeChangeReque files := []llm.File{} for _, path := range fileList { path = strings.TrimSpace(path) - nextFile, err := p.vcClient.GetLocalFile(path) + nextFile, err := p.ghClient.GetLocalFile(path) if err != nil { return issue, changeRequest, err } @@ -224,6 +239,7 @@ func (p *PullPal) PickIssue() (issue vc.Issue, changeRequest llm.CodeChangeReque return issue, changeRequest, nil } +/* // ProcessResponseFromFile is the same as ProcessResponse, but the response is inputted into a file rather than passed directly as an argument. func (p *PullPal) ProcessResponseFromFile(codeChangeRequest llm.CodeChangeRequest, llmResponsePath string) (url string, err error) { data, err := ioutil.ReadFile(llmResponsePath) @@ -239,31 +255,32 @@ func (p *PullPal) ProcessResponse(codeChangeRequest llm.CodeChangeRequest, llmRe codeChangeResponse := llm.ParseCodeChangeResponse(llmResponse) // 2. create commit with file changes - err = p.vcClient.StartCommit() + err = p.ghClient.StartCommit() if err != nil { return "", err } for _, f := range codeChangeResponse.Files { - err = p.vcClient.ReplaceOrAddLocalFile(f) + err = p.ghClient.ReplaceOrAddLocalFile(f) if err != nil { return "", err } } commitMessage := codeChangeRequest.Subject + "\n\n" + codeChangeResponse.Notes + "\n\nResolves: #" + codeChangeRequest.IssueID - err = p.vcClient.FinishCommit(commitMessage) + err = p.ghClient.FinishCommit(commitMessage) if err != nil { return "", err } // 3. open code change request - _, url, err = p.vcClient.OpenCodeChangeRequest(codeChangeRequest, codeChangeResponse) + _, url, err = p.ghClient.OpenCodeChangeRequest(codeChangeRequest, codeChangeResponse) return url, err } +*/ // ListIssues gets a list of all issues meeting the provided criteria. func (p *PullPal) ListIssues(handles, labels []string) ([]vc.Issue, error) { - issues, err := p.vcClient.ListOpenIssues(vc.ListIssueOptions{ + issues, err := p.ghClient.ListOpenIssues(vc.ListIssueOptions{ Handles: handles, Labels: labels, }) @@ -276,7 +293,7 @@ func (p *PullPal) ListIssues(handles, labels []string) ([]vc.Issue, error) { // ListComments gets a list of all comments meeting the provided criteria on a PR. func (p *PullPal) ListComments(changeID string, handles []string) ([]vc.Comment, error) { - comments, err := p.vcClient.ListOpenComments(vc.ListCommentOptions{ + comments, err := p.ghClient.ListOpenComments(vc.ListCommentOptions{ ChangeID: changeID, Handles: handles, }) @@ -302,7 +319,7 @@ func (p *PullPal) MakeLocalChange(issue vc.Issue) error { files := []llm.File{} for _, path := range fileList { path = strings.TrimSpace(path) - nextFile, err := p.vcClient.GetLocalFile(path) + nextFile, err := p.ghClient.GetLocalFile(path) if err != nil { return err } diff --git a/vc/common.go b/vc/common.go index 9570601..45b92a3 100644 --- a/vc/common.go +++ b/vc/common.go @@ -3,8 +3,6 @@ package vc import ( "fmt" - "github.com/mobyvb/pull-pal/llm" - "github.com/go-git/go-git/v5" ) @@ -84,6 +82,7 @@ func (repo Repository) HTTPS() string { } // VCClient is an interface for version control server's client, e.g. a Github or Gerrit client. +/* type VCClient interface { // ListOpenIssues lists unresolved issues meeting the provided criteria on the version control server. ListOpenIssues(opts ListIssueOptions) ([]Issue, error) @@ -103,3 +102,4 @@ type VCClient interface { // FinishCommit completes a commit, after which a code change request can be opened or updated. FinishCommit(message string) error } +*/ diff --git a/vc/github.go b/vc/github.go index 2aa4f7c..1ec6231 100644 --- a/vc/github.go +++ b/vc/github.go @@ -17,8 +17,6 @@ import ( "github.com/mobyvb/pull-pal/llm" "github.com/go-git/go-git/v5" - "github.com/go-git/go-git/v5/config" - "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/google/go-github/github" @@ -91,62 +89,66 @@ func NewGithubClient(ctx context.Context, log *zap.Logger, self Author, repo Rep } // OpenCodeChangeRequest pushes to a new remote branch and opens a PR on Github. -func (gc *GithubClient) OpenCodeChangeRequest(req llm.CodeChangeRequest, res llm.CodeChangeResponse) (id, url string, err error) { +func (gc *GithubClient) OpenCodeChangeRequest(req llm.CodeChangeRequest, res llm.CodeChangeResponse, fromBranch, toBranch string) (id, url string, err error) { // TODO handle gc.ctx canceled title := req.Subject if title == "" { title = "update files" } - branchName := randomBranchName() - branchRefName := plumbing.NewBranchReferenceName(branchName) - baseBranch := "main" - remoteName := "origin" + /* + branchName := randomBranchName() + branchRefName := plumbing.NewBranchReferenceName(branchName) + baseBranch := "main" + remoteName := "origin" + */ body := res.Notes body += fmt.Sprintf("\n\nResolves #%s", req.IssueID) // Create new local branch - headRef, err := gc.repo.localRepo.Head() - if err != nil { - return "", "", err - } - err = gc.repo.localRepo.CreateBranch(&config.Branch{ - Name: branchName, - Remote: remoteName, - Merge: branchRefName, - }) - if err != nil { - return "", "", err - } + /* + headRef, err := gc.repo.localRepo.Head() + if err != nil { + return "", "", err + } + err = gc.repo.localRepo.CreateBranch(&config.Branch{ + Name: branchName, + Remote: remoteName, + Merge: branchRefName, + }) + if err != nil { + return "", "", err + } - // Update the branch to point to the new commit - err = gc.repo.localRepo.Storer.SetReference(plumbing.NewHashReference(branchRefName, headRef.Hash())) - if err != nil { - return "", "", err - } + // Update the branch to point to the new commit + err = gc.repo.localRepo.Storer.SetReference(plumbing.NewHashReference(branchRefName, headRef.Hash())) + if err != nil { + return "", "", err + } - // Push the new branch to the remote repository - remote, err := gc.repo.localRepo.Remote(remoteName) - if err != nil { - return "", "", err - } + // 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{ - RefSpecs: []config.RefSpec{config.RefSpec(fmt.Sprintf("%s:refs/heads/%s", branchRefName, branchName))}, - Auth: &http.BasicAuth{ - Username: gc.self.Handle, - Password: gc.self.Token, - }, - }) - if err != nil { - return "", "", err - } + err = remote.Push(&git.PushOptions{ + RefSpecs: []config.RefSpec{config.RefSpec(fmt.Sprintf("%s:refs/heads/%s", branchRefName, branchName))}, + Auth: &http.BasicAuth{ + Username: gc.self.Handle, + Password: gc.self.Token, + }, + }) + if err != nil { + return "", "", err + } + */ // Finally, open a pull request from the new branch. pr, _, err := gc.client.PullRequests.Create(gc.ctx, gc.repo.Owner.Handle, gc.repo.Name, &github.NewPullRequest{ Title: &title, - Head: &branchName, - Base: &baseBranch, + Head: &fromBranch, + Base: &toBranch, Body: &body, }) if err != nil {