Skip to content

Commit 82749d6

Browse files
authored
Merge branch 'main' into feature/deployment-policy-tag-pattern
2 parents 92243c5 + 4ca9b21 commit 82749d6

File tree

256 files changed

+13535
-7552
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

256 files changed

+13535
-7552
lines changed

github/data_source_github_organization.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ func dataSourceGithubOrganization() *schema.Resource {
1717
Type: schema.TypeString,
1818
Required: true,
1919
},
20+
"ignore_archived_repos": {
21+
Type: schema.TypeBool,
22+
Default: false,
23+
Optional: true,
24+
},
2025
"orgname": {
2126
Type: schema.TypeString,
2227
Computed: true,
@@ -176,8 +181,15 @@ func dataSourceGithubOrganizationRead(d *schema.ResourceData, meta interface{})
176181
break
177182
}
178183
}
184+
185+
ignoreArchiveRepos := d.Get("ignore_archived_repos").(bool)
179186
for index := range allRepos {
180-
repoList = append(repoList, allRepos[index].GetFullName())
187+
repo := allRepos[index]
188+
if ignoreArchiveRepos && repo.GetArchived() {
189+
continue
190+
}
191+
192+
repoList = append(repoList, repo.GetFullName())
181193
}
182194

183195
var query struct {

github/data_source_github_organization_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"testing"
66

7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
89
)
910

@@ -72,4 +73,69 @@ func TestAccGithubOrganizationDataSource(t *testing.T) {
7273
})
7374

7475
})
76+
77+
t.Run("queries for an organization with archived repos", func(t *testing.T) {
78+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
79+
80+
config := fmt.Sprintf(`
81+
resource "github_repository" "archived" {
82+
name = "tf-acc-archived-%s"
83+
archived = true
84+
}
85+
86+
data "github_organization" "skip_archived" {
87+
name = "%s"
88+
ignore_archived_repos = true
89+
depends_on = [
90+
github_repository.archived,
91+
]
92+
}
93+
data "github_organization" "all_repos" {
94+
name = "%s"
95+
ignore_archived_repos = false
96+
depends_on = [
97+
github_repository.archived,
98+
]
99+
}
100+
101+
output "should_be_false" {
102+
value = contains(data.github_organization.skip_archived.repositories, github_repository.archived.full_name)
103+
}
104+
output "should_be_true" {
105+
value = contains(data.github_organization.all_repos.repositories, github_repository.archived.full_name)
106+
}
107+
`, randomID, testOrganization, testOrganization)
108+
109+
check := resource.ComposeTestCheckFunc(
110+
resource.TestCheckOutput("should_be_false", "false"),
111+
resource.TestCheckOutput("should_be_true", "true"),
112+
)
113+
114+
testCase := func(t *testing.T, mode string) {
115+
resource.Test(t, resource.TestCase{
116+
PreCheck: func() { skipUnlessMode(t, mode) },
117+
Providers: testAccProviders,
118+
Steps: []resource.TestStep{
119+
{
120+
Config: config,
121+
Check: check,
122+
},
123+
},
124+
})
125+
}
126+
127+
t.Run("with an anonymous account", func(t *testing.T) {
128+
t.Skip("anonymous account not supported for this operation")
129+
})
130+
131+
t.Run("with an individual account", func(t *testing.T) {
132+
testCase(t, individual)
133+
})
134+
135+
t.Run("with an organization account", func(t *testing.T) {
136+
testCase(t, organization)
137+
})
138+
139+
})
140+
75141
}

