Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] archive repo on destroy #432

Merged
merged 7 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/dotcom-acceptance-tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Dotcom Acceptance Tests

on:
push:
jcudit marked this conversation as resolved.
Show resolved Hide resolved
pull_request:
types: [opened, synchronize, reopened]

Expand Down
36 changes: 35 additions & 1 deletion github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ func resourceGithubRepository() *schema.Resource {
Optional: true,
Default: false,
},
"archive_on_destroy": {
Type: schema.TypeBool,
Optional: true,
},
"topics": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -352,6 +356,14 @@ func resourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) erro
}

func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) error {

// Can only update a repository if it is not archived or the update is to
// archive the repository (unarchiving is not supported by the Github API)
if d.Get("archived").(bool) && !d.HasChange("archived") {
log.Printf("[DEBUG] Skipping update of archived repository")
return nil
}

client := meta.(*Owner).v3client

repoReq := resourceGithubRepositoryObject(d)
Expand Down Expand Up @@ -387,6 +399,15 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er
if err != nil {
return err
}
d.SetId(*repo.Name)

if d.HasChange("topics") {
topics := repoReq.Topics
_, _, err = client.Repositories.ReplaceAllTopics(ctx, owner, *repo.Name, topics)
if err != nil {
return err
}
}
}

return resourceGithubRepositoryRead(d, meta)
Expand All @@ -398,8 +419,21 @@ func resourceGithubRepositoryDelete(d *schema.ResourceData, meta interface{}) er
owner := meta.(*Owner).name
ctx := context.WithValue(context.Background(), ctxId, d.Id())

archiveOnDestroy := d.Get("archive_on_destroy").(bool)
if archiveOnDestroy {
if d.Get("archived").(bool) {
log.Printf("[DEBUG] Repository already archived, nothing to do on delete: %s/%s", owner, repoName)
return nil
} else {
d.Set("archived", true)
repoReq := resourceGithubRepositoryObject(d)
log.Printf("[DEBUG] Archiving repository on delete: %s/%s", owner, repoName)
_, _, err := client.Repositories.Edit(ctx, owner, repoName, repoReq)
return err
}
}

log.Printf("[DEBUG] Deleting repository: %s/%s", owner, repoName)
_, err := client.Repositories.Delete(ctx, owner, repoName)

return err
}
59 changes: 59 additions & 0 deletions github/resource_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,65 @@ func TestAccGithubRepositories(t *testing.T) {

})

t.Run("archives repositories on destroy", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-%[1]s"
auto_init = true
archive_on_destroy = true
archived = false
}
`, randomID)

checks := map[string]resource.TestCheckFunc{
"before": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "archived",
"false",
),
),
"after": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "archived",
"true",
),
),
}

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: checks["before"],
},
{
Config: strings.Replace(config,
`archived = false`,
`archived = true`, 1),
Check: checks["after"],
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
testCase(t, individual)
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})

})

}

func testSweepRepositories(region string) error {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ initial repository creation and create the target branch inside of the repositor

* `archived` - (Optional) Specifies if the repository should be archived. Defaults to `false`. **NOTE** Currently, the API does not support unarchiving.

* `archive_on_destroy` - (Optional) Set to `true` to archive the repository instead of deleting on destroy.

* `topics` - (Optional) The list of topics of the repository.

* `template` - (Optional) Use a template repository to create this resource. See [Template Repositories](#template-repositories) below for details.
Expand Down