|
1 | | -# Branch Protection Configuration |
| 1 | +# Branch Protection and Status Checks |
2 | 2 |
|
3 | | -This document provides instructions for configuring required status checks for the GitHub Actions CI pipeline. |
| 3 | +This document describes the branch protection rules and approval requirements configured for the usbipd-mac repository to ensure code quality and prevent broken code from being merged. |
4 | 4 |
|
5 | | -## Required Status Checks |
| 5 | +## Overview |
6 | 6 |
|
7 | | -The following status checks must be configured as required in the GitHub repository settings to ensure pull requests cannot be merged with failing checks: |
| 7 | +The `main` branch is protected with comprehensive rules that require all code changes to pass automated checks before merging. These protections ensure that: |
8 | 8 |
|
9 | | -### Status Check Names |
10 | | -Based on the CI workflow (`.github/workflows/ci.yml`), the following job names should be configured as required status checks: |
| 9 | +- Code meets quality standards (SwiftLint validation) |
| 10 | +- Project builds successfully on macOS |
| 11 | +- All unit tests pass |
| 12 | +- Integration tests with QEMU validate end-to-end functionality |
| 13 | +- Pull requests are reviewed before merging |
| 14 | +- Administrators cannot bypass checks without explicit approval |
11 | 15 |
|
12 | | -1. **Code Quality (SwiftLint)** - `lint` |
13 | | -2. **Build Validation** - `build` |
14 | | -3. **Unit Tests** - `test` |
15 | | -4. **Integration Tests (QEMU)** - `integration-test` |
| 16 | +## Required Status Checks |
16 | 17 |
|
17 | | -## Configuration Steps |
| 18 | +All pull requests must pass the following status checks before merging: |
18 | 19 |
|
19 | | -### Via GitHub Web Interface |
| 20 | +### 1. Code Quality (SwiftLint) |
| 21 | +- **Purpose**: Validates Swift code style and quality |
| 22 | +- **Requirements**: No SwiftLint violations in strict mode |
| 23 | +- **Configuration**: Uses project's `.swiftlint.yml` rules |
| 24 | +- **Failure Action**: Merge blocked until violations are fixed |
20 | 25 |
|
21 | | -1. Navigate to your repository on GitHub |
22 | | -2. Go to **Settings** → **Branches** |
23 | | -3. Click **Add rule** or edit the existing rule for the `main` branch |
24 | | -4. Enable **Require status checks to pass before merging** |
25 | | -5. Enable **Require branches to be up to date before merging** |
26 | | -6. In the status checks search box, add each of the following: |
27 | | - - `Code Quality (SwiftLint)` |
28 | | - - `Build Validation` |
29 | | - - `Unit Tests` |
30 | | - - `Integration Tests (QEMU)` |
31 | | -7. Enable **Require pull request reviews before merging** |
32 | | - - Set **Required number of reviewers before merging** to **1** |
33 | | - - Enable **Dismiss stale pull request approvals when new commits are pushed** |
34 | | - - Enable **Require review from code owners** (if CODEOWNERS file exists) |
35 | | -8. Enable **Restrict pushes that create files that do not exist in the current branch** |
36 | | -9. Enable **Do not allow bypassing the above settings** (enforces rules for administrators) |
37 | | -10. Disable **Allow force pushes** and **Allow deletions** for additional protection |
38 | | -11. Save the branch protection rule |
| 26 | +### 2. Build Validation |
| 27 | +- **Purpose**: Ensures project compiles successfully |
| 28 | +- **Requirements**: Swift Package Manager build must succeed |
| 29 | +- **Environment**: Latest macOS and Swift versions |
| 30 | +- **Failure Action**: Merge blocked until build errors are resolved |
39 | 31 |
|
40 | | -### Via Setup Script (Recommended) |
| 32 | +### 3. Unit Tests |
| 33 | +- **Purpose**: Validates functionality through automated tests |
| 34 | +- **Requirements**: All unit tests must pass |
| 35 | +- **Coverage**: USBIPDCore and USBIPDCLI test suites |
| 36 | +- **Failure Action**: Merge blocked until failing tests are fixed |
41 | 37 |
|
42 | | -The easiest way to configure branch protection is using the provided setup script: |
| 38 | +### 4. Integration Tests (QEMU) |
| 39 | +- **Purpose**: Validates end-to-end system functionality |
| 40 | +- **Requirements**: QEMU test server validation must succeed |
| 41 | +- **Coverage**: Complete protocol flow and network communication |
| 42 | +- **Failure Action**: Merge blocked until integration issues are resolved |
43 | 43 |
|
44 | | -```bash |
45 | | -# Run the automated setup script |
46 | | -./.github/scripts/setup-branch-protection.sh |
| 44 | +## Pull Request Requirements |
47 | 45 |
|
48 | | -# Validate the configuration |
49 | | -./.github/scripts/validate-branch-protection.sh |
50 | | -``` |
| 46 | +### Review Requirements |
| 47 | +- **Minimum reviewers**: 1 approved review required |
| 48 | +- **Stale review dismissal**: Enabled (reviews dismissed on new commits) |
| 49 | +- **Code owner reviews**: Enabled if CODEOWNERS file exists |
| 50 | +- **Last push approval**: Not required (allows self-approval after addressing feedback) |
51 | 51 |
|
52 | | -### Via GitHub CLI (Alternative) |
| 52 | +### Branch Requirements |
| 53 | +- **Up-to-date requirement**: Branches must be current with main before merging |
| 54 | +- **Linear history**: Merge commits or squash merging preferred |
| 55 | +- **Force push protection**: Force pushes to main branch are blocked |
53 | 56 |
|
54 | | -If you have GitHub CLI installed, you can configure branch protection using: |
| 57 | +## Administrator Bypass Rules |
55 | 58 |
|
56 | | -```bash |
57 | | -# Enable branch protection with required status checks and approval requirements |
58 | | -gh api repos/:owner/:repo/branches/main/protection \ |
59 | | - --method PUT \ |
60 | | - --field required_status_checks='{"strict":true,"contexts":["Code Quality (SwiftLint)","Build Validation","Unit Tests","Integration Tests (QEMU)"]}' \ |
61 | | - --field enforce_admins=true \ |
62 | | - --field required_pull_request_reviews='{"required_approving_review_count":1,"dismiss_stale_reviews":true,"require_code_owner_reviews":true}' \ |
63 | | - --field restrictions=null \ |
64 | | - --field allow_force_pushes=false \ |
65 | | - --field allow_deletions=false |
66 | | -``` |
| 59 | +### Bypass Permissions |
| 60 | +- **Administrator bypass**: Enabled for emergency situations |
| 61 | +- **Approval requirement**: Administrator approval required for bypassing checks |
| 62 | +- **Audit trail**: All bypass actions are logged and tracked |
| 63 | + |
| 64 | +### When Bypass May Be Used |
| 65 | +- Critical security fixes requiring immediate deployment |
| 66 | +- Infrastructure emergencies affecting CI/CD pipeline |
| 67 | +- Hotfixes for production-breaking issues |
| 68 | + |
| 69 | +### Bypass Process |
| 70 | +1. Administrator identifies need for bypass |
| 71 | +2. Documents reason for bypass in pull request |
| 72 | +3. Obtains explicit approval from another administrator |
| 73 | +4. Merges with bypass, ensuring immediate follow-up to address any issues |
67 | 74 |
|
68 | | -### Via GitHub API (Alternative) |
| 75 | +## Configuration Management |
69 | 76 |
|
70 | | -You can also configure branch protection using the GitHub REST API: |
| 77 | +### Automated Configuration |
| 78 | +Use the provided script to configure branch protection rules: |
71 | 79 |
|
72 | 80 | ```bash |
73 | | -curl -X PUT \ |
74 | | - -H "Accept: application/vnd.github.v3+json" \ |
75 | | - -H "Authorization: token YOUR_TOKEN" \ |
76 | | - https://api.github.com/repos/OWNER/REPO/branches/main/protection \ |
77 | | - -d '{ |
78 | | - "required_status_checks": { |
79 | | - "strict": true, |
80 | | - "contexts": [ |
81 | | - "Code Quality (SwiftLint)", |
82 | | - "Build Validation", |
83 | | - "Unit Tests", |
84 | | - "Integration Tests (QEMU)" |
85 | | - ] |
86 | | - }, |
87 | | - "enforce_admins": true, |
88 | | - "required_pull_request_reviews": { |
89 | | - "required_approving_review_count": 1, |
90 | | - "dismiss_stale_reviews": true, |
91 | | - "require_code_owner_reviews": true, |
92 | | - "require_last_push_approval": false |
93 | | - }, |
94 | | - "restrictions": null, |
95 | | - "allow_force_pushes": false, |
96 | | - "allow_deletions": false, |
97 | | - "block_creations": false |
98 | | - }' |
| 81 | +# Configure branch protection rules automatically |
| 82 | +./.github/scripts/configure-branch-protection.sh |
99 | 83 | ``` |
100 | 84 |
|
101 | | -## Verification |
| 85 | +### Manual Configuration |
| 86 | +1. Navigate to repository **Settings** → **Branches** |
| 87 | +2. Add or edit rule for `main` branch |
| 88 | +3. Configure settings as documented in `.github/branch-protection-config.md` |
| 89 | +4. Save and verify configuration |
102 | 90 |
|
103 | | -After configuring branch protection: |
| 91 | +### Validation |
| 92 | +Validate current configuration using the validation workflow: |
104 | 93 |
|
105 | | -1. Create a test pull request |
106 | | -2. Verify that the PR shows "Merging is blocked" until all checks pass |
107 | | -3. Confirm that each of the 4 status checks appears in the PR status section |
108 | | -4. Test that the PR can only be merged when all checks are green |
| 94 | +```bash |
| 95 | +# Trigger validation workflow manually |
| 96 | +gh workflow run validate-branch-protection.yml |
| 97 | +``` |
109 | 98 |
|
110 | | -## Status Check Behavior |
| 99 | +## Status Check Integration |
111 | 100 |
|
112 | | -With these settings configured: |
| 101 | +### GitHub Actions Integration |
| 102 | +- Status checks are automatically reported by CI workflow |
| 103 | +- Check names match job names in `.github/workflows/ci.yml` |
| 104 | +- Detailed status messages provide actionable feedback |
| 105 | +- Parallel execution optimizes feedback time |
113 | 106 |
|
114 | | -- **Pull requests cannot be merged** if any of the 4 required checks fail |
115 | | -- **Branches must be up to date** with main before merging |
116 | | -- **All 4 checks must pass** for the merge button to become available |
117 | | -- **At least 1 maintainer approval** is required before merging |
118 | | -- **Administrators cannot bypass** these requirements without explicit approval |
119 | | -- **Stale reviews are dismissed** when new commits are pushed |
120 | | -- **Status is clearly visible** in the PR interface showing which checks are pending/passing/failing |
121 | | -- **Force pushes and branch deletions** are blocked for additional protection |
| 107 | +### Status Reporting |
| 108 | +Each status check provides: |
| 109 | +- **Clear success/failure indication** |
| 110 | +- **Detailed error messages with line numbers** |
| 111 | +- **Actionable guidance for fixing issues** |
| 112 | +- **Links to relevant documentation** |
122 | 113 |
|
123 | 114 | ## Troubleshooting |
124 | 115 |
|
125 | | -If status checks are not appearing: |
126 | | -1. Ensure the workflow has run at least once on a PR |
127 | | -2. Check that job names in the workflow match the configured status check names exactly |
128 | | -3. Verify the workflow is triggered on `pull_request` events |
129 | | -4. Confirm the workflow file is in the correct location (`.github/workflows/ci.yml`) |
130 | | - |
131 | | -## Requirements Addressed |
132 | | - |
133 | | -This configuration addresses the following requirements: |
134 | | - |
135 | | -- **Requirement 6.1**: Pull requests with failing checks are prevented from merging |
136 | | -- **Requirement 6.2**: Pull requests with passing checks are allowed to merge (with maintainer approval) |
137 | | -- **Requirement 6.3**: Check status is clearly reported during execution (handled by workflow design) |
138 | | -- **Requirement 6.4**: Maintainer approval is required for bypassing checks (enforced via branch protection) |
| 116 | +### Common Issues |
139 | 117 |
|
140 | | -## Approval Requirements for Bypassing Checks |
141 | | - |
142 | | -The branch protection configuration includes specific settings to ensure maintainer oversight: |
| 118 | +#### Status Checks Not Required |
| 119 | +**Problem**: Pull requests can be merged despite failing checks |
| 120 | +**Solution**: Verify required status checks are configured correctly |
| 121 | +```bash |
| 122 | +# Validate configuration |
| 123 | +./.github/workflows/validate-branch-protection.yml |
| 124 | +``` |
143 | 125 |
|
144 | | -### Review Requirements |
145 | | -- **Required approving reviews**: 1 maintainer must approve before merging |
146 | | -- **Dismiss stale reviews**: Approvals are dismissed when new commits are pushed |
147 | | -- **Code owner reviews**: Required when CODEOWNERS file is present |
148 | | -- **Administrator enforcement**: Admins cannot bypass without following the approval process |
149 | | - |
150 | | -### Bypass Prevention |
151 | | -- **Enforce for administrators**: Prevents admins from bypassing protection rules |
152 | | -- **Force push protection**: Blocks force pushes that could bypass checks |
153 | | -- **Branch deletion protection**: Prevents accidental or malicious branch deletion |
154 | | - |
155 | | -### Approval Workflow |
156 | | -1. Developer creates pull request |
157 | | -2. All 4 status checks must pass (lint, build, unit tests, integration tests) |
158 | | -3. At least 1 maintainer must review and approve the changes |
159 | | -4. Branch must be up to date with main before merging |
160 | | -5. Only then can the pull request be merged |
161 | | - |
162 | | -This ensures that even if checks could theoretically be bypassed, maintainer approval acts as a safeguard to maintain code quality and project stability. |
| 126 | +#### Missing Status Checks |
| 127 | +**Problem**: Some CI jobs don't appear as required checks |
| 128 | +**Solution**: Ensure job names in workflow match required check names |
| 129 | +- Check `.github/workflows/ci.yml` job names |
| 130 | +- Verify names in branch protection settings match exactly |
| 131 | + |
| 132 | +#### Administrator Bypass Not Working |
| 133 | +**Problem**: Administrators cannot bypass checks when needed |
| 134 | +**Solution**: Verify bypass settings are properly configured |
| 135 | +- Ensure "Allow administrators to bypass" is enabled |
| 136 | +- Confirm approval requirements are set appropriately |
| 137 | + |
| 138 | +### Getting Help |
| 139 | + |
| 140 | +1. **Documentation**: Review `.github/branch-protection-config.md` |
| 141 | +2. **Validation**: Run validation workflow to check configuration |
| 142 | +3. **Manual Check**: Visit GitHub Settings → Branches to verify rules |
| 143 | +4. **Support**: Contact repository administrators for assistance |
| 144 | + |
| 145 | +## Best Practices |
| 146 | + |
| 147 | +### For Developers |
| 148 | +- Run checks locally before pushing: `swift test && swiftlint` |
| 149 | +- Keep pull requests focused and atomic |
| 150 | +- Address feedback promptly to avoid stale review dismissal |
| 151 | +- Ensure branches are up-to-date before requesting review |
| 152 | + |
| 153 | +### For Reviewers |
| 154 | +- Verify all status checks pass before approving |
| 155 | +- Review both code changes and test coverage |
| 156 | +- Consider impact on system integration and compatibility |
| 157 | +- Provide constructive feedback for improvement |
| 158 | + |
| 159 | +### For Administrators |
| 160 | +- Use bypass sparingly and only for genuine emergencies |
| 161 | +- Document bypass reasons thoroughly |
| 162 | +- Follow up on bypassed changes to ensure quality |
| 163 | +- Regularly review and update protection rules as needed |
| 164 | + |
| 165 | +## Compliance and Auditing |
| 166 | + |
| 167 | +### Audit Trail |
| 168 | +- All merge attempts are logged with status check results |
| 169 | +- Bypass actions are recorded with administrator approval |
| 170 | +- Pull request history maintains complete change tracking |
| 171 | +- CI workflow logs provide detailed execution records |
| 172 | + |
| 173 | +### Compliance Verification |
| 174 | +- Regular validation of branch protection configuration |
| 175 | +- Monitoring of bypass usage and justification |
| 176 | +- Review of status check effectiveness and coverage |
| 177 | +- Assessment of code quality trends and improvements |
0 commit comments