Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add gh repository #1

Merged
merged 6 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,4 @@ import (

func main() {
cli.Run()
// pullRequestId := os.Getenv("BITBUCKET_PR_ID")
// if pullRequestId == "" {
// panic("Failed Get PULL REQUEST ID")
// }
// cli.RunE(pullRequestId)
}
14 changes: 8 additions & 6 deletions go.mod
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit adds a new dependency, go.uber.org/goleak, to the project. This dependency is used to detect potential memory leaks in Go code. You can find more information about it here: https://pkg.go.dev/go.uber.org/goleak. It is important to make sure that you are not introducing any new vulnerabilities by adding this dependency. You should review the source code of the dependency, as well as any associated documentation, to make sure that you are comfortable with the security implications of adding it. If you have any concerns, please ask the author of this commit for more information.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit adds new dependencies to the project, which are not needed for this PR, consider removing unused libraries.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ module codebleu

go 1.22.4

require github.com/google/generative-ai-go v0.14.0
require (
github.com/go-chi/chi/v5 v5.0.14
github.com/go-playground/webhooks/v6 v6.3.0
github.com/google/generative-ai-go v0.14.0
github.com/urfave/cli/v2 v2.27.2
go.uber.org/fx v1.22.0
google.golang.org/api v0.180.0
)

require (
cloud.google.com/go v0.113.0 // indirect
Expand All @@ -13,18 +20,15 @@ require (
cloud.google.com/go/longrunning v0.5.7 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-chi/chi/v5 v5.0.14 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/webhooks/v6 v6.3.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.4 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/urfave/cli/v2 v2.27.2 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.51.0 // indirect
Expand All @@ -33,7 +37,6 @@ require (
go.opentelemetry.io/otel/metric v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
go.uber.org/dig v1.17.1 // indirect
go.uber.org/fx v1.22.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
Expand All @@ -43,7 +46,6 @@ require (
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/api v0.180.0 // indirect
google.golang.org/genproto v0.0.0-20240401170217-c3f982113cda // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240513163218-0867130af1f8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240509183442-62759503f434 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit adds a new dependency, go.uber.org/goleak, to the project. This dependency is used to detect potential memory leaks in Go code. You can find more information about it here: https://pkg.go.dev/go.uber.org/goleak. It is important to make sure that you are not introducing any new vulnerabilities by adding this dependency. You should review the source code of the dependency, as well as any associated documentation, to make sure that you are comfortable with the security implications of adding it. If you have any concerns, please ask the author of this commit for more information.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit adds new dependencies to the project, which are not needed for this PR, consider removing unused libraries.

Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ go.uber.org/dig v1.17.1 h1:Tga8Lz8PcYNsWsyHMZ1Vm0OQOUaJNDyvPImgbAu9YSc=
go.uber.org/dig v1.17.1/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE=
go.uber.org/fx v1.22.0 h1:pApUK7yL0OUHMd8vkunWSlLxZVFFk70jR2nKde8X2NM=
go.uber.org/fx v1.22.0/go.mod h1:HT2M7d7RHo+ebKGh9NRcrsrHHfpZ60nW3QRubMRfv48=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
Expand Down
26 changes: 0 additions & 26 deletions internal/app/cli/app.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package cli

import (
infraBitbucket "codebleu/internal/infrastructure/http/bitbucket"
infraGemini "codebleu/internal/infrastructure/thirdparty/gemini"
codeAssistantUseCase "codebleu/internal/usecase/codeassistant"
gitRepoUseCase "codebleu/internal/usecase/gitrepo"
llmUseCase "codebleu/internal/usecase/llm"
"context"
"log"
"os"
)
Expand All @@ -16,23 +10,3 @@ func Run() {
log.Fatalln(err)
}
}

func RunE(pullRequestId string) {
cfg := NewConfigFromEnv()
bitbucketClient := infraBitbucket.NewClient(
cfg.BitbucketRepositoryAccessConfig.Workspace,
cfg.BitbucketRepositoryAccessConfig.RepoSlug,
cfg.BitbucketRepositoryAccessConfig.AccessToken,
)
geminiClient := infraGemini.NewClient("gemini-1.5-flash", cfg.GeminiConfig.ApiKey)
getPullRequest := gitRepoUseCase.GetPullRequest(bitbucketClient)
postPullRequestComment := gitRepoUseCase.PostPullRequestComment(bitbucketClient)
sendPromptUseCase := llmUseCase.SendPromptUseCase(geminiClient)
reviewPullRequest := codeAssistantUseCase.ReviewPullRequest(sendPromptUseCase)
reviewAndCommentPullRequest := codeAssistantUseCase.ReviewAndCommentPullRequest(getPullRequest, reviewPullRequest, postPullRequestComment)

_, err := reviewAndCommentPullRequest.Invoke(context.Background(), pullRequestId)
if err != nil {
panic(err)
}
}
28 changes: 26 additions & 2 deletions internal/app/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package cli

import (
"codebleu/internal/domain/codeassistant"
"codebleu/internal/infrastructure/factories/gitrepo"
"codebleu/internal/infrastructure/factories/llm"
"context"
"errors"
"fmt"
"io/ioutil"

codeAssistantUseCase "codebleu/internal/usecase/codeassistant"
gitRepoUseCase "codebleu/internal/usecase/gitrepo"
Expand All @@ -22,9 +25,10 @@ func NewCliApp() *cli.App {
cliApp.Flags = []cli.Flag{
&cli.StringFlag{
Name: "model",
Usage: `uses model to review pull request (options: "gemini-1.5-flash", "gemini-1.5-pro", "gemini-1.0-pro")`,
Usage: `uses model to review pull request (options: "gemini-1.5-flash" (default), "gemini-1.5-pro", "gemini-1.0-pro")`,
Aliases: []string{"m"},
EnvVars: []string{"MODEL"},
Value: "gemini-1.5-flash",
Required: true,
},
&cli.StringFlag{
Expand All @@ -40,6 +44,11 @@ func NewCliApp() *cli.App {
EnvVars: []string{"PULL_REQUEST_ID"},
Required: true,
},
&cli.StringFlag{
Name: "system-instruction",
Usage: "Custom system instruction for review pull request diff chages",
EnvVars: []string{"SYSTEM_INSTRUCTION"},
},
}
cliApp.Action = action

Expand All @@ -63,12 +72,27 @@ func action(ctx *cli.Context) error {
if err != nil {
return err
}
systemInstructionPath := ctx.String("system-instruction")
systemInstruction := ""
if systemInstructionPath != "" {
content, err := ioutil.ReadFile(systemInstructionPath)
if err != nil {
return errors.Join(err, fmt.Errorf("failed read file %s", systemInstructionPath))
}
systemInstruction = string(content)
}
getPullRequest := gitRepoUseCase.GetPullRequest(remoteRepo)
postPullRequestComment := gitRepoUseCase.PostPullRequestComment(remoteRepo)
sendPromptUseCase := llmUseCase.SendPromptUseCase(llmRepo)
reviewPullRequest := codeAssistantUseCase.ReviewPullRequest(sendPromptUseCase)
reviewAndCommentPullRequest := codeAssistantUseCase.ReviewAndCommentPullRequest(getPullRequest, reviewPullRequest, postPullRequestComment)
if _, err := reviewAndCommentPullRequest.Invoke(context.Background(), ctx.String("id")); err != nil {
if _, err := reviewAndCommentPullRequest.Invoke(
context.Background(),
codeassistant.ReviewAndCommentPullRequestInput{
PullRequestId: ctx.String("id"),
SystemInstruction: systemInstruction,
},
); err != nil {
return err
}
return nil
Expand Down
18 changes: 15 additions & 3 deletions internal/domain/codeassistant/entity.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
package codeassistant

import "codebleu/internal/domain/gitrepo"
import (
"codebleu/internal/domain/gitrepo"
)

type PullRequestReviewInput struct {
PullRequest *gitrepo.PullRequest
ReviewPrompt string
PullRequest *gitrepo.PullRequest
SystemInstruction string
}

type ReviewAndCommentPullRequestInput struct {
PullRequestId string
SystemInstruction string
}

type ReviewResult struct {
Path string `json:"path"`
Comment string `json:"comment_in_markdown"`
}
3 changes: 3 additions & 0 deletions internal/domain/gitrepo/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package gitrepo

type PostPullRequestCommentInput struct {
PullRequestId string
CommitHash string
Comment string
Path string
}

type PullRequest struct {
Id string
Title string
Description string
DiffPatch string
CommitHash string
}
6 changes: 6 additions & 0 deletions internal/domain/llm/entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package llm

type PromptInput struct {
SystemInstruction string
Prompt string
}
2 changes: 1 addition & 1 deletion internal/domain/llm/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package llm
import "context"

type Repository interface {
SendPrompt(ctx context.Context, prompt string) (string, error)
SendPrompt(ctx context.Context, input PromptInput) (string, error)
}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit adds a new case to the NewRepository function. This case allows the function to create a new repository instance for GitHub. The new code is well-formatted and follows the existing style of the codebase. You can find more information about GitHub's API here: https://docs.github.com/en/rest.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit adds a new github repository provider, which is great. However, consider moving the implementation details of each provider to a separate file for better organization and maintainability.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gitrepo
import (
"codebleu/internal/domain/gitrepo"
"codebleu/internal/infrastructure/http/bitbucket"
"codebleu/internal/infrastructure/http/github"
"codebleu/pkg/env"
"fmt"
)
Expand All @@ -15,6 +16,12 @@ func NewRepository(provider string) (gitrepo.Repository, error) {
env.MustString("BITBUCKET_REPO_SLUG"),
env.MustString("BITBUCKET_ACCESS_TOKEN"),
), nil
case "github":
return github.NewClient(
env.MustString("GH_OWNER"),
env.MustString("GH_REPO_SLUG"),
env.MustString("GH_ACCESS_TOKEN"),
), nil
default:
return nil, fmt.Errorf("unsupported remote repository provider %s", provider)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/infrastructure/http/bitbucket/client.go
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit changes the type of the httpClient field from http.Client to *http.Client. This change is necessary because the http.Client type is not a pointer. The & operator is used to create a pointer to the http.Client object. This change is made to ensure that the httpClient field is initialized with a valid pointer to an http.Client object.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The http.Client is initialized with its default values, consider setting timeout and other options in the constructor.

Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
)

type client struct {
httpClient http.Client
httpClient *http.Client
workspace string
repoSlug string
accessToken string
}

func NewClient(workspace string, repoSlug string, accessToken string) domain.Repository {
httpClient := http.Client{}
httpClient := &http.Client{}
return &client{
httpClient: httpClient,
workspace: workspace,
Expand Down
1 change: 1 addition & 0 deletions internal/infrastructure/http/bitbucket/get_pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (c *client) GetPullRequest(ctx context.Context, id string) (*domain.PullReq
Title: pullRequestResponse.Title,
Description: pullRequestResponse.Description,
DiffPatch: diff,
CommitHash: pullRequestResponse.Source.Commit.Hash,
}, nil
}

Expand Down
5 changes: 5 additions & 0 deletions internal/infrastructure/http/bitbucket/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package bitbucket

type PostPullRequestCommentRequest struct {
Content *PullRequestCommentContent `json:"content"`
Inline *PullRequestCommentInline `json:"inline"`
}

type PullRequestCommentContent struct {
Raw string `json:"raw"`
}

type PullRequestCommentInline struct {
Path string `json:"path"`
}

type PullRequestResponse struct {
CommentCount int64 `json:"comment_count"`
TaskCount int64 `json:"task_count"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ func (c *client) PostPullRequestComment(ctx context.Context, input domain.PostPu
Content: &PullRequestCommentContent{
Raw: input.Comment,
},
Inline: &PullRequestCommentInline{
Path: input.Path,
},
}
if err := c.doRequest(ctx, http.MethodPost, fmt.Sprintf("/pullrequests/%s/comments", input.PullRequestId), payload, &response); err != nil {
return err
}
// TODO: REMOVE THIS
fmt.Printf("Response\n%+v\n", response)
return nil
}
27 changes: 27 additions & 0 deletions internal/infrastructure/http/github/client.go
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit adds a new file, client.go, to the project. This file contains the implementation of the github client, which is used to interact with the GitHub API. The code is well-formatted and follows the existing style of the codebase. You can find more information about GitHub's API here: https://docs.github.com/en/rest.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PostPullRequestComment function doesn't implement the actual logic of posting a comment. Consider adding the implementation.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package github

import (
"codebleu/internal/domain/gitrepo"
"net/http"
)

type client struct {
accessToken string
owner string
repoSlug string
httpClient *http.Client
}

func NewClient(
owner string,
repoSlug string,
accessToken string,
) gitrepo.Repository {
httpClient := &http.Client{}
return &client{
owner: owner,
repoSlug: repoSlug,
accessToken: accessToken,
httpClient: httpClient,
}
}
Loading