Skip to content

Commit

Permalink
Implement PR creation in the pullrequest upload.
Browse files Browse the repository at this point in the history
This creates new PRs in the upstream repository for pullrequest outputs, when the "Number" is 0.
  • Loading branch information
bigkevmcd committed Jan 24, 2020
1 parent 0c58b56 commit 0a88aa7
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 56 deletions.
16 changes: 16 additions & 0 deletions docs/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,22 @@ spec:
value: github
```

### Creating a new PullRequest

This resource can also create new PRs, you'll need to populate the following
fields in the resource `pr.json` file:

```json
{
"title": "Shiny PR",
"head": "my-fork:new-feature-branch",
"base": "master",
"body:" "My Shiny new PR"
}
```

Comments and labels will also be added to the newly created PR.

### Image Resource

An `image` resource represents an image that lives in a remote repository. It is
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/golang-lru v0.5.3
github.com/imdario/mergo v0.3.8 // indirect
github.com/jenkins-x/go-scm v1.5.65
github.com/jenkins-x/go-scm v1.5.72
github.com/json-iterator/go v1.1.8 // indirect
github.com/markbates/inflect v1.0.4 // indirect
github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bigkevmcd/go-scm v0.0.0-20200122140456-4effdca1d2f5 h1:YoBjz3+xOBcvwleAqLKgGYvFNzwoN78X+glEAvX/3Aw=
github.com/bigkevmcd/go-scm v0.0.0-20200122140456-4effdca1d2f5/go.mod h1:PCT338UhP/pQ0IeEeMEf/hoLTYKcH7qjGEKd7jPkeYg=
github.com/bigkevmcd/go-scm v0.0.0-20200122142159-0df85a345017 h1:Cx7UyMS512h2GVlE6Ioh3GJaRxizHZLxOUJO9/sGNmI=
github.com/bigkevmcd/go-scm v0.0.0-20200122142159-0df85a345017/go.mod h1:PCT338UhP/pQ0IeEeMEf/hoLTYKcH7qjGEKd7jPkeYg=
github.com/bigkevmcd/go-scm v0.0.0-20200122142912-a6fb9d6535c5 h1:X2cRKhzs/lRwo7kHh9y9Hp+q4v/JLaQU+DlmPmRhtOs=
github.com/bigkevmcd/go-scm v0.0.0-20200122142912-a6fb9d6535c5/go.mod h1:PCT338UhP/pQ0IeEeMEf/hoLTYKcH7qjGEKd7jPkeYg=
github.com/blang/semver v3.5.0+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/census-instrumentation/opencensus-proto v0.2.1 h1:glEXhBS5PSLLv4IXzLA5yPRVX4bilULVyxxbrfOtDAk=
Expand Down Expand Up @@ -301,6 +307,10 @@ github.com/jcmturner/gofork v0.0.0-20190328161633-dc7c13fece03/go.mod h1:MK8+TM0
github.com/jcmturner/gofork v1.0.0/go.mod h1:MK8+TM0La+2rjBD4jE12Kj1pCCxK7d2LK/UM3ncEo0o=
github.com/jenkins-x/go-scm v1.5.65 h1:ieH+0JSWENObn1SDWFj2K40iV5Eia4aTl6W6bDdLwI0=
github.com/jenkins-x/go-scm v1.5.65/go.mod h1:MgGRkJScE/rJ30J/bXYqduN5sDPZqZFITJopsnZmTOw=
github.com/jenkins-x/go-scm v1.5.71 h1:NWbwd9s5vrxnn0vCJIYhRsUYHY1OZa9WsMx+nTHyfP0=
github.com/jenkins-x/go-scm v1.5.71/go.mod h1:PCT338UhP/pQ0IeEeMEf/hoLTYKcH7qjGEKd7jPkeYg=
github.com/jenkins-x/go-scm v1.5.72 h1:QA6VGYBzX9dSxKchHzZGIiwU6VL4xkfkhuWItCc4/g0=
github.com/jenkins-x/go-scm v1.5.72/go.mod h1:PCT338UhP/pQ0IeEeMEf/hoLTYKcH7qjGEKd7jPkeYg=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down
25 changes: 25 additions & 0 deletions pkg/pullrequest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ func (h *Handler) Upload(ctx context.Context, r *Resource) error {

var merr error

if r.PR.Number == 0 {
newPR, err := h.makeNewPR(ctx, r.PR)
if err != nil {
return fmt.Errorf("failed to create new pr: %w", err)
} else {
r.PR = newPR
}
}

if err := h.uploadLabels(ctx, r.Manifests["labels"], r.PR.Labels); err != nil {
merr = multierror.Append(merr, err)
}
Expand Down Expand Up @@ -300,3 +309,19 @@ func (h *Handler) uploadStatuses(ctx context.Context, statuses []*scm.Status, sh

return merr
}

func (h *Handler) makeNewPR(ctx context.Context, pr *scm.PullRequest) (*scm.PullRequest, error) {
input := &scm.PullRequestInput{
Title: pr.Title,
Head: pr.Head.Ref,
Base: pr.Base.Ref,
Body: pr.Body,
}

newPR, _, err := h.client.PullRequests.Create(ctx, h.repo, input)
if err != nil {
return nil, fmt.Errorf("creating pull request: %w", err)
}
h.prNum = newPR.Number
return newPR, nil
}
193 changes: 174 additions & 19 deletions pkg/pullrequest/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package pullrequest

import (
"context"
"errors"
"io/ioutil"
"os"
"testing"
Expand All @@ -32,12 +33,12 @@ import (

const (
repo = "foo/bar"
prNum = 1
prNum = 123
)

func defaultResource() *Resource {
pr := &scm.PullRequest{
Number: 123,
Number: prNum,
Sha: "sha1",
Head: scm.PullRequestBranch{
Ref: "refs/heads/branch1",
Expand Down Expand Up @@ -76,22 +77,23 @@ func defaultResource() *Resource {
return r
}

func newHandler(t *testing.T) (*Handler, *fake.Data) {
func newHandler(t *testing.T, r *Resource) (*Handler, *fake.Data) {
logger := zaptest.NewLogger(t, zaptest.WrapOptions(zap.AddCaller())).Sugar()
client, data := fake.NewDefault()

r := defaultResource()
data.PullRequests[prNum] = r.PR
data.IssueComments[prNum] = r.Comments
data.PullRequestComments[prNum] = r.Comments
data.Statuses[r.PR.Sha] = r.Statuses
if r.PR.Number > 0 {
data.PullRequests[r.PR.Number] = r.PR
data.IssueComments[r.PR.Number] = r.Comments
data.PullRequestComments[r.PR.Number] = r.Comments
data.Statuses[r.PR.Sha] = r.Statuses
}

return NewHandler(logger, client, repo, prNum), data
return NewHandler(logger, client, repo, r.PR.Number), data
}

func TestDownload(t *testing.T) {
ctx := context.Background()
h, data := newHandler(t)
h, data := newHandler(t, defaultResource())

dir, err := ioutil.TempDir("", t.Name())
if err != nil {
Expand All @@ -118,7 +120,7 @@ func TestDownload(t *testing.T) {

func TestUploadFromDisk(t *testing.T) {
ctx := context.Background()
h, _ := newHandler(t)
h, _ := newHandler(t, defaultResource())

dir, err := ioutil.TempDir("", t.Name())
if err != nil {
Expand All @@ -144,7 +146,7 @@ func TestUploadFromDisk(t *testing.T) {

func TestUpload_NewComment(t *testing.T) {
ctx := context.Background()
h, _ := newHandler(t)
h, _ := newHandler(t, defaultResource())

r := defaultResource()
c := &scm.Comment{Body: "hello world!"}
Expand All @@ -169,7 +171,7 @@ func TestUpload_NewComment(t *testing.T) {

func TestUpload_DeleteComment(t *testing.T) {
ctx := context.Background()
h, _ := newHandler(t)
h, _ := newHandler(t, defaultResource())

r := defaultResource()
r.Comments = r.Comments[1:]
Expand All @@ -190,7 +192,7 @@ func TestUpload_DeleteComment(t *testing.T) {

func TestUpload_ManifestComment(t *testing.T) {
ctx := context.Background()
h, _ := newHandler(t)
h, _ := newHandler(t, defaultResource())

r := defaultResource()

Expand Down Expand Up @@ -224,7 +226,7 @@ func TestUpload_ManifestComment(t *testing.T) {

func TestUpload_NewStatus(t *testing.T) {
ctx := context.Background()
h, _ := newHandler(t)
h, _ := newHandler(t, defaultResource())

r := defaultResource()
s := &scm.Status{
Expand All @@ -249,7 +251,7 @@ func TestUpload_NewStatus(t *testing.T) {

func TestUpload_UpdateStatus(t *testing.T) {
ctx := context.Background()
h, _ := newHandler(t)
h, _ := newHandler(t, defaultResource())

r := defaultResource()
r.Statuses[0].State = scm.StateCanceled
Expand All @@ -270,7 +272,7 @@ func TestUpload_UpdateStatus(t *testing.T) {

func TestUpload_NewLabel(t *testing.T) {
ctx := context.Background()
h, _ := newHandler(t)
h, _ := newHandler(t, defaultResource())

r := defaultResource()
r.PR.Labels = append(r.PR.Labels, &scm.Label{Name: "z"})
Expand All @@ -291,7 +293,7 @@ func TestUpload_NewLabel(t *testing.T) {

func TestUpload_DeleteLabel(t *testing.T) {
ctx := context.Background()
h, _ := newHandler(t)
h, _ := newHandler(t, defaultResource())

r := defaultResource()
r.PR.Labels = []*scm.Label{}
Expand All @@ -312,7 +314,7 @@ func TestUpload_DeleteLabel(t *testing.T) {

func TestUpload_ManifestLabel(t *testing.T) {
ctx := context.Background()
h, _ := newHandler(t)
h, _ := newHandler(t, defaultResource())

r := defaultResource()

Expand All @@ -336,3 +338,156 @@ func TestUpload_ManifestLabel(t *testing.T) {
t.Errorf("-want +got: %s", diff)
}
}

func TestUpload_NewPR(t *testing.T) {
pr := &scm.PullRequest{
Title: "this is a test",
Body: "this is the body",
Sha: "sha1",
Head: scm.PullRequestBranch{
Ref: "refs/heads/branch1",
Sha: "sha1",
Repo: scm.Repository{Name: "repo1"},
},
Base: scm.PullRequestBranch{
Ref: "refs/heads/branch1",
Sha: "sha2",
Repo: scm.Repository{Name: "repo1"},
},
}
r := &Resource{
PR: pr,
}
ctx := context.Background()

h, _ := newHandler(t, r)

if err := h.Upload(ctx, r); err != nil {
t.Fatal(err)
}

got, err := h.Download(ctx)
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(r.PR, got.PR); diff != "" {
t.Errorf("-want +got: %s", diff)
}
}

func TestUpload_NewPRHandlesErrorCreating(t *testing.T) {
pr := &scm.PullRequest{
Title: "this is a test",
Body: "this is the body",
Sha: "sha1",
Head: scm.PullRequestBranch{
Ref: "refs/heads/branch1",
Sha: "sha1",
Repo: scm.Repository{Name: "repo1"},
},
Base: scm.PullRequestBranch{
Ref: "refs/heads/branch1",
Sha: "sha2",
Repo: scm.Repository{Name: "repo1"},
},
}
r := &Resource{
PR: pr,
Comments: []*scm.Comment{
{
ID: 1,
Body: "testing",
},
{
Body: "test",
},
},
}
ctx := context.Background()

failed := errors.New("failed request")
h, _ := newHandler(t, r)
svc := &errPullService{err: failed}
h.client.PullRequests = svc
if err := h.Upload(ctx, r); !errors.Is(err, failed) {
t.Fatal(err)
}

if _, err := h.Download(ctx); !errors.Is(err, failed) {
t.Fatal(err)
}
if svc.calls > 1 {
t.Errorf("pull service calls: got %d, wanted 1", svc.calls)
}

}

type errPullService struct {
err error
calls int
}

func (s *errPullService) Find(ctx context.Context, repo string, number int) (*scm.PullRequest, *scm.Response, error) {
s.calls++
return nil, nil, s.err
}

func (s *errPullService) FindComment(context.Context, string, int, int) (*scm.Comment, *scm.Response, error) {
s.calls++
return nil, nil, s.err
}

func (s *errPullService) List(context.Context, string, scm.PullRequestListOptions) ([]*scm.PullRequest, *scm.Response, error) {
s.calls++
return nil, nil, s.err
}

func (s *errPullService) ListChanges(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
s.calls++
return nil, nil, s.err
}

func (s *errPullService) ListComments(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Comment, *scm.Response, error) {
s.calls++
return nil, nil, s.err
}

func (s *errPullService) ListLabels(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
s.calls++
return nil, nil, s.err
}

func (s *errPullService) AddLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
s.calls++
return nil, s.err
}

func (s *errPullService) DeleteLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
s.calls++
return nil, s.err
}

func (s *errPullService) Merge(context.Context, string, int) (*scm.Response, error) {
s.calls++
return nil, s.err
}

func (s *errPullService) Close(context.Context, string, int) (*scm.Response, error) {
s.calls++
return nil, s.err
}

func (s *errPullService) CreateComment(ctx context.Context, repo string, number int, comment *scm.CommentInput) (*scm.Comment, *scm.Response, error) {
s.calls++
return nil, nil, s.err
}

func (s *errPullService) DeleteComment(ctx context.Context, repo string, number int, id int) (*scm.Response, error) {
s.calls++
return nil, s.err
}

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

0 comments on commit 0a88aa7

Please sign in to comment.