/ fetchx / .github / workflows / branch-protection.yml
Swadhin Biswas feat: add multi-platform packaging and CI automation · 3/7/2026
492a637
80 lines · 2.7 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
name: Branch Protection Setup

on:
  workflow_dispatch:
    inputs:
      branch:
        description: 'Branch to protect'
        required: true
        default: 'main'
        type: string

jobs:
  setup-protection:
    name: Setup Branch Protection
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
      issues: write
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Configure Branch Protection
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          BRANCH: ${{ github.event.inputs.branch }}
        run: |
          # Set branch protection rules
          echo "Setting up branch protection for: $BRANCH"
          
          # Enable required reviews
          curl -X PUT "https://api.github.com/repos/${{ github.repository }}/branches/$BRANCH/protection" \
            -H "Authorization: token $GITHUB_TOKEN" \
            -H "Accept: application/vnd.github.v3+json" \
            -d '{
              "required_status_checks": {
                "strict": true,
                "contexts": ["ci/lint", "ci/test", "ci/coverage"]
              },
              "enforce_admins": true,
              "required_pull_request_reviews": {
                "dismiss_stale_reviews": true,
                "require_code_owner_reviews": true,
                "required_reviewers": 1,
                "dismiss_restrictions": []
              },
              "restrictions": null,
              "allow_force_pushes": false,
              "allow_deletions": false,
              "require_linear_history": true,
              "required_conversation_resolution": true
            }'
          
          echo "Branch protection configured successfully!"

      - name: Enable branch protection for PRs
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          # Require branches to be up to date before merging
          curl -X PUT "https://api.github.com/repos/${{ github.repository }}/branches/main/protection/required_status_checks" \
            -H "Authorization: token $GITHUB_TOKEN" \
            -H "Accept: application/vnd.github.v3+json" \
            -d '{
              "strict": true,
              "contexts": ["ci/lint", "ci/test", "ci/coverage"]
            }'
          
          echo "Required status checks configured!"

      - name: Configure branch protection rules
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          # Get current protection status
          curl -s "https://api.github.com/repos/${{ github.repository }}/branches/main/protection" \
            -H "Authorization: token $GITHUB_TOKEN" \
            -H "Accept: application/vnd.github.v3+json" | jq '.'