-
Notifications
You must be signed in to change notification settings - Fork 984
Expand file tree
/
Copy pathresource_github_repository_environment_migration.go
More file actions
113 lines (103 loc) · 3.74 KB
/
resource_github_repository_environment_migration.go
File metadata and controls
113 lines (103 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package github
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func resourceGithubRepositoryEnvironmentV0() *schema.Resource {
return &schema.Resource{
SchemaVersion: 0,
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
Description: "The repository of the environment.",
},
"environment": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of the environment.",
},
"can_admins_bypass": {
Type: schema.TypeBool,
Optional: true,
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,
ValidateDiagFunc: validation.ToDiagFunc(validation.IntBetween(0, 43200)),
Description: "Amount of time to delay a job after the job is initially triggered.",
},
"reviewers": {
Type: schema.TypeList,
Optional: true,
MaxItems: 6,
Description: "The environment reviewers configuration.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"teams": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeInt},
Optional: true,
Description: "Up to 6 IDs for teams who may review jobs that reference the environment. Reviewers must have at least read access to the repository. Only one of the required reviewers needs to approve the job for it to proceed.",
},
"users": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeInt},
Optional: true,
Description: "Up to 6 IDs for users who may review jobs that reference the environment. Reviewers must have at least read access to the repository. Only one of the required reviewers needs to approve the job for it to proceed.",
},
},
},
},
"deployment_branch_policy": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "The deployment branch policy configuration",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"protected_branches": {
Type: schema.TypeBool,
Required: true,
Description: "Whether only branches with branch protection rules can deploy to this environment.",
},
"custom_branch_policies": {
Type: schema.TypeBool,
Required: true,
Description: "Whether only branches that match the specified name patterns can deploy to this environment.",
},
},
},
},
},
}
}
func resourceGithubRepositoryEnvironmentStateUpgradeV0(ctx context.Context, rawState map[string]any, m any) (map[string]any, error) {
meta := m.(*Owner)
client := meta.v3client
owner := meta.name
tflog.Debug(ctx, "Starting state upgrade for GitHub Repository Environment.", map[string]any{"raw_state": rawState})
repoName, ok := rawState["repository"].(string)
if !ok {
return nil, fmt.Errorf("repository not found or is not a string")
}
repo, _, err := client.Repositories.Get(ctx, owner, repoName)
if err != nil {
return nil, fmt.Errorf("failed to retrieve repository %s: %w", repoName, err)
}
rawState["repository_id"] = int(repo.GetID())
tflog.Debug(ctx, "Completed state upgrade for GitHub Repository Environment.", map[string]any{"upgraded_state": rawState})
return rawState, nil
}