Skip to content

Commit 1b6bfaa

Browse files
authored
Merge branch 'main' into repository-custom-properties
2 parents 2b169f1 + 0419c6b commit 1b6bfaa

File tree

112 files changed

+12247
-2528
lines changed

Some content is hidden

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

112 files changed

+12247
-2528
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
GITHUB_TEST_ORGANIZATION: 'kfcampbell-terraform-provider'
1313
steps:
1414
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
15-
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
15+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
1616
with:
1717
go-version-file: 'go.mod'
1818
cache: true

.github/workflows/codeql.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@ jobs:
2727
- name: Checkout repository
2828
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2929

30-
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
30+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
3131
with:
3232
go-version-file: 'go.mod'
3333
cache: true
3434

3535
# Initializes the CodeQL tools for scanning.
3636
- name: Initialize CodeQL
37-
uses: github/codeql-action/init@f09c1c0a94de965c15400f5634aa42fac8fb8f88 # v3.27.5
37+
uses: github/codeql-action/init@48ab28a6f5dbc2a99bf1e0131198dd8f1df78169 # v3.28.0
3838
with:
3939
languages: ${{ matrix.language }}
4040

4141
- name: Autobuild
42-
uses: github/codeql-action/autobuild@f09c1c0a94de965c15400f5634aa42fac8fb8f88 # v3.27.5
42+
uses: github/codeql-action/autobuild@48ab28a6f5dbc2a99bf1e0131198dd8f1df78169 # v3.28.0
4343

4444
- name: Perform CodeQL Analysis
45-
uses: github/codeql-action/analyze@f09c1c0a94de965c15400f5634aa42fac8fb8f88 # v3.27.5
45+
uses: github/codeql-action/analyze@48ab28a6f5dbc2a99bf1e0131198dd8f1df78169 # v3.28.0
4646
with:
4747
category: "/language:${{matrix.language}}"

.github/workflows/dotcom-acceptance-tests-all.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
with:
2121
ref: ${{ github.event.pull_request.head.ref }}
2222
fetch-depth: 2
23-
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
23+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
2424
with:
2525
go-version-file: 'go.mod'
2626
cache: true
@@ -38,7 +38,7 @@ jobs:
3838
with:
3939
ref: ${{ github.event.pull_request.head.ref }}
4040
fetch-depth: 2
41-
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
41+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
4242
with:
4343
go-version-file: 'go.mod'
4444
cache: true
@@ -71,7 +71,7 @@ jobs:
7171
with:
7272
ref: ${{ github.event.pull_request.head.ref }}
7373
fetch-depth: 2
74-
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
74+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
7575
with:
7676
go-version-file: 'go.mod'
7777
cache: true

.github/workflows/dotcom-acceptance-tests-manual.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2121
with:
2222
ref: ${{ github.event.pull_request.head.sha }}
23-
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
23+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
2424
with:
2525
go-version-file: 'go.mod'
2626
cache: true
@@ -52,7 +52,7 @@ jobs:
5252
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
5353
with:
5454
ref: ${{ github.event.pull_request.head.sha }}
55-
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
55+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
5656
with:
5757
go-version-file: 'go.mod'
5858
cache: true
@@ -89,7 +89,7 @@ jobs:
8989
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
9090
with:
9191
ref: ${{ github.event.pull_request.head.sha }}
92-
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
92+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
9393
with:
9494
go-version-file: 'go.mod'
9595
cache: true

.github/workflows/dotcom-acceptance-tests.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
with:
2929
ref: ${{ github.event.pull_request.head.ref }}
3030
fetch-depth: 2
31-
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
31+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
3232
with:
3333
go-version-file: 'go.mod'
3434
cache: true
@@ -48,7 +48,7 @@ jobs:
4848
with:
4949
ref: ${{ github.event.pull_request.head.ref }}
5050
fetch-depth: 2
51-
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
51+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
5252
with:
5353
go-version-file: 'go.mod'
5454
cache: true

.github/workflows/ghes-acceptance-tests-all.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
with:
3333
ref: ${{ github.event.pull_request.head.ref }}
3434
fetch-depth: 2
35-
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
35+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
3636
with:
3737
go-version-file: 'go.mod'
3838
cache: true
@@ -51,7 +51,7 @@ jobs:
5151
with:
5252
ref: ${{ github.event.pull_request.head.ref }}
5353
fetch-depth: 2
54-
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
54+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
5555
with:
5656
go-version-file: 'go.mod'
5757
cache: true
@@ -86,7 +86,7 @@ jobs:
8686
with:
8787
ref: ${{ github.event.pull_request.head.ref }}
8888
fetch-depth: 2
89-
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
89+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
9090
with:
9191
go-version-file: 'go.mod'
9292
cache: true

