feat: gitea client
This commit is contained in:
31
internal/gitadapters/baseadapter/rest.go
Normal file
31
internal/gitadapters/baseadapter/rest.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package baseadapter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type Rest struct {
|
||||
baseURL string
|
||||
bearerToken string
|
||||
}
|
||||
|
||||
func (b *Rest) 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
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"bitbucket.bit.admin.ch/scm/~u80859501/pierre-bot/internal/pierre"
|
||||
)
|
||||
|
||||
type BitbucketAdapter struct {
|
||||
@@ -36,3 +38,9 @@ func (b *BitbucketAdapter) GetDiff(projectKey, repositorySlug string, pullReques
|
||||
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
|
||||
}
|
||||
|
||||
46
internal/gitadapters/gitea/adapter.go
Normal file
46
internal/gitadapters/gitea/adapter.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
|
||||
"bitbucket.bit.admin.ch/scm/~u80859501/pierre-bot/internal/pierre"
|
||||
"code.gitea.io/sdk/gitea"
|
||||
)
|
||||
|
||||
type Adapter struct {
|
||||
client *gitea.Client
|
||||
}
|
||||
|
||||
func New(baseURL, token string) (*Adapter, error) {
|
||||
client, err := gitea.NewClient(baseURL, gitea.SetToken(token))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Adapter{
|
||||
client: client,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (g *Adapter) GetDiff(owner, repo string, prID int) (io.Reader, error) {
|
||||
diff, _, err := g.client.GetPullRequestDiff(owner, repo, int64(prID), gitea.PullRequestDiffOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bytes.NewReader(diff), nil
|
||||
}
|
||||
|
||||
func (g *Adapter) AddComment(owner, repo string, prID int, comment pierre.Comment) error {
|
||||
opts := gitea.CreatePullReviewOptions{
|
||||
State: gitea.ReviewStateComment,
|
||||
Comments: []gitea.CreatePullReviewComment{
|
||||
{
|
||||
Path: comment.File,
|
||||
Body: comment.Message,
|
||||
NewLineNum: int64(comment.Line),
|
||||
},
|
||||
},
|
||||
}
|
||||
_, _, err := g.client.CreatePullReview(owner, repo, int64(prID), opts)
|
||||
return err
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
package gitadapters
|
||||
Reference in New Issue
Block a user