Skip to content

Commit 1e27175

Browse files
authored
Merge pull request # add-pr-detection-and-restriction
Add automatic PR detection and main branch protection
2 parents 0179ffb + 4ba9312 commit 1e27175

File tree

5 files changed

+194
-4
lines changed

5 files changed

+194
-4
lines changed

.github/CODEOWNERS

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# CODEOWNERS file for the Python_Git_exam repository
2+
#
3+
# This file defines individuals or teams that are responsible for code in this repository.
4+
# When a PR modifies files, the defined owners will be automatically requested for review.
5+
#
6+
# Pattern: path/to/files @username @org/team-name
7+
#
8+
# For exam purposes, all files should be reviewed before merging to main
9+
10+
# Default owners for everything in the repo
11+
* @guomics-lab
12+
13+
# Workflow files should be carefully reviewed
14+
/.github/workflows/ @guomics-lab
15+
16+
# Source code requires review
17+
/src/ @guomics-lab
18+
19+
# Tests require review
20+
/tests/ @guomics-lab
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Main Branch Protection
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
permissions:
12+
contents: read
13+
pull-requests: write
14+
issues: write
15+
16+
jobs:
17+
check-protection:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Check if push is via PR merge
21+
if: github.event_name == 'push'
22+
run: |
23+
echo "::warning::Direct push to main branch detected"
24+
echo "⚠️ All changes to the main branch should come through Pull Requests."
25+
echo "📖 Please refer to BRANCH_PROTECTION.md for proper workflow guidelines."
26+
echo "🔧 Repository administrators should configure branch protection rules in Settings → Branches"
27+
28+
- name: PR Protection Check
29+
if: github.event_name == 'pull_request'
30+
run: |
31+
echo "✅ Changes are being submitted via Pull Request - this is the correct workflow!"
32+
echo "PR #${{ github.event.pull_request.number }}: ${{ github.event.pull_request.title }}"
33+
echo "Author: ${{ github.event.pull_request.user.login }}"

