Skip to content

Commit 4740338

Browse files
committed
Add repository_id diff logic and unify naming
Signed-off-by: Timo Sand <[email protected]>
1 parent ad643d9 commit 4740338

4 files changed

Lines changed: 64 additions & 23 deletions

github/data_source_github_repository_pages.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package github
33
import (
44
"context"
55
"net/http"
6+
"strconv"
67

78
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -14,7 +15,7 @@ func dataSourceGithubRepositoryPages() *schema.Resource {
1415
ReadContext: dataSourceGithubRepositoryPagesRead,
1516

1617
Schema: map[string]*schema.Schema{
17-
"repository_name": {
18+
"repository": {
1819
Type: schema.TypeString,
1920
Required: true,
2021
Description: "The repository name to get GitHub Pages information for.",
@@ -82,7 +83,7 @@ func dataSourceGithubRepositoryPagesRead(ctx context.Context, d *schema.Resource
8283
client := meta.v3client
8384

8485
owner := d.Get("owner").(string)
85-
repoName := d.Get("repository_name").(string)
86+
repoName := d.Get("repository").(string)
8687

8788
pages, resp, err := client.Repositories.GetPagesInfo(ctx, owner, repoName)
8889
if err != nil {
@@ -92,11 +93,12 @@ func dataSourceGithubRepositoryPagesRead(ctx context.Context, d *schema.Resource
9293
return diag.Errorf("error reading repository pages: %s", err.Error())
9394
}
9495

95-
id, err := buildID(owner, repoName)
96+
repo, _, err := client.Repositories.Get(ctx, owner, repoName)
9697
if err != nil {
9798
return diag.FromErr(err)
9899
}
99-
d.SetId(id)
100+
101+
d.SetId(strconv.Itoa(int(repo.GetID())))
100102

101103
if err := d.Set("build_type", pages.GetBuildType()); err != nil {
102104
return diag.FromErr(err)

github/data_source_github_repository_pages_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestAccGithubRepositoryPagesDataSource(t *testing.T) {
2727
2828
resource "github_repository_pages" "test" {
2929
owner = "%s"
30-
repository_name = github_repository.test.name
30+
repository = github_repository.test.name
3131
build_type = "legacy"
3232
source {
3333
branch = "main"
@@ -37,7 +37,7 @@ func TestAccGithubRepositoryPagesDataSource(t *testing.T) {
3737
3838
data "github_repository_pages" "test" {
3939
owner = "%s"
40-
repository_name = github_repository.test.name
40+
repository = github_repository.test.name
4141
4242
depends_on = [github_repository_pages.test]
4343
}

github/resource_github_repository_pages.go

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

github/resource_github_repository_pages_test.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package github
22

33
import (
44
"fmt"
5+
"strings"
56
"testing"
67

78
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
89
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-testing/terraform"
911
)
1012

1113
func TestAccGithubRepositoryPages(t *testing.T) {
@@ -28,7 +30,7 @@ func TestAccGithubRepositoryPages(t *testing.T) {
2830
2931
resource "github_repository_pages" "test" {
3032
owner = "%s"
31-
repository_name = github_repository.test.name
33+
repository = github_repository.test.name
3234
build_type = "legacy"
3335
source {
3436
branch = "main"
@@ -68,7 +70,7 @@ func TestAccGithubRepositoryPages(t *testing.T) {
6870
6971
resource "github_repository_pages" "test" {
7072
owner = "%s"
71-
repository_name = github_repository.test.name
73+
repository = github_repository.test.name
7274
build_type = "workflow"
7375
}
7476
`, repoName, baseRepoVisibility, testAccConf.owner)
@@ -107,7 +109,7 @@ source {
107109
108110
resource "github_repository_pages" "test" {
109111
owner = "%s"
110-
repository_name = github_repository.test.name
112+
repository = github_repository.test.name
111113
build_type = "%s"
112114
%s
113115
}
@@ -147,7 +149,7 @@ source {
147149
148150
resource "github_repository_pages" "test" {
149151
owner = "%s"
150-
repository_name = github_repository.test.name
152+
repository = github_repository.test.name
151153
build_type = "legacy"
152154
source {
153155
branch = "main"
@@ -167,9 +169,21 @@ source {
167169
),
168170
},
169171
{
170-
ResourceName: "github_repository_pages.test",
171-
ImportState: true,
172-
ImportStateVerify: true,
172+
ResourceName: "github_repository_pages.test",
173+
ImportState: true,
174+
ImportStateVerify: true,
175+
ImportStateIdFunc: func(state *terraform.State) (string, error) {
176+
repo := state.RootModule().Resources["github_repository.test"]
177+
178+
if repo == nil {
179+
return "", fmt.Errorf("github_repository.test not found in state")
180+
}
181+
repoID := repo.Primary.ID
182+
if repoID == "" {
183+
return "", fmt.Errorf("github_repository.test does not have an id in terraform state")
184+
}
185+
return fmt.Sprintf("%s:%s", strings.Split(repo.Primary.Attributes["full_name"], "/")[0], repoID), nil
186+
},
173187
ImportStateVerifyIgnore: []string{"build_status"},
174188
},
175189
},

0 commit comments

Comments
 (0)