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 2 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
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
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
35 changes: 35 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,35 @@
package github

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

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

// PostPullRequestComment implements gitrepo.Repository.
func (c *client) PostPullRequestComment(ctx context.Context, input gitrepo.PostPullRequestCommentInput) error {
fmt.Println(input.Comment)
return nil
}

func NewClient(
owner string,
repoSlug string,
accessToken string,
) gitrepo.Repository {
httpClient := &http.Client{}
return &client{
owner: owner,
repoSlug: repoSlug,
accessToken: accessToken,
httpClient: httpClient,
}
}
71 changes: 71 additions & 0 deletions internal/infrastructure/http/github/get_pull_request.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, get_pull_request.go, to the project. This file contains the implementation of the GetPullRequest function, which is used to retrieve a pull request from GitHub. 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.

This commit adds the logic for fetching the pull request data from GitHub API. Consider adding tests to ensure its functionality and robustness.

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

import (
"codebleu/internal/domain/gitrepo"
infraHttp "codebleu/internal/infrastructure/http"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)

// GetPullRequest implements gitrepo.Repository.
func (c *client) GetPullRequest(ctx context.Context, id string) (*gitrepo.PullRequest, error) {
pullRequest, err := c.getPullRequest(ctx, id)
if err != nil {
return nil, err
}
diff, err := c.getPullRequestDiff(ctx, id)
if err != nil {
return nil, err
}
return &gitrepo.PullRequest{
Id: id,
Title: pullRequest.Title,
Description: pullRequest.Body,
DiffPatch: diff,
}, nil
}

func (c *client) getPullRequestDiff(ctx context.Context, id string) (string, error) {
url := fmt.Sprintf("%s/repos/%s/%s/pulls/%s", c.getBaseUrl(), c.owner, c.repoSlug, id)
req, err := c.buildRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return "", err
}
req.Header.Add("Accept", "application/vnd.github.v3.diff")
httpResponse, err := c.httpClient.Do(req)
if err != nil {
return "", err
}
defer httpResponse.Body.Close()
body, err := io.ReadAll(httpResponse.Body)
if err != nil {
return "", err
}
if httpResponse.StatusCode != http.StatusOK {
return "", infraHttp.NewHttpClientError(fmt.Sprintf("response failed %s", string(body)), url)
}
return string(body), nil
}

func (c *client) getPullRequest(ctx context.Context, id string) (*PullRequestResponse, error) {
url := fmt.Sprintf("%s/repos/%s/%s/pulls/%s", c.getBaseUrl(), c.owner, c.repoSlug, id)
req, err := c.buildRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
httpResponse, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer httpResponse.Body.Close()
var responseBody *PullRequestResponse
err = json.NewDecoder(httpResponse.Body).Decode(&responseBody)
if err != nil {
return nil, errors.Join(infraHttp.NewHttpClientError("failed decode repsonse", url))
}
return responseBody, nil
}
27 changes: 27 additions & 0 deletions internal/infrastructure/http/github/helper.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, helper.go, to the project. This file contains helper functions for the github client. 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 getBaseUrl function can be refactored into a constant for better readability and maintainability.

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

import (
infraHttp "codebleu/internal/infrastructure/http"
"context"
"errors"
"fmt"
"io"
"net/http"
)

func (c *client) getBaseUrl() string {
return "https://api.github.com"
}

func (c *client) buildRequest(ctx context.Context, method string, requestUrl string, bodyPayload io.Reader) (*http.Request, error) {
request, err := http.NewRequestWithContext(ctx, method, requestUrl, bodyPayload)
if err != nil {
return nil, errors.Join(infraHttp.NewHttpClientError("failed construct request", requestUrl), err)
}
if bodyPayload != nil {
request.Header.Add("Accept", "application/vnd.github+json")
request.Header.Add("X-GitHub-Api-Version", "2022-11-28")
}
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.accessToken))
return request, nil
}
7 changes: 7 additions & 0 deletions internal/infrastructure/http/github/payload.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, payload.go, to the project. This file contains the data structures for the GitHub API payloads. 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.

Consider adding documentation to the PullRequestResponse struct to clarify the purpose of each field.

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

type PullRequestResponse struct {
ID int `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
}