BRANCH_PROTECTION.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# 分支保护规则说明 (Branch Protection Rules)
2+
3+
## 概述
4+
本仓库已配置分支保护机制,确保代码质量和规范的协作流程。
5+
6+
## 保护规则
7+
8+
### 1. Main 分支保护
9+
- **禁止直接推送到 main 分支**
10+
- 所有更改必须通过 Pull Request (PR) 提交
11+
- PR 必须通过所有 CI 检查才能合并
12+
13+
### 2. 必需的 CI 检查
14+
在 PR 合并到 main 分支之前,必须通过以下检查:
15+
- ✅ Ruff (代码质量检查)
16+
- ✅ Black (代码格式化检查)
17+
- ✅ isort (导入排序检查)
18+
- ✅ Pytest (单元测试)
19+
20+
### 3. 代码审查要求
21+
- 建议启用代码审查(Code Review)要求
22+
- PR 需要至少一位审查者批准(在仓库设置中配置)
23+
24+
## 工作流程
25+
26+
### 正确的提交流程 ✅
27+
```bash
28+
# 1. 从 main 分支创建新分支
29+
git checkout main
30+
git pull origin main
31+
git checkout -b fix/your-feature
32+
33+
# 2. 进行代码修改
34+
# ... 修改文件 ...
35+
36+
# 3. 提交更改
37+
git add .
38+
git commit -m "fix: your change description"
39+
40+
# 4. 推送到远程分支
41+
git push -u origin fix/your-feature
42+
43+
# 5. GitHub Actions 会自动创建 PR(如果启用了 auto-pr.yml)
44+
# 或者手动在 GitHub 网站上创建 PR
45+
46+
# 6. 等待 CI 检查通过
47+
# 7. 在 GitHub 网站上合并 PR
48+
```
49+
50+
### 错误的提交流程 ❌
51+
```bash
52+
# ❌ 不要直接推送到 main 分支
53+
git checkout main
54+
git add .
55+
git commit -m "some changes"
56+
git push origin main # 这将被拒绝或触发保护警告
57+
```
58+
59+
## 启用严格保护(需要仓库管理员操作)
60+
61+
要完全阻止直接推送到 main 分支,需要在 GitHub 仓库设置中配置:
62+
63+
1. 进入仓库的 **Settings****Branches**
64+
2. 添加分支保护规则(Branch protection rule)
65+
3. 规则名称:`main`
66+
4. 启用以下选项:
67+
-**Require a pull request before merging**
68+
- ✅ Require approvals (建议至少 1 个审查者)
69+
-**Require status checks to pass before merging**
70+
- ✅ Require branches to be up to date before merging
71+
- 添加必需的检查:
72+
- `grade (lint + format + tests)` (来自 grading-ci.yml)
73+
-**Do not allow bypassing the above settings**
74+
- ⚠️ **Include administrators** (可选,但建议启用以确保规则一致性)
75+
76+
## 自动化特性
77+
78+
### 自动创建 PR
79+
本仓库配置了 `auto-pr.yml` 工作流:
80+
- 当您推送到非 main 分支时,会自动创建 PR
81+
- PR 会包含标准的检查清单
82+
- 节省手动创建 PR 的时间
83+
84+
### 自动运行检查
85+
- 每次 PR 更新时,自动运行所有质量检查
86+
- 检查结果显示在 PR 页面
87+
- 只有全部通过才能合并
88+
89+
## 常见问题
90+
91+
### Q: 我不小心推送到了 main 分支怎么办?
92+
A: 如果分支保护已正确配置,推送会被拒绝。如果推送成功了,说明需要在仓库设置中启用更严格的保护规则。
93+
94+
### Q: 如何查看 CI 检查结果?
95+
A: 在 PR 页面的 "Checks" 标签中可以看到所有检查的详细结果。
96+
97+
### Q: 所有检查都通过了,但无法合并?
98+
A: 检查是否启用了代码审查要求,可能需要其他人批准您的 PR。
99+
100+
## 参考资料
101+
- [GitHub 分支保护文档](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches)
102+
- [GitHub Actions 工作流文档](https://docs.github.com/en/actions/using-workflows)

EXAMINER_NOTES.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@
1717
## 3. 出题上线流程(建议)
1818
1. 将该仓库设置为模板仓库(GitHub Template Repository)
1919
2. 考生从模板生成自己的考试仓库(或你们系统自动创建)
20-
3. 要求考生在规定时间内提交 PR
21-
4. CI 自动给出是否通过
22-
5. 管理端收集 PR 链接与 CI 状态,进行最终评审
20+
3. **配置仓库的分支保护规则**(重要):
21+
- Settings → Branches → Add branch protection rule
22+
- Branch name pattern: `main`
23+
- 启用以下选项:
24+
- ✅ Require a pull request before merging
25+
- ✅ Require status checks to pass before merging
26+
- 添加必需检查:`grade (lint + format + tests)`
27+
- ✅ Do not allow bypassing the above settings
28+
- 详细配置请参考 `BRANCH_PROTECTION.md`
29+
4. 要求考生在规定时间内提交 PR
30+
5. CI 自动给出是否通过
31+
6. 管理端收集 PR 链接与 CI 状态,进行最终评审
32+
33+
## 4. 自动化功能说明
34+
- **自动 PR 创建**`auto-pr.yml` 会在学生推送分支时自动创建 PR
35+
- **自动 CI 检查**`grading-ci.yml` 会在 PR 上自动运行所有检查
36+
- **分支保护监控**`main-protection.yml` 监控对 main 分支的操作
37+
- **代码审查要求**`.github/CODEOWNERS` 定义了代码审查者

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,30 @@ CI 会检查:
2222
- 进行至少 1 次 commit(建议 1~3 次,小步提交)
2323
- 提交信息建议格式:`fix: make code pep8 compliant`(也可使用你们团队规范)
2424
4. **发起 Pull Request**
25+
- **⚠️ 注意:本仓库禁止直接推送到 `main` 分支,所有更改必须通过 PR 提交**
26+
- 推送分支后,系统会自动创建 PR(或手动创建)
2527
- PR 标题清晰说明做了什么
2628
- PR 描述中写明:你修复了哪些类型的问题(例如:imports/formatting/naming/docstring)
29+
- 等待 GitHub Actions 自动运行所有检查
2730

2831
## 三、通过标准
2932
- GitHub Actions 绿灯(全部 job 通过)
3033
- PR 内容清晰、可读
3134
- 不引入功能回归(pytest 通过)
3235

36+
## 📋 自动化功能
37+
本仓库已配置以下自动化功能:
38+
39+
### ✅ 自动 PR 检测
40+
- 当你推送分支后,系统会**自动创建 Pull Request**
41+
- PR 会自动触发 CI 检查(ruff、black、isort、pytest)
42+
- 检查结果会显示在 PR 页面,全部通过后显示绿色 ✅
43+
44+
### 🔒 Main 分支保护
45+
- **禁止直接推送到 `main` 分支**
46+
- 所有代码更改必须通过 Pull Request 提交
47+
- 详细的分支保护规则请参考 [BRANCH_PROTECTION.md](BRANCH_PROTECTION.md)
48+
3349
## 四、本地运行建议
3450
1、创建环境(建议使用 Python 3.11)
3551

@@ -56,7 +72,11 @@ isort --check-only .
5672
git add .
5773
git commit -m "fix: pep8 refactor"
5874
git push -u origin fix/user (请将user替换为实际的用户名)
59-
PR 页面等待 GitHub Actions 执行,看到 grading-ci / grade (lint + format + tests) 为绿色即通过。
75+
76+
# ✅ 系统会自动创建 PR
77+
# 在 GitHub 网站上查看 PR 页面,等待 GitHub Actions 执行
78+
# 看到 grading-ci / grade (lint + format + tests) 为绿色即通过
79+
# ⚠️ 注意:不要直接推送到 main 分支,必须通过 PR 合并
6080

6181
```
6282

0 commit comments

Comments
 (0)