Skip to content

Commit bbee84b

Browse files
committed
Add data source for repository pages and deprecate it from repository DS
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
1 parent 7768a52 commit bbee84b

6 files changed

Lines changed: 264 additions & 3 deletions

github/data_source_github_repository.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,9 @@ func dataSourceGithubRepository() *schema.Resource {
241241
},
242242
},
243243
"pages": {
244-
Type: schema.TypeList,
245-
Computed: true,
244+
Type: schema.TypeList,
245+
Computed: true,
246+
Deprecated: "Use the github_repository_pages data source instead. This field will be removed in a future version.",
246247
Elem: &schema.Resource{
247248
Schema: map[string]*schema.Schema{
248249
"source": {
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceGithubRepositoryPages() *schema.Resource {
12+
return &schema.Resource{
13+
Description: "Use this data source to retrieve GitHub Pages configuration for a repository.",
14+
ReadContext: dataSourceGithubRepositoryPagesRead,
15+
16+
Schema: map[string]*schema.Schema{
17+
"repository_name": {
18+
Type: schema.TypeString,
19+
Required: true,
20+
Description: "The repository name to get GitHub Pages information for.",
21+
},
22+
"owner": {
23+
Type: schema.TypeString,
24+
Required: true,
25+
Description: "The owner of the repository.",
26+
},
27+
"source": {
28+
Type: schema.TypeList,
29+
Computed: true,
30+
Description: "The source branch and directory for the rendered Pages site.",
31+
Elem: &schema.Resource{
32+
Schema: map[string]*schema.Schema{
33+
"branch": {
34+
Type: schema.TypeString,
35+
Computed: true,
36+
Description: "The repository branch used to publish the site's source files.",
37+
},
38+
"path": {
39+
Type: schema.TypeString,
40+
Computed: true,
41+
Description: "The repository directory from which the site publishes.",
42+
},
43+
},
44+
},
45+
},
46+
"build_type": {
47+
Type: schema.TypeString,
48+
Computed: true,
49+
Description: "The type of GitHub Pages site. Can be 'legacy' or 'workflow'.",
50+
},
51+
"cname": {
52+
Type: schema.TypeString,
53+
Computed: true,
54+
Description: "The custom domain for the repository.",
55+
},
56+
"custom_404": {
57+
Type: schema.TypeBool,
58+
Computed: true,
59+
Description: "Whether the rendered GitHub Pages site has a custom 404 page.",
60+
},
61+
"html_url": {
62+
Type: schema.TypeString,
63+
Computed: true,
64+
Description: "The absolute URL (with scheme) to the rendered GitHub Pages site.",
65+
},
66+
"status": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
Description: "The GitHub Pages site's build status e.g. 'building' or 'built'.",
70+
},
71+
"url": {
72+
Type: schema.TypeString,
73+
Computed: true,
74+
Description: "The API URL of the GitHub Pages resource.",
75+
},
76+
},
77+
}
78+
}
79+
80+
func dataSourceGithubRepositoryPagesRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
81+
meta := m.(*Owner)
82+
client := meta.v3client
83+
84+
owner := d.Get("owner").(string)
85+
repoName := d.Get("repository_name").(string)
86+
87+
pages, resp, err := client.Repositories.GetPagesInfo(ctx, owner, repoName)
88+
if err != nil {
89+
if resp != nil && resp.StatusCode == http.StatusNotFound {
90+
return diag.Errorf("GitHub Pages not found for repository %s/%s", owner, repoName)
91+
}
92+
return diag.Errorf("error reading repository pages: %s", err.Error())
93+
}
94+
95+
id, err := buildID(owner, repoName)
96+
if err != nil {
97+
return diag.FromErr(err)
98+
}
99+
d.SetId(id)
100+
101+
if err := d.Set("build_type", pages.GetBuildType()); err != nil {
102+
return diag.FromErr(err)
103+
}
104+
if err := d.Set("cname", pages.GetCNAME()); err != nil {
105+
return diag.FromErr(err)
106+
}
107+
if err := d.Set("custom_404", pages.GetCustom404()); err != nil {
108+
return diag.FromErr(err)
109+
}
110+
if err := d.Set("html_url", pages.GetHTMLURL()); err != nil {
111+
return diag.FromErr(err)
112+
}
113+
if err := d.Set("status", pages.GetStatus()); err != nil {
114+
return diag.FromErr(err)
115+
}
116+
if err := d.Set("url", pages.GetURL()); err != nil {
117+
return diag.FromErr(err)
118+
}
119+
120+
// Set source only for legacy build type
121+
if pages.GetBuildType() == "legacy" && pages.GetSource() != nil {
122+
source := []map[string]any{
123+
{
124+
"branch": pages.GetSource().GetBranch(),
125+
"path": pages.GetSource().GetPath(),
126+
},
127+
}
128+
if err := d.Set("source", source); err != nil {
129+
return diag.FromErr(err)
130+
}
131+
} else {
132+
if err := d.Set("source", []map[string]any{}); err != nil {
133+
return diag.FromErr(err)
134+
}
135+
}
136+
137+
return nil
138+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package github
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
func TestAccGithubRepositoryPagesDataSource(t *testing.T) {
12+
baseRepoVisibility := "public"
13+
if testAccConf.authMode == enterprise {
14+
baseRepoVisibility = "private"
15+
}
16+
17+
t.Run("reads_pages_configuration", func(t *testing.T) {
18+
randomID := acctest.RandString(5)
19+
repoName := fmt.Sprintf("%spages-ds-%s", testResourcePrefix, randomID)
20+
21+
config := fmt.Sprintf(`
22+
resource "github_repository" "test" {
23+
name = "%s"
24+
visibility = "%s"
25+
auto_init = true
26+
27+
lifecycle {
28+
ignore_changes = [
29+
pages,
30+
]
31+
}
32+
}
33+
34+
resource "github_repository_pages" "test" {
35+
owner = "%s"
36+
repository_name = github_repository.test.name
37+
build_type = "legacy"
38+
source {
39+
branch = "main"
40+
path = "/"
41+
}
42+
}
43+
44+
data "github_repository_pages" "test" {
45+
owner = "%s"
46+
repository_name = github_repository.test.name
47+
48+
depends_on = [github_repository_pages.test]
49+
}
50+
`, repoName, baseRepoVisibility, testAccConf.owner, testAccConf.owner)
51+
52+
resource.Test(t, resource.TestCase{
53+
PreCheck: func() { skipUnauthenticated(t) },
54+
ProviderFactories: providerFactories,
55+
Steps: []resource.TestStep{
56+
{
57+
Config: config,
58+
Check: resource.ComposeTestCheckFunc(
59+
resource.TestCheckResourceAttr("data.github_repository_pages.test", "build_type", "legacy"),
60+
resource.TestCheckResourceAttr("data.github_repository_pages.test", "source.0.branch", "main"),
61+
resource.TestCheckResourceAttr("data.github_repository_pages.test", "source.0.path", "/"),
62+
resource.TestCheckResourceAttrSet("data.github_repository_pages.test", "url"),
63+
),
64+
},
65+
},
66+
})
67+
})
68+
}

github/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ func Provider() *schema.Provider {
282282
"github_repository_deployment_branch_policies": dataSourceGithubRepositoryDeploymentBranchPolicies(),
283283
"github_repository_file": dataSourceGithubRepositoryFile(),
284284
"github_repository_milestone": dataSourceGithubRepositoryMilestone(),
285+
"github_repository_pages": dataSourceGithubRepositoryPages(),
285286
"github_repository_pull_request": dataSourceGithubRepositoryPullRequest(),
286287
"github_repository_pull_requests": dataSourceGithubRepositoryPullRequests(),
287288
"github_repository_teams": dataSourceGithubRepositoryTeams(),

website/docs/d/repository.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ The following arguments are supported:
7575

7676
* `archived` - Whether the repository is archived.
7777

78-
* `pages` - The repository's GitHub Pages configuration.
78+
* `pages` - (**DEPRECATED**) The repository's GitHub Pages configuration. Use the `github_repository_pages` data source instead. This field will be removed in a future version.
7979

8080
* `topics` - The list of topics of the repository.
8181

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
layout: "github"
3+
page_title: "GitHub: github_repository_pages"
4+
description: |-
5+
Get information on GitHub Pages for a repository
6+
---
7+
8+
# github_repository_pages
9+
10+
Use this data source to retrieve GitHub Pages configuration for a repository.
11+
12+
## Example Usage
13+
14+
```hcl
15+
data "github_repository_pages" "example" {
16+
owner = "my-org"
17+
repository = "my-repo"
18+
}
19+
```
20+
21+
## Argument Reference
22+
23+
The following arguments are supported:
24+
25+
- `owner` - (Required) The owner of the repository.
26+
27+
- `repository` - (Required) The repository name to get GitHub Pages information for.
28+
29+
## Attribute Reference
30+
31+
The following attributes are exported:
32+
33+
- `build_type` - The type of GitHub Pages site. Can be `legacy` or `workflow`.
34+
35+
- `cname` - The custom domain for the repository.
36+
37+
- `custom_404` - Whether the rendered GitHub Pages site has a custom 404 page.
38+
39+
- `html_url` - The absolute URL (with scheme) to the rendered GitHub Pages site.
40+
41+
- `source` - The source branch and directory for the rendered Pages site. See [Source](#source) below for details.
42+
43+
- `status` - The GitHub Pages site's build status (e.g., `building` or `built`).
44+
45+
- `url` - The API URL of the GitHub Pages resource.
46+
47+
### Source
48+
49+
The `source` block contains:
50+
51+
- `branch` - The repository branch used to publish the site's source files.
52+
53+
- `path` - The repository directory from which the site publishes.

0 commit comments

Comments
 (0)