1
+ terraform {
2
+ required_providers {
3
+ github = {
4
+ source = " integrations/github"
5
+ version = " ~> 6.0"
6
+ }
7
+ }
8
+ }
9
+ variable "github_token" {
10
+ type = string
11
+ description = " GitHub token"
12
+ }
13
+ variable "discord_webhook_url" {
14
+ description = " The Discord webhook URL to send notifications"
15
+ type = string
16
+ }
17
+
18
+ # Configure the GitHub Provider
19
+ provider "github" {
20
+ token = var. github_token
21
+ owner = " DevKor-github"
22
+ }
23
+
24
+ data "local_file" "users" {
25
+ filename = " ${ path . module } /users.json"
26
+ }
27
+
28
+ locals {
29
+ users = jsondecode (data. local_file . users . content )
30
+ }
31
+
32
+ data "local_file" "teams" {
33
+ filename = " ${ path . module } /teams.json"
34
+ }
35
+
36
+ locals {
37
+ teams = jsondecode (data. local_file . teams . content )
38
+ }
39
+ data "local_file" "repos" {
40
+ filename = " ${ path . module } /repos.json"
41
+ }
42
+
43
+ locals {
44
+ repos = jsondecode (data. local_file . repos . content )
45
+ }
46
+ data "local_file" "repo_permissions" {
47
+ filename = " ${ path . module } /repo_permissions.json"
48
+ }
49
+
50
+ locals {
51
+ repo_permissions = jsondecode (data. local_file . repo_permissions . content )
52
+ }
53
+
54
+
55
+ resource "github_organization_settings" "org_settings" {
56
+ billing_email = " [email protected] "
57
+ company = " DevKor"
58
+ blog = " https://devkor.club"
59
+
60
+ location = " Seoul, Korea"
61
+ name = " DevKor"
62
+ description = " 고려대학교 SW 프로덕트 학회 DevKor Github Organization"
63
+ has_organization_projects = true
64
+ has_repository_projects = true
65
+ members_can_create_repositories = false
66
+ members_can_create_private_pages = false
67
+
68
+ advanced_security_enabled_for_new_repositories = true
69
+ dependabot_alerts_enabled_for_new_repositories = true
70
+ dependabot_security_updates_enabled_for_new_repositories = true
71
+ dependency_graph_enabled_for_new_repositories = true
72
+ secret_scanning_enabled_for_new_repositories = true
73
+ secret_scanning_push_protection_enabled_for_new_repositories = true
74
+ }
75
+
76
+
77
+ # user 초대
78
+ resource "github_membership" "user" {
79
+ for_each = { for user in local . users : user . user => user }
80
+
81
+ username = each. value . user
82
+ role = each. value . role
83
+ }
84
+
85
+ # team 생성
86
+ resource "github_team" "team" {
87
+ for_each = { for team in local . teams : team . name => team }
88
+
89
+ name = each. key
90
+ description = " DevKor ${ each . key } team"
91
+ privacy = " closed"
92
+ }
93
+
94
+ # 팀별 2 repositories 생성
95
+ resource "github_repository" "repo" {
96
+ for_each = { for repo in local . repos : repo . name => repo }
97
+
98
+
99
+ name = each. key
100
+ description = " DevKor ${ each . key } repository"
101
+ visibility = " public"
102
+ has_projects = true
103
+ has_wiki = true
104
+ has_downloads = true
105
+ has_issues = true
106
+ has_discussions = true
107
+
108
+ topics = [" devkor" ]
109
+ license_template = " MIT"
110
+
111
+ archive_on_destroy = true
112
+ vulnerability_alerts = true
113
+
114
+ security_and_analysis {
115
+ secret_scanning {
116
+ status = " enabled"
117
+ }
118
+ secret_scanning_push_protection {
119
+ status = " enabled"
120
+ }
121
+ }
122
+
123
+ }
124
+ # team - repo permission
125
+ resource "github_team_repository" "team_repos" {
126
+ for_each = { for permission in local . repo_permissions : " ${ permission . team } :${ permission . repo } " => permission }
127
+ team_id = github_team. team [each . value . team ]. id
128
+ repository = each. value . repo
129
+ permission = each. value . permission
130
+ }
131
+
132
+
133
+ # main branch must have Reviews
134
+ resource "github_organization_ruleset" "review_ruleset" {
135
+ name = " restrict-repo-deletion"
136
+ target = " branch"
137
+
138
+ enforcement = " active"
139
+
140
+ conditions {
141
+ ref_name {
142
+ include = [ " main" , " deploy" ]
143
+ exclude = []
144
+ }
145
+ repository_name {
146
+ include = [" ~ALL" ]
147
+ exclude = []
148
+ }
149
+ }
150
+
151
+ rules {
152
+ pull_request {
153
+ required_approving_review_count = 1
154
+ require_last_push_approval = true
155
+ }
156
+
157
+ }
158
+ }
159
+
160
+
161
+ # PR -> discord webhook
162
+ resource "github_repository_webhook" "discord_pr_webhook" {
163
+ for_each = { for repo in local . repos : repo . name => repo }
164
+
165
+ repository = each. value
166
+
167
+ configuration {
168
+ url = var. discord_webhook_url
169
+ content_type = " json"
170
+ insecure_ssl = false
171
+ }
172
+
173
+ events = [" pull_request" , " pull_request_review" , " pull_request_review_comment" ]
174
+ }
0 commit comments