forked from chroe/gitlink-cli
379 lines
10 KiB
Go
Executable File
379 lines
10 KiB
Go
Executable File
package pr
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gitlink-org/gitlink-cli/internal/client"
|
|
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
|
|
)
|
|
|
|
func TestPRCommentPostsToCorrectIssueJournal(t *testing.T) {
|
|
var journalPayload map[string]interface{}
|
|
var journalPath string
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch {
|
|
case r.Method == "GET" && r.URL.Path == "/owner/repo/pulls/13.json":
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"issue": map[string]interface{}{
|
|
"id": float64(142301),
|
|
"subject": "test PR",
|
|
},
|
|
"pull_request": map[string]interface{}{
|
|
"id": float64(14791),
|
|
},
|
|
})
|
|
case r.Method == "POST" && r.URL.Path == "/v1/owner/repo/issues/142301/journals.json":
|
|
journalPath = r.URL.Path
|
|
journalPayload = decodeJSON(t, r)
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"id": float64(12345),
|
|
"message": "评论成功",
|
|
})
|
|
default:
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runPRShortcut(t, server, "comment", map[string]string{
|
|
"id": "13",
|
|
"body": "LGTM, looks good!",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("comment shortcut failed: %v", err)
|
|
}
|
|
|
|
if journalPath == "" {
|
|
t.Fatal("journal endpoint was not called")
|
|
}
|
|
assertEqual(t, journalPayload["notes"], "LGTM, looks good!")
|
|
}
|
|
|
|
func TestPRCommentFailsWhenPRNotFound(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"status": 404,
|
|
"error": "Not Found",
|
|
})
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runPRShortcut(t, server, "comment", map[string]string{
|
|
"id": "999",
|
|
"body": "test",
|
|
})
|
|
if err == nil {
|
|
t.Fatal("expected error for non-existent PR, got nil")
|
|
}
|
|
}
|
|
|
|
func TestPRCommentFailsWhenIssueFieldMissing(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"pull_request": map[string]interface{}{
|
|
"id": float64(14791),
|
|
},
|
|
})
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runPRShortcut(t, server, "comment", map[string]string{
|
|
"id": "13",
|
|
"body": "test",
|
|
})
|
|
if err == nil {
|
|
t.Fatal("expected error when issue field is missing, got nil")
|
|
}
|
|
}
|
|
|
|
func runPRShortcut(t *testing.T, server *httptest.Server, name string, args map[string]string) error {
|
|
t.Helper()
|
|
shortcut := findPRShortcut(t, name)
|
|
ctx := &common.RuntimeContext{
|
|
Client: &client.Client{
|
|
HTTP: server.Client(),
|
|
BaseURL: server.URL,
|
|
},
|
|
Owner: "owner",
|
|
Repo: "repo",
|
|
Format: "json",
|
|
Args: args,
|
|
}
|
|
return shortcut.Run(ctx)
|
|
}
|
|
|
|
func findPRShortcut(t *testing.T, name string) *common.Shortcut {
|
|
t.Helper()
|
|
for _, shortcut := range Shortcuts() {
|
|
if shortcut.Name == name {
|
|
return shortcut
|
|
}
|
|
}
|
|
t.Fatalf("shortcut %q not found", name)
|
|
return nil
|
|
}
|
|
|
|
func decodeJSON(t *testing.T, r *http.Request) map[string]interface{} {
|
|
t.Helper()
|
|
var payload map[string]interface{}
|
|
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
|
t.Fatalf("failed to decode request body: %v", err)
|
|
}
|
|
return payload
|
|
}
|
|
|
|
func writeJSON(t *testing.T, w http.ResponseWriter, payload interface{}) {
|
|
t.Helper()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(payload); err != nil {
|
|
t.Fatalf("failed to write response: %v", err)
|
|
}
|
|
}
|
|
|
|
func assertEqual(t *testing.T, got interface{}, want interface{}) {
|
|
t.Helper()
|
|
if fmt.Sprintf("%v", got) != fmt.Sprintf("%v", want) {
|
|
t.Fatalf("got %v (%T), want %v (%T)", got, got, want, want)
|
|
}
|
|
}
|
|
|
|
func TestPRList(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "GET" && r.URL.Path == "/owner/repo/pulls.json" {
|
|
writeJSON(t, w, []map[string]interface{}{
|
|
{"id": float64(1), "title": "fix bug", "state": "open"},
|
|
{"id": float64(2), "title": "add feature", "state": "merged"},
|
|
})
|
|
return
|
|
}
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runPRShortcut(t, server, "list", map[string]string{})
|
|
if err != nil {
|
|
t.Fatalf("pr list failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPRCreate(t *testing.T) {
|
|
var createPayload map[string]interface{}
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "POST" && r.URL.Path == "/owner/repo/pulls.json" {
|
|
createPayload = decodeJSON(t, r)
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"id": float64(1), "title": "new feature",
|
|
})
|
|
return
|
|
}
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runPRShortcut(t, server, "create", map[string]string{
|
|
"title": "new feature",
|
|
"head": "feature-branch",
|
|
"base": "master",
|
|
"body": "This adds a new feature",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("pr create failed: %v", err)
|
|
}
|
|
|
|
assertEqual(t, createPayload["title"], "new feature")
|
|
assertEqual(t, createPayload["head"], "feature-branch")
|
|
assertEqual(t, createPayload["base"], "master")
|
|
assertEqual(t, createPayload["body"], "This adds a new feature")
|
|
}
|
|
|
|
func TestPRView(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "GET" && r.URL.Path == "/owner/repo/pulls/1.json" {
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"id": float64(1), "title": "fix bug", "state": "open",
|
|
})
|
|
return
|
|
}
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runPRShortcut(t, server, "view", map[string]string{"id": "1"})
|
|
if err != nil {
|
|
t.Fatalf("pr view failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPRMerge(t *testing.T) {
|
|
var mergePayload map[string]interface{}
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "POST" && r.URL.Path == "/owner/repo/pulls/1/pr_merge.json" {
|
|
mergePayload = decodeJSON(t, r)
|
|
writeJSON(t, w, map[string]interface{}{"message": "merged"})
|
|
return
|
|
}
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runPRShortcut(t, server, "merge", map[string]string{
|
|
"id": "1",
|
|
"method": "squash",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("pr merge failed: %v", err)
|
|
}
|
|
|
|
assertEqual(t, mergePayload["do"], "squash")
|
|
}
|
|
|
|
func TestPRClose(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "POST" && r.URL.Path == "/owner/repo/pulls/1/refuse_merge.json" {
|
|
writeJSON(t, w, map[string]interface{}{"message": "closed"})
|
|
return
|
|
}
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runPRShortcut(t, server, "close", map[string]string{"id": "1"})
|
|
if err != nil {
|
|
t.Fatalf("pr close failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPRFiles(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "GET" && r.URL.Path == "/owner/repo/pulls/1/files.json" {
|
|
writeJSON(t, w, []map[string]interface{}{
|
|
{"filename": "src/main.go", "status": "modified"},
|
|
{"filename": "src/test.go", "status": "added"},
|
|
})
|
|
return
|
|
}
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runPRShortcut(t, server, "files", map[string]string{"id": "1"})
|
|
if err != nil {
|
|
t.Fatalf("pr files failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPRDiff(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "GET" && r.URL.Path == "/owner/repo/pulls/1/files.json" {
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"files": []interface{}{
|
|
map[string]interface{}{"filename": "main.go", "patch": "@@ -1,3 +1,4 @@"},
|
|
},
|
|
})
|
|
return
|
|
}
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runPRShortcut(t, server, "diff", map[string]string{"id": "1"})
|
|
if err != nil {
|
|
t.Fatalf("pr diff failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPRReviewSubmitsApproveAction(t *testing.T) {
|
|
var reviewPath string
|
|
var reviewPayload map[string]interface{}
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch {
|
|
case r.Method == "POST" && r.URL.Path == "/owner/repo/pulls/13/reviews.json":
|
|
reviewPath = r.URL.Path
|
|
reviewPayload = decodeJSON(t, r)
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"id": float64(1),
|
|
"state": "approved",
|
|
})
|
|
default:
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runPRShortcut(t, server, "review", map[string]string{
|
|
"id": "13",
|
|
"body": "LGTM!",
|
|
"action": "approve",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("review shortcut failed: %v", err)
|
|
}
|
|
|
|
if reviewPath == "" {
|
|
t.Fatal("review endpoint was not called")
|
|
}
|
|
assertEqual(t, reviewPayload["body"], "LGTM!")
|
|
assertEqual(t, reviewPayload["action"], "approved")
|
|
}
|
|
|
|
func TestPRReviewDefaultsToCommentAction(t *testing.T) {
|
|
var reviewPayload map[string]interface{}
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch {
|
|
case r.Method == "POST" && r.URL.Path == "/owner/repo/pulls/13/reviews.json":
|
|
reviewPayload = decodeJSON(t, r)
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"id": float64(2),
|
|
"state": "commented",
|
|
})
|
|
default:
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runPRShortcut(t, server, "review", map[string]string{
|
|
"id": "13",
|
|
"body": "Looks good",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("review shortcut failed: %v", err)
|
|
}
|
|
|
|
assertEqual(t, reviewPayload["action"], "commented")
|
|
}
|
|
|
|
func TestPRReviewSubmitsRequestChangesAction(t *testing.T) {
|
|
var reviewPayload map[string]interface{}
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch {
|
|
case r.Method == "POST" && r.URL.Path == "/owner/repo/pulls/13/reviews.json":
|
|
reviewPayload = decodeJSON(t, r)
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"id": float64(3),
|
|
"state": "changes_requested",
|
|
})
|
|
default:
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runPRShortcut(t, server, "review", map[string]string{
|
|
"id": "13",
|
|
"body": "Please fix the null check",
|
|
"action": "request-changes",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("review shortcut failed: %v", err)
|
|
}
|
|
|
|
assertEqual(t, reviewPayload["action"], "changes_requested")
|
|
assertEqual(t, reviewPayload["body"], "Please fix the null check")
|
|
}
|