Skip to content

Commit 844892e

Browse files
authored
Add django-tasks-scheduler project (#31)
1 parent bb7d6c7 commit 844892e

10 files changed

+251
-91
lines changed

.github/workflows/plan.yml

+27-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,36 @@ on:
77
- 'terraform/production/*.tfvars'
88

99
jobs:
10+
format-terraform-code:
11+
name: "Format Terraform code"
12+
runs-on: ubuntu-latest
13+
permissions:
14+
contents: write
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v4
18+
with:
19+
ref: "${{ github.event.pull_request.head.ref }}"
20+
21+
22+
- name: terraform fmt
23+
uses: dflook/terraform-fmt@2ec321e746af7edf90e43513dda2086a92a07b4c
24+
with:
25+
path: "terraform"
26+
27+
- name: Commit changes
28+
uses: devops-infra/[email protected]
29+
with:
30+
github_token: "${{ secrets.GITHUB_TOKEN }}"
31+
commit_prefix: "[AUTO]"
32+
commit_message: "Format code"
33+
force: false
34+
# target_branch: "${{ github.event.pull_request.head.ref }}"
35+
1036
plan-changes:
1137
name: "Org changes plan"
1238
runs-on: ubuntu-latest
13-
39+
needs: [ "format-terraform-code" ]
1440
permissions:
1541
pull-requests: write
1642
contents: write

README.md

+61-30
Original file line numberDiff line numberDiff line change
@@ -112,39 +112,68 @@ Django Commons packages.
112112

113113
### Terraform changes to add a new project
114114

115+
#### Using the GitHub UI
116+
117+
1. Transfer the existing repository to the Django Commons organization using the GitHub UI, so old information is
118+
preserved.
119+
2. Make sure the there are no teams `repo-name`, `repo-name-admins` and `repo-name-committers` in the Django Commons
120+
organization. Teams can be viewed [here](https://github.com/orgs/django-commons/teams). The teams will be created by
121+
the terraform apply process.
122+
123+
#### Locally
124+
115125
Assuming repository name is `repo-name`:
116126

117-
1. In [`terraform/production/respositories.tfvars`](https://github.com/django-commons/controls/blob/main/terraform/production/respositories.tfvars),
118-
add the new repository to the `repositories` section:
127+
1.
128+
In [`terraform/production/respositories.tfvars`](https://github.com/django-commons/controls/blob/main/terraform/production/respositories.tfvars),
129+
add the new repository to the `repositories` section:
119130

120131
```terraform
121132
repositories = {
122-
# ...
123-
"repo-name" = {
124-
description = "repo description"
125-
allow_auto_merge = false # optional, default is false
126-
allow_merge_commit = false # optional, default is false
127-
allow_rebase_merge = false # optional, default is false
128-
allow_squash_merge = false # optional, default is false
129-
allow_update_branch = false # optional, default is false
130-
enable_branch_protection = true # optional, default is true
131-
has_discussions = true # optional, default is true
132-
has_downloads = true # optional, default is true
133-
has_wiki = false # optional, default is false
134-
is_template = false # optional, default is false
135-
push_allowances = []
136-
required_status_checks_contexts = [] # optional, default is []
137-
template = "" # optional, default is ""
138-
topics = []
139-
visibility = "public" # optional, default is "public"
140-
skip_team_creation = false # Optional, default is false => create 3 teams for the repository
141-
admins = [] # Members of the repository's admin and repository teams. Have admin permissions
142-
committers = [] # Members of the repository's committers and repository teams. Have write permissions
143-
members = [] # Members of the repository team. Have triage permissions
144-
}
133+
# ...
134+
"repo-name" = {
135+
description = "repo description"
136+
homepage_url = "" # optional, default is ""
137+
allow_auto_merge = false # optional, default is false
138+
allow_merge_commit = false # optional, default is false
139+
allow_rebase_merge = false # optional, default is false
140+
allow_squash_merge = true # optional, default is true
141+
allow_update_branch = true # optional, default is true
142+
delete_branch_on_merge = true # optional, default is true
143+
has_discussions = true # optional, default is true
144+
has_downloads = true # optional, default is true
145+
has_wiki = false # optional, default is false
146+
is_template = false # optional, default is false
147+
push_allowances = []
148+
template = "" # optional, default is ""
149+
topics = []
150+
visibility = "public" # optional, default is "public"
151+
skip_team_creation = optional(bool, false) # Do not create teams for repository
152+
enable_branch_protection = true # optional, default is true
153+
required_status_checks_contexts = [] # optional, default is []
154+
admins = [] # Members of the repository's admin and repository teams. Have admin permissions
155+
committers = [] # Members of the repository's committers and repository teams. Have write permissions
156+
members = [] # Members of the repository team. Have triage permissions
157+
}
145158
}
146159
```
147160

161+
2. Import the repository into the terraform state by
162+
running `terraform import -var-file=production/org.tfvars -var-file=production/repositories.tfvars -var github_token=... 'github_repository.this["repo-name"]' repo-name`
163+
3. Run `terraform plan -var-file=production/org.tfvars -var-file=production/repositories.tfvars -var github_token=...`
164+
to see the changes that will be made.
165+
166+
4. Create a pull-request to `main` branch. This will trigger terraform to plan the changes in the organization to be
167+
executed.
168+
Review the changes and make sure they align with the request.
169+
5. Merge the pull request. This will trigger terraform to apply the changes in the organization.
170+
171+
The expected changes:
172+
173+
- New teams `repo-name`, `repo-name-admins`, `repo-name-committers` with the relevant members based on the
174+
repository's description.
175+
- The repository changes are accepted by the project maintainers.
176+
148177
## Remove Project Playbook
149178

150179
1. Confirm there's agreement amongst current project maintainers to move project out of Django Commons
@@ -158,10 +187,12 @@ repositories = {
158187

159188
1. Remove the repository from the `repositories` section
160189
in [`terraform/production/respositories.tfvars`](https://github.com/django-commons/controls/blob/main/terraform/production/respositories.tfvars)
161-
2. Remove the parent team and child teams for the repository from the `teams_repositories`
162-
and `teams_repositories_privileged` sections in
163-
[`terraform/production/teams.tfvars`](https://github.com/django-commons/controls/blob/main/terraform/production/teams.tfvars)
164-
3. Create a pull-request to `main` branch. This will trigger terraform to plan the changes in the organization to be
190+
2. Create a pull-request to `main` branch. This will trigger terraform to plan the changes in the organization to be
165191
executed.
166192
Review the changes and make sure they align with the request.
167-
4. Merge the pull request. This will trigger terraform to apply the changes in the organization.
193+
3. Merge the pull request. This will trigger terraform to apply the changes in the organization.
194+
195+
The expected changes:
196+
197+
- The repository will be removed from the organization.
198+
- The repository's teams will be removed from the organization.

terraform/README.md

+13-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ GitHub Organization as Terraform
1010
- `production/*.tfvars` - instances, should strictly follow the types in `variables.tf`.
1111
- `main.tf` - build configuration based on instances values from `production.tfvars` (or, if not defined explicitly,
1212
then default value from `variables.tf`)
13+
- `resources-*.tf` - define resources, like `github_repository`, `github_team`, etc.
1314
- `tfstate.json` - Current state file, pulled using `terraform import ..`
1415

1516
# Why Terraform?
@@ -34,24 +35,27 @@ All changes should be made in `production/*.tfvars`:
3435
repositories = {
3536
"repo-name" = {
3637
description = "repo description"
38+
homepage_url = "" # optional, default is ""
3739
allow_auto_merge = false # optional, default is false
3840
allow_merge_commit = false # optional, default is false
3941
allow_rebase_merge = false # optional, default is false
40-
allow_squash_merge = false # optional, default is false
41-
allow_update_branch = false # optional, default is false
42-
enable_branch_protection = true # optional, default is true
42+
allow_squash_merge = true # optional, default is true
43+
allow_update_branch = true # optional, default is true
44+
delete_branch_on_merge = true # optional, default is true
4345
has_discussions = true # optional, default is true
4446
has_downloads = true # optional, default is true
4547
has_wiki = false # optional, default is false
4648
is_template = false # optional, default is false
4749
push_allowances = []
48-
required_status_checks_contexts = [] # optional, default is []
4950
template = "" # optional, default is ""
5051
topics = []
51-
visibility = "public" # optional, default is "public"
52+
visibility = "public" # optional, default is "public"
5253
skip_team_creation = optional(bool, false) # Do not create teams for repository
53-
admins = optional(set(string), []) # Members of the repository admin team
54-
committers = optional(set(string), []) # Members of the repository committers team
54+
enable_branch_protection = true # optional, default is true
55+
required_status_checks_contexts = [] # optional, default is []
56+
admins = [] # Members of the repository's admin and repository teams. Have admin permissions
57+
committers = [] # Members of the repository's committers and repository teams. Have write permissions
58+
members = [] # Members of the repository team. Have triage permissions
5559
}
5660
# ...
5761
}
@@ -72,6 +76,7 @@ To do so, you can use the following steps:
7276
4. Make changes to `production/*.tfvars` to reflect the desired state (add/update users, repositories, teams, etc.)
7377
5. To see what changes between the current state of the GitHub organization and the plan
7478
run: `terraform plan -var-file=production/org.tfvars -var-file=production/repositories.tfvars -var github_token=...`
75-
6. To apply the changes, run: `terraform apply -var-file=production/org.tfvars -var-file=production/repositories.tfvars -var github_token=...`
79+
6. To apply the changes,
80+
run: `terraform apply -var-file=production/org.tfvars -var-file=production/repositories.tfvars -var github_token=...`
7681
7782
[1]: https://developer.hashicorp.com/terraform/tutorials/it-saas/github-user-teams#configure-your-credentials

terraform/production/repositories.tfvars

+22-11
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ repositories = {
66
description = "A Special Repository."
77
enable_branch_protection = false
88

9-
topics = []
10-
push_allowances = []
9+
topics = []
10+
push_allowances = []
1111
skip_team_creation = true
1212
}
1313

1414
"controls" = {
1515
description = "The controls for managing Django Commons projects"
1616
enable_branch_protection = false
17-
allow_merge_commit = true
18-
allow_rebase_merge = true
19-
allow_squash_merge = true
20-
topics = []
21-
push_allowances = []
22-
skip_team_creation = true
17+
allow_merge_commit = true
18+
allow_rebase_merge = true
19+
allow_squash_merge = true
20+
topics = []
21+
push_allowances = []
22+
skip_team_creation = true
2323
}
2424

2525
"membership" = {
@@ -28,14 +28,14 @@ repositories = {
2828
allow_merge_commit = true
2929
allow_rebase_merge = true
3030
allow_squash_merge = true
31-
topics = []
32-
push_allowances = []
31+
topics = []
32+
push_allowances = []
3333
skip_team_creation = true
3434
}
3535

3636
"django-commons-playground" = {
3737
description = "A sample project to test things out"
38-
topics = []
38+
topics = []
3939
# People with GitHub admin repo permissions
4040
admins = [
4141
"tim-schilling",
@@ -52,4 +52,15 @@ repositories = {
5252
members = [
5353
]
5454
}
55+
56+
"django-tasks-scheduler" = {
57+
description = "Schedule async tasks using redis pub/sub."
58+
homepage_url = "https://django-tasks-scheduler.readthedocs.io/"
59+
60+
admins = [
61+
"cunla",
62+
]
63+
committers = []
64+
members = []
65+
}
5566
}

terraform/resources-repo-admin-teams.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Define the admin team for each repository
22
resource "github_team" "repo_admin_team" {
3-
for_each = {for k, v in var.repositories : k => v if v.skip_team_creation == false}
3+
for_each = { for k, v in var.repositories : k => v if v.skip_team_creation == false }
44

55
parent_team_id = github_team.repo_team[each.key].id
66
name = "${each.key}-admins"
@@ -10,7 +10,7 @@ resource "github_team" "repo_admin_team" {
1010

1111
# Add the people to the team
1212
resource "github_team_members" "repo_admin_members" {
13-
for_each = {for k, v in var.repositories : k => v if v.skip_team_creation == false}
13+
for_each = { for k, v in var.repositories : k => v if v.skip_team_creation == false }
1414

1515
team_id = github_team.repo_admin_team[each.key].id
1616

@@ -26,7 +26,7 @@ resource "github_team_members" "repo_admin_members" {
2626

2727
# Define the team's permissions for the repositories
2828
resource "github_team_repository" "repo_admin_team_access" {
29-
for_each = {for k, v in var.repositories : k => v if v.skip_team_creation == false}
29+
for_each = { for k, v in var.repositories : k => v if v.skip_team_creation == false }
3030
repository = each.key
3131
team_id = github_team.repo_admin_team[each.key].id
3232
permission = "admin"

terraform/resources-repo-committer-teams.tf

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Define the committers team for each repository
22
resource "github_team" "repo_committer_team" {
3-
for_each = {for k, v in var.repositories : k => v if v.skip_team_creation == false}
3+
for_each = { for k, v in var.repositories : k => v if v.skip_team_creation == false }
44

55
parent_team_id = github_team.repo_team[each.key].id
66
name = "${each.key}-committers"
@@ -10,7 +10,10 @@ resource "github_team" "repo_committer_team" {
1010

1111
# Add the people to the team
1212
resource "github_team_members" "repo_committer_team_members" {
13-
for_each = {for k, v in var.repositories : k => v if v.skip_team_creation == false}
13+
for_each = {
14+
for k, v in var.repositories : k => v
15+
if v.skip_team_creation == false && length(v.committers) > 0
16+
}
1417

1518
team_id = github_team.repo_committer_team[each.key].id
1619

@@ -19,13 +22,17 @@ resource "github_team_members" "repo_committer_team_members" {
1922

2023
content {
2124
username = members.value
22-
role = contains(var.admins, members.value) ? "maintainer" : "member"
25+
role = contains(each.value.admins, members.value) ? "maintainer" : "member"
2326
}
2427
}
2528
}
29+
2630
# Define the team's permissions for the repositories
2731
resource "github_team_repository" "repo_committer_team_access" {
28-
for_each = {for k, v in var.repositories : k => v if v.skip_team_creation == false}
32+
for_each = {
33+
for k, v in var.repositories : k => v
34+
if v.skip_team_creation == false
35+
}
2936
repository = each.key
3037
team_id = github_team.repo_committer_team[each.key].id
3138
permission = "maintain"

terraform/resources-repo-teams.tf

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Create the main repository team for Django Commons.
22
resource "github_team" "repo_team" {
3-
for_each = {for k, v in var.repositories : k => v if v.skip_team_creation == false}
3+
for_each = { for k, v in var.repositories : k => v if v.skip_team_creation == false }
44

55
name = each.key
66
description = "Main team for the ${each.key} repository"
77
privacy = "closed"
88
}
99
# Add the people to the team
1010
resource "github_team_members" "repo_team_members" {
11-
for_each = {for k, v in var.repositories : k => v if v.skip_team_creation == false}
11+
for_each = { for k, v in var.repositories : k => v if v.skip_team_creation == false }
1212

1313
team_id = github_team.repo_team[each.key].id
1414

@@ -27,7 +27,7 @@ resource "github_team_members" "repo_team_members" {
2727
}
2828
# Define the team's permissions for the repositories
2929
resource "github_team_repository" "repo_team_access" {
30-
for_each = {for k, v in var.repositories : k => v if v.skip_team_creation == false}
30+
for_each = { for k, v in var.repositories : k => v if v.skip_team_creation == false }
3131
repository = each.key
3232
team_id = github_team.repo_team[each.key].id
3333
permission = "triage"
@@ -37,7 +37,7 @@ resource "github_team_repository" "repo_team_access" {
3737

3838
# This is used to enable automatic PR review requests
3939
resource "github_team_settings" "this" {
40-
for_each = {for k, v in var.repositories : k => v if v.skip_team_creation == false}
40+
for_each = { for k, v in var.repositories : k => v if v.skip_team_creation == false }
4141

4242
review_request_delegation {
4343
algorithm = "LOAD_BALANCE"

terraform/resources-repos.tf

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
resource "github_repository" "this" {
55
for_each = var.repositories
66

7+
homepage_url = each.value.homepage_url
78
allow_auto_merge = each.value.allow_auto_merge
89
allow_merge_commit = each.value.allow_merge_commit
910
merge_commit_title = each.value.merge_commit_title

0 commit comments

Comments
 (0)