Skip to content

Commit

Permalink
project default reviewers use sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
DrFaust92 committed Feb 18, 2024
1 parent e49bdb4 commit 5c0562c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
35 changes: 11 additions & 24 deletions bitbucket/resource_project_default_reviewers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package bitbucket

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"

"github.com/DrFaust92/bitbucket-go-client"
"github.com/antihax/optional"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -63,39 +63,27 @@ func resourceProjectDefaultReviewersCreate(ctx context.Context, d *schema.Resour
}

func resourceProjectDefaultReviewersRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(Clients).httpClient
c := m.(Clients).genClient
projectsApi := c.ApiClient.ProjectsApi

workspace, project, err := defaultProjectReviewersId(d.Id())
if err != nil {
return diag.FromErr(err)
}

resourceURL := fmt.Sprintf("2.0/workspaces/%s/projects/%s/default-reviewers", workspace, project)

res, err := client.Get(resourceURL)
if err != nil {
return diag.FromErr(err)
}

if res.StatusCode == http.StatusNotFound {
log.Printf("[WARN] Project Default Reviewers (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

var reviewers bitbucket.PaginatedDefaultReviewerAndType
options := bitbucket.ProjectsApiWorkspacesWorkspaceProjectsProjectKeyDefaultReviewersGetOpts{}
var terraformReviewers []string

for {
reviewersResponse, err := client.Get(resourceURL)
if err != nil {
reviewers, res, err := projectsApi.WorkspacesWorkspaceProjectsProjectKeyDefaultReviewersGet(c.AuthContext, project, workspace, &options)
if err := handleClientError(err); err != nil {
return diag.FromErr(err)
}

decoder := json.NewDecoder(reviewersResponse.Body)
err = decoder.Decode(&reviewers)
if err != nil {
return diag.FromErr(err)
if res.StatusCode == http.StatusNotFound {
log.Printf("[WARN] Project Default Reviewers (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

for _, reviewer := range reviewers.Values {
Expand All @@ -104,8 +92,7 @@ func resourceProjectDefaultReviewersRead(ctx context.Context, d *schema.Resource

if reviewers.Next != "" {
nextPage := reviewers.Page + 1
resourceURL = fmt.Sprintf("2.0/workspaces/%s/projects/%s/default-reviewers?page=%d", workspace, project, nextPage)
reviewers = bitbucket.PaginatedDefaultReviewerAndType{}
options.Page = optional.NewInt32(nextPage)
} else {
break
}
Expand Down
7 changes: 5 additions & 2 deletions bitbucket/resource_project_default_reviewers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,15 @@ resource "bitbucket_project_default_reviewers" "test" {
}

func testAccCheckBitbucketProjectDefaultReviewersDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(Clients).httpClient
client := testAccProvider.Meta().(Clients).genClient
projectsApi := client.ApiClient.ProjectsApi

for _, rs := range s.RootModule().Resources {
if rs.Type != "bitbucket_project_default_reviewers" {
continue
}
response, _ := client.Get(fmt.Sprintf("2.0/workspaces/%s/projects/%s/default-reviewers", rs.Primary.Attributes["workspace"], rs.Primary.Attributes["project"]))

_, response, _ := projectsApi.WorkspacesWorkspaceProjectsProjectKeyDefaultReviewersGet(client.AuthContext, rs.Primary.Attributes["project"], rs.Primary.Attributes["workspace"], nil)

if response.StatusCode != http.StatusNotFound {
return fmt.Errorf("Project Defaults Reviewer still exists")
Expand Down

0 comments on commit 5c0562c

Please sign in to comment.