Skip to content

Commit

Permalink
Merge pull request #351 from garethjevans/improve-azure-integration-t…
Browse files Browse the repository at this point in the history
…ests

chore: improve azure integration tests
  • Loading branch information
jenkins-x-bot authored Nov 15, 2022
2 parents 648bf6d + 649c75c commit 6dae22e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
26 changes: 23 additions & 3 deletions scm/driver/azure/integration/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,36 @@ func TestCreatePR(t *testing.T) {
},
},
}

existingPRs, response, err := client.PullRequests.List(context.Background(), repositoryID, &scm.PullRequestListOptions{})
if err != nil {
t.Errorf("PullRequests.List got an error %v", err)
}
if response.Status != http.StatusOK {
t.Errorf("PullRequests.List did not get a 200 back %v", response.Status)
}

for _, existingPR := range existingPRs {
t.Logf("Closing PR %d", existingPR.Number)
response, err = client.PullRequests.Close(context.Background(), repositoryID, existingPR.Number)
if err != nil {
t.Errorf("PullRequests.Close got an error %v", err)
}
if response.Status != http.StatusOK {
t.Errorf("PullRequests.Close did not get a 200 back %v", response.Status)
}
}

input := &scm.PullRequestInput{
Title: "test_pr",
Body: "test_pr_body",
Head: "pr_branch",
Base: "main",
}

outputPR, response, listerr := client.PullRequests.Create(context.Background(), repositoryID, input)
if listerr != nil {
t.Errorf("PullRequests.Create got an error %v", listerr)
outputPR, response, err := client.PullRequests.Create(context.Background(), repositoryID, input)
if err != nil {
t.Errorf("PullRequests.Create got an error %v", err)
}
if response.Status != http.StatusCreated {
t.Errorf("PullRequests.Create did not get a 201 back %v", response.Status)
Expand Down
35 changes: 33 additions & 2 deletions scm/driver/azure/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ 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) {
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?api-version=6.0", s.client.owner, s.client.project, repo)
out := new(prList)
res, err := s.client.do(ctx, "GET", endpoint, nil, out)
return convertPullRequests(out), res, err
}

func (s *pullService) ListChanges(ctx context.Context, repo string, number int, opts *scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
Expand All @@ -60,7 +64,14 @@ func (s *pullService) Merge(ctx context.Context, repo string, number int, opts *
}

func (s *pullService) Close(ctx context.Context, repo string, number int) (*scm.Response, error) {
return 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?api-version=6.0",
s.client.owner, s.client.project, repo, number)
body := prUpdateInput{
Status: PrAbandoned,
}
res, err := s.client.do(ctx, "PATCH", endpoint, body, nil)
return res, err
}

func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRequestInput) (*scm.PullRequest, *scm.Response, error) {
Expand Down Expand Up @@ -171,6 +182,26 @@ type pr struct {
ArtifactID string `json:"artifactId"`
}

type prList struct {
Values []pr `json:"value"`
}

var (
PrAbandoned = "abandoned"
)

type prUpdateInput struct {
Status string `json:"status"`
}

func convertPullRequests(from *prList) []*scm.PullRequest {
var prs []*scm.PullRequest
for index := range from.Values {
prs = append(prs, convertPullRequest(&from.Values[index]))
}
return prs
}

func convertPullRequest(from *pr) *scm.PullRequest {
return &scm.PullRequest{
Number: from.PullRequestID,
Expand Down
4 changes: 2 additions & 2 deletions scm/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ func ExampleWebhook() {
}

http.HandleFunc("/hook", handler)
err := http.ListenAndServe(":8000", nil)
err := http.ListenAndServe(":8000", nil) // #nosec G114
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -742,7 +742,7 @@ func ExampleWebhook_lookupSecret() {
}

http.HandleFunc("/hook", handler)
err := http.ListenAndServe(":8000", nil)
err := http.ListenAndServe(":8000", nil) // #nosec G114
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 6dae22e

Please sign in to comment.