Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| be68e51898 | |||
| 49db84fb82 | |||
| 08b5d4051b | |||
| d606d0a202 | |||
| b2c83c00bc |
@@ -485,7 +485,7 @@ func main() {
|
||||
reviewReq := vcs.ReviewRequest{
|
||||
Body: reviewBody,
|
||||
Event: event,
|
||||
CommitID: pr.Head.SHA,
|
||||
CommitID: evaluatedSHA,
|
||||
Comments: inlineComments,
|
||||
}
|
||||
posted, err := client.PostReview(ctx, owner, repoName, prNumber, reviewReq)
|
||||
|
||||
@@ -214,6 +214,40 @@ func TestAdapter_PostReview_EventTranslation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdapter_PostReview_CommitID(t *testing.T) {
|
||||
var gotCommitID string
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var payload struct {
|
||||
CommitID string `json:"commit_id"`
|
||||
}
|
||||
json.NewDecoder(r.Body).Decode(&payload)
|
||||
gotCommitID = payload.CommitID
|
||||
json.NewEncoder(w).Encode(map[string]any{
|
||||
"id": 1,
|
||||
"body": "test",
|
||||
"user": map[string]any{"login": "bot"},
|
||||
"commit_id": payload.CommitID,
|
||||
})
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := gitea.NewClient(server.URL, "token")
|
||||
adapter := gitea.NewAdapter(client)
|
||||
|
||||
_, err := adapter.PostReview(context.Background(), "owner", "repo", 1, vcs.ReviewRequest{
|
||||
Body: "test",
|
||||
Event: vcs.ReviewEventApprove,
|
||||
CommitID: "sha256abc",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if gotCommitID != "sha256abc" {
|
||||
t.Errorf("expected commit_id %q forwarded to client, got %q", "sha256abc", gotCommitID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdapter_PostReview_WithComments_PositionTranslation(t *testing.T) {
|
||||
diff := `diff --git a/main.go b/main.go
|
||||
--- a/main.go
|
||||
|
||||
@@ -147,6 +147,46 @@ func TestPostReview(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostReview_CommitID(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
t.Fatalf("expected POST, got %s", r.Method)
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
var payload struct {
|
||||
Body string `json:"body"`
|
||||
Event string `json:"event"`
|
||||
CommitID string `json:"commit_id"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &payload); err != nil {
|
||||
t.Fatalf("unmarshal payload: %v", err)
|
||||
}
|
||||
if payload.CommitID != "deadbeef123" {
|
||||
t.Errorf("expected commit_id %q, got %q", "deadbeef123", payload.CommitID)
|
||||
}
|
||||
if payload.Event != "APPROVED" {
|
||||
t.Errorf("expected event APPROVED, got %q", payload.Event)
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"id":101,"user":{"login":"review-bot"},"state":"APPROVED","stale":false,"commit_id":"deadbeef123"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := NewClient(server.URL, "test-token")
|
||||
review, err := client.PostReview(context.Background(), "owner", "repo", 3, "APPROVED", "LGTM", "deadbeef123", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if review.ID != 101 {
|
||||
t.Errorf("expected review ID 101, got %d", review.ID)
|
||||
}
|
||||
if review.CommitID != "deadbeef123" {
|
||||
t.Errorf("expected commit_id %q, got %q", "deadbeef123", review.CommitID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPullRequest_Non200(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
|
||||
+1
-1
@@ -103,7 +103,7 @@ func (c *Client) PostReview(ctx context.Context, owner, repo string, number int,
|
||||
// the resolved commit_id.
|
||||
for _, comment := range req.Comments {
|
||||
if comment.CommitID != "" {
|
||||
if payload.CommitID == "" {
|
||||
if payload.CommitID == "" { // only reachable when req.CommitID is empty
|
||||
payload.CommitID = comment.CommitID
|
||||
} else if payload.CommitID != comment.CommitID {
|
||||
return nil, ErrConflictingCommitIDs
|
||||
|
||||
@@ -98,6 +98,5 @@ type ReviewRequest struct {
|
||||
// If empty, the platform defaults to the current PR head.
|
||||
// Adapters use this as the primary commit anchor for the review submission.
|
||||
CommitID string `json:"commit_id,omitempty"`
|
||||
|
||||
Comments []ReviewComment `json:"comments,omitempty"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user