From d5b2d3adca7c65c63a15c9cba56383b03f5dfa10 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 25 Dec 2025 03:04:14 +0000 Subject: [PATCH 1/3] Initial plan From 4c465fd58f973e08df5969f9cdb5b7e4442df926 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 25 Dec 2025 03:08:05 +0000 Subject: [PATCH 2/3] Add automatic PR detection and main branch protection Co-authored-by: zhiyuajun <156592628+zhiyuajun@users.noreply.github.com> --- .github/CODEOWNERS | 20 +++++ .github/workflows/main-protection.yml | 32 ++++++++ BRANCH_PROTECTION.md | 103 ++++++++++++++++++++++++++ EXAMINER_NOTES.md | 21 +++++- README.md | 22 +++++- 5 files changed, 194 insertions(+), 4 deletions(-) create mode 100644 .github/CODEOWNERS create mode 100644 .github/workflows/main-protection.yml create mode 100644 BRANCH_PROTECTION.md diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..e9766ad --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,20 @@ +# CODEOWNERS file for the Python_Git_exam repository +# +# This file defines individuals or teams that are responsible for code in this repository. +# When a PR modifies files, the defined owners will be automatically requested for review. +# +# Pattern: path/to/files @username @org/team-name +# +# For exam purposes, all files should be reviewed before merging to main + +# Default owners for everything in the repo +* @guomics-lab + +# Workflow files should be carefully reviewed +/.github/workflows/ @guomics-lab + +# Source code requires review +/src/ @guomics-lab + +# Tests require review +/tests/ @guomics-lab diff --git a/.github/workflows/main-protection.yml b/.github/workflows/main-protection.yml new file mode 100644 index 0000000..9453288 --- /dev/null +++ b/.github/workflows/main-protection.yml @@ -0,0 +1,32 @@ +name: Main Branch Protection + +on: + push: + branches: + - main + pull_request: + branches: + - main + +permissions: + contents: read + pull-requests: write + issues: write + +jobs: + check-protection: + runs-on: ubuntu-latest + steps: + - name: Check if push is via PR merge + if: github.event_name == 'push' + run: | + echo "::notice::Direct push to main branch detected" + echo "This workflow monitors main branch activity." + echo "For this exam repository, all changes should come through Pull Requests." + + - name: PR Protection Check + if: github.event_name == 'pull_request' + run: | + echo "✅ Changes are being submitted via Pull Request - this is the correct workflow!" + echo "PR #${{ github.event.pull_request.number }}: ${{ github.event.pull_request.title }}" + echo "Author: ${{ github.event.pull_request.user.login }}" diff --git a/BRANCH_PROTECTION.md b/BRANCH_PROTECTION.md new file mode 100644 index 0000000..1d4fa89 --- /dev/null +++ b/BRANCH_PROTECTION.md @@ -0,0 +1,103 @@ +# 分支保护规则说明 (Branch Protection Rules) + +## 概述 +本仓库已配置分支保护机制,确保代码质量和规范的协作流程。 + +## 保护规则 + +### 1. Main 分支保护 +- **禁止直接推送到 main 分支** +- 所有更改必须通过 Pull Request (PR) 提交 +- PR 必须通过所有 CI 检查才能合并 + +### 2. 必需的 CI 检查 +在 PR 合并到 main 分支之前,必须通过以下检查: +- ✅ Ruff (代码质量检查) +- ✅ Black (代码格式化检查) +- ✅ isort (导入排序检查) +- ✅ Pytest (单元测试) + +### 3. 代码审查要求 +- 建议启用代码审查(Code Review)要求 +- PR 需要至少一位审查者批准(在仓库设置中配置) + +## 工作流程 + +### 正确的提交流程 ✅ +```bash +# 1. 从 main 分支创建新分支 +git checkout main +git pull origin main +git checkout -b fix/your-feature + +# 2. 进行代码修改 +# ... 修改文件 ... + +# 3. 提交更改 +git add . +git commit -m "fix: your change description" + +# 4. 推送到远程分支 +git push -u origin fix/your-feature + +# 5. GitHub Actions 会自动创建 PR(如果启用了 auto-pr.yml) +# 或者手动在 GitHub 网站上创建 PR + +# 6. 等待 CI 检查通过 +# 7. 在 GitHub 网站上合并 PR +``` + +### 错误的提交流程 ❌ +```bash +# ❌ 不要直接推送到 main 分支 +git checkout main +git add . +git commit -m "some changes" +git push origin main # 这将被拒绝或触发保护警告 +``` + +## 启用严格保护(需要仓库管理员操作) + +要完全阻止直接推送到 main 分支,需要在 GitHub 仓库设置中配置: + +1. 进入仓库的 **Settings** → **Branches** +2. 添加分支保护规则(Branch protection rule) +3. 规则名称:`main` +4. 启用以下选项: + - ✅ **Require a pull request before merging** + - ✅ Require approvals (建议至少 1 个审查者) + - ✅ **Require status checks to pass before merging** + - ✅ Require branches to be up to date before merging + - 添加必需的检查: + - `grade (lint + format + tests)` (来自 grading-ci.yml) + - `test-and-lint` (来自 ci.yml) + - ✅ **Do not allow bypassing the above settings** + - ⚠️ **Include administrators** (可选,但建议启用以确保规则一致性) + +## 自动化特性 + +### 自动创建 PR +本仓库配置了 `auto-pr.yml` 工作流: +- 当您推送到非 main 分支时,会自动创建 PR +- PR 会包含标准的检查清单 +- 节省手动创建 PR 的时间 + +### 自动运行检查 +- 每次 PR 更新时,自动运行所有质量检查 +- 检查结果显示在 PR 页面 +- 只有全部通过才能合并 + +## 常见问题 + +### Q: 我不小心推送到了 main 分支怎么办? +A: 如果分支保护已正确配置,推送会被拒绝。如果推送成功了,说明需要在仓库设置中启用更严格的保护规则。 + +### Q: 如何查看 CI 检查结果? +A: 在 PR 页面的 "Checks" 标签中可以看到所有检查的详细结果。 + +### Q: 所有检查都通过了,但无法合并? +A: 检查是否启用了代码审查要求,可能需要其他人批准您的 PR。 + +## 参考资料 +- [GitHub 分支保护文档](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches) +- [GitHub Actions 工作流文档](https://docs.github.com/en/actions/using-workflows) diff --git a/EXAMINER_NOTES.md b/EXAMINER_NOTES.md index 056ee74..f211b16 100644 --- a/EXAMINER_NOTES.md +++ b/EXAMINER_NOTES.md @@ -17,6 +17,21 @@ ## 3. 出题上线流程(建议) 1. 将该仓库设置为模板仓库(GitHub Template Repository) 2. 考生从模板生成自己的考试仓库(或你们系统自动创建) -3. 要求考生在规定时间内提交 PR -4. CI 自动给出是否通过 -5. 管理端收集 PR 链接与 CI 状态,进行最终评审 \ No newline at end of file +3. **配置仓库的分支保护规则**(重要): + - Settings → Branches → Add branch protection rule + - Branch name pattern: `main` + - 启用以下选项: + - ✅ Require a pull request before merging + - ✅ Require status checks to pass before merging + - 添加必需检查:`grade (lint + format + tests)` + - ✅ Do not allow bypassing the above settings + - 详细配置请参考 `BRANCH_PROTECTION.md` +4. 要求考生在规定时间内提交 PR +5. CI 自动给出是否通过 +6. 管理端收集 PR 链接与 CI 状态,进行最终评审 + +## 4. 自动化功能说明 +- **自动 PR 创建**:`auto-pr.yml` 会在学生推送分支时自动创建 PR +- **自动 CI 检查**:`grading-ci.yml` 会在 PR 上自动运行所有检查 +- **分支保护监控**:`main-protection.yml` 监控对 main 分支的操作 +- **代码审查要求**:`.github/CODEOWNERS` 定义了代码审查者 \ No newline at end of file diff --git a/README.md b/README.md index 3a2dc9f..00876fb 100644 --- a/README.md +++ b/README.md @@ -22,14 +22,30 @@ CI 会检查: - 进行至少 1 次 commit(建议 1~3 次,小步提交) - 提交信息建议格式:`fix: make code pep8 compliant`(也可使用你们团队规范) 4. **发起 Pull Request**: + - **⚠️ 注意:本仓库禁止直接推送到 `main` 分支,所有更改必须通过 PR 提交** + - 推送分支后,系统会自动创建 PR(或手动创建) - PR 标题清晰说明做了什么 - PR 描述中写明:你修复了哪些类型的问题(例如:imports/formatting/naming/docstring) + - 等待 GitHub Actions 自动运行所有检查 ## 三、通过标准 - GitHub Actions 绿灯(全部 job 通过) - PR 内容清晰、可读 - 不引入功能回归(pytest 通过) +## 📋 自动化功能 +本仓库已配置以下自动化功能: + +### ✅ 自动 PR 检测 +- 当你推送分支后,系统会**自动创建 Pull Request** +- PR 会自动触发 CI 检查(ruff、black、isort、pytest) +- 检查结果会显示在 PR 页面,全部通过后显示绿色 ✅ + +### 🔒 Main 分支保护 +- **禁止直接推送到 `main` 分支** +- 所有代码更改必须通过 Pull Request 提交 +- 详细的分支保护规则请参考 [BRANCH_PROTECTION.md](BRANCH_PROTECTION.md) + ## 四、本地运行建议 1、创建环境(建议使用 Python 3.11) @@ -56,7 +72,11 @@ isort --check-only . git add . git commit -m "fix: pep8 refactor" git push -u origin fix/user (请将user替换为实际的用户名) -PR 页面等待 GitHub Actions 执行,看到 grading-ci / grade (lint + format + tests) 为绿色即通过。 + +# ✅ 系统会自动创建 PR +# 在 GitHub 网站上查看 PR 页面,等待 GitHub Actions 执行 +# 看到 grading-ci / grade (lint + format + tests) 为绿色即通过 +# ⚠️ 注意:不要直接推送到 main 分支,必须通过 PR 合并 ``` From 4ba9312836b345f88ea670469785ecdbe428103b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 25 Dec 2025 03:09:58 +0000 Subject: [PATCH 3/3] Improve branch protection messages and documentation based on code review Co-authored-by: zhiyuajun <156592628+zhiyuajun@users.noreply.github.com> --- .github/workflows/main-protection.yml | 7 ++++--- BRANCH_PROTECTION.md | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main-protection.yml b/.github/workflows/main-protection.yml index 9453288..9f7382e 100644 --- a/.github/workflows/main-protection.yml +++ b/.github/workflows/main-protection.yml @@ -20,9 +20,10 @@ jobs: - name: Check if push is via PR merge if: github.event_name == 'push' run: | - echo "::notice::Direct push to main branch detected" - echo "This workflow monitors main branch activity." - echo "For this exam repository, all changes should come through Pull Requests." + echo "::warning::Direct push to main branch detected" + echo "⚠️ All changes to the main branch should come through Pull Requests." + echo "📖 Please refer to BRANCH_PROTECTION.md for proper workflow guidelines." + echo "🔧 Repository administrators should configure branch protection rules in Settings → Branches" - name: PR Protection Check if: github.event_name == 'pull_request' diff --git a/BRANCH_PROTECTION.md b/BRANCH_PROTECTION.md index 1d4fa89..d15a3c5 100644 --- a/BRANCH_PROTECTION.md +++ b/BRANCH_PROTECTION.md @@ -70,7 +70,6 @@ git push origin main # 这将被拒绝或触发保护警告 - ✅ Require branches to be up to date before merging - 添加必需的检查: - `grade (lint + format + tests)` (来自 grading-ci.yml) - - `test-and-lint` (来自 ci.yml) - ✅ **Do not allow bypassing the above settings** - ⚠️ **Include administrators** (可选,但建议启用以确保规则一致性)