Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
kfcampbell committed Jan 22, 2024
2 parents 3e93676 + dbda378 commit b33cc48
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 18 deletions.
7 changes: 4 additions & 3 deletions github/resource_github_organization_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
Description: "Possible values for Enforcement are `disabled`, `active`, `evaluate`. Note: `evaluate` is currently only supported for owners of type `organization`.",
},
"bypass_actors": {
Type: schema.TypeList,
Optional: true,
Description: "The actors that can bypass the rules in this ruleset.",
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: bypassActorsDiffSuppressFunc,
Description: "The actors that can bypass the rules in this ruleset.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"actor_id": {
Expand Down
10 changes: 10 additions & 0 deletions github/resource_github_repository_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func resourceGithubRepositoryEnvironment() *schema.Resource {
Default: true,
Description: "Can Admins bypass deployment protections",
},
"prevent_self_review": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Prevent users from approving workflows runs that they triggered.",
},
"wait_timer": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -170,6 +176,8 @@ func resourceGithubRepositoryEnvironmentRead(d *schema.ResourceData, meta interf
"users": users,
},
})

d.Set("prevent_self_review", pr.PreventSelfReview)
}
}

Expand Down Expand Up @@ -233,6 +241,8 @@ func createUpdateEnvironmentData(d *schema.ResourceData, meta interface{}) githu

data.CanAdminsBypass = github.Bool(d.Get("can_admins_bypass").(bool))

data.PreventSelfReview = github.Bool(d.Get("prevent_self_review").(bool))

if v, ok := d.GetOk("reviewers"); ok {
envReviewers := make([]*github.EnvReviewers, 0)

Expand Down
2 changes: 2 additions & 0 deletions github/resource_github_repository_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestAccGithubRepositoryEnvironment(t *testing.T) {
environment = "environment / test"
can_admins_bypass = false
wait_timer = 10000
prevent_self_review = true
reviewers {
users = [data.github_user.current.id]
}
Expand All @@ -44,6 +45,7 @@ func TestAccGithubRepositoryEnvironment(t *testing.T) {
check := resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("github_repository_environment.test", "environment", "environment / test"),
resource.TestCheckResourceAttr("github_repository_environment.test", "can_admins_bypass", "false"),
resource.TestCheckResourceAttr("github_repository_environment.test", "prevent_self_review", "true"),
resource.TestCheckResourceAttr("github_repository_environment.test", "wait_timer", "10000"),
)

Expand Down
7 changes: 4 additions & 3 deletions github/resource_github_repository_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ func resourceGithubRepositoryRuleset() *schema.Resource {
Description: "Possible values for Enforcement are `disabled`, `active`, `evaluate`. Note: `evaluate` is currently only supported for owners of type `organization`.",
},
"bypass_actors": {
Type: schema.TypeList,
Optional: true,
Description: "The actors that can bypass the rules in this ruleset.",
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: bypassActorsDiffSuppressFunc,
Description: "The actors that can bypass the rules in this ruleset.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"actor_id": {
Expand Down
26 changes: 22 additions & 4 deletions github/respository_rules_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package github
import (
"encoding/json"
"log"
"reflect"
"sort"

"github.com/google/go-github/v57/github"
Expand Down Expand Up @@ -63,10 +64,6 @@ func flattenBypassActors(bypassActors []*github.BypassActor) []interface{} {
return []interface{}{}
}

sort.SliceStable(bypassActors, func(i, j int) bool {
return bypassActors[i].GetActorID() > bypassActors[j].GetActorID()
})

actorsSlice := make([]interface{}, 0)
for _, v := range bypassActors {
actorMap := make(map[string]interface{})
Expand Down Expand Up @@ -480,3 +477,24 @@ func flattenRules(rules []*github.RepositoryRule, org bool) []interface{} {

return []interface{}{rulesMap}
}

func bypassActorsDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
// If the length has changed, no need to suppress
if k == "bypass_actors.#" {
return old == new
}

// Get change to bypass actors
o, n := d.GetChange("bypass_actors")
oldBypassActors := o.([]interface{})
newBypassActors := n.([]interface{})

sort.SliceStable(oldBypassActors, func(i, j int) bool {
return oldBypassActors[i].(map[string]interface{})["actor_id"].(int) > oldBypassActors[j].(map[string]interface{})["actor_id"].(int)
})
sort.SliceStable(newBypassActors, func(i, j int) bool {
return newBypassActors[i].(map[string]interface{})["actor_id"].(int) > newBypassActors[j].(map[string]interface{})["actor_id"].(int)
})

return reflect.DeepEqual(oldBypassActors, newBypassActors)
}
11 changes: 8 additions & 3 deletions website/docs/r/repository_deploy_key.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ Further documentation on GitHub repository deploy keys:
## Example Usage

```hcl
# Add a deploy key
# Generate an ssh key using provider "hashicorp/tls"
resource "tls_private_key" "example_repository_deploy_key" {
algorithm = "ED25519"
}
# Add the ssh key as a deploy key
resource "github_repository_deploy_key" "example_repository_deploy_key" {
title = "Repository test key"
repository = "test-repo"
key = "ssh-rsa AAA..."
read_only = "false"
key = tls_private_key.example_repository_deploy_key.public_key_openssh
read_only = true
}
```

Expand Down
13 changes: 8 additions & 5 deletions website/docs/r/repository_environment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ data "github_user" "current" {
}
resource "github_repository" "example" {
name = "A Repository Project"
description = "My awesome codebase"
name = "A Repository Project"
description = "My awesome codebase"
}
resource "github_repository_environment" "example" {
environment = "example"
repository = github_repository.example.name
environment = "example"
repository = github_repository.example.name
prevent_self_review = true
reviewers {
users = [data.github_user.current.id]
}
deployment_branch_policy {
protected_branches = true
protected_branches = true
custom_branch_policies = false
}
}
Expand All @@ -46,6 +47,8 @@ The following arguments are supported:

* `can_admins_bypass` - (Optional) Can repository admins bypass the environment protections. Defaults to `true`.

* `prevent_self_review` - (Optional) Whether or not a user who created the job is prevented from approving their own job. Defaults to `false`.

### Reviewers

The `reviewers` block supports the following:
Expand Down

0 comments on commit b33cc48

Please sign in to comment.