forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresource_github_app_installation_repository.go
More file actions
133 lines (114 loc) · 3.4 KB
/
resource_github_app_installation_repository.go
File metadata and controls
133 lines (114 loc) · 3.4 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package github
import (
"context"
"log"
"strconv"
"github.com/google/go-github/v81/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceGithubAppInstallationRepository() *schema.Resource {
return &schema.Resource{
Create: resourceGithubAppInstallationRepositoryCreate,
Read: resourceGithubAppInstallationRepositoryRead,
Delete: resourceGithubAppInstallationRepositoryDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"installation_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The GitHub app installation id.",
},
"repository": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The repository to install the app on.",
},
"repo_id": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func resourceGithubAppInstallationRepositoryCreate(d *schema.ResourceData, meta any) error {
installationIDString := d.Get("installation_id").(string)
installationID, err := strconv.ParseInt(installationIDString, 10, 64)
if err != nil {
return unconvertibleIdErr(installationIDString, err)
}
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
ctx := context.Background()
repoName := d.Get("repository").(string)
repo, _, err := client.Repositories.Get(ctx, owner, repoName)
if err != nil {
return err
}
repoID := repo.GetID()
_, _, err = client.Apps.AddRepository(ctx, installationID, repoID)
if err != nil {
return err
}
d.SetId(buildTwoPartID(installationIDString, repoName))
return resourceGithubAppInstallationRepositoryRead(d, meta)
}
func resourceGithubAppInstallationRepositoryRead(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
installationIDString, repoName, err := parseTwoPartID(d.Id(), "installation_id", "repository")
if err != nil {
return err
}
installationID, err := strconv.ParseInt(installationIDString, 10, 64)
if err != nil {
return unconvertibleIdErr(installationIDString, err)
}
ctx := context.WithValue(context.Background(), ctxId, d.Id())
opt := &github.ListOptions{PerPage: maxPerPage}
for {
repos, resp, err := client.Apps.ListUserRepos(ctx, installationID, opt)
if err != nil {
return err
}
for _, r := range repos.Repositories {
if r.GetName() == repoName {
if err = d.Set("installation_id", installationIDString); err != nil {
return err
}
if err = d.Set("repository", repoName); err != nil {
return err
}
if err = d.Set("repo_id", r.GetID()); err != nil {
return err
}
return nil
}
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
log.Printf("[INFO] Removing app installation repository association %s from state because it no longer exists in GitHub",
d.Id())
d.SetId("")
return nil
}
func resourceGithubAppInstallationRepositoryDelete(d *schema.ResourceData, meta any) error {
installationIDString := d.Get("installation_id").(string)
installationID, err := strconv.ParseInt(installationIDString, 10, 64)
if err != nil {
return unconvertibleIdErr(installationIDString, err)
}
client := meta.(*Owner).v3client
ctx := context.Background()
repoID := d.Get("repo_id").(int)
_, err = client.Apps.RemoveRepository(ctx, installationID, int64(repoID))
if err != nil {
return err
}
return nil
}