Skip to content

Commit aaa0db2

Browse files
author
chhsia0
committed
Implemented PR creation in Stash driver.
1 parent 46ae078 commit aaa0db2

File tree

3 files changed

+83
-11
lines changed

3 files changed

+83
-11
lines changed

scm/driver/stash/pr.go

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type pullService struct {
1919
func (s *pullService) Find(ctx context.Context, repo string, number int) (*scm.PullRequest, *scm.Response, error) {
2020
namespace, name := scm.Split(repo)
2121
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests/%d", namespace, name, number)
22-
out := new(pullRequest)
22+
out := new(pr)
2323
res, err := s.client.do(ctx, "GET", path, nil, out)
2424
return convertPullRequest(out), res, err
2525
}
@@ -35,7 +35,7 @@ func (s *pullService) FindComment(ctx context.Context, repo string, number int,
3535
func (s *pullService) List(ctx context.Context, repo string, opts scm.PullRequestListOptions) ([]*scm.PullRequest, *scm.Response, error) {
3636
namespace, name := scm.Split(repo)
3737
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests", namespace, name)
38-
out := new(pullRequests)
38+
out := new(prs)
3939
res, err := s.client.do(ctx, "GET", path, nil, out)
4040
if !out.pagination.LastPage.Bool {
4141
res.Page.First = 1
@@ -79,6 +79,23 @@ func (s *pullService) Close(ctx context.Context, repo string, number int) (*scm.
7979
return res, err
8080
}
8181

82+
func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRequestInput) (*scm.PullRequest, *scm.Response, error) {
83+
namespace, name := scm.Split(repo)
84+
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests", namespace, name)
85+
in := new(prInput)
86+
in.Title = input.Title
87+
in.Description = input.Body
88+
in.FromRef.Repository.Project.Key = namespace
89+
in.FromRef.Repository.Slug = name
90+
in.FromRef.ID = scm.ExpandRef(input.Source, "refs/heads")
91+
in.ToRef.Repository.Project.Key = namespace
92+
in.ToRef.Repository.Slug = name
93+
in.ToRef.ID = scm.ExpandRef(input.Target, "refs/heads")
94+
out := new(pr)
95+
res, err := s.client.do(ctx, "POST", path, in, out)
96+
return convertPullRequest(out), res, err
97+
}
98+
8299
func (s *pullService) CreateComment(ctx context.Context, repo string, number int, in *scm.CommentInput) (*scm.Comment, *scm.Response, error) {
83100
input := pullRequestCommentInput{Text: in.Body}
84101
namespace, name := scm.Split(repo)
@@ -97,7 +114,7 @@ func (s *pullService) DeleteComment(context.Context, string, int, int) (*scm.Res
97114
return nil, scm.ErrNotSupported
98115
}
99116

100-
type pullRequest struct {
117+
type pr struct {
101118
ID int `json:"id"`
102119
Version int `json:"version"`
103120
Title string `json:"title"`
@@ -146,20 +163,43 @@ type pullRequest struct {
146163
} `json:"links"`
147164
}
148165

149-
type pullRequests struct {
166+
type prs struct {
150167
pagination
151-
Values []*pullRequest `json:"values"`
168+
Values []*pr `json:"values"`
169+
}
170+
171+
type prInput struct {
172+
Title string `json:"title"`
173+
Description string `json:"description"`
174+
FromRef struct {
175+
ID string `json:"id"`
176+
Repository struct {
177+
Slug string `json:"slug"`
178+
Project struct {
179+
Key string `json:"key"`
180+
} `json:"project"`
181+
} `json:"repository"`
182+
} `json:"fromRef"`
183+
ToRef struct {
184+
ID string `json:"id"`
185+
Repository struct {
186+
Slug string `json:"slug"`
187+
Project struct {
188+
Key string `json:"key"`
189+
} `json:"project"`
190+
} `json:"repository"`
191+
} `json:"toRef"`
152192
}
153193

154-
func convertPullRequests(from *pullRequests) []*scm.PullRequest {
194+
func convertPullRequests(from *prs) []*scm.PullRequest {
155195
to := []*scm.PullRequest{}
156196
for _, v := range from.Values {
157197
to = append(to, convertPullRequest(v))
158198
}
159199
return to
160200
}
161201

162-
func convertPullRequest(from *pullRequest) *scm.PullRequest {
202+
func convertPullRequest(from *pr) *scm.PullRequest {
163203
fork := scm.Join(
164204
from.FromRef.Repository.Project.Key,
165205
from.FromRef.Repository.Slug,

scm/driver/stash/pr_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,38 @@ func TestPullClose(t *testing.T) {
150150
}
151151
}
152152

153+
func TestPullCreate(t *testing.T) {
154+
defer gock.Off()
155+
156+
gock.New("http://example.com:7990").
157+
Post("rest/api/1.0/projects/PRJ/repos/my-repo/pull-requests").
158+
Reply(201).
159+
Type("application/json").
160+
File("testdata/pr.json")
161+
162+
input := scm.PullRequestInput{
163+
Title: "Updated Files",
164+
Body: `* added LICENSE\r\n* update files\r\n* update files`,
165+
Source: "feature/x",
166+
Target: "master",
167+
}
168+
169+
client, _ := New("http://example.com:7990")
170+
got, _, err := client.PullRequests.Create(context.Background(), "PRJ/my-repo", &input)
171+
if err != nil {
172+
t.Error(err)
173+
}
174+
175+
want := new(scm.PullRequest)
176+
raw, _ := ioutil.ReadFile("testdata/pr.json.golden")
177+
json.Unmarshal(raw, &want)
178+
179+
if diff := cmp.Diff(got, want); diff != "" {
180+
t.Errorf("Unexpected Results")
181+
t.Log(diff)
182+
}
183+
}
184+
153185
func TestPullCreateComment(t *testing.T) {
154186
defer gock.Off()
155187

scm/driver/stash/webhook.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ type pushHook struct {
123123
}
124124

125125
type pullRequestHook struct {
126-
EventKey string `json:"eventKey"`
127-
Date string `json:"date"`
128-
Actor *user `json:"actor"`
129-
PullRequest *pullRequest `json:"pullRequest"`
126+
EventKey string `json:"eventKey"`
127+
Date string `json:"date"`
128+
Actor *user `json:"actor"`
129+
PullRequest *pr `json:"pullRequest"`
130130
}
131131

132132
type change struct {

0 commit comments

Comments
 (0)