-
Notifications
You must be signed in to change notification settings - Fork 958
Expand file tree
/
Copy pathdata_source_github_repository_pages.go
More file actions
156 lines (145 loc) · 4.83 KB
/
data_source_github_repository_pages.go
File metadata and controls
156 lines (145 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package github
import (
"context"
"net/http"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceGithubRepositoryPages() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to retrieve GitHub Pages configuration for a repository.",
ReadContext: dataSourceGithubRepositoryPagesRead,
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
Description: "The repository name to get GitHub Pages information for.",
},
// TODO: Uncomment this when we are ready to support owner fields properly. https://github.com/integrations/terraform-provider-github/pull/3166#discussion_r2816053082
// "owner": {
// Type: schema.TypeString,
// Required: true,
// Description: "The owner of the repository.",
// },
"source": {
Type: schema.TypeList,
Computed: true,
Description: "The source branch and directory for the rendered Pages site.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"branch": {
Type: schema.TypeString,
Computed: true,
Description: "The repository branch used to publish the site's source files.",
},
"path": {
Type: schema.TypeString,
Computed: true,
Description: "The repository directory from which the site publishes.",
},
},
},
},
"build_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of GitHub Pages site. Can be 'legacy' or 'workflow'.",
},
"cname": {
Type: schema.TypeString,
Computed: true,
Description: "The custom domain for the repository.",
},
"custom_404": {
Type: schema.TypeBool,
Computed: true,
Description: "Whether the rendered GitHub Pages site has a custom 404 page.",
},
"html_url": {
Type: schema.TypeString,
Computed: true,
Description: "The absolute URL (with scheme) to the rendered GitHub Pages site.",
},
"build_status": {
Type: schema.TypeString,
Computed: true,
Description: "The GitHub Pages site's build status e.g. 'building' or 'built'.",
},
"api_url": {
Type: schema.TypeString,
Computed: true,
Description: "The API URL of the GitHub Pages resource.",
},
"public": {
Type: schema.TypeBool,
Computed: true,
Description: "Whether the GitHub Pages site is publicly visible. If set to `true`, the site is accessible to anyone on the internet. If set to `false`, the site will only be accessible to users who have at least `read` access to the repository that published the site.",
},
"https_enforced": {
Type: schema.TypeBool,
Computed: true,
Description: "Whether the rendered GitHub Pages site will only be served over HTTPS.",
},
},
}
}
func dataSourceGithubRepositoryPagesRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
meta := m.(*Owner)
client := meta.v3client
owner := meta.name // TODO: Add owner support // d.Get("owner").(string)
repoName := d.Get("repository").(string)
pages, resp, err := client.Repositories.GetPagesInfo(ctx, owner, repoName)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return diag.Errorf("GitHub Pages not found for repository %s/%s", owner, repoName)
}
return diag.Errorf("error reading repository pages: %s", err.Error())
}
repo, _, err := client.Repositories.Get(ctx, owner, repoName)
if err != nil {
return diag.FromErr(err)
}
d.SetId(strconv.Itoa(int(repo.GetID())))
if err := d.Set("build_type", pages.GetBuildType()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("cname", pages.GetCNAME()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("custom_404", pages.GetCustom404()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("html_url", pages.GetHTMLURL()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("build_status", pages.GetStatus()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("api_url", pages.GetURL()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("public", pages.GetPublic()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("https_enforced", pages.GetHTTPSEnforced()); err != nil {
return diag.FromErr(err)
}
// Set source only for legacy build type
if pages.GetBuildType() == "legacy" && pages.GetSource() != nil {
source := []map[string]any{
{
"branch": pages.GetSource().GetBranch(),
"path": pages.GetSource().GetPath(),
},
}
if err := d.Set("source", source); err != nil {
return diag.FromErr(err)
}
} else {
if err := d.Set("source", []map[string]any{}); err != nil {
return diag.FromErr(err)
}
}
return nil
}