diff --git a/bitbucket/data_deployment.go b/bitbucket/data_deployment.go index 9c2feb86..e1811447 100644 --- a/bitbucket/data_deployment.go +++ b/bitbucket/data_deployment.go @@ -57,11 +57,11 @@ func dataReadDeployment(ctx context.Context, d *schema.ResourceData, m interface } if res.StatusCode == http.StatusNotFound { - return diag.Errorf("user not found") + return diag.Errorf("deployment not found") } if res.StatusCode >= http.StatusInternalServerError { - return diag.Errorf("internal server error fetching user") + return diag.Errorf("internal server error fetching deployment") } var deploy Deployment diff --git a/bitbucket/data_repository.go b/bitbucket/data_repository.go new file mode 100644 index 00000000..ed92232e --- /dev/null +++ b/bitbucket/data_repository.go @@ -0,0 +1,80 @@ +package bitbucket + +import ( + "context" + "encoding/json" + "fmt" + "io" + "log" + "net/http" + + "github.com/DrFaust92/bitbucket-go-client" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataRepository() *schema.Resource { + return &schema.Resource{ + ReadWithoutTimeout: dataReadRepository, + + Schema: map[string]*schema.Schema{ + "uuid": { + Type: schema.TypeString, + Computed: true, + }, + "slug": { + Type: schema.TypeString, + Required: true, + }, + "workspace": { + Type: schema.TypeString, + Required: true, + }, + }, + } +} + +func dataReadRepository(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + c := m.(Clients).httpClient + + slug := d.Get("slug").(string) + workspace := d.Get("workspace").(string) + + res, err := c.Get(fmt.Sprintf("2.0/repositories/%s/%s", + workspace, + slug)) + + if err != nil { + return diag.FromErr(err) + } + + if res.StatusCode == http.StatusNotFound { + return diag.Errorf(("repository not found")) + } + + if res.StatusCode == http.StatusInternalServerError { + return diag.Errorf("internal server error fetching repository") + } + + var repository bitbucket.Repository + body, readerr := io.ReadAll(res.Body) + + if readerr != nil { + return diag.FromErr(readerr) + } + + log.Printf("[DEBUG] Deployment response raw: %s", string(body)) + + decodeerr := json.Unmarshal(body, &repository) + if decodeerr != nil { + return diag.FromErr(decodeerr) + } + + log.Printf("[DEBUG] Deployment response: %#v", repository) + + d.SetId(fmt.Sprintf("%s/%s", workspace, slug)) + d.Set("workspace", workspace) + d.Set("slug", slug) + + return nil +} diff --git a/bitbucket/provider.go b/bitbucket/provider.go index e241b1df..7a04b922 100644 --- a/bitbucket/provider.go +++ b/bitbucket/provider.go @@ -106,6 +106,7 @@ func Provider() *schema.Provider { "bitbucket_ip_ranges": dataIPRanges(), "bitbucket_pipeline_oidc_config": dataPipelineOidcConfig(), "bitbucket_pipeline_oidc_config_keys": dataPipelineOidcConfigKeys(), + "bitbucket_repository": dataRepository(), "bitbucket_user": dataUser(), "bitbucket_workspace": dataWorkspace(), "bitbucket_workspace_members": dataWorkspaceMembers(), diff --git a/docs/data-sources/repository.md b/docs/data-sources/repository.md new file mode 100644 index 00000000..f07c2127 --- /dev/null +++ b/docs/data-sources/repository.md @@ -0,0 +1,44 @@ +--- +layout: bitbucket +page_title: "Bitbucket: bitbucket_repository" +sidebar_current: "docs-bitbucket-data-repository" +description: |- + Provides a data source for a Bitbucket repository +--- + +# bitbucket\_repository + +Provide a way to fetch data about a repository. + +OAuth2 Scopes: `repository` + +## Example Usage + +```hcl +data "bitbucket_repository" "my_repo" { + workspace = "myworspace" + slug = "my-repo-slug" +} +``` + +## Argument Reference + +* `workspace` - (Required) This can either be the workspace ID (slug) or the workspace UUID surrounded by curly-braces +* `slug`: - (Required) This can either be the repository slug or the UUID of the repository, surrounded by curly-braces + +## Attribute Reference + +* `owner` - The owner of this repository. +* `name` - The name of the repository. +* `slug` - The slug of the repository. +* `scm` - The SCM (`git` or `hg`) of the repository. +* `is_private` - If this repository is private or not. +* `website` - URL of website associated with this repository. +* `language` - The programming language of this repository. +* `has_issues` - If this repository has issues turned on. +* `has_wiki` - If this repository has wiki turned on. +* `project_key` - The key of a project the repository is linked to, if applicable. +* `fork_policy` - The repository fork policy. Valid values are + `allow_forks`. Valid values are `allow_forks`, `no_public_forks`, `no_forks`. +* `description` - The description of the repo. +* `pipelines_enabled` - If this repository has pipelines turned on.