Skip to content

Commit

Permalink
Add repository data source.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joachim Desroches committed Aug 13, 2024
1 parent 8de67e9 commit e6e10d2
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 2 deletions.
4 changes: 2 additions & 2 deletions bitbucket/data_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
80 changes: 80 additions & 0 deletions bitbucket/data_repository.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions bitbucket/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
44 changes: 44 additions & 0 deletions docs/data-sources/repository.md
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit e6e10d2

Please sign in to comment.