Skip to content

Commit 0690ddd

Browse files
authored
Merge pull request #905 from jxs1211/feat-pkg-util-github-pullrequest
feat: pkg-util-github-pullrequest enhancement
2 parents 70d0121 + 22d1a2a commit 0690ddd

File tree

2 files changed

+114
-77
lines changed

2 files changed

+114
-77
lines changed

pkg/util/github/pullrequest_test.go

Lines changed: 112 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,106 +6,145 @@ import (
66

77
. "github.com/onsi/ginkgo/v2"
88
. "github.com/onsi/gomega"
9+
"github.com/onsi/gomega/ghttp"
910

1011
"github.com/devstream-io/devstream/pkg/util/github"
1112
)
1213

13-
var _ = Describe("Pullrequest", func() {
14-
Context("does NewPullRequest 200", func() {
14+
var _ = Describe("NewPullRequest", func() {
15+
const (
16+
fromBranch, toBranch = "fb", "tb"
17+
owner, repo = "owner", "repo"
18+
rightOrg, wrongOrg = "org", "/"
19+
)
20+
21+
var (
22+
s *ghttp.Server
23+
org string
24+
opts *github.Option
25+
)
26+
27+
JustBeforeEach(func() {
28+
opts = &github.Option{
29+
Owner: owner,
30+
Repo: repo,
31+
Org: org,
32+
}
33+
})
34+
35+
AfterEach(func() {
36+
s.Close()
37+
})
1538

39+
When("Create", func() {
1640
BeforeEach(func() {
17-
mux.HandleFunc("/repos/devstream-io/dtm-scaffolding-golang/pulls", func(w http.ResponseWriter, r *http.Request) {
18-
fmt.Fprint(w, `{"number":1}`)
19-
})
41+
s = ghttp.NewServer()
42+
org = wrongOrg
2043
})
21-
22-
It("does create a new pr", func() {
23-
ghClient, err := github.NewClientWithOption(github.OptNotNeedAuth, serverURL)
44+
It("url is incorrect", func() {
45+
s.SetAllowUnhandledRequests(true)
46+
c, err := github.NewClientWithOption(opts, s.URL())
2447
Expect(err).NotTo(HaveOccurred())
25-
Expect(ghClient).NotTo(Equal(nil))
26-
n, err := ghClient.NewPullRequest("from", "to")
27-
Expect(n).To(Equal(1))
28-
Expect(err).To(Succeed())
48+
Expect(c).NotTo(Equal(nil))
49+
n, err := c.NewPullRequest(fromBranch, toBranch)
50+
Expect(err).To(HaveOccurred())
51+
fmt.Println(err)
52+
Expect(n).To(Equal(0))
2953
})
30-
3154
})
32-
33-
Context("does NewPullRequest 404", func() {
34-
35-
It("does create a new pr with wrong url", func() {
36-
ghClient, err := github.NewClientWithOption(&github.Option{
37-
Owner: "",
38-
Org: "or",
39-
Repo: "r",
40-
}, serverURL)
55+
When("Create", func() {
56+
BeforeEach(func() {
57+
s = ghttp.NewServer()
58+
org = rightOrg
59+
})
60+
It("url is correct", func() {
61+
u := github.BaseURLPath + fmt.Sprintf("/repos/%v/%v/pulls", org, repo)
62+
s.RouteToHandler("POST", u, func(w http.ResponseWriter, r *http.Request) {
63+
fmt.Fprint(w, `{"number": 1}`)
64+
})
65+
c, err := github.NewClientWithOption(opts, s.URL())
4166
Expect(err).NotTo(HaveOccurred())
42-
Expect(ghClient).NotTo(Equal(nil))
43-
n, err := ghClient.NewPullRequest("from", "to")
44-
Expect(n).To(Equal(0))
45-
Expect(err).NotTo(Equal(nil))
67+
Expect(c).NotTo(Equal(nil))
68+
n, err := c.NewPullRequest(fromBranch, toBranch)
69+
Expect(err).NotTo(HaveOccurred())
70+
Expect(n).To(Equal(1))
4671
})
4772
})
73+
})
4874

49-
Context("does MergePullRequest", func() {
75+
var _ = Describe("MergePullRequest", func() {
76+
const (
77+
fromBranch, toBranch = "fb", "tb"
78+
owner, repo = "owner", "repo"
79+
rightOrg, wrongOrg = "org", "/"
80+
number = 1
81+
)
82+
83+
var (
84+
s *ghttp.Server
85+
org string
86+
opts *github.Option
87+
)
88+
89+
JustBeforeEach(func() {
90+
opts = &github.Option{
91+
Owner: owner,
92+
Repo: repo,
93+
Org: org,
94+
}
95+
})
96+
97+
AfterEach(func() {
98+
s.Close()
99+
})
50100

101+
When("Merge", func() {
51102
BeforeEach(func() {
52-
mux.HandleFunc("/repos/devstream-io/dtm-scaffolding-golang/pulls/1/merge", func(w http.ResponseWriter, r *http.Request) {
53-
fmt.Fprint(w, `
54-
{
55-
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
56-
"merged": true,
57-
"message": "Pull Request successfully merged"
58-
}`)
59-
})
103+
s = ghttp.NewServer()
104+
org = wrongOrg
60105
})
61-
62-
It("200", func() {
63-
ghClient, err := github.NewClientWithOption(github.OptNotNeedAuth, serverURL)
106+
It("url is incorrect", func() {
107+
s.SetAllowUnhandledRequests(true)
108+
c, err := github.NewClientWithOption(opts, s.URL())
64109
Expect(err).NotTo(HaveOccurred())
65-
Expect(ghClient).NotTo(Equal(nil))
66-
err = ghClient.MergePullRequest(1, github.MergeMethodRebase)
67-
Expect(err).To(Succeed())
110+
Expect(c).NotTo(Equal(nil))
111+
err = c.MergePullRequest(number, github.MergeMethodMerge)
112+
Expect(err).To(HaveOccurred())
68113
})
69114
})
70-
71-
Context("does MergePullRequest", func() {
72-
73-
It("404", func() {
74-
ghClient, err := github.NewClientWithOption(&github.Option{
75-
Owner: "",
76-
Org: "or",
77-
Repo: "r",
78-
}, serverURL)
115+
When("Merge", func() {
116+
BeforeEach(func() {
117+
s = ghttp.NewServer()
118+
org = rightOrg
119+
})
120+
It("url is correct but merged is false", func() {
121+
u := github.BaseURLPath + fmt.Sprintf("/repos/%v/%v/pulls/%d/merge", org, repo, number)
122+
s.RouteToHandler("PUT", u, func(w http.ResponseWriter, r *http.Request) {
123+
fmt.Fprint(w, `{}`)
124+
})
125+
c, err := github.NewClientWithOption(opts, s.URL())
79126
Expect(err).NotTo(HaveOccurred())
80-
Expect(ghClient).NotTo(Equal(nil))
81-
err = ghClient.MergePullRequest(1, github.MergeMethodRebase)
127+
Expect(c).NotTo(Equal(nil))
128+
err = c.MergePullRequest(number, github.MergeMethodMerge)
82129
Expect(err).To(HaveOccurred())
130+
Expect(err.Error()).To(ContainSubstring("merge failed"))
83131
})
84132
})
85-
86-
Context("does MergePullRequest", func() {
87-
133+
When("Merge", func() {
88134
BeforeEach(func() {
89-
mux.HandleFunc("/repos/or/r/pulls/1/merge", func(w http.ResponseWriter, r *http.Request) {
90-
fmt.Fprint(w, `
91-
{
92-
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
93-
"merged": false,
94-
"message": "Pull Request successfully merged"
95-
}`)
96-
})
135+
s = ghttp.NewServer()
136+
org = rightOrg
97137
})
98-
99-
It("return merged false", func() {
100-
ghClient, err := github.NewClientWithOption(&github.Option{
101-
Owner: "",
102-
Org: "or",
103-
Repo: "r",
104-
}, serverURL)
138+
It("url is correct", func() {
139+
u := github.BaseURLPath + fmt.Sprintf("/repos/%v/%v/pulls/%d/merge", org, repo, number)
140+
s.RouteToHandler("PUT", u, func(w http.ResponseWriter, r *http.Request) {
141+
fmt.Fprint(w, `{"merged": true}`)
142+
})
143+
c, err := github.NewClientWithOption(opts, s.URL())
105144
Expect(err).NotTo(HaveOccurred())
106-
Expect(ghClient).NotTo(Equal(nil))
107-
err = ghClient.MergePullRequest(1, github.MergeMethodRebase)
108-
Expect(err).NotTo(Succeed())
145+
Expect(c).NotTo(Equal(nil))
146+
err = c.MergePullRequest(number, github.MergeMethodMerge)
147+
Expect(err).To(Succeed())
109148
})
110149
})
111150
})

pkg/util/github/repo_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ var _ = Describe("Repo", func() {
4747
})
4848

4949
Context("CreateRepo", func() {
50-
It("create with err 500", func() {
50+
It("create with status 500", func() {
5151
s.Reset()
5252
s.SetAllowUnhandledRequests(true)
5353
err := wrongClient.CreateRepo(org, defaultBranch)
5454
Expect(err).NotTo(Succeed())
5555
Expect(err.Error()).To(ContainSubstring(strconv.Itoa(http.StatusInternalServerError)))
5656
})
57-
It("create with err 500", func() {
57+
It("create with status 200", func() {
5858
s.Reset()
5959
s.SetAllowUnhandledRequests(true)
6060
s.SetUnhandledRequestStatusCode(http.StatusOK)
@@ -67,7 +67,6 @@ var _ = Describe("Repo", func() {
6767
It("DeleteRepo with status 500", func() {
6868
s.Reset()
6969
s.SetAllowUnhandledRequests(true)
70-
s.SetUnhandledRequestStatusCode(http.StatusInternalServerError)
7170
err := rightClient.DeleteRepo()
7271
Expect(err).NotTo(Succeed())
7372
Expect(err.Error()).To(ContainSubstring(strconv.Itoa(http.StatusInternalServerError)))
@@ -103,7 +102,6 @@ var _ = Describe("Repo", func() {
103102
u := fmt.Sprintf("/repos/%v/%v", org, repo)
104103
s.Reset()
105104
s.RouteToHandler("GET", gh.BaseURLPath+u, func(w http.ResponseWriter, r *http.Request) {
106-
// w.WriteHeader(http.StatusNotFound)
107105
fmt.Fprint(w, ``)
108106
})
109107
r, err := rightClient.GetRepoDescription()

0 commit comments

Comments
 (0)