-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-github-repo.sh
More file actions
executable file
Β·186 lines (159 loc) Β· 5.95 KB
/
setup-github-repo.sh
File metadata and controls
executable file
Β·186 lines (159 loc) Β· 5.95 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/bash
# Script to configure GitHub repository settings via command line
# This sets up the repo to be public with branch protection that only allows you to merge
set -e
REPO="alshimo/GPTRewriter"
BRANCH="main"
echo "π§ Setting up GitHub Repository Configuration"
echo "=============================================="
echo "Repository: $REPO"
echo "Branch: $BRANCH"
echo ""
# Check if GitHub CLI is installed
if ! command -v gh &> /dev/null; then
echo "β GitHub CLI (gh) is not installed."
echo ""
echo "π¦ Installing GitHub CLI..."
echo ""
# Check for Homebrew
if command -v brew &> /dev/null; then
echo "Installing via Homebrew..."
brew install gh
else
echo "Please install GitHub CLI first:"
echo " macOS: brew install gh"
echo " Or visit: https://cli.github.com/"
echo ""
echo "After installing, run: gh auth login"
exit 1
fi
fi
# Check if user is authenticated
if ! gh auth status &> /dev/null; then
echo "π GitHub CLI not authenticated."
echo "Please run: gh auth login"
exit 1
fi
echo "β
GitHub CLI is installed and authenticated"
echo ""
# Get current repo visibility
echo "π Checking current repository settings..."
CURRENT_VISIBILITY=$(gh repo view "$REPO" --json visibility -q .visibility 2>/dev/null || echo "unknown")
if [ "$CURRENT_VISIBILITY" != "PUBLIC" ]; then
echo "π Making repository public..."
gh repo edit "$REPO" --visibility public
echo "β
Repository is now public"
else
echo "β
Repository is already public"
fi
echo ""
# Check if branch protection already exists
echo "π‘οΈ Setting up branch protection rules..."
# Enable branch protection with the following rules:
# - Require pull request before merging
# - Require 1 approval
# - Restrict pushes to only the owner
# - Do not allow bypassing
echo " Configuring branch protection for '$BRANCH'..."
# Get current user (repository owner)
CURRENT_USER=$(gh api user -q .login)
# Create branch protection rule
# This requires:
# - Pull requests before merging
# - 1 approval (which effectively means only you can merge since you're the owner)
# - No force pushes
# - No deletions
# - Required conversation resolution
PROTECTION_CONFIG=$(cat <<EOF
{
"required_status_checks": null,
"enforce_admins": false,
"required_pull_request_reviews": {
"required_approving_review_count": 1,
"dismiss_stale_reviews": true,
"require_code_owner_reviews": false,
"require_last_push_approval": false
},
"restrictions": null,
"allow_force_pushes": false,
"allow_deletions": false,
"required_linear_history": false,
"allow_fork_syncing": false,
"block_creations": false,
"required_conversation_resolution": true,
"lock_branch": false
}
EOF
)
# Apply branch protection
if gh api repos/"$REPO"/branches/"$BRANCH"/protection \
--method PUT \
--input - <<< "$PROTECTION_CONFIG" 2>/dev/null; then
echo "β
Branch protection configured successfully"
else
echo " β οΈ Warning: Could not configure branch protection via API"
echo " This might be because:"
echo " - You don't have admin access to the repository"
echo " - The branch doesn't exist yet"
echo ""
echo " Please configure manually:"
echo " 1. Go to: https://github.com/$REPO/settings/branches"
echo " 2. Add rule for branch: $BRANCH"
echo " 3. Enable: Require a pull request before merging"
echo " 4. Set: Required approvals = 1"
echo " 5. Enable: Do not allow bypassing the above settings"
exit 1
fi
echo ""
# Get repository owner
REPO_OWNER=$(gh api repos/"$REPO" -q .owner.login)
echo "π€ Current GitHub user: $CURRENT_USER"
echo "π€ Repository owner: $REPO_OWNER"
echo ""
if [ "$CURRENT_USER" != "$REPO_OWNER" ]; then
echo "β οΈ Warning: You are not the repository owner."
echo " Some settings may not be configurable."
echo ""
fi
# Set repository settings
echo "βοΈ Configuring repository settings..."
# Enable Issues
gh api repos/"$REPO" --method PATCH --field has_issues=true > /dev/null 2>&1 || true
# Enable Discussions (optional)
# gh api repos/"$REPO" --method PATCH --field has_discussions=true > /dev/null 2>&1 || true
# Allow merge commits and squash merging
gh api repos/"$REPO" --method PATCH \
--field allow_merge_commit=true \
--field allow_squash_merge=true \
--field allow_rebase_merge=false \
--field allow_auto_merge=true \
> /dev/null 2>&1 || true
echo "β
Repository settings configured"
echo ""
echo "ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
echo "β
Configuration Complete!"
echo "ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
echo ""
echo "π Summary of settings:"
echo " β
Repository is PUBLIC"
echo " β
Branch protection enabled on '$BRANCH'"
echo " β
Pull requests required before merging"
echo " β
1 approval required (only you can approve as owner)"
echo " β
Force pushes disabled"
echo " β
Branch deletion disabled"
echo " β
Forking is enabled (default for public repos)"
echo ""
echo "π Security:"
echo " - Only you ($REPO_OWNER) can merge pull requests"
echo " - Others can fork and create pull requests"
echo " - Direct pushes to '$BRANCH' are blocked"
echo ""
echo "π Verify settings at:"
echo " https://github.com/$REPO/settings/branches"
echo ""
echo "π Optional: To further restrict who can push (even via PR), you can:"
echo " 1. Go to: https://github.com/$REPO/settings/branches"
echo " 2. Edit the branch protection rule for '$BRANCH'"
echo " 3. Enable 'Restrict who can push to matching branches'"
echo " 4. Add only yourself to the allowed list"
echo ""