Skip to content

Commit

Permalink
Merge pull request drone#34 from jstrachan/stuff
Browse files Browse the repository at this point in the history
fix: add support for get PRs to bitbucket cloud
  • Loading branch information
jenkins-x-bot authored Oct 3, 2019
2 parents 210d615 + c01408e commit 6fc527f
Show file tree
Hide file tree
Showing 13 changed files with 634 additions and 26 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ To help hack on the different drivers here's a list of docs which outline the gi

### Bitbucket Server

* REST API reference: https://docs.atlassian.com/bitbucket-server/rest/6.5.1/bitbucket-rest.html
* REST API reference: https://docs.atlassian.com/bitbucket-server/rest/6.5.1/bitbucket-rest.html
* Webhooks: https://confluence.atlassian.com/bitbucketserver/event-payload-938025882.html

### Bitbucket Cloud

* REST API reference: https://developer.atlassian.com/bitbucket/api/2/reference/

### Gitlab

* REST API reference: https://docs.gitlab.com/ee/api/
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/pkg/errors v0.8.1
github.com/shurcooL/githubv4 v0.0.0-20190718010115-4ba037080260
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f // indirect
github.com/sirupsen/logrus v1.4.2 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/stretchr/testify v1.3.0
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
k8s.io/apimachinery v0.0.0-20190703205208-4cfb76a8bf76
Expand Down
106 changes: 101 additions & 5 deletions scm/driver/bitbucket/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
package bitbucket

import (
"bytes"
"context"
"fmt"
"strings"
"time"

"github.com/jenkins-x/go-scm/scm"
)
Expand All @@ -15,6 +18,8 @@ type pullService struct {
*issueService
}

const debugDump = false

