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

r/github_repository: Add license_template and gitignore_template #24

Merged
merged 1 commit into from
Jun 28, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
43 changes: 30 additions & 13 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
99 changes: 86 additions & 13 deletions github/resource_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions website/docs/r/repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down