|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "github.com/google/go-github/v53/github" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 12 | +) |
| 13 | + |
| 14 | +func resourceGithubRepositoryEnvironmentDeploymentPolicy() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Create: resourceGithubRepositoryEnvironmentDeploymentPolicyCreate, |
| 17 | + Read: resourceGithubRepositoryEnvironmentDeploymentPolicyRead, |
| 18 | + Update: resourceGithubRepositoryEnvironmentDeploymentPolicyUpdate, |
| 19 | + Delete: resourceGithubRepositoryEnvironmentDeploymentPolicyDelete, |
| 20 | + Importer: &schema.ResourceImporter{ |
| 21 | + State: schema.ImportStatePassthrough, |
| 22 | + }, |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "repository": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Required: true, |
| 27 | + ForceNew: true, |
| 28 | + Description: "The name of the repository. The name is not case sensitive.", |
| 29 | + }, |
| 30 | + "environment": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + ForceNew: true, |
| 34 | + Description: "The name of the environment.", |
| 35 | + }, |
| 36 | + "branch_pattern": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Required: true, |
| 39 | + ForceNew: false, |
| 40 | + Description: "The name pattern that branches must match in order to deploy to the environment.", |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | +} |
| 46 | + |
| 47 | +func resourceGithubRepositoryEnvironmentDeploymentPolicyCreate(d *schema.ResourceData, meta interface{}) error { |
| 48 | + client := meta.(*Owner).v3client |
| 49 | + ctx := context.Background() |
| 50 | + |
| 51 | + owner := meta.(*Owner).name |
| 52 | + repoName := d.Get("repository").(string) |
| 53 | + envName := d.Get("environment").(string) |
| 54 | + branchPattern := d.Get("branch_pattern").(string) |
| 55 | + escapedEnvName := url.PathEscape(envName) |
| 56 | + |
| 57 | + createData := github.DeploymentBranchPolicyRequest{ |
| 58 | + Name: github.String(branchPattern), |
| 59 | + } |
| 60 | + |
| 61 | + resultKey, _, err := client.Repositories.CreateDeploymentBranchPolicy(ctx, owner, repoName, escapedEnvName, &createData) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + d.SetId(buildThreePartID(repoName, escapedEnvName, strconv.FormatInt(resultKey.GetID(), 10))) |
| 67 | + return resourceGithubRepositoryEnvironmentDeploymentPolicyRead(d, meta) |
| 68 | +} |
| 69 | + |
| 70 | +func resourceGithubRepositoryEnvironmentDeploymentPolicyRead(d *schema.ResourceData, meta interface{}) error { |
| 71 | + client := meta.(*Owner).v3client |
| 72 | + ctx := context.WithValue(context.Background(), ctxId, d.Id()) |
| 73 | + |
| 74 | + owner := meta.(*Owner).name |
| 75 | + repoName, envName, branchPolicyIdString, err := parseThreePartID(d.Id(), "repository", "environment", "branchPolicyId") |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + |
| 80 | + branchPolicyId, err := strconv.ParseInt(branchPolicyIdString, 10, 64) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + branchPolicy, _, err := client.Repositories.GetDeploymentBranchPolicy(ctx, owner, repoName, envName, branchPolicyId) |
| 86 | + if err != nil { |
| 87 | + if ghErr, ok := err.(*github.ErrorResponse); ok { |
| 88 | + if ghErr.Response.StatusCode == http.StatusNotModified { |
| 89 | + return nil |
| 90 | + } |
| 91 | + if ghErr.Response.StatusCode == http.StatusNotFound { |
| 92 | + log.Printf("[INFO] Removing branch deployment policy for %s/%s/%s from state because it no longer exists in GitHub", |
| 93 | + owner, repoName, envName) |
| 94 | + d.SetId("") |
| 95 | + return nil |
| 96 | + } |
| 97 | + } |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + d.Set("branch_pattern", branchPolicy.GetName()) |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +func resourceGithubRepositoryEnvironmentDeploymentPolicyUpdate(d *schema.ResourceData, meta interface{}) error { |
| 106 | + client := meta.(*Owner).v3client |
| 107 | + ctx := context.Background() |
| 108 | + |
| 109 | + owner := meta.(*Owner).name |
| 110 | + repoName := d.Get("repository").(string) |
| 111 | + envName := d.Get("environment").(string) |
| 112 | + branchPattern := d.Get("branch_pattern").(string) |
| 113 | + escapedEnvName := url.PathEscape(envName) |
| 114 | + _, _, branchPolicyIdString, err := parseThreePartID(d.Id(), "repository", "environment", "branchPolicyId") |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + branchPolicyId, err := strconv.ParseInt(branchPolicyIdString, 10, 64) |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + |
| 124 | + updateData := github.DeploymentBranchPolicyRequest{ |
| 125 | + Name: github.String(branchPattern), |
| 126 | + } |
| 127 | + |
| 128 | + resultKey, _, err := client.Repositories.UpdateDeploymentBranchPolicy(ctx, owner, repoName, escapedEnvName, branchPolicyId, &updateData) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + d.SetId(buildThreePartID(repoName, escapedEnvName, strconv.FormatInt(resultKey.GetID(), 10))) |
| 133 | + return resourceGithubRepositoryEnvironmentDeploymentPolicyRead(d, meta) |
| 134 | +} |
| 135 | + |
| 136 | +func resourceGithubRepositoryEnvironmentDeploymentPolicyDelete(d *schema.ResourceData, meta interface{}) error { |
| 137 | + client := meta.(*Owner).v3client |
| 138 | + ctx := context.Background() |
| 139 | + |
| 140 | + owner := meta.(*Owner).name |
| 141 | + repoName, envName, branchPolicyIdString, err := parseThreePartID(d.Id(), "repository", "environment", "branchPolicyId") |
| 142 | + if err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + |
| 146 | + branchPolicyId, err := strconv.ParseInt(branchPolicyIdString, 10, 64) |
| 147 | + if err != nil { |
| 148 | + return err |
| 149 | + } |
| 150 | + |
| 151 | + _, err = client.Repositories.DeleteDeploymentBranchPolicy(ctx, owner, repoName, envName, branchPolicyId) |
| 152 | + if err != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + |
| 156 | + return nil |
| 157 | +} |
0 commit comments