diff --git a/github/resource_github_repository.go b/github/resource_github_repository.go index fa6e340abc..d2d1314d29 100644 --- a/github/resource_github_repository.go +++ b/github/resource_github_repository.go @@ -75,6 +75,14 @@ func resourceGithubRepository() *schema.Resource { Computed: true, Description: "Can only be set after initial repository creation, and only if the target branch exists", }, + "license_template": { + Type: schema.TypeString, + Optional: true, + }, + "gitignore_template": { + Type: schema.TypeString, + Optional: true, + }, "full_name": { Type: schema.TypeString, @@ -112,19 +120,23 @@ func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository { allowRebaseMerge := d.Get("allow_rebase_merge").(bool) hasDownloads := d.Get("has_downloads").(bool) autoInit := d.Get("auto_init").(bool) + licenseTemplate := d.Get("license_template").(string) + gitIgnoreTemplate := d.Get("gitignore_template").(string) repo := &github.Repository{ - Name: &name, - Description: &description, - Homepage: &homepageUrl, - Private: &private, - HasIssues: &hasIssues, - HasWiki: &hasWiki, - AllowMergeCommit: &allowMergeCommit, - AllowSquashMerge: &allowSquashMerge, - AllowRebaseMerge: &allowRebaseMerge, - HasDownloads: &hasDownloads, - AutoInit: &autoInit, + Name: &name, + Description: &description, + Homepage: &homepageUrl, + Private: &private, + HasIssues: &hasIssues, + HasWiki: &hasWiki, + AllowMergeCommit: &allowMergeCommit, + AllowSquashMerge: &allowSquashMerge, + AllowRebaseMerge: &allowRebaseMerge, + HasDownloads: &hasDownloads, + AutoInit: &autoInit, + LicenseTemplate: &licenseTemplate, + GitignoreTemplate: &gitIgnoreTemplate, } return repo @@ -189,8 +201,13 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er client := meta.(*Organization).client repoReq := resourceGithubRepositoryObject(d) // Can only set `default_branch` on an already created repository with the target branches ref already in-place - defaultBranch := d.Get("default_branch").(string) - repoReq.DefaultBranch = &defaultBranch + if v, ok := d.GetOk("default_branch"); ok { + branch := v.(string) + // If branch is "master", and the repository hasn't been initialized yet, setting this value will fail + if branch != "master" { + repoReq.DefaultBranch = &branch + } + } repoName := d.Id() log.Printf("[DEBUG] update github repository %s/%s", meta.(*Organization).name, repoName) diff --git a/github/resource_github_repository_test.go b/github/resource_github_repository_test.go index 500aaea8f6..22cff2a595 100644 --- a/github/resource_github_repository_test.go +++ b/github/resource_github_repository_test.go @@ -137,6 +137,42 @@ func TestAccGithubRepository_defaultBranch(t *testing.T) { }) } +func TestAccGithubRepository_templates(t *testing.T) { + var repo github.Repository + randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) + name := fmt.Sprintf("tf-acc-test-%s", randString) + description := fmt.Sprintf("Terraform acceptance tests %s", randString) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckGithubRepositoryDestroy, + Steps: []resource.TestStep{ + { + Config: testAccGithubRepositoryConfigTemplates(randString), + Check: resource.ComposeTestCheckFunc( + testAccCheckGithubRepositoryExists("github_repository.foo", &repo), + testAccCheckGithubRepositoryAttributes(&repo, &testAccGithubRepositoryExpectedAttributes{ + Name: name, + Description: description, + Homepage: "http://example.com/", + HasIssues: true, + HasWiki: true, + AllowMergeCommit: true, + AutoInit: true, + AllowSquashMerge: false, + AllowRebaseMerge: false, + HasDownloads: true, + DefaultBranch: "master", + LicenseTemplate: "ms-pl", + GitignoreTemplate: "C++", + }), + ), + }, + }, + }) +} + func testAccCheckGithubRepositoryExists(n string, repo *github.Repository) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -161,19 +197,20 @@ func testAccCheckGithubRepositoryExists(n string, repo *github.Repository) resou } type testAccGithubRepositoryExpectedAttributes struct { - Name string - Description string - Homepage string - Private bool - HasIssues bool - HasWiki bool - AllowMergeCommit bool - AllowSquashMerge bool - AllowRebaseMerge bool - HasDownloads bool - AutoInit bool - - DefaultBranch string + Name string + Description string + Homepage string + Private bool + HasIssues bool + HasWiki bool + AllowMergeCommit bool + AllowSquashMerge bool + AllowRebaseMerge bool + HasDownloads bool + AutoInit bool + DefaultBranch string + LicenseTemplate string + GitignoreTemplate string } func testAccCheckGithubRepositoryAttributes(repo *github.Repository, want *testAccGithubRepositoryExpectedAttributes) resource.TestCheckFunc { @@ -220,6 +257,18 @@ func testAccCheckGithubRepositoryAttributes(repo *github.Repository, want *testA } } + if repo.GitignoreTemplate != nil { + if *repo.GitignoreTemplate != want.GitignoreTemplate { + return fmt.Errorf("got gitignore_template %q; want %q", *repo.GitignoreTemplate, want.GitignoreTemplate) + } + } + + if repo.LicenseTemplate != nil { + if *repo.LicenseTemplate != want.LicenseTemplate { + return fmt.Errorf("got license_template %q; want %q", *repo.LicenseTemplate, want.LicenseTemplate) + } + } + // For the rest of these, we just want to make sure they've been // populated with something that seems somewhat reasonable. if !strings.HasSuffix(*repo.FullName, "/"+want.Name) { @@ -399,3 +448,27 @@ resource "github_repository" "foo" { } `, randString, randString) } + +func testAccGithubRepositoryConfigTemplates(randString string) string { + return fmt.Sprintf(` +resource "github_repository" "foo" { + name = "tf-acc-test-%s" + description = "Terraform acceptance tests %s" + homepage_url = "http://example.com/" + + # So that acceptance tests can be run in a github organization + # with no billing + private = false + + has_issues = true + has_wiki = true + allow_merge_commit = true + allow_squash_merge = false + allow_rebase_merge = false + has_downloads = true + + license_template = "ms-pl" + gitignore_template = "C++" +} +`, randString, randString) +} diff --git a/website/docs/r/repository.html.markdown b/website/docs/r/repository.html.markdown index fc7850d6b2..699fab028e 100644 --- a/website/docs/r/repository.html.markdown +++ b/website/docs/r/repository.html.markdown @@ -56,6 +56,10 @@ The following arguments are supported: * `auto_init` - (Optional) Meaningful only during create; set to `true` to produce an initial commit in the repository. +* `gitignore_template` - (Optional) Meaningful only during create, will be ignored after repository creation. Use the [name of the template](https://github.com/github/gitignore) without the extension. For example, "Haskell". + +* `license_template` - (Optional) Meaningful only during create, will be ignored after repository creation. Use the [name of the template](https://github.com/github/choosealicense.com/tree/gh-pages/_licenses) without the extension. For example, "mit" or "mozilla". + * `default_branch` - (Optional) The name of the default branch of the repository. **NOTE:** This can only be set after a repository has already been created, and after a correct reference has been created for the target branch inside the repository. This means a user will have to omit this parameter from the initial repository creation and create the target branch inside of the repository prior to setting this attribute.