func (s *pullService) Find(ctx context.Context, repo string, number int) (*scm.PullRequest, *scm.Response, error) {
path := fmt.Sprintf("2.0/repositories/%s/pullrequests/%d", repo, number)
out := new(pullRequest)
Expand All @@ -25,6 +30,12 @@ func (s *pullService) Find(ctx context.Context, repo string, number int) (*scm.P
func (s *pullService) List(ctx context.Context, repo string, opts scm.PullRequestListOptions) ([]*scm.PullRequest, *scm.Response, error) {
path := fmt.Sprintf("2.0/repositories/%s/pullrequests?%s", repo, encodePullRequestListOptions(opts))
out := new(pullRequests)
if debugDump {
var buf bytes.Buffer
res, err := s.client.do(ctx, "GET", path, nil, &buf)
fmt.Printf("%s\n", buf.String())
return nil, res, err
}
res, err := s.client.do(ctx, "GET", path, nil, out)
copyPagination(out.pagination, res)
return convertPullRequests(out), res, err
Expand All @@ -48,17 +59,102 @@ func (s *pullService) Close(ctx context.Context, repo string, number int) (*scm.
return nil, scm.ErrNotSupported
}

type pullRequest struct{}
type prCommit struct {
LatestCommit string `json:"hash"`
}

type prSource struct {
Commit struct {
Type string `json:"type"`
Ref string `json:"ref"`
Commit string `json:"hash"`
} `json:"commit"`
Repository repository `json:"repository"`
Branch struct {
Name string `json:"name"`
} `json:"branch"`
}

type prDestination struct {
Commit struct {
Type string `json:"type"`
Ref string `json:"ref"`
Commit string `json:"Commit"`
} `json:"commit"`
Repository repository `json:"repository"`
Branch struct {
Name string `json:"name"`
} `json:"branch"`
}

type pullRequest struct {
ID int `json:"id"`
//Version int `json:"version"`
Title string `json:"title"`
Description string `json:"description"`
State string `json:"state"`
CreatedDate time.Time `json:"created_on"`
UpdatedDate time.Time `json:"updated_on"`
Source prSource `json:"source"`
Destination prDestination `json:"destination"`
Locked bool `json:"locked"`
Author user `json:"author"`
Reviewers []user `json:"reviewers"`
Participants []user `json:"participants"`
Links struct {
Self link `json:"self"`
HTML link `json:"html"`
} `json:"links"`
}

type pullRequests struct {
pagination
Values []*pullRequest `json:"values"`
}

func convertPullRequests(from *pullRequests) []*scm.PullRequest {
return nil
func convertPullRequest(from *pullRequest) *scm.PullRequest {
// TODO
fork := "false"
closed := strings.ToLower(from.State) != "open"
return &scm.PullRequest{
Number: from.ID,
Title: from.Title,
Body: from.Description,
Sha: from.Source.Commit.Commit,
Ref: fmt.Sprintf("refs/pull-requests/%d/from", from.ID),
Source: from.Source.Commit.Commit,
Target: from.Destination.Commit.Commit,
Fork: fork,
Base: convertPullRequestBranch(from.Destination.Commit.Ref, from.Destination.Commit.Commit, from.Destination.Repository),
Head: convertPullRequestBranch(from.Source.Commit.Ref, from.Source.Commit.Commit, from.Source.Repository),
Link: from.Links.HTML.Href,
State: strings.ToLower(from.State),
Closed: closed,
Merged: from.State == "MERGED",
Created: from.CreatedDate,
Updated: from.UpdatedDate,
Author: scm.User{
Login: from.Author.GetLogin(),
Name: from.Author.DisplayName,
Email: from.Author.EmailAddress,
Link: from.Author.Links.Self.Href,
Avatar: from.Author.Links.Avatar.Href,
},
}
}

func convertPullRequest(from *pullRequest) *scm.PullRequest {
return nil
func convertPullRequestBranch(ref string, sha string, repo repository) scm.PullRequestBranch {
return scm.PullRequestBranch{
Ref: ref,
Sha: sha,
Repo: *convertRepository(&repo),
}
}

func convertPullRequests(from *pullRequests) []*scm.PullRequest {
answer := []*scm.PullRequest{}
for _, pr := range from.Values {
answer = append(answer, convertPullRequest(pr))
}
return answer
}
32 changes: 31 additions & 1 deletion scm/driver/bitbucket/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,37 @@ func TestPullFind(t *testing.T) {
}

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

gock.New("https://api.bitbucket.org").
Get("/2.0/repositories/octocat/hello-world/pullrequests").
MatchParam("pagelen", "30").
MatchParam("page", "1").
MatchParam("state", "all").
Reply(200).
Type("application/json").
//SetHeaders(mockHeaders).
//SetHeaders(mockPageHeaders).
File("testdata/pulls.json")

client := NewDefault()
got, _, err := client.PullRequests.List(context.Background(), "octocat/hello-world", scm.PullRequestListOptions{Page: 1, Size: 30, Open: true, Closed: true})
if err != nil {
t.Error(err)
return
}

want := []*scm.PullRequest{}
raw, _ := ioutil.ReadFile("testdata/pulls.json.golden")
json.Unmarshal(raw, &want)

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

data, _ := json.Marshal(got)
t.Logf("got JSON: %s", data)
}
}

func TestPullListChanges(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions scm/driver/bitbucket/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func convertRepository(from *repository) *scm.Repository {
ID: from.UUID,
Name: name,
Namespace: namespace,
FullName: from.FullName,
Link: fmt.Sprintf("https://bitbucket.org/%s", from.FullName),
Branch: from.Mainbranch.Name,
Private: from.IsPrivate,
Expand Down
3 changes: 2 additions & 1 deletion scm/driver/bitbucket/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ func TestRepositoryList(t *testing.T) {
client, _ := New("https://api.bitbucket.org")

for {
repos, res, err := client.Repositories.List(context.Background(), opts)
ctx := context.Background()
repos, res, err := client.Repositories.List(ctx, opts)
if err != nil {
t.Error(err)
}
Expand Down
Loading

0 comments on commit 6fc527f

Please sign in to comment.