-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenforce.sh
executable file
·48 lines (38 loc) · 1.81 KB
/
enforce.sh
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
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
# The only required variable is the repository name
REPOSITORY="$1"
# Infer the owner from the current logged in GitHub user if not specified explicitely
OWNER="${2:-$(gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /user | jq -r -c '.login')}"
# Define a couple of helper functions
cleanup() {
rm -rf terraform.tfstate terraform.tfstate.backup
}
import_ruleset() {
RULESET_ID=$(gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/"$OWNER"/"$REPOSITORY"/rulesets | jq -c '.[] | select(.name=='\""$1"\"') | .id')
if [[ -n "${RULESET_ID}" ]]; then
terraform import -var owner="$OWNER" -var repository="$REPOSITORY" github_repository_ruleset."$1" "$REPOSITORY":"$RULESET_ID"
fi
}
# Use GitHub's CLI as an authentication helper
GITHUB_TOKEN=$(gh auth token)
export GITHUB_TOKEN
# Clean up any previous local state
cleanup
# Defer cleaning up local state once done
trap cleanup EXIT
# Initialize state
terraform init
# Import the repository if it already exists
REPOSITORY_ID=$(gh repo view "$REPOSITORY" --json id || echo '{"id": ""}' | jq -r -c '.id')
if [[ -n "${REPOSITORY_ID}" ]]; then
terraform import -var owner="$OWNER" -var repository="$REPOSITORY" github_repository.repository "$REPOSITORY"
terraform import -var owner="$OWNER" -var repository="$REPOSITORY" github_actions_repository_permissions.actions_permissions "$REPOSITORY"
terraform import -var owner="$OWNER" -var repository="$REPOSITORY" github_repository_dependabot_security_updates.dependabot_security_updates "$REPOSITORY"
# Import eventually defined rulesets
import_ruleset "releases"
import_ruleset "default"
fi
# Enforce settings
terraform apply -var owner="$OWNER" -var repository="$REPOSITORY"