-
Notifications
You must be signed in to change notification settings - Fork 0
/
reset-repo-settings.sh
129 lines (117 loc) · 3.45 KB
/
reset-repo-settings.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
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
#!/bin/sh
# shellcheck disable=SC2086
if ! gh auth status >/dev/null 2>&1; then
echo "GitHub CLI is not authenticated. Please authenticate using 'gh auth login' and try again."
exit 1
fi
# Common headers and endpoint
headers="-H Accept:application/vnd.github json -H X-GitHub-Api-Version:2022-11-28"
owner=$(gh api user | jq -r .login)
repo=$(git config --get remote.origin.url | sed -E 's/.*\/([^/] )\.git/\1/')
repo_endpoint="repos/$owner/$repo"
# Repository settings
repo_settings='{
"name": "monorepo",
"description": "Generic monorepo template",
"homepage": "arifbalik.github.io/monorepo/",
"private": false,
"visibility": "public",
"security_and_analysis": {
"secret_scanning": {
"status": "enabled"
},
"secret_scanning_push_protection": {
"status": "enabled"
},
"dependabot_security_updates": {
"status": "enabled"
},
"secret_scanning_non_provider_patterns": {
"status": "enabled"
},
"secret_scanning_validity_checks": {
"status": "enabled"
}
},
"has_issues": true,
"has_projects": true,
"has_wiki": true,
"is_template": true,
"default_branch": "main",
"allow_squash_merge": true,
"allow_merge_commit": false,
"allow_rebase_merge": false,
"allow_auto_merge": false,
"delete_branch_on_merge": true,
"allow_update_branch": true,
"squash_merge_commit_title": "COMMIT_OR_PR_TITLE",
"squash_merge_commit_message": "COMMIT_MESSAGES",
"archived": false,
"web_commit_signoff_required": true
}'
# Ruleset settings
ruleset_name="default ruleset"
ruleset_id=$(gh api "$repo_endpoint/rulesets" | jq -r ".[] | select(.name == \"$ruleset_name\") | .id")
rules='{
"name": "'$ruleset_name'",
"target": "branch",
"enforcement": "active",
"conditions": {
"ref_name": {
"exclude": [],
"include": ["~DEFAULT_BRANCH"]
}
},
"rules": [
{"type": "deletion"},
{"type": "non_fast_forward"},
{"type": "required_signatures"},
{
"type": "pull_request",
"parameters": {
"required_approving_review_count": 1,
"dismiss_stale_reviews_on_push": true,
"require_code_owner_review": false,
"require_last_push_approval": true,
"required_review_thread_resolution": true
}
},
{
"type": "required_status_checks",
"parameters": {
"strict_required_status_checks_policy": false,
"do_not_enforce_on_create": false,
"required_status_checks": [
{"context": "check-pr-size"},
{"context": "MegaLinter"},
{"context": "commitlint"}
]
}
}
],
"bypass_actors": [
{
"actor_id": 5,
"actor_type": "RepositoryRole",
"bypass_mode": "always"
}
]
}'
# Apply repo settings
echo "$repo_settings" | gh api --method PATCH $headers "$repo_endpoint" --input -
# Check and configure GitHub Pages
if ! gh api $headers "$repo_endpoint/pages"; then
gh api --method DELETE $headers "$repo_endpoint/pages"
fi
gh api --method POST $headers "$repo_endpoint/pages" -f "source[branch]=main" -f "source[path]=/"
# Apply ruleset
if [ -n "$ruleset_id" ]; then
gh api --method DELETE $headers "$repo_endpoint/rulesets/$ruleset_id"
fi
echo "$rules" | gh api --method POST $headers "$repo_endpoint/rulesets" --input -
# Check if the remote 'template' exists, if not add it
if ! git remote | grep -q '^template$'; then
git remote add template https://github.com/arifbalik/monorepo.git
fi
gh repo set-default "$owner/$repo"
git maintenance start