Skip to content

Commit 4cfdf42

Browse files
committed
feat: add fork-PR contributor approval resources and data sources
Wrap the GitHub REST `actions/permissions/fork-pr-contributor-approval` endpoints (repo + org) in four new Terraform building blocks: - resource `github_actions_repository_fork_pr_contributor_approval` - resource `github_actions_organization_fork_pr_contributor_approval` - data source `github_actions_repository_fork_pr_contributor_approval` - data source `github_actions_organization_fork_pr_contributor_approval` Each accepts `approval_policy` in the three GitHub-documented enum values (`first_time_contributors_new_to_github`, `first_time_contributors`, `all_external_contributors`). The API has no "off" state for this policy. On Delete, the resource resets the policy to GitHub's documented default (`first_time_contributors`) to avoid leaving non-default residual state, matching the precedent set by `github_actions_organization_permissions` Delete (which resets to `all`). go-github already exposes the matching service methods on `*ActionsService` (GetForkPRContributorApprovalPermissions / Update... and the Organization* variants), so this is purely a provider-side wrapper. New resources are implemented with context-aware CRUD functions (`CreateContext` / `ReadContext` / `UpdateContext` / `DeleteContext` returning `diag.Diagnostics`) per the migration tracked in #2996, rather than copying the legacy pattern from the nearby `access_level` and `organization_permissions` resources that are themselves on that migration's to-do list. Acceptance test notes: - Repo-level tests use `visibility = "public"` because configuring `fork-pr-contributor-approval` on private repos returns 422 when the org's `fork-pr-workflows-private-repos` setting has fork-PR workflows disabled. Public repos exercise the endpoint without that prerequisite. - The repo-level test allows both `individual` and `organization` auth modes so it can be exercised against an org-scoped test token. Doc generation was authored against `make generatedocs` templates and the rendered output is included; `make validatedocs` was not run locally because the dev environment lacks a `terraform` binary. CI will validate.
1 parent 1060042 commit 4cfdf42

21 files changed