github/resource_github_actions_organization_permissions.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package github
33
import (
44
"context"
55
"errors"
6+
"log"
67

78
"github.com/google/go-github/v57/github"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -106,8 +107,7 @@ func resourceGithubActionsOrganizationAllowedObject(d *schema.ResourceData) (*gi
106107

107108
allowed.PatternsAllowed = patternsAllowed
108109
} else {
109-
return &github.ActionsAllowed{},
110-
errors.New("the allowed_actions_config {} block must be specified if allowed_actions == 'selected'")
110+
return nil, nil
111111
}
112112

113113
return allowed, nil
@@ -162,11 +162,16 @@ func resourceGithubActionsOrganizationPermissionsCreateOrUpdate(d *schema.Resour
162162
if err != nil {
163163
return err
164164
}
165-
_, _, err = client.Actions.EditActionsAllowed(ctx,
166-
orgName,
167-
*actionsAllowedData)
168-
if err != nil {
169-
return err
165+
if actionsAllowedData != nil {
166+
log.Printf("[DEBUG] Allowed actions config is set")
167+
_, _, err = client.Actions.EditActionsAllowed(ctx,
168+
orgName,
169+
*actionsAllowedData)
170+
if err != nil {
171+
return err
172+
}
173+
} else {
174+
log.Printf("[DEBUG] Allowed actions config not set, skipping")
170175
}
171176
}
172177

@@ -201,7 +206,12 @@ func resourceGithubActionsOrganizationPermissionsRead(d *schema.ResourceData, me
201206
return err
202207
}
203208

204-
if actionsPermissions.GetAllowedActions() == "selected" {
209+
// only load and fill allowed_actions_config if allowed_actions_config is also set
210+
// in the TF code. (see #2105)
211+
// on initial import there might not be any value in the state, then we have to import the data
212+
allowedActions := d.Get("allowed_actions").(string)
213+
allowedActionsConfig := d.Get("allowed_actions_config").([]interface{})
214+
if (allowedActions == "selected" && len(allowedActionsConfig) > 0) || allowedActions == "" {
205215
actionsAllowed, _, err := client.Actions.GetActionsAllowed(ctx, d.Id())
206216
if err != nil {
207217
return err

github/resource_github_actions_organization_permissions_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,49 @@ func TestAccGithubActionsOrganizationPermissions(t *testing.T) {
166166
})
167167
})
168168

169+
t.Run("test not setting of organization allowed actions without error", func(t *testing.T) {
170+
171+
allowedActions := "selected"
172+
enabledRepositories := "all"
173+
174+
config := fmt.Sprintf(`
175+
176+
resource "github_actions_organization_permissions" "test" {
177+
allowed_actions = "%s"
178+
enabled_repositories = "%s"
179+
}
180+
`, allowedActions, enabledRepositories)
181+
182+
check := resource.ComposeTestCheckFunc(
183+
resource.TestCheckResourceAttr(
184+
"github_actions_organization_permissions.test", "allowed_actions", allowedActions,
185+
),
186+
resource.TestCheckResourceAttr(
187+
"github_actions_organization_permissions.test", "enabled_repositories", enabledRepositories,
188+
),
189+
resource.TestCheckResourceAttr(
190+
"github_actions_organization_permissions.test", "allowed_actions_config.#", "0",
191+
),
192+
)
193+
194+
testCase := func(t *testing.T, mode string) {
195+
resource.Test(t, resource.TestCase{
196+
PreCheck: func() { skipUnlessMode(t, mode) },
197+
Providers: testAccProviders,
198+
Steps: []resource.TestStep{
199+
{
200+
Config: config,
201+
Check: check,
202+
},
203+
},
204+
})
205+
}
206+
207+
t.Run("with an organization account", func(t *testing.T) {
208+
testCase(t, organization)
209+
})
210+
})
211+
169212
t.Run("test setting of organization enabled repositories", func(t *testing.T) {
170213

171214
allowedActions := "all"

github/resource_github_actions_repository_permissions.go

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

33
import (
44
"context"
5-
"errors"
65
"log"
76

87
"github.com/google/go-github/v57/github"
@@ -97,8 +96,7 @@ func resourceGithubActionsRepositoryAllowedObject(d *schema.ResourceData) (*gith
9796

9897
allowed.PatternsAllowed = patternsAllowed
9998
} else {
100-
return &github.ActionsAllowed{},
101-
errors.New("the allowed_actions_config {} block must be specified if allowed_actions == 'selected'")
99+
return nil, nil
102100
}
103101

104102
return allowed, nil
@@ -141,12 +139,17 @@ func resourceGithubActionsRepositoryPermissionsCreateOrUpdate(d *schema.Resource
141139
if err != nil {
142140
return err
143141
}
144-
_, _, err = client.Repositories.EditActionsAllowed(ctx,
145-
owner,
146-
repoName,
147-
*actionsAllowedData)
148-
if err != nil {
149-
return err
142+
if actionsAllowedData != nil {
143+
log.Printf("[DEBUG] Allowed actions config is set")
144+
_, _, err = client.Repositories.EditActionsAllowed(ctx,
145+
owner,
146+
repoName,
147+
*actionsAllowedData)
148+
if err != nil {
149+
return err
150+
}
151+
} else {
152+
log.Printf("[DEBUG] Allowed actions config not set, skipping")
150153
}
151154
}
152155

@@ -166,7 +169,12 @@ func resourceGithubActionsRepositoryPermissionsRead(d *schema.ResourceData, meta
166169
return err
167170
}
168171

169-
if actionsPermissions.GetAllowedActions() == "selected" {
172+
// only load and fill allowed_actions_config if allowed_actions_config is also set
173+
// in the TF code. (see #2105)
174+
// on initial import there might not be any value in the state, then we have to import the data
175+
allowedActions := d.Get("allowed_actions").(string)
176+
allowedActionsConfig := d.Get("allowed_actions_config").([]interface{})
177+
if (allowedActions == "selected" && len(allowedActionsConfig) > 0) || allowedActions == "" {
170178
actionsAllowed, _, err := client.Repositories.GetActionsAllowed(ctx, owner, repoName)
171179
if err != nil {
172180
return err

github/resource_github_actions_repository_permissions_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,62 @@ func TestAccGithubActionsRepositoryPermissions(t *testing.T) {
188188

189189
})
190190

191+
t.Run("test not setting of repository allowed actions without error", func(t *testing.T) {
192+
193+
allowedActions := "selected"
194+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
195+
196+
config := fmt.Sprintf(`
197+
resource "github_repository" "test" {
198+
name = "tf-acc-test-topic-%[1]s"
199+
description = "Terraform acceptance tests %[1]s"
200+
topics = ["terraform", "testing"]
201+
}
202+
203+
resource "github_actions_repository_permissions" "test" {
204+
allowed_actions = "%s"
205+
repository = github_repository.test.name
206+
}
207+
`, randomID, allowedActions)
208+
209+
check := resource.ComposeTestCheckFunc(
210+
resource.TestCheckResourceAttr(
211+
"github_actions_repository_permissions.test", "allowed_actions", allowedActions,
212+
),
213+
// Even if we do not set the allowed_actions_config,
214+
// it will be set to the organization level settings
215+
resource.TestCheckResourceAttr(
216+
"github_actions_repository_permissions.test", "allowed_actions_config.#", "0",
217+
),
218+
)
219+
220+
testCase := func(t *testing.T, mode string) {
221+
resource.Test(t, resource.TestCase{
222+
PreCheck: func() { skipUnlessMode(t, mode) },
223+
Providers: testAccProviders,
224+
Steps: []resource.TestStep{
225+
{
226+
Config: config,
227+
Check: check,
228+
},
229+
},
230+
})
231+
}
232+
233+
t.Run("with an anonymous account", func(t *testing.T) {
234+
t.Skip("anonymous account not supported for this operation")
235+
})
236+
237+
t.Run("with an individual account", func(t *testing.T) {
238+
testCase(t, individual)
239+
})
240+
241+
t.Run("with an organization account", func(t *testing.T) {
242+
testCase(t, organization)
243+
})
244+
245+
})
246+
191247
t.Run("test disabling actions on a repository", func(t *testing.T) {
192248

193249
actionsEnabled := false

0 commit comments

Comments
 (0)