github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/github/repositories.go (about) 1 // Copyright 2018 The Terraformer Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package github 16 17 import ( 18 "context" 19 "log" 20 "strconv" 21 22 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 23 githubAPI "github.com/google/go-github/v35/github" 24 ) 25 26 type RepositoriesGenerator struct { 27 GithubService 28 } 29 30 // Generate TerraformResources from github API, 31 func (g *RepositoriesGenerator) InitResources() error { 32 ctx := context.Background() 33 client, err := g.createClient() 34 if err != nil { 35 return err 36 } 37 38 opt := &githubAPI.RepositoryListByOrgOptions{ 39 ListOptions: githubAPI.ListOptions{PerPage: 100}, 40 } 41 // list all repositories for the authenticated user 42 for { 43 repos, resp, err := client.Repositories.ListByOrg(ctx, g.GetArgs()["owner"].(string), opt) 44 if err != nil { 45 log.Println(err) 46 return nil 47 } 48 for _, repo := range repos { 49 resource := terraformutils.NewSimpleResource( 50 repo.GetName(), 51 repo.GetName(), 52 "github_repository", 53 "github", 54 []string{}, 55 ) 56 resource.SlowQueryRequired = true 57 g.Resources = append(g.Resources, resource) 58 g.Resources = append(g.Resources, g.createRepositoryWebhookResources(ctx, client, repo)...) 59 g.Resources = append(g.Resources, g.createRepositoryBranchProtectionResources(ctx, client, repo)...) 60 g.Resources = append(g.Resources, g.createRepositoryCollaboratorResources(ctx, client, repo)...) 61 g.Resources = append(g.Resources, g.createRepositoryDeployKeyResources(ctx, client, repo)...) 62 } 63 64 if resp.NextPage == 0 { 65 break 66 } 67 opt.Page = resp.NextPage 68 } 69 70 return nil 71 } 72 73 func (g *RepositoriesGenerator) createRepositoryWebhookResources(ctx context.Context, client *githubAPI.Client, repo *githubAPI.Repository) []terraformutils.Resource { 74 resources := []terraformutils.Resource{} 75 hooks, _, err := client.Repositories.ListHooks(ctx, g.GetArgs()["owner"].(string), repo.GetName(), nil) 76 if err != nil { 77 log.Println(err) 78 } 79 for _, hook := range hooks { 80 resources = append(resources, terraformutils.NewResource( 81 strconv.FormatInt(hook.GetID(), 10), 82 repo.GetName()+"_"+strconv.FormatInt(hook.GetID(), 10), 83 "github_repository_webhook", 84 "github", 85 map[string]string{ 86 "repository": repo.GetName(), 87 }, 88 []string{}, 89 map[string]interface{}{}, 90 )) 91 } 92 return resources 93 } 94 95 func (g *RepositoriesGenerator) createRepositoryBranchProtectionResources(ctx context.Context, client *githubAPI.Client, repo *githubAPI.Repository) []terraformutils.Resource { 96 resources := []terraformutils.Resource{} 97 branches, _, err := client.Repositories.ListBranches(ctx, g.GetArgs()["owner"].(string), repo.GetName(), nil) 98 if err != nil { 99 log.Println(err) 100 } 101 for _, branch := range branches { 102 if branch.GetProtected() { 103 resources = append(resources, terraformutils.NewSimpleResource( 104 repo.GetName()+":"+branch.GetName(), 105 repo.GetName()+"_"+branch.GetName(), 106 "github_branch_protection", 107 "github", 108 []string{}, 109 )) 110 } 111 } 112 return resources 113 } 114 115 func (g *RepositoriesGenerator) createRepositoryCollaboratorResources(ctx context.Context, client *githubAPI.Client, repo *githubAPI.Repository) []terraformutils.Resource { 116 resources := []terraformutils.Resource{} 117 collaborators, _, err := client.Repositories.ListCollaborators(ctx, g.GetArgs()["owner"].(string), repo.GetName(), nil) 118 if err != nil { 119 log.Println(err) 120 } 121 for _, collaborator := range collaborators { 122 resources = append(resources, terraformutils.NewSimpleResource( 123 repo.GetName()+":"+collaborator.GetLogin(), 124 repo.GetName()+":"+collaborator.GetLogin(), 125 "github_repository_collaborator", 126 "github", 127 []string{}, 128 )) 129 } 130 return resources 131 } 132 133 func (g *RepositoriesGenerator) createRepositoryDeployKeyResources(ctx context.Context, client *githubAPI.Client, repo *githubAPI.Repository) []terraformutils.Resource { 134 resources := []terraformutils.Resource{} 135 deployKeys, _, err := client.Repositories.ListKeys(ctx, g.GetArgs()["owner"].(string), repo.GetName(), nil) 136 if err != nil { 137 log.Println(err) 138 } 139 for _, key := range deployKeys { 140 resources = append(resources, terraformutils.NewSimpleResource( 141 repo.GetName()+":"+strconv.FormatInt(key.GetID(), 10), 142 repo.GetName()+":"+key.GetTitle(), 143 "github_repository_deploy_key", 144 "github", 145 []string{}, 146 )) 147 } 148 return resources 149 } 150 151 // PostGenerateHook for connect between resources 152 func (g *RepositoriesGenerator) PostConvertHook() error { 153 for _, repo := range g.Resources { 154 if repo.InstanceInfo.Type != "github_repository" { 155 continue 156 } 157 for i, member := range g.Resources { 158 if member.InstanceInfo.Type != "github_repository_webhook" { 159 continue 160 } 161 if member.InstanceState.Attributes["repository"] == repo.InstanceState.Attributes["name"] { 162 g.Resources[i].Item["repository"] = "${github_repository." + repo.ResourceName + ".name}" 163 } 164 } 165 for i, branch := range g.Resources { 166 if branch.InstanceInfo.Type != "github_branch_protection" { 167 continue 168 } 169 if branch.InstanceState.Attributes["repository"] == repo.InstanceState.Attributes["name"] { 170 g.Resources[i].Item["repository"] = "${github_repository." + repo.ResourceName + ".name}" 171 } 172 } 173 for i, collaborator := range g.Resources { 174 if collaborator.InstanceInfo.Type != "github_repository_collaborator" { 175 continue 176 } 177 if collaborator.InstanceState.Attributes["repository"] == repo.InstanceState.Attributes["name"] { 178 g.Resources[i].Item["repository"] = "${github_repository." + repo.ResourceName + ".name}" 179 } 180 } 181 for i, key := range g.Resources { 182 if key.InstanceInfo.Type != "github_repository_deploy_key" { 183 continue 184 } 185 if key.InstanceState.Attributes["repository"] == repo.InstanceState.Attributes["name"] { 186 g.Resources[i].Item["repository"] = "${github_repository." + repo.ResourceName + ".name}" 187 } 188 } 189 } 190 return nil 191 }