From 446da4b4616882fe20d38fcc07ffcd960cabc3e4 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Wed, 2 Oct 2019 14:27:49 +0100 Subject: [PATCH 1/2] fix: better example for listing a PR or PRs --- scm/factory/examples/pr/main.go | 36 +++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/scm/factory/examples/pr/main.go b/scm/factory/examples/pr/main.go index ab4821267..eac6065ec 100644 --- a/scm/factory/examples/pr/main.go +++ b/scm/factory/examples/pr/main.go @@ -7,6 +7,7 @@ import ( "strconv" "github.com/ghodss/yaml" + "github.com/jenkins-x/go-scm/scm" "github.com/pkg/errors" "github.com/jenkins-x/go-scm/scm/factory" @@ -15,27 +16,46 @@ import ( func main() { args := os.Args - if len(args) < 3 { + if len(args) < 2 { fmt.Printf("usage: org/repo prNumber") return } repo := args[1] - prText := args[2] - number, err := strconv.Atoi(prText) + + client, err := factory.NewClientFromEnvironment() if err != nil { - helpers.Fail(errors.Wrapf(err, "failed to parse PR number: %s", prText)) + helpers.Fail(err) return } + ctx := context.Background() - client, err := factory.NewClientFromEnvironment() + if len(args) < 3 { + fmt.Printf("Getting PRs\n") + + prs, _, err := client.PullRequests.List(ctx, repo, scm.PullRequestListOptions{}) + if err != nil { + helpers.Fail(err) + return + } + for _, pr := range prs { + fmt.Printf("Found PullRequest:\n") + data, err := yaml.Marshal(pr) + if err != nil { + helpers.Fail(errors.Wrap(err, "failed to marshal PR as YAML")) + return + } + fmt.Printf("%s:\n", string(data)) + } + } + prText := args[2] + number, err := strconv.Atoi(prText) if err != nil { - helpers.Fail(err) + helpers.Fail(errors.Wrapf(err, "failed to parse PR number: %s", prText)) return } - fmt.Printf("Getting PRs\n") + fmt.Printf("Getting PR\n") - ctx := context.Background() pr, _, err := client.PullRequests.Find(ctx, repo, number) if err != nil { helpers.Fail(err) From 4a1196dffcbeb705086fe5dcea5173a5265ec39a Mon Sep 17 00:00:00 2001 From: James Strachan Date: Wed, 2 Oct 2019 14:28:45 +0100 Subject: [PATCH 2/2] fix: avoid failing early on bitbucket cloud --- scm/driver/bitbucket/issue.go | 50 ++++++++++++++++++++---------- scm/driver/bitbucket/issue_test.go | 20 ++++++------ 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/scm/driver/bitbucket/issue.go b/scm/driver/bitbucket/issue.go index 689ff2d11..25e270309 100644 --- a/scm/driver/bitbucket/issue.go +++ b/scm/driver/bitbucket/issue.go @@ -15,70 +15,86 @@ type issueService struct { } func (s *issueService) Search(context.Context, scm.SearchOptions) ([]*scm.SearchIssue, *scm.Response, error) { - // TODO implemment + // TODO implement return nil, nil, nil } func (s *issueService) AssignIssue(ctx context.Context, repo string, number int, logins []string) (*scm.Response, error) { - panic("implement me") + // TODO implement + return nil, nil } func (s *issueService) UnassignIssue(ctx context.Context, repo string, number int, logins []string) (*scm.Response, error) { - panic("implement me") + // TODO implement + return nil, nil } func (s *issueService) ListEvents(context.Context, string, int, scm.ListOptions) ([]*scm.ListedIssueEvent, *scm.Response, error) { - panic("implement me") + // TODO implement + return nil, nil, nil } func (s *issueService) ListLabels(context.Context, string, int, scm.ListOptions) ([]*scm.Label, *scm.Response, error) { - panic("implement me") + // TODO implement + return nil, nil, nil } func (s *issueService) AddLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) { - return nil, scm.ErrNotSupported + // TODO implement + return nil, nil } func (s *issueService) DeleteLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) { - return nil, scm.ErrNotSupported + // TODO implement + return nil, nil } func (s *issueService) Find(ctx context.Context, repo string, number int) (*scm.Issue, *scm.Response, error) { - return nil, nil, scm.ErrNotSupported + // TODO implement + return nil, nil, nil } func (s *issueService) FindComment(ctx context.Context, repo string, index, id int) (*scm.Comment, *scm.Response, error) { - return nil, nil, scm.ErrNotSupported + // TODO implement + return nil, nil, nil } func (s *issueService) List(ctx context.Context, repo string, opts scm.IssueListOptions) ([]*scm.Issue, *scm.Response, error) { - return nil, nil, scm.ErrNotSupported + // TODO implement + return nil, nil, nil } func (s *issueService) ListComments(ctx context.Context, repo string, index int, opts scm.ListOptions) ([]*scm.Comment, *scm.Response, error) { - return nil, nil, scm.ErrNotSupported + // TODO implement + return nil, nil, nil } func (s *issueService) Create(ctx context.Context, repo string, input *scm.IssueInput) (*scm.Issue, *scm.Response, error) { - return nil, nil, scm.ErrNotSupported + // TODO implement + return nil, nil, nil } func (s *issueService) CreateComment(ctx context.Context, repo string, number int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { - return nil, nil, scm.ErrNotSupported + // TODO implement + return nil, nil, nil } func (s *issueService) DeleteComment(ctx context.Context, repo string, number, id int) (*scm.Response, error) { - return nil, scm.ErrNotSupported + // TODO implement + return nil, nil } func (s *issueService) Close(ctx context.Context, repo string, number int) (*scm.Response, error) { - return nil, scm.ErrNotSupported + // TODO implement + return nil, nil } func (s *issueService) Lock(ctx context.Context, repo string, number int) (*scm.Response, error) { - return nil, scm.ErrNotSupported + // TODO implement + return nil, nil } func (s *issueService) Unlock(ctx context.Context, repo string, number int) (*scm.Response, error) { - return nil, scm.ErrNotSupported + // TODO implement + return nil, nil } diff --git a/scm/driver/bitbucket/issue_test.go b/scm/driver/bitbucket/issue_test.go index efad79aff..fdc71b874 100644 --- a/scm/driver/bitbucket/issue_test.go +++ b/scm/driver/bitbucket/issue_test.go @@ -13,70 +13,70 @@ import ( func TestIssueFind(t *testing.T) { _, _, err := NewDefault().Issues.Find(context.Background(), "", 0) - if err != scm.ErrNotSupported { + if err != nil && err != scm.ErrNotSupported { t.Errorf("Expect Not Supported error") } } func TestIssueCommentFind(t *testing.T) { _, _, err := NewDefault().Issues.FindComment(context.Background(), "", 0, 0) - if err != scm.ErrNotSupported { + if err != nil && err != scm.ErrNotSupported { t.Errorf("Expect Not Supported error") } } func TestIssueList(t *testing.T) { _, _, err := NewDefault().Issues.List(context.Background(), "", scm.IssueListOptions{}) - if err != scm.ErrNotSupported { + if err != nil && err != scm.ErrNotSupported { t.Errorf("Expect Not Supported error") } } func TestIssueListComments(t *testing.T) { _, _, err := NewDefault().Issues.ListComments(context.Background(), "", 0, scm.ListOptions{}) - if err != scm.ErrNotSupported { + if err != nil && err != scm.ErrNotSupported { t.Errorf("Expect Not Supported error") } } func TestIssueCreate(t *testing.T) { _, _, err := NewDefault().Issues.Create(context.Background(), "", &scm.IssueInput{}) - if err != scm.ErrNotSupported { + if err != nil && err != scm.ErrNotSupported { t.Errorf("Expect Not Supported error") } } func TestIssueCreateComment(t *testing.T) { _, _, err := NewDefault().Issues.CreateComment(context.Background(), "", 0, &scm.CommentInput{}) - if err != scm.ErrNotSupported { + if err != nil && err != scm.ErrNotSupported { t.Errorf("Expect Not Supported error") } } func TestIssueCommentDelete(t *testing.T) { _, err := NewDefault().Issues.DeleteComment(context.Background(), "", 0, 0) - if err != scm.ErrNotSupported { + if err != nil && err != scm.ErrNotSupported { t.Errorf("Expect Not Supported error") } } func TestIssueClose(t *testing.T) { _, err := NewDefault().Issues.Close(context.Background(), "", 0) - if err != scm.ErrNotSupported { + if err != nil && err != scm.ErrNotSupported { t.Errorf("Expect Not Supported error") } } func TestIssueLock(t *testing.T) { _, err := NewDefault().Issues.Lock(context.Background(), "", 0) - if err != scm.ErrNotSupported { + if err != nil && err != scm.ErrNotSupported { t.Errorf("Expect Not Supported error") } } func TestIssueUnlock(t *testing.T) { _, err := NewDefault().Issues.Unlock(context.Background(), "", 0) - if err != scm.ErrNotSupported { + if err != nil && err != scm.ErrNotSupported { t.Errorf("Expect Not Supported error") } }