Skip to content
This repository has been archived by the owner on Dec 8, 2020. It is now read-only.

Commit

Permalink
Merge pull request #5 from solutionDrive/add-slug
Browse files Browse the repository at this point in the history
Add the ability to define a seperate slug for a repository
  • Loading branch information
grubernaut authored Aug 14, 2017
2 parents 805061e + ae2b54a commit 325b91b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
44 changes: 39 additions & 5 deletions bitbucket/resource_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Repository struct {
Language string `json:"language,omitempty"`
Description string `json:"description,omitempty"`
Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"`
UUID string `json:"uuid,omitempty"`
Project struct {
Key string `json:"key,omitempty"`
Expand Down Expand Up @@ -101,13 +102,18 @@ func resourceRepository() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"slug": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}

func newRepositoryFromResource(d *schema.ResourceData) *Repository {
repo := &Repository{
Name: d.Get("name").(string),
Slug: d.Get("slug").(string),
Language: d.Get("language").(string),
IsPrivate: d.Get("is_private").(bool),
Description: d.Get("description").(string),
Expand All @@ -132,9 +138,15 @@ func resourceRepositoryUpdate(d *schema.ResourceData, m interface{}) error {
enc := json.NewEncoder(jsonpayload)
enc.Encode(repository)

var repoSlug string
repoSlug = d.Get("slug").(string)
if repoSlug == "" {
repoSlug = d.Get("name").(string)
}

_, err := client.Put(fmt.Sprintf("2.0/repositories/%s/%s",
d.Get("owner").(string),
d.Get("name").(string),
repoSlug,
), jsonpayload)

if err != nil {
Expand All @@ -154,25 +166,37 @@ func resourceRepositoryCreate(d *schema.ResourceData, m interface{}) error {
return err
}

var repoSlug string
repoSlug = d.Get("slug").(string)
if repoSlug == "" {
repoSlug = d.Get("name").(string)
}

_, err = client.Post(fmt.Sprintf("2.0/repositories/%s/%s",
d.Get("owner").(string),
d.Get("name").(string),
repoSlug,
), bytes.NewBuffer(bytedata))

if err != nil {
return err
}

d.SetId(string(fmt.Sprintf("%s/%s", d.Get("owner").(string), d.Get("name").(string))))
d.SetId(string(fmt.Sprintf("%s/%s", d.Get("owner").(string), repoSlug)))

return resourceRepositoryRead(d, m)
}
func resourceRepositoryRead(d *schema.ResourceData, m interface{}) error {

var repoSlug string
repoSlug = d.Get("slug").(string)
if repoSlug == "" {
repoSlug = d.Get("name").(string)
}

client := m.(*BitbucketClient)
repo_req, _ := client.Get(fmt.Sprintf("2.0/repositories/%s/%s",
d.Get("owner").(string),
d.Get("name").(string),
repoSlug,
))

if repo_req.StatusCode == 200 {
Expand All @@ -194,6 +218,9 @@ func resourceRepositoryRead(d *schema.ResourceData, m interface{}) error {
d.Set("has_wiki", repo.HasWiki)
d.Set("has_issues", repo.HasIssues)
d.Set("name", repo.Name)
if repo.Slug != "" && repo.Name != repo.Slug {
d.Set("slug", repo.Slug)
}
d.Set("language", repo.Language)
d.Set("fork_policy", repo.ForkPolicy)
d.Set("website", repo.Website)
Expand All @@ -213,10 +240,17 @@ func resourceRepositoryRead(d *schema.ResourceData, m interface{}) error {
}

func resourceRepositoryDelete(d *schema.ResourceData, m interface{}) error {

var repoSlug string
repoSlug = d.Get("slug").(string)
if repoSlug == "" {
repoSlug = d.Get("name").(string)
}

client := m.(*BitbucketClient)
_, err := client.Delete(fmt.Sprintf("2.0/repositories/%s/%s",
d.Get("owner").(string),
d.Get("name").(string),
repoSlug,
))

return err
Expand Down
27 changes: 27 additions & 0 deletions bitbucket/resource_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ func TestAccBitbucketRepository_basic(t *testing.T) {
})
}

func TestAccBitbucketRepository_camelcase(t *testing.T) {
var repo Repository

testUser := os.Getenv("BITBUCKET_USERNAME")
testAccBitbucketRepositoryConfig := fmt.Sprintf(`
resource "bitbucket_repository" "test_repo" {
owner = "%s"
name = "TestRepoForRepositoryTest"
slug = "test-repo-for-repository-test"
}
`, testUser)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckBitbucketRepositoryDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccBitbucketRepositoryConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckBitbucketRepositoryExists("bitbucket_repository.test_repo", &repo),
),
},
},
})
}

func testAccCheckBitbucketRepositoryDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*BitbucketClient)
rs, ok := s.RootModule().Resources["bitbucket_repository.test_repo"]
Expand Down
15 changes: 14 additions & 1 deletion website/docs/r/repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,26 @@ resource "bitbucket_repository" "infrastructure" {
}
```

If you want to create a repository with a CamelCase name, you should provide
a seperate slug

```hcl
# Manage your repository
resource "bitbucket_repository" "infrastructure" {
owner = "myteam"
name = "TerraformCode"
slug = "terraform-code"
}
```

## Argument Reference

The following arguments are supported:

* `owner` - (Required) The owner of this repository. Can be you or any team you
have write access to.
* `name` - (Optional) The name of the repository.
* `name` - (Required) The name of the repository.
* `slug` - (Optional) The slug of the repository.
* `scm` - (Optional) What SCM you want to use. Valid options are hg or git.
Defaults to git.
* `is_private` - (Optional) If this should be private or not. Defaults to `true`.
Expand Down

0 comments on commit 325b91b

Please sign in to comment.