feat: correctly implement bitbucket & add OpenAIAdapter
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
package gitadapters
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type baseHTTP struct {
|
||||
baseURL string
|
||||
bearerToken string
|
||||
}
|
||||
|
||||
func (b *baseHTTP) createRequest(method string, body io.Reader, path ...string) (r *http.Request, err error) {
|
||||
target, err := url.JoinPath(b.baseURL, path...)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("can not parse path: %w", err)
|
||||
return
|
||||
}
|
||||
req, err := http.NewRequest(method, target, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if b.bearerToken != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+b.bearerToken)
|
||||
}
|
||||
|
||||
return req, nil
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
package baseadapter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -12,17 +15,45 @@ type Rest struct {
|
||||
bearerToken string
|
||||
}
|
||||
|
||||
func (b *Rest) createRequest(method string, body io.Reader, path ...string) (r *http.Request, err error) {
|
||||
func NewRest(baseURL string, bearerToken string) Rest {
|
||||
return Rest{
|
||||
baseURL: baseURL,
|
||||
bearerToken: bearerToken,
|
||||
}
|
||||
}
|
||||
|
||||
const defaultBodyBufferSize = 100
|
||||
|
||||
func (b *Rest) CreateRequest(ctx context.Context, method string, body any, path ...string) (r *http.Request, err error) {
|
||||
target, err := url.JoinPath(b.baseURL, path...)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("can not parse path: %w", err)
|
||||
return
|
||||
}
|
||||
req, err := http.NewRequest(method, target, body)
|
||||
|
||||
var bodyReader io.Reader
|
||||
if body != nil {
|
||||
bodyBuff := bytes.NewBuffer(make([]byte, 0, defaultBodyBufferSize))
|
||||
|
||||
err = json.NewEncoder(bodyBuff).Encode(body)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
bodyReader = bodyBuff
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, target, bodyReader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
if b.bearerToken != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+b.bearerToken)
|
||||
}
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
package gitadapters
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/pierre"
|
||||
)
|
||||
|
||||
type BitbucketAdapter struct {
|
||||
baseHTTP
|
||||
}
|
||||
|
||||
func NewBitbucket(baseURL string, bearerToken string) *BitbucketAdapter {
|
||||
return &BitbucketAdapter{
|
||||
baseHTTP{
|
||||
baseURL: baseURL,
|
||||
bearerToken: bearerToken,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BitbucketAdapter) GetDiff(projectKey, repositorySlug string, pullRequestID int) (diff io.Reader, err error) {
|
||||
r, err := b.createRequest(
|
||||
http.MethodGet,
|
||||
nil,
|
||||
"/rest/api/1.0/projects/", projectKey, "repos", repositorySlug, "pull-requests", fmt.Sprintf("%d.diff", pullRequestID),
|
||||
)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
response, err := http.DefaultClient.Do(r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
diff = response.Body
|
||||
return
|
||||
}
|
||||
|
||||
func (b *BitbucketAdapter) AddComment(projectKey, repositorySlug string, pullRequestID int, comment pierre.Comment) error {
|
||||
fmt.Printf("[MOCK BITBUCKET] Adding comment to PR %s/%s #%d: %s at %s:%d\n",
|
||||
projectKey, repositorySlug, pullRequestID, comment.Message, comment.File, comment.Line)
|
||||
return nil
|
||||
}
|
||||
101
internal/gitadapters/bitbucket/controller.go
Normal file
101
internal/gitadapters/bitbucket/controller.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package bitbucket
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/pierre"
|
||||
)
|
||||
|
||||
func (b *BitbucketAdapter) GetDiff(ctx context.Context, projectKey, repositorySlug string, pullRequestID int) (diff io.ReadCloser, err error) {
|
||||
r, err := b.CreateRequest(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
nil,
|
||||
"/projects/", projectKey, "repos", repositorySlug, "pull-requests", fmt.Sprintf("%d.diff", pullRequestID),
|
||||
)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
response, err := http.DefaultClient.Do(r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
sb := &strings.Builder{}
|
||||
io.Copy(sb, response.Body)
|
||||
err = fmt.Errorf("error while fetching bitbucket diff staus %d, body %s", response.Status, sb.String())
|
||||
}
|
||||
|
||||
diff = response.Body
|
||||
return
|
||||
}
|
||||
|
||||
func (b *BitbucketAdapter) GetPR(ctx context.Context, projectKey, repositorySlug string, pullRequestID int) (pr PullRequest, err error) {
|
||||
r, err := b.CreateRequest(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
nil,
|
||||
"/projects/", projectKey, "repos", repositorySlug, "pull-requests", strconv.Itoa(pullRequestID),
|
||||
)
|
||||
|
||||
response, err := http.DefaultClient.Do(r)
|
||||
defer response.Body.Close() // Add this
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = json.NewDecoder(response.Body).Decode(&pr)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (b *BitbucketAdapter) AddComment(ctx context.Context, owner, repo string, prID int, comment pierre.Comment) (err error) {
|
||||
// pr, err := b.GetPR(ctx, owner, repo, prID)
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
|
||||
commentDTO := Comment{
|
||||
Content: comment.Message,
|
||||
Anchor: Anchor{
|
||||
Path: comment.File,
|
||||
Line: comment.Line,
|
||||
LineType: "ADDED",
|
||||
FileType: "TO",
|
||||
DiffType: "EFFECTIVE",
|
||||
// FromHash: pr.ToRef.LatestCommit,
|
||||
// ToHash: pr.FromRef.LatestCommit,
|
||||
},
|
||||
}
|
||||
|
||||
r, err := b.CreateRequest(ctx,
|
||||
http.MethodPost,
|
||||
commentDTO,
|
||||
"/projects/", owner, "/repos/", repo, "/pull-requests/", strconv.Itoa(prID), "/comments",
|
||||
)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
response, err := http.DefaultClient.Do(r)
|
||||
defer response.Body.Close() // Add this
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if response.StatusCode >= 300 || response.StatusCode < 200 {
|
||||
sb := &strings.Builder{}
|
||||
io.Copy(sb, response.Body)
|
||||
err = fmt.Errorf("error while creating comment staus %d, body %s", response.StatusCode, sb.String())
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
44
internal/gitadapters/bitbucket/model.go
Normal file
44
internal/gitadapters/bitbucket/model.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package bitbucket
|
||||
|
||||
type Anchor struct {
|
||||
Path string `json:"path"`
|
||||
Line int `json:"line"`
|
||||
LineType string `json:"lineType,omitempty"`
|
||||
FileType string `json:"fileType"`
|
||||
FromHash string `json:"fromHash,omitempty"`
|
||||
ToHash string `json:"toHash,omitempty"`
|
||||
DiffType string `json:"diffType,omitempty"`
|
||||
}
|
||||
|
||||
type Comment struct {
|
||||
Content string `json:"text"`
|
||||
Anchor Anchor `json:"anchor"`
|
||||
}
|
||||
|
||||
type PullRequest struct {
|
||||
ID int64 `json:"id"`
|
||||
Version int `json:"version"`
|
||||
Title string `json:"title"`
|
||||
State string `json:"state"`
|
||||
Open bool `json:"open"`
|
||||
Closed bool `json:"closed"`
|
||||
FromRef Ref `json:"fromRef"`
|
||||
ToRef Ref `json:"toRef"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type Ref struct {
|
||||
ID string `json:"id"`
|
||||
DisplayID string `json:"displayId"`
|
||||
LatestCommit string `json:"latestCommit"`
|
||||
Repository Repository `json:"repository"`
|
||||
}
|
||||
|
||||
type Repository struct {
|
||||
Slug string `json:"slug"`
|
||||
Project Project `json:"project"`
|
||||
}
|
||||
|
||||
type Project struct {
|
||||
Key string `json:"key"`
|
||||
}
|
||||
20
internal/gitadapters/bitbucket/resource.go
Normal file
20
internal/gitadapters/bitbucket/resource.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package bitbucket
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/gitadapters/baseadapter"
|
||||
)
|
||||
|
||||
type BitbucketAdapter struct {
|
||||
baseadapter.Rest
|
||||
}
|
||||
|
||||
func NewBitbucket(baseURL string, bearerToken string) *BitbucketAdapter {
|
||||
baseURL, _ = strings.CutSuffix(baseURL, "/")
|
||||
baseURL += "/rest/api/1.0"
|
||||
|
||||
return &BitbucketAdapter{
|
||||
Rest: baseadapter.NewRest(baseURL, bearerToken),
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,11 @@ package gitea
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/pierre"
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/pierre"
|
||||
)
|
||||
|
||||
type Adapter struct {
|
||||
@@ -22,15 +23,17 @@ func New(baseURL, token string) (*Adapter, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (g *Adapter) GetDiff(owner, repo string, prID int) (io.Reader, error) {
|
||||
func (g *Adapter) GetDiff(ctx context.Context, owner, repo string, prID int) (io.ReadCloser, error) {
|
||||
g.client.SetContext(ctx)
|
||||
diff, _, err := g.client.GetPullRequestDiff(owner, repo, int64(prID), gitea.PullRequestDiffOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bytes.NewReader(diff), nil
|
||||
return io.NopCloser(bytes.NewReader(diff)), nil
|
||||
}
|
||||
|
||||
func (g *Adapter) AddComment(owner, repo string, prID int, comment pierre.Comment) error {
|
||||
func (g *Adapter) AddComment(ctx context.Context, owner, repo string, prID int, comment pierre.Comment) error {
|
||||
g.client.SetContext(ctx)
|
||||
opts := gitea.CreatePullReviewOptions{
|
||||
State: gitea.ReviewStateComment,
|
||||
Comments: []gitea.CreatePullReviewComment{
|
||||
|
||||
Reference in New Issue
Block a user