feat(pierre): sanity check
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package bitbucket
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/gitadapters/baseadapter"
|
||||
@@ -18,3 +22,43 @@ func NewBitbucket(baseURL string, bearerToken string) *BitbucketAdapter {
|
||||
Rest: baseadapter.NewRest(baseURL, bearerToken),
|
||||
}
|
||||
}
|
||||
|
||||
// GetFileContent returns the raw file content at the given ref (commit SHA) or HEAD if ref is empty.
|
||||
func (b *BitbucketAdapter) GetFileContent(ctx context.Context, projectKey, repositorySlug, path, ref string) (string, error) {
|
||||
// Use the Rest helper to build the base URL, then add the "at" query param if needed.
|
||||
r, err := b.CreateRequest(ctx, http.MethodGet, nil,
|
||||
"/projects/", projectKey, "repos", repositorySlug, "raw", path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if ref != "" {
|
||||
q := r.URL.Query()
|
||||
q.Set("at", ref)
|
||||
r.URL.RawQuery = q.Encode()
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
sb := &strings.Builder{}
|
||||
io.Copy(sb, resp.Body)
|
||||
return "", fmt.Errorf("error fetching file %s status %d, body %s", path, resp.StatusCode, sb.String())
|
||||
}
|
||||
content, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(content), nil
|
||||
}
|
||||
|
||||
// GetPRHeadSHA fetches the PR and returns the SHA of the source (to) branch.
|
||||
func (b *BitbucketAdapter) GetPRHeadSHA(ctx context.Context, projectKey, repositorySlug string, pullRequestID int) (string, error) {
|
||||
pr, err := b.GetPR(ctx, projectKey, repositorySlug, pullRequestID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return pr.ToRef.LatestCommit, nil
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package gitea
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
@@ -47,3 +48,28 @@ func (g *Adapter) AddComment(ctx context.Context, owner, repo string, prID int,
|
||||
_, _, err := g.client.CreatePullReview(owner, repo, int64(prID), opts)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetFileContent returns the file content at a given path and ref (commit SHA).
|
||||
func (g *Adapter) GetFileContent(ctx context.Context, owner, repo, path, ref string) (string, error) {
|
||||
g.client.SetContext(ctx)
|
||||
// The SDK's GetFile returns the raw bytes of the file.
|
||||
data, _, err := g.client.GetFile(owner, repo, ref, path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
// GetPRHeadSHA fetches the pull request and returns the head commit SHA.
|
||||
func (g *Adapter) GetPRHeadSHA(ctx context.Context, owner, repo string, prID int) (string, error) {
|
||||
g.client.SetContext(ctx)
|
||||
// GetPullRequest returns the detailed PR information.
|
||||
pr, _, err := g.client.GetPullRequest(owner, repo, int64(prID))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if pr == nil || pr.Head == nil {
|
||||
return "", fmt.Errorf("pull request %d has no head information", prID)
|
||||
}
|
||||
return pr.Head.Sha, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user