Repository navigation
Expand file tree
/
Copy pathgithub_test.go
More file actions
127 lines (118 loc) · 4.43 KB
/
Copy pathgithub_test.go
File metadata and controls
127 lines (118 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
type recordedRequest struct {
method, path string
body map[string]any
}
// fakeGitHub records requests and answers each path with the given status/body.
func fakeGitHub(t *testing.T, responses map[string]string) *[]recordedRequest {
t.Helper()
var reqs []recordedRequest
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data, _ := io.ReadAll(r.Body)
rec := recordedRequest{method: r.Method, path: r.URL.EscapedPath()}
json.Unmarshal(data, &rec.body)
reqs = append(reqs, rec)
if resp, ok := responses[r.Method+" "+r.URL.EscapedPath()]; ok {
status, body, _ := strings.Cut(resp, " ")
if status == "422" {
w.WriteHeader(422)
}
w.Write([]byte(body))
return
}
w.Write([]byte(`{}`))
}))
t.Cleanup(srv.Close)
old := githubAPI
githubAPI = srv.URL
t.Cleanup(func() { githubAPI = old })
return &reqs
}
func TestSubmitReviewIsNative(t *testing.T) {
reqs := fakeGitHub(t, nil)
pr := testPR("sup", "neil", 7, false, "")
msg := submitReviewCmd("request-changes", pr, "please fix")().(reviewSubmittedMsg)
if msg.err != nil {
t.Fatal(msg.err)
}
r := (*reqs)[0]
if r.method != "POST" || r.path != "/repos/superultrainc/sup/pulls/7/reviews" ||
r.body["event"] != "REQUEST_CHANGES" || r.body["body"] != "please fix" {
t.Fatalf("unexpected request: %+v", r)
}
}
func TestSaveReviewersSplitsTeams(t *testing.T) {
reqs := fakeGitHub(t, nil)
pr := testPR("sup", "neil", 7, false, "")
msg := saveReviewersCmd(pr, []string{"max", "superultrainc/backend"}, []string{"ben"})().(reviewersSavedMsg)
if msg.err != nil {
t.Fatal(msg.err)
}
add, remove := (*reqs)[0], (*reqs)[1]
if add.method != "POST" || remove.method != "DELETE" || add.path != "/repos/superultrainc/sup/pulls/7/requested_reviewers" {
t.Fatalf("unexpected requests: %+v / %+v", add, remove)
}
if got := add.body["team_reviewers"].([]any); len(got) != 1 || got[0] != "backend" {
t.Errorf("team slug not split out: %v", add.body)
}
if got := add.body["reviewers"].([]any); len(got) != 1 || got[0] != "max" {
t.Errorf("user reviewers wrong: %v", add.body)
}
}
func TestMergePinsHeadAndDeletesBranch(t *testing.T) {
reqs := fakeGitHub(t, map[string]string{
"DELETE /repos/superultrainc/sup/git/refs/heads/feature/x": "422 " + `{"message":"Reference does not exist"}`,
})
pr := testPR("sup", "neil", 7, false, "")
d := &prDetail{HeadRefOid: "abc123", HeadRefName: "feature/x"}
md := mergeDialog{deleteBranch: true}
ref, _, err := mergePR(pr, d, md, allMergeMethods[0])
if err != nil || ref != "feature/x" {
t.Fatalf("merge should succeed and hand back the branch, got %q, %v", ref, err)
}
if msg := deleteBranchCmd(pr, ref)().(branchDeletedMsg); msg.err != nil {
t.Fatalf("an already-deleted branch is fine, got %v", msg.err)
}
merge, del := (*reqs)[0], (*reqs)[1]
if merge.method != "PUT" || merge.path != "/repos/superultrainc/sup/pulls/7/merge" ||
merge.body["merge_method"] != "squash" || merge.body["sha"] != "abc123" {
t.Fatalf("unexpected merge request: %+v", merge)
}
if del.method != "DELETE" || del.path != "/repos/superultrainc/sup/git/refs/heads/feature/x" {
t.Fatalf("unexpected branch delete: %+v", del)
}
// A fork's branch is never deleted.
*reqs = nil
d.IsCrossRepo = true
if ref, _, _ := mergePR(pr, d, md, allMergeMethods[0]); ref != "" || len(*reqs) != 1 {
t.Fatalf("fork merge should not delete a branch, sent %d requests", len(*reqs))
}
}
func TestAutoMergeUsesMutation(t *testing.T) {
reqs := fakeGitHub(t, nil)
pr := testPR("sup", "neil", 7, false, "")
d := &prDetail{ID: "PR_kwDO123", HeadRefOid: "abc"}
if _, _, err := mergePR(pr, d, mergeDialog{auto: true}, allMergeMethods[0]); err != nil {
t.Fatal(err)
}
r := (*reqs)[0]
q, _ := r.body["query"].(string)
if r.path != "/graphql" || !strings.Contains(q, "enablePullRequestAutoMerge") || !strings.Contains(q, `"PR_kwDO123"`) || !strings.Contains(q, "SQUASH") {
t.Fatalf("unexpected auto-merge request: %+v", r)
}
}
func TestAPIErrorMessage(t *testing.T) {
fakeGitHub(t, map[string]string{"PUT /repos/superultrainc/sup/pulls/7/merge": "422 " + `{"message":"Head branch was modified","errors":[{"message":"Review and try again"}]}`})
_, err := githubREST("PUT", "/repos/superultrainc/sup/pulls/7/merge", map[string]string{})
if err == nil || err.Error() != "Head branch was modified: Review and try again" {
t.Fatalf("got %v", err)
}
}