Skip to content

Commit

Permalink
Merge pull request drone#63 from bigkevmcd/create-pull-request
Browse files Browse the repository at this point in the history
feat: Add support for creating PullRequests.
  • Loading branch information
jenkins-x-bot authored Jan 21, 2020
2 parents 1a198b1 + 93c48c7 commit 8039690
Show file tree
Hide file tree
Showing 19 changed files with 1,013 additions and 0 deletions.
4 changes: 4 additions & 0 deletions scm/driver/bitbucket/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (s *pullService) Close(ctx context.Context, repo string, number int) (*scm.
return nil, scm.ErrNotSupported
}

func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRequestInput) (*scm.PullRequest, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}

type prCommit struct {
LatestCommit string `json:"hash"`
}
Expand Down
15 changes: 15 additions & 0 deletions scm/driver/bitbucket/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,18 @@ func TestPullClose(t *testing.T) {
t.Errorf("Expect Not Supported error")
}
}

func TestPullCreate(t *testing.T) {
client, _ := New("https://api.bitbucket.org")
input := &scm.PullRequestInput{
Title: "Bitbucket feature",
Body: "New Bitbucket feature",
Head: "new-feature",
Base: "master",
}

_, _, err := client.PullRequests.Create(context.Background(), "atlassian/atlaskit", input)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
3 changes: 3 additions & 0 deletions scm/driver/fake/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type Data struct {
IssueEvents map[int][]*scm.ListedIssueEvent
Commits map[string]*scm.Commit
TestRef string
PullRequestsCreated map[int]*scm.PullRequestInput
PullRequestID int

//All Labels That Exist In The Repo
RepoLabelsExisting []string
Expand Down Expand Up @@ -77,6 +79,7 @@ func NewData() *Data {
PullRequestLabelsAdded: []string{},
PullRequestLabelsRemoved: []string{},
PullRequestLabelsExisting: []string{},
PullRequestsCreated: map[int]*scm.PullRequestInput{},
Reviews: map[int][]*scm.Review{},
Statuses: map[string][]*scm.Status{},
IssueEvents: map[int][]*scm.ListedIssueEvent{},
Expand Down
18 changes: 18 additions & 0 deletions scm/driver/fake/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,21 @@ func (s *pullService) DeleteComment(ctx context.Context, repo string, number int
}
return nil, fmt.Errorf("could not find issue comment %d", id)
}

func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRequestInput) (*scm.PullRequest, *scm.Response, error) {
f := s.data
answer := &scm.PullRequest{
Number: f.PullRequestID,
Title: input.Title,
Body: input.Body,
Base: scm.PullRequestBranch{
Ref: input.Base,
},
Head: scm.PullRequestBranch{
Ref: input.Head,
},
}
f.PullRequestsCreated[f.PullRequestID] = input
f.PullRequestID++
return answer, nil, nil
}
4 changes: 4 additions & 0 deletions scm/driver/gitea/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func (s *pullService) Close(context.Context, string, int) (*scm.Response, error)
return nil, scm.ErrNotSupported
}

func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRequestInput) (*scm.PullRequest, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}

//
// native data structures
//
Expand Down
15 changes: 15 additions & 0 deletions scm/driver/gitea/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,18 @@ func TestPullRequestCommentDelete(t *testing.T) {
t.Errorf("Expect Not Supported error")
}
}

func TestPullCreate(t *testing.T) {
client, _ := New("https://try.gitea.io")
input := &scm.PullRequestInput{
Title: "Gitea feature",
Body: "New Gitea feature",
Head: "new-feature",
Base: "master",
}

_, _, err := client.PullRequests.Create(context.Background(), "go-gitea/gitea", input)
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
21 changes: 21 additions & 0 deletions scm/driver/github/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ func (s *pullService) Close(ctx context.Context, repo string, number int) (*scm.
return res, err
}

func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRequestInput) (*scm.PullRequest, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls", repo)
in := &prInput{
Title: input.Title,
Head: input.Head,
Base: input.Base,
Body: input.Body,
}

out := new(pr)
res, err := s.client.do(ctx, "POST", path, in, out)
return convertPullRequest(out), res, err
}

type prBranch struct {
Ref string `json:"ref"`
Sha string `json:"sha"`
Expand Down Expand Up @@ -102,6 +116,13 @@ type file struct {
PreviousFilename string `json:"previous_filename"`
}

type prInput struct {
Title string `json:"title"`
Body string `json:"body"`
Head string `json:"head"`
Base string `json:"base"`
}

func convertPullRequestList(from []*pr) []*scm.PullRequest {
to := []*scm.PullRequest{}
for _, v := range from {
Expand Down
37 changes: 37 additions & 0 deletions scm/driver/github/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,40 @@ func TestPullClose(t *testing.T) {
t.Run("Request", testRequest(res))
t.Run("Rate", testRate(res))
}

func TestPullCreate(t *testing.T) {
defer gock.Off()

gock.New("https://api.github.com").
Post("/repos/octocat/hello-world/pulls").
Reply(201).
Type("application/json").
SetHeaders(mockHeaders).
File("testdata/pr_create.json")

input := &scm.PullRequestInput{
Title: "Amazing new feature",
Body: "Please pull these awesome changes in!",
Head: "octocat:new-feature",
Base: "master",
}

client := NewDefault()

got, res, err := client.PullRequests.Create(context.Background(), "octocat/hello-world", input)
if err != nil {
t.Fatal(err)
}

want := new(scm.PullRequest)
raw, err := ioutil.ReadFile("testdata/pr_create.json.golden")
json.Unmarshal(raw, want)

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}

t.Run("Request", testRequest(res))
t.Run("Rate", testRate(res))
}
Loading

0 comments on commit 8039690

Please sign in to comment.