Skip to content

Commit

Permalink
[CI-4621]: Added PR find and listCommits API support in go-scm
Browse files Browse the repository at this point in the history
  • Loading branch information
raghavharness committed Jun 1, 2022
1 parent 1383324 commit 9acf83b
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 2 deletions.
51 changes: 51 additions & 0 deletions scm/driver/azure/integration/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,54 @@ func TestCreatePR(t *testing.T) {
t.Errorf("PullRequests.Create does not have the correct title %v", outputPR.Title)
}
}

func TestPullRequestFind(t *testing.T) {
if token == "" {
t.Skip("Skipping, Acceptance test")
}
client = azure.NewDefault(organization, project)
client.Client = &http.Client{
Transport: &transport.Custom{
Before: func(r *http.Request) {
r.Header.Set("Authorization", fmt.Sprintf("Basic %s", token))
},
},
}
outputPR, response, err := client.PullRequests.Find(context.Background(), repoID, 1)
if err != nil {
t.Errorf("PullRequests.Find got an error %v", err)
}
if response.Status != http.StatusOK {
t.Errorf("PullRequests.Find did not get a 200 back %v", response.Status)
}
if outputPR.Title != "test_pr" {
t.Errorf("PullRequests.Find does not have the correct title %v", outputPR.Title)
}
}

func TestPullRequestCommits(t *testing.T) {
if token == "" {
t.Skip("Skipping, Acceptance test")
}
client = azure.NewDefault(organization, project)
client.Client = &http.Client{
Transport: &transport.Custom{
Before: func(r *http.Request) {
r.Header.Set("Authorization", fmt.Sprintf("Basic %s", token))
},
},
}
commits, response, err := client.PullRequests.ListCommits(context.Background(), repoID, 1, scm.ListOptions{})
if err != nil {
t.Errorf("PullRequests.ListCommits got an error %v", err)
}
if response.Status != http.StatusOK {
t.Errorf("PullRequests.ListCommits did not get a 200 back %v", response.Status)
}
if len(commits) < 1 {
t.Errorf("PullRequests.ListCommits there should be at least 1 commit %d", len(commits))
}
if commits[0].Sha == "" {
t.Errorf("PullRequests.ListCommits first entry did not get a sha back %v", commits[0].Sha)
}
}
14 changes: 12 additions & 2 deletions scm/driver/azure/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ type pullService struct {
}

func (s *pullService) Find(ctx context.Context, repo string, number int) (*scm.PullRequest, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
// https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pull-requests/get-pull-request?view=azure-devops-rest-6.0
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/pullrequests/%d?api-version=6.0",
s.client.owner, s.client.project, repo, number)
out := new(pr)
res, err := s.client.do(ctx, "GET", endpoint, nil, out)
return convertPullRequest(out), res, err
}

func (s *pullService) List(ctx context.Context, repo string, opts scm.PullRequestListOptions) ([]*scm.PullRequest, *scm.Response, error) {
Expand All @@ -29,7 +34,12 @@ func (s *pullService) ListChanges(ctx context.Context, repo string, number int,
}

func (s *pullService) ListCommits(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Commit, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
// https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pull-request-commits/get-pull-request-commits?view=azure-devops-rest-6.0
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/pullRequests/%d/commits?api-version=6.0",
s.client.owner, s.client.project, repo, number)
out := new(commitList)
res, err := s.client.do(ctx, "GET", endpoint, nil, out)
return convertCommitList(out.Value), res, err
}

func (s *pullService) Merge(ctx context.Context, repo string, number int) (*scm.Response, error) {
Expand Down
53 changes: 53 additions & 0 deletions scm/driver/azure/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,56 @@ func TestPullCreate(t *testing.T) {
t.Log(diff)
}
}

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

gock.New("https:/dev.azure.com/").
Get("/ORG/PROJ/_apis/git/repositories/REPOID/pullrequests/1").
Reply(200).
Type("application/json").
File("testdata/pr.json")


client := NewDefault("ORG", "PROJ")
got, _, err := client.PullRequests.Find(context.Background(), "REPOID", 1)
if err != nil {
t.Error(err)
return
}

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

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

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

gock.New("https:/dev.azure.com/").
Get("/ORG/PROJ/_apis/git/repositories/REPOID/pullRequests/1/commits").
Reply(200).
Type("application/json").
File("testdata/commits.json")

client := NewDefault("ORG", "PROJ")
got, _, err := client.PullRequests.ListCommits(context.Background(), "REPOID", 1, scm.ListOptions{})
if err != nil {
t.Error(err)
return
}

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

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

0 comments on commit 9acf83b

Please sign in to comment.