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

feat: Add build_type to github_repository (pages) #1663

Merged
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
13 changes: 12 additions & 1 deletion github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func resourceGithubRepository() *schema.Resource {
"source": {
Type: schema.TypeList,
MaxItems: 1,
Required: true,
Optional: true,
Description: "The source branch and directory for the rendered Pages site.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -265,6 +265,13 @@ func resourceGithubRepository() *schema.Resource {
},
},
},
"build_type": {
Type: schema.TypeString,
Optional: true,
Default: "legacy",
Description: "The type the page should be sourced.",
ValidateFunc: validateValueFunc([]string{"legacy", "workflow"}),
},
"cname": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -860,6 +867,9 @@ func expandPagesUpdate(input []interface{}) *github.PagesUpdate {
}
update.Source = &github.PagesSource{Branch: &sourceBranch, Path: &sourcePath}

pagesBuildType := pages["build_type"].(string)
update.BuildType = &pagesBuildType

return update
}

Expand All @@ -874,6 +884,7 @@ func flattenPages(pages *github.Pages) []interface{} {

pagesMap := make(map[string]interface{})
pagesMap["source"] = []interface{}{sourceMap}
pagesMap["build_type"] = pages.GetBuildType()
pagesMap["url"] = pages.GetURL()
pagesMap["status"] = pages.GetStatus()
pagesMap["cname"] = pages.GetCNAME()
Expand Down
48 changes: 47 additions & 1 deletion github/resource_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ func TestAccGithubRepositoryPages(t *testing.T) {

randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

t.Run("manages the pages feature for a repository", func(t *testing.T) {
t.Run("manages the legacy pages feature for a repository", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_repository" "test" {
Expand Down Expand Up @@ -813,6 +813,52 @@ func TestAccGithubRepositoryPages(t *testing.T) {

})

t.Run("manages the pages from workflow feature for a repository", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-%s"
auto_init = true
pages {
build_type = "workflow"
}
}
`, randomID)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "pages.0.source.0.branch",
"main",
),
)

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: check,
},
},
})
}

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 TestAccGithubRepositorySecurity(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion website/docs/r/repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ initial repository creation and create the target branch inside of the repositor

The `pages` block supports the following:

* `source` - (Required) The source branch and directory for the rendered Pages site. See [GitHub Pages Source](#github-pages-source) below for details.
* `source` - (Optional) The source branch and directory for the rendered Pages site. See [GitHub Pages Source](#github-pages-source) below for details.

* `build_type` - (Optional) The type of GitHub Pages site to build. Can be `legacy` or `workflow`. If you use `legacy` as build type you need to set the option `source`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if both source and build_type are present, which one takes precedence? What happens if legacy is set for build_type but source isn't set?

Copy link
Contributor Author

@0x46616c6b 0x46616c6b May 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest I am not aware what the GitHub API will return when the options are not set correctly. I can only outline the possible usage. Would like to prevent this with the Terraform validation but not sure if this is possible.

Allowed Options

resource "github_repository" "example" {
  name = "example"

  pages {
    source {
      branch = "main"
      path = "/"
    }
    build_type = "legacy"
  }
}
resource "github_repository" "example" {
  name = "example"

  pages {
    source {
      branch = "main"
      path = "/"
    }
  }
}
resource "github_repository" "example" {
  name = "example"

  pages {
    build_type = "workflow"
  }
}

Problematic / wrong Configuration

resource "github_repository" "example" {
  name = "example"

  pages {
    source {
      branch = "main"
      path = "/"
    }
    build_type = "workflow"
  }
}


* `cname` - (Optional) The custom domain for the repository. This can only be set after the repository has been created.

Expand Down