Skip to content

Commit 399a8d5

Browse files
author
chhsia0
committed
Implement PR creation in Gitlab driver.
1 parent 1ef3a6c commit 399a8d5

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

scm/driver/gitlab/pr.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ func (s *pullService) ListComments(ctx context.Context, repo string, index int,
5252
return convertIssueCommentList(out), res, err
5353
}
5454

55+
func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRequestInput) (*scm.PullRequest, *scm.Response, error) {
56+
in := url.Values{}
57+
in.Set("title", input.Title)
58+
in.Set("description", input.Body)
59+
path := fmt.Sprintf("api/v4/projects/%s/merge_requests?%s", encode(repo), in.Encode())
60+
out := new(pr)
61+
res, err := s.client.do(ctx, "POST", path, nil, out)
62+
return convertPullRequest(out), res, err
63+
}
64+
5565
func (s *pullService) CreateComment(ctx context.Context, repo string, index int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) {
5666
in := url.Values{}
5767
in.Set("body", input.Body)

scm/driver/gitlab/pr_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,43 @@ func TestPullClose(t *testing.T) {
150150
t.Run("Rate", testRate(res))
151151
}
152152

153+
func TestPullCreate(t *testing.T) {
154+
defer gock.Off()
155+
156+
gock.New("https://gitlab.com").
157+
Post("/api/v4/projects/diaspora/diaspora/merge_requests").
158+
Reply(200).
159+
Type("application/json").
160+
SetHeaders(mockHeaders).
161+
File("testdata/merge.json")
162+
163+
input := scm.PullRequestInput{
164+
Title: "JS fix",
165+
Body: `Signed-off-by: Dmitriy Zaporozhets \[email protected]\u003e`,
166+
Source: "fix",
167+
Target: "master",
168+
}
169+
170+
client := NewDefault()
171+
got, res, err := client.PullRequests.Create(context.Background(), "diaspora/diaspora", &input)
172+
if err != nil {
173+
t.Error(err)
174+
return
175+
}
176+
177+
want := new(scm.PullRequest)
178+
raw, _ := ioutil.ReadFile("testdata/merge.json.golden")
179+
json.Unmarshal(raw, want)
180+
181+
if diff := cmp.Diff(got, want); diff != "" {
182+
t.Errorf("Unexpected Results")
183+
t.Log(diff)
184+
}
185+
186+
t.Run("Request", testRequest(res))
187+
t.Run("Rate", testRate(res))
188+
}
189+
153190
func TestPullCommentFind(t *testing.T) {
154191
defer gock.Off()
155192

0 commit comments

Comments
 (0)