|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/google/go-github/v66/github" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 11 | +) |
| 12 | + |
| 13 | +func resourceGithubRepositoryPreReceiveHook() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Create: resourceGithubRepositoryPreReceiveHookCreate, |
| 16 | + Read: resourceGithubRepositoryPreReceiveHookRead, |
| 17 | + Update: resourceGithubRepositoryPreReceiveHookUpdate, |
| 18 | + Delete: resourceGithubRepositoryPreReceiveHookDelete, |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + "repository": { |
| 21 | + Type: schema.TypeString, |
| 22 | + Required: true, |
| 23 | + ForceNew: true, |
| 24 | + Description: "The repository of the pre-receive hook.", |
| 25 | + }, |
| 26 | + "name": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + ForceNew: true, |
| 30 | + Description: "The name of the pre-receive hook.", |
| 31 | + }, |
| 32 | + "enforcement": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Required: true, |
| 35 | + ValidateFunc: validation.StringInSlice([]string{"enabled", "disabled", "testing"}, false), |
| 36 | + Description: "The state of enforcement for the hook on the repository. Possible values for enforcement are 'enabled', 'disabled' and 'testing'. 'disabled' indicates the pre-receive hook will not run. 'enabled' indicates it will run and reject any pushes that result in a non-zero status. 'testing' means the script will run but will not cause any pushes to be rejected.", |
| 37 | + }, |
| 38 | + "configuration_url": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Computed: true, |
| 41 | + Description: "The URL for the endpoint where enforcement is set.", |
| 42 | + }, |
| 43 | + }, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func resourceGithubRepositoryPreReceiveHookCreate(d *schema.ResourceData, meta interface{}) error { |
| 48 | + client := meta.(*Owner).v3client |
| 49 | + |
| 50 | + owner := meta.(*Owner).name |
| 51 | + repoName := d.Get("repository").(string) |
| 52 | + hookName := d.Get("name").(string) |
| 53 | + |
| 54 | + hook, err := fetchGitHubRepositoryPreReceiveHookByName(meta, repoName, hookName) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + enforcement := d.Get("enforcement").(string) |
| 60 | + hook.Enforcement = &enforcement |
| 61 | + |
| 62 | + ctx := context.WithValue(context.Background(), ctxId, d.Id()) |
| 63 | + |
| 64 | + _, _, err = client.Repositories.UpdatePreReceiveHook(ctx, owner, repoName, hook.GetID(), hook) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + d.SetId(strconv.FormatInt(hook.GetID(), 10)) |
| 69 | + |
| 70 | + return resourceGithubRepositoryPreReceiveHookRead(d, meta) |
| 71 | +} |
| 72 | + |
| 73 | +func resourceGithubRepositoryPreReceiveHookRead(d *schema.ResourceData, meta interface{}) error { |
| 74 | + client := meta.(*Owner).v3client |
| 75 | + ctx := context.WithValue(context.Background(), ctxId, d.Id()) |
| 76 | + |
| 77 | + owner := meta.(*Owner).name |
| 78 | + repoName := d.Get("repository").(string) |
| 79 | + |
| 80 | + hookID, err := strconv.ParseInt(d.Id(), 10, 64) |
| 81 | + if err != nil { |
| 82 | + return unconvertibleIdErr(d.Id(), err) |
| 83 | + } |
| 84 | + |
| 85 | + hook, _, err := client.Repositories.GetPreReceiveHook(ctx, owner, repoName, hookID) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + if err = d.Set("enforcement", hook.Enforcement); err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + if err = d.Set("configuration_url", hook.ConfigURL); err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func resourceGithubRepositoryPreReceiveHookUpdate(d *schema.ResourceData, meta interface{}) error { |
| 100 | + client := meta.(*Owner).v3client |
| 101 | + ctx := context.WithValue(context.Background(), ctxId, d.Id()) |
| 102 | + |
| 103 | + owner := meta.(*Owner).name |
| 104 | + repoName := d.Get("repository").(string) |
| 105 | + enforcement := d.Get("enforcement").(string) |
| 106 | + |
| 107 | + hookID, err := strconv.ParseInt(d.Id(), 10, 64) |
| 108 | + if err != nil { |
| 109 | + return unconvertibleIdErr(d.Id(), err) |
| 110 | + } |
| 111 | + |
| 112 | + hook := &github.PreReceiveHook{ |
| 113 | + Enforcement: &enforcement, |
| 114 | + } |
| 115 | + _, _, err = client.Repositories.UpdatePreReceiveHook(ctx, owner, repoName, hookID, hook) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + return resourceGithubRepositoryPreReceiveHookRead(d, meta) |
| 121 | +} |
| 122 | + |
| 123 | +func resourceGithubRepositoryPreReceiveHookDelete(d *schema.ResourceData, meta interface{}) error { |
| 124 | + client := meta.(*Owner).v3client |
| 125 | + ctx := context.WithValue(context.Background(), ctxId, d.Id()) |
| 126 | + |
| 127 | + owner := meta.(*Owner).name |
| 128 | + repoName := d.Get("repository").(string) |
| 129 | + |
| 130 | + hookID, err := strconv.ParseInt(d.Id(), 10, 64) |
| 131 | + if err != nil { |
| 132 | + return unconvertibleIdErr(d.Id(), err) |
| 133 | + } |
| 134 | + _, err = client.Repositories.DeletePreReceiveHook(ctx, owner, repoName, hookID) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + |
| 139 | + return nil |
| 140 | +} |
| 141 | + |
| 142 | +func fetchGitHubRepositoryPreReceiveHookByName(meta interface{}, repoName, hookName string) (*github.PreReceiveHook, error) { |
| 143 | + ctx := context.Background() |
| 144 | + client := meta.(*Owner).v3client |
| 145 | + owner := meta.(*Owner).name |
| 146 | + |
| 147 | + opt := &github.ListOptions{ |
| 148 | + PerPage: 100, |
| 149 | + } |
| 150 | + |
| 151 | + var hook *github.PreReceiveHook |
| 152 | + |
| 153 | + for { |
| 154 | + hooks, resp, err := client.Repositories.ListPreReceiveHooks(ctx, owner, repoName, opt) |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + |
| 159 | + for _, h := range hooks { |
| 160 | + n := *h.Name |
| 161 | + if n == hookName { |
| 162 | + hook = h |
| 163 | + break |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + if resp.NextPage == 0 { |
| 168 | + break |
| 169 | + } |
| 170 | + opt.Page = resp.NextPage |
| 171 | + } |
| 172 | + |
| 173 | + if *hook.ID <= 0 { |
| 174 | + return nil, fmt.Errorf("no pre-receive hook with name %s found on %s/%s", hookName, owner, repoName) |
| 175 | + } |
| 176 | + |
| 177 | + return hook, nil |
| 178 | +} |
0 commit comments