@@ -4,9 +4,11 @@ import (
44 "context"
55 "fmt"
66 "net/http"
7+ "strconv"
78
89 "github.com/google/go-github/v82/github"
910 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1012 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1113 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1214)
@@ -23,12 +25,17 @@ func resourceGithubRepositoryPages() *schema.Resource {
2325 },
2426
2527 Schema : map [string ]* schema.Schema {
26- "repository_name " : {
28+ "repository " : {
2729 Type : schema .TypeString ,
2830 Required : true ,
2931 ForceNew : true ,
3032 Description : "The repository name to configure GitHub Pages for." ,
3133 },
34+ "repository_id" : {
35+ Type : schema .TypeInt ,
36+ Computed : true ,
37+ Description : "The ID of the repository to configure GitHub Pages for." ,
38+ },
3239 "owner" : {
3340 Type : schema .TypeString ,
3441 Required : true ,
@@ -89,7 +96,7 @@ func resourceGithubRepositoryPages() *schema.Resource {
8996 Description : "The API URL of the GitHub Pages resource." ,
9097 },
9198 },
92- CustomizeDiff : resourceGithubRepositoryPagesDiff ,
99+ CustomizeDiff : customdiff . All ( resourceGithubRepositoryPagesDiff , diffRepository ) ,
93100 }
94101}
95102
@@ -98,19 +105,24 @@ func resourceGithubRepositoryPagesCreate(ctx context.Context, d *schema.Resource
98105 client := meta .v3client
99106
100107 owner := d .Get ("owner" ).(string )
101- repoName := d .Get ("repository_name " ).(string )
108+ repoName := d .Get ("repository " ).(string )
102109
103110 pages := expandPagesForCreate (d )
104111 pages , _ , err := client .Repositories .EnablePages (ctx , owner , repoName , pages )
105112 if err != nil {
106113 return diag .FromErr (err )
107114 }
108115
109- id , err := buildID ( owner , repoName )
116+ repo , _ , err := client . Repositories . Get ( ctx , owner , repoName )
110117 if err != nil {
111118 return diag .FromErr (err )
112119 }
113- d .SetId (id )
120+
121+ d .SetId (strconv .Itoa (int (repo .GetID ())))
122+
123+ if err = d .Set ("repository_id" , int (repo .GetID ())); err != nil {
124+ return diag .FromErr (err )
125+ }
114126
115127 if err := d .Set ("build_type" , pages .GetBuildType ()); err != nil {
116128 return diag .FromErr (err )
@@ -150,7 +162,7 @@ func resourceGithubRepositoryPagesRead(ctx context.Context, d *schema.ResourceDa
150162 client := meta .v3client
151163
152164 owner := d .Get ("owner" ).(string )
153- repoName := d .Get ("repository_name " ).(string )
165+ repoName := d .Get ("repository " ).(string )
154166
155167 pages , resp , err := client .Repositories .GetPagesInfo (ctx , owner , repoName )
156168 if err != nil {
@@ -205,7 +217,7 @@ func resourceGithubRepositoryPagesUpdate(ctx context.Context, d *schema.Resource
205217 client := meta .v3client
206218
207219 owner := d .Get ("owner" ).(string )
208- repoName := d .Get ("repository_name " ).(string )
220+ repoName := d .Get ("repository " ).(string )
209221
210222 update := & github.PagesUpdate {}
211223
@@ -252,7 +264,7 @@ func resourceGithubRepositoryPagesDelete(ctx context.Context, d *schema.Resource
252264 client := meta .v3client
253265
254266 owner := d .Get ("owner" ).(string )
255- repoName := d .Get ("repository_name " ).(string )
267+ repoName := d .Get ("repository " ).(string )
256268
257269 _ , err := client .Repositories .DisablePages (ctx , owner , repoName )
258270 if err != nil {
@@ -262,18 +274,31 @@ func resourceGithubRepositoryPagesDelete(ctx context.Context, d *schema.Resource
262274 return nil
263275}
264276
265- func resourceGithubRepositoryPagesImport (ctx context.Context , d * schema.ResourceData , meta any ) ([]* schema.ResourceData , error ) {
277+ func resourceGithubRepositoryPagesImport (ctx context.Context , d * schema.ResourceData , m any ) ([]* schema.ResourceData , error ) {
266278 owner , repoName , err := parseID2 (d .Id ())
267279 if err != nil {
268280 return nil , fmt .Errorf ("invalid ID specified: supplied ID must be written as <owner>:<repository>. Original error: %w" , err )
269281 }
270282 if err := d .Set ("owner" , owner ); err != nil {
271283 return nil , err
272284 }
273- if err := d .Set ("repository_name" , repoName ); err != nil {
285+ if err := d .Set ("repository" , repoName ); err != nil {
286+ return nil , err
287+ }
288+
289+ meta := m .(* Owner )
290+ client := meta .v3client
291+
292+ repo , _ , err := client .Repositories .Get (ctx , owner , repoName )
293+ if err != nil {
294+ return nil , err
295+ }
296+ if err = d .Set ("repository_id" , int (repo .GetID ())); err != nil {
274297 return nil , err
275298 }
276299
300+ d .SetId (strconv .Itoa (int (repo .GetID ())))
301+
277302 return []* schema.ResourceData {d }, nil
278303}
279304
0 commit comments