.github/workflows/ghes-acceptance-tests.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
with:
2828
ref: ${{ github.event.pull_request.head.ref }}
2929
fetch-depth: 2
30-
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
30+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
3131
with:
3232
go-version-file: 'go.mod'
3333
cache: true
@@ -46,7 +46,7 @@ jobs:
4646
with:
4747
ref: ${{ github.event.pull_request.head.ref }}
4848
fetch-depth: 2
49-
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
49+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
5050
with:
5151
go-version-file: 'go.mod'
5252
cache: true
@@ -67,7 +67,7 @@ jobs:
6767
with:
6868
ref: ${{ github.event.pull_request.head.ref }}
6969
fetch-depth: 2
70-
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
70+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
7171
with:
7272
go-version-file: 'go.mod'
7373
cache: true

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
# Allow goreleaser to access older tag information.
2323
fetch-depth: 0
2424

25-
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
25+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
2626
with:
2727
go-version-file: 'go.mod'
2828
cache: true

CONTRIBUTING.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Setting a `processId` of 0 allows a dropdown to select the process of the provid
8181

8282
0. Add a sleep call (e.g. `time.Sleep(10 * time.Second)`) in the [`func providerConfigure(p *schema.Provider) schema.ConfigureFunc`](https://github.com/integrations/terraform-provider-github/blob/cec7e175c50bb091feecdc96ba117067c35ee351/github/provider.go#L274C1-L274C64) before the immediate `return` call. This will allow time to connect the debugger while the provider is initializing, before any critical logic happens.
8383

84-
0. Build the terraform provider with debug flags enabled and copy it to the appropriate bin folder with a command like `go build -gcflags="all=-N -l" -o ~/go/bin`.
84+
0. Build the terraform provider with debug flags enabled and copy it to the appropriate bin folder with a command like `go build -gcflags="all=-N -l" -o ~/go/bin/`.
8585

8686
0. Create or edit a `dev.tfrc` that points toward the newly-built binary, and export the `TF_CLI_CONFIG_FILE` variable to point to it. Further instructions on this process may be found in the [Building the provider](#using-a-local-version-of-the-provider) section.
8787

@@ -99,7 +99,7 @@ Manual testing should be performed on each PR opened in order to validate the pr
9999
Build the provider and specify the output directory:
100100

101101
```sh
102-
$ go build -gcflags="all=-N -l" -o ~/go/bin
102+
$ go build -gcflags="all=-N -l" -o ~/go/bin/
103103
```
104104

105105
This enables verifying your locally built provider using examples available in the `examples/` directory.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Organization Security Manager Example
2+
3+
This example demonstrates creating an organization security manager team.
4+
5+
It will:
6+
- Create a team with the specified `team_name` in the specified `owner` organization
7+
- Assign the organization security manager role to the team
8+
9+
The GitHub token must have the `admin:org` scope.
10+
11+
```console
12+
export GITHUB_OWNER=my-organization
13+
export GITHUB_TOKEN=ghp_###
14+
export GITHUB_TEAM_NAME="My Security Manager Team"
15+
```
16+
17+
```console
18+
terraform apply \
19+
-var "owner=${GITHUB_OWNER}" \
20+
-var "github_token=${GITHUB_TOKEN}" \
21+
-var "team_name=${GITHUB_TEAM_NAME}"
22+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
resource "github_team" "security_managers" {
2+
name = var.team_name
3+
description = "A team of organization security managers"
4+
}
5+
6+
resource "github_organization_security_manager" "security_managers" {
7+
team_slug = github_team.security_managers.slug
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "github_security_managers_team" {
2+
description = "The organization security managers team"
3+
value = github_organization_security_manager.security_managers
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
provider "github" {
2+
owner = var.owner
3+
token = var.github_token
4+
}
5+
6+
terraform {
7+
required_providers {
8+
github = {
9+
source = "integrations/github"
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
variable "github_token" {
2+
description = "GitHub access token used to configure the provider"
3+
type = string
4+
}
5+
6+
variable "owner" {
7+
description = "GitHub owner used to configure the provider"
8+
type = string
9+
}
10+
11+
variable "team_name" {
12+
description = "The name to use for the GitHub team"
13+
type = string
14+
}

github/resource_github_organization_security_manager.go

+57-20
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package github
22

33
import (
44
"context"
5+
"errors"
56
"log"
6-
"net/http"
77
"strconv"
88

99
"github.com/google/go-github/v66/github"
@@ -30,6 +30,21 @@ func resourceGithubOrganizationSecurityManager() *schema.Resource {
3030
}
3131
}
3232

33+
func getSecurityManagerRole(client *github.Client, ctx context.Context, orgName string) (*github.CustomOrgRoles, error) {
34+
roles, _, err := client.Organizations.ListRoles(ctx, orgName)
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
for _, role := range roles.CustomRepoRoles {
40+
if *role.Name == "security_manager" {
41+
return role, nil
42+
}
43+
}
44+
45+
return nil, errors.New("security manager role not found")
46+
}
47+
3348
func resourceGithubOrganizationSecurityManagerCreate(d *schema.ResourceData, meta interface{}) error {
3449
err := checkOrganization(meta)
3550
if err != nil {
@@ -44,18 +59,16 @@ func resourceGithubOrganizationSecurityManagerCreate(d *schema.ResourceData, met
4459

4560
team, _, err := client.Teams.GetTeamBySlug(ctx, orgName, teamSlug)
4661
if err != nil {
47-
log.Printf("[INFO] Team %s/%s was not found in GitHub", orgName, teamSlug)
4862
return err
4963
}
5064

51-
_, err = client.Organizations.AddSecurityManagerTeam(ctx, orgName, teamSlug)
65+
smRole, err := getSecurityManagerRole(client, ctx, orgName)
66+
if err != nil {
67+
return err
68+
}
69+
70+
_, err = client.Organizations.AssignOrgRoleToTeam(ctx, orgName, teamSlug, smRole.GetID())
5271
if err != nil {
53-
if ghErr, ok := err.(*github.ErrorResponse); ok {
54-
if ghErr.Response.StatusCode == http.StatusConflict {
55-
log.Printf("[WARN] Organization %s has reached the maximum number of security manager teams", orgName)
56-
return nil
57-
}
58-
}
5972
return err
6073
}
6174

@@ -79,28 +92,42 @@ func resourceGithubOrganizationSecurityManagerRead(d *schema.ResourceData, meta
7992
client := meta.(*Owner).v3client
8093
ctx := context.WithValue(context.Background(), ctxId, d.Id())
8194

82-
// There is no endpoint for getting a single security manager team, so get the list and filter.
83-
// There is a maximum number of security manager teams (currently 10), so this should be fine.
84-
teams, _, err := client.Organizations.ListSecurityManagerTeams(ctx, orgName)
95+
smRole, err := getSecurityManagerRole(client, ctx, orgName)
8596
if err != nil {
8697
return err
8798
}
8899

89-
var team *github.Team
90-
for _, t := range teams {
91-
if t.GetID() == teamId {
92-
team = t
100+
// There is no endpoint for getting a single security manager team, so get the list and filter.
101+
options := &github.ListOptions{PerPage: 100}
102+
var smTeam *github.Team = nil
103+
for {
104+
smTeams, resp, err := client.Organizations.ListTeamsAssignedToOrgRole(ctx, orgName, smRole.GetID(), options)
105+
if err != nil {
106+
return err
107+
}
108+
109+
for _, t := range smTeams {
110+
if t.GetID() == teamId {
111+
smTeam = t
112+
break
113+
}
114+
}
115+
116+
// Break when we've found the team or there are no more pages.
117+
if smTeam != nil || resp.NextPage == 0 {
93118
break
94119
}
120+
121+
options.Page = resp.NextPage
95122
}
96123

97-
if team == nil {
124+
if smTeam == nil {
98125
log.Printf("[WARN] Removing organization security manager team %s from state because it no longer exists in GitHub", d.Id())
99126
d.SetId("")
100127
return nil
101128
}
102129

103-
if err = d.Set("team_slug", team.GetSlug()); err != nil {
130+
if err = d.Set("team_slug", smTeam.GetSlug()); err != nil {
104131
return err
105132
}
106133

@@ -128,8 +155,13 @@ func resourceGithubOrganizationSecurityManagerUpdate(d *schema.ResourceData, met
128155
return err
129156
}
130157

158+
smRole, err := getSecurityManagerRole(client, ctx, orgName)
159+
if err != nil {
160+
return err
161+
}
162+
131163
// Adding the same team is a no-op.
132-
_, err = client.Organizations.AddSecurityManagerTeam(ctx, orgName, team.GetSlug())
164+
_, err = client.Organizations.AssignOrgRoleToTeam(ctx, orgName, team.GetSlug(), smRole.GetID())
133165
if err != nil {
134166
return err
135167
}
@@ -149,6 +181,11 @@ func resourceGithubOrganizationSecurityManagerDelete(d *schema.ResourceData, met
149181
client := meta.(*Owner).v3client
150182
ctx := context.WithValue(context.Background(), ctxId, d.Id())
151183

152-
_, err = client.Organizations.RemoveSecurityManagerTeam(ctx, orgName, teamSlug)
184+
smRole, err := getSecurityManagerRole(client, ctx, orgName)
185+
if err != nil {
186+
return err
187+
}
188+
189+
_, err = client.Organizations.RemoveOrgRoleFromTeam(ctx, orgName, teamSlug, smRole.GetID())
153190
return err
154191
}

0 commit comments

Comments
 (0)