Lines changed: 747 additions & 0 deletions
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
page_title: "github_actions_organization_fork_pr_contributor_approval (Data Source) - GitHub"
3+
description: |-
4+
Read the organization-wide fork PR contributor approval policy
5+
---
6+
7+
# github_actions_organization_fork_pr_contributor_approval (Data Source)
8+
9+
Use this data source to retrieve the current organization-wide fork pull request contributor approval policy.
10+
11+
## Example Usage
12+
13+
```terraform
14+
data "github_actions_organization_fork_pr_contributor_approval" "example" {}
15+
```
16+
17+
## Argument Reference
18+
19+
This data source takes no arguments. The organization is determined by the provider configuration.
20+
21+
## Attributes Reference
22+
23+
- `approval_policy` - The organization-wide fork PR contributor approval policy currently configured. One of `first_time_contributors_new_to_github`, `first_time_contributors`, or `all_external_contributors`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
page_title: "github_actions_repository_fork_pr_contributor_approval (Data Source) - GitHub"
3+
description: |-
4+
Read the fork PR contributor approval policy for a GitHub repository
5+
---
6+
7+
# github_actions_repository_fork_pr_contributor_approval (Data Source)
8+
9+
Use this data source to retrieve the current fork pull request contributor approval policy configured on a GitHub repository.
10+
11+
## Example Usage
12+
13+
```terraform
14+
data "github_actions_repository_fork_pr_contributor_approval" "example" {
15+
repository = "my-repository"
16+
}
17+
```
18+
19+
## Argument Reference
20+
21+
- `repository` - (Required) The GitHub repository.
22+
23+
## Attributes Reference
24+
25+
- `approval_policy` - The fork PR contributor approval policy currently configured on the repository. One of `first_time_contributors_new_to_github`, `first_time_contributors`, or `all_external_contributors`.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
page_title: "github_actions_organization_fork_pr_contributor_approval (Resource) - GitHub"
3+
description: |-
4+
Manages the organization-wide fork PR contributor approval policy
5+
---
6+
7+
# github_actions_organization_fork_pr_contributor_approval (Resource)
8+
9+
This resource allows you to set the organization-wide fork pull request contributor approval policy. This controls which fork PR contributors need maintainer approval before their workflows can run on any public repository in the organization. You must be an organization owner to use this resource.
10+
11+
Repositories may override this policy at the repository level (see [`github_actions_repository_fork_pr_contributor_approval`](actions_repository_fork_pr_contributor_approval.md)). Setting the policy at the organization level only establishes the default for repositories that do not have a repository-level override.
12+
13+
The GitHub API for this setting does not expose an "off" state — the policy is always set to one of the three strictness values. If you remove this resource, the policy is reset to GitHub's documented default (`first_time_contributors`).
14+
15+
## Example Usage
16+
17+
```terraform
18+
resource "github_actions_organization_fork_pr_contributor_approval" "test" {
19+
approval_policy = "all_external_contributors"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are supported:
26+
27+
- `approval_policy` - (Required) The organization-wide policy controlling which fork PR contributors need maintainer approval. Possible values are `first_time_contributors_new_to_github`, `first_time_contributors`, or `all_external_contributors`.
28+
29+
## Import
30+
31+
This resource can be imported using the name of the organization:
32+
33+
```shell
34+
terraform import github_actions_organization_fork_pr_contributor_approval.test my-organization
35+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
page_title: "github_actions_repository_fork_pr_contributor_approval (Resource) - GitHub"
3+
description: |-
4+
Manages the fork PR contributor approval policy for a GitHub repository
5+
---
6+
7+
# github_actions_repository_fork_pr_contributor_approval (Resource)
8+
9+
This resource allows you to set the fork pull request contributor approval policy on a GitHub repository. This controls which fork PR contributors need maintainer approval before their workflows can run on the repository. You must have admin access to a repository to use this resource.
10+
11+
This setting governs fork PRs from outside contributors. On private repositories, the [`fork-pr-workflows-private-repos`](https://docs.github.com/en/rest/actions/permissions?apiVersion=2022-11-28#set-private-repo-fork-pr-workflow-settings-for-a-repository) org/repo settings control whether fork PR workflows run at all; if fork PR workflows are disabled at that level, configuring `approval_policy` via this resource may return `422 Unprocessable Entity`.
12+
13+
The GitHub API for this setting does not expose an "off" state — the policy is always one of the three strictness values. On Delete, this resource resets the policy to GitHub's documented default (`first_time_contributors`).
14+
15+
## Example Usage
16+
17+
```terraform
18+
resource "github_repository" "example" {
19+
name = "my-repository"
20+
visibility = "public"
21+
}
22+
23+
resource "github_actions_repository_fork_pr_contributor_approval" "test" {
24+
approval_policy = "all_external_contributors"
25+
repository = github_repository.example.name
26+
}
27+
```
28+
29+
## Argument Reference
30+
31+
The following arguments are supported:
32+
33+
- `repository` - (Required) The GitHub repository.
34+
- `approval_policy` - (Required) The policy controlling which fork PR contributors need maintainer approval. Possible values are `first_time_contributors_new_to_github`, `first_time_contributors`, or `all_external_contributors`.
35+
36+
## Import
37+
38+
This resource can be imported using the name of the GitHub repository:
39+
40+
```shell
41+
terraform import github_actions_repository_fork_pr_contributor_approval.test my-repository
42+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data "github_actions_organization_fork_pr_contributor_approval" "example" {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data "github_actions_repository_fork_pr_contributor_approval" "example" {
2+
repository = "my-repository"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
resource "github_actions_organization_fork_pr_contributor_approval" "test" {
2+
approval_policy = "all_external_contributors"
3+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
resource "github_repository" "example" {
2+
name = "my-repository"
3+
visibility = "public"
4+
}
5+
6+
resource "github_actions_repository_fork_pr_contributor_approval" "test" {
7+
approval_policy = "all_external_contributors"
8+
repository = github_repository.example.name
9+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package github
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceGithubActionsOrganizationForkPRContributorApproval() *schema.Resource {
11+
return &schema.Resource{
12+
ReadContext: dataSourceGithubActionsOrganizationForkPRContributorApprovalRead,
13+
14+
Schema: map[string]*schema.Schema{
15+
"approval_policy": {
16+
Type: schema.TypeString,
17+
Computed: true,
18+
Description: "The organization-wide fork PR contributor approval policy currently configured. One of 'first_time_contributors_new_to_github', 'first_time_contributors', or 'all_external_contributors'.",
19+
},
20+
},
21+
}
22+
}
23+
24+
func dataSourceGithubActionsOrganizationForkPRContributorApprovalRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
25+
if err := checkOrganization(meta); err != nil {
26+
return diag.FromErr(err)
27+
}
28+
29+
client := meta.(*Owner).v3client
30+
orgName := meta.(*Owner).name
31+
32+
policy, _, err := client.Actions.GetOrganizationForkPRContributorApprovalPermissions(ctx, orgName)
33+
if err != nil {
34+
return diag.FromErr(err)
35+
}
36+
37+
d.SetId(orgName)
38+
if err := d.Set("approval_policy", policy.ApprovalPolicy); err != nil {
39+
return diag.FromErr(err)
40+
}
41+
42+
return nil
43+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package github
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
7+
)
8+
9+
func TestAccGithubActionsOrganizationForkPRContributorApprovalDataSource(t *testing.T) {
10+
t.Run("read the organization fork PR contributor approval policy", func(t *testing.T) {
11+
approvalPolicy := "all_external_contributors"
12+
13+
config := `
14+
resource "github_actions_organization_fork_pr_contributor_approval" "test" {
15+
approval_policy = "all_external_contributors"
16+
}
17+
`
18+
19+
config2 := config + `
20+
data "github_actions_organization_fork_pr_contributor_approval" "test" {}
21+
`
22+
23+
check := resource.ComposeTestCheckFunc(
24+
resource.TestCheckResourceAttr(
25+
"data.github_actions_organization_fork_pr_contributor_approval.test", "approval_policy", approvalPolicy,
26+
),
27+
)
28+
29+
resource.Test(t, resource.TestCase{
30+
PreCheck: func() { skipUnlessHasOrgs(t) },
31+
ProviderFactories: providerFactories,
32+
Steps: []resource.TestStep{
33+
{
34+
Config: config,
35+
Check: resource.ComposeTestCheckFunc(),
36+
},
37+
{
38+
Config: config2,
39+
Check: check,
40+
},
41+
},
42+
})
43+
})
44+
}

0 commit comments

Comments
 (0)