Skip to content

Commit 1ef3a6c

Browse files
author
chhsia0
committed
Implemented PR creation in Github driver.
1 parent cfcf1ca commit 1ef3a6c

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

scm/driver/github/pr.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ func (s *pullService) Close(ctx context.Context, repo string, number int) (*scm.
5151
return res, err
5252
}
5353

54+
func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRequestInput) (*scm.PullRequest, *scm.Response, error) {
55+
path := fmt.Sprintf("repos/%s/pulls", repo)
56+
in := &prInput{
57+
Title: input.Title,
58+
Body: input.Body,
59+
Head: input.Source,
60+
Base: input.Target,
61+
}
62+
out := new(pr)
63+
res, err := s.client.do(ctx, "POST", path, in, out)
64+
return convertPullRequest(out), res, err
65+
}
66+
5467
type pr struct {
5568
Number int `json:"number"`
5669
State string `json:"state"`
@@ -85,6 +98,13 @@ type pr struct {
8598
UpdatedAt time.Time `json:"updated_at"`
8699
}
87100

101+
type prInput struct {
102+
Title string `json:"title"`
103+
Body string `json:"body"`
104+
Head string `json:"head"`
105+
Base string `json:"base"`
106+
}
107+
88108
type file struct {
89109
Sha string `json:"sha"`
90110
Filename string `json:"filename"`

scm/driver/github/pr_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,40 @@ func TestPullClose(t *testing.T) {
152152
t.Run("Request", testRequest(res))
153153
t.Run("Rate", testRate(res))
154154
}
155+
156+
func TestPullCreate(t *testing.T) {
157+
defer gock.Off()
158+
159+
gock.New("https://api.github.com").
160+
Post("/repos/octocat/hello-world/pulls").
161+
Reply(201).
162+
Type("application/json").
163+
SetHeaders(mockHeaders).
164+
File("testdata/pr.json")
165+
166+
input := scm.PullRequestInput{
167+
Title: "new-feature",
168+
Body: "Please pull these awesome changes",
169+
Source: "new-topic",
170+
Target: "master",
171+
}
172+
173+
client := NewDefault()
174+
got, res, err := client.PullRequests.Create(context.Background(), "octocat/hello-world", &input)
175+
if err != nil {
176+
t.Error(err)
177+
return
178+
}
179+
180+
want := new(scm.PullRequest)
181+
raw, _ := ioutil.ReadFile("testdata/pr.json.golden")
182+
json.Unmarshal(raw, want)
183+
184+
if diff := cmp.Diff(got, want); diff != "" {
185+
t.Errorf("Unexpected Results")
186+
t.Log(diff)
187+
}
188+
189+
t.Run("Request", testRequest(res))
190+
t.Run("rate", testRate(res))
191+
}

0 commit comments

Comments
 (0)