feat: correctly implement bitbucket & add OpenAIAdapter
This commit is contained in:
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
|
||||
}
|
||||
Reference in New Issue
Block a user