|
| 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 | +} |
0 commit comments