File size: 2,145 Bytes
184a5a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash

# Branch Protection Setup Script
# Run this script to automatically configure branch protection

echo "πŸ›‘οΈ Setting up branch protection for algorithmic trading repository..."

# Configuration
REPO="EAName/algorithmic_trading"
BRANCH="main"
REQUIRED_REVIEWS=2
REQUIRED_CHECKS='["ci-cd/quality-check","ci-cd/test","ci-cd/security","ci-cd/backtesting"]'

echo "πŸ“‹ Configuration:"
echo "  Repository: $REPO"
echo "  Branch: $BRANCH"
echo "  Required reviews: $REQUIRED_REVIEWS"
echo "  Required checks: $REQUIRED_CHECKS"

echo ""
echo "⚠️  IMPORTANT: You need a GitHub Personal Access Token with 'repo' permissions"
echo "   Get one from: https://github.com/settings/tokens"
echo ""

read -p "Enter your GitHub Personal Access Token: " GITHUB_TOKEN

if [ -z "$GITHUB_TOKEN" ]; then
    echo "❌ No token provided. Exiting."
    exit 1
fi

echo ""
echo "πŸ”§ Applying branch protection rules..."

# Apply branch protection
curl -X PUT \
  -H "Authorization: token $GITHUB_TOKEN" \
  -H "Accept: application/vnd.github.v3+json" \
  "https://api.github.com/repos/$REPO/branches/$BRANCH/protection" \
  -d "{
    \"required_status_checks\": {
      \"strict\": true,
      \"contexts\": $REQUIRED_CHECKS
    },
    \"enforce_admins\": true,
    \"required_pull_request_reviews\": {
      \"required_approving_review_count\": $REQUIRED_REVIEWS,
      \"dismiss_stale_reviews\": true,
      \"require_code_owner_reviews\": true
    },
    \"restrictions\": null,
    \"allow_force_pushes\": false,
    \"allow_deletions\": false
  }"

if [ $? -eq 0 ]; then
    echo ""
    echo "βœ… Branch protection successfully applied!"
    echo ""
    echo "πŸ“‹ Applied rules:"
    echo "  - Require pull request before merging"
    echo "  - Require $REQUIRED_REVIEWS approvals"
    echo "  - Require code owner reviews"
    echo "  - Require status checks: $REQUIRED_CHECKS"
    echo "  - No force pushes allowed"
    echo "  - No deletions allowed"
    echo ""
    echo "πŸ”— View settings: https://github.com/$REPO/settings/branches"
else
    echo ""
    echo "❌ Failed to apply branch protection. Check your token and permissions."
fi