SuperAPI AI 工具集

git-repo

部署与运维

查看项目

更新于 10-16 10:33

技能介绍

用途

git-repo 用于通过 OpenTofu 在 github-infrastructure 仓库中创建新的 GitHub 仓库。它把新建仓库的流程标准化为添加一个 OpenTofu 模块,从而让仓库配置(可见性、分支保护、状态检查等)以基础设施即代码的方式管理。

主要能力

  • 收集并校验创建仓库所需的参数:
    • name(必填,kebab-case,例如 my-new-repo)
    • description(必填,一行描述)
    • visibility(可选,默认 private,可选 private 或 public)
    • is_template(可选,默认 false)
    • default_branch(可选,默认 main,用于分支保护)
    • extra_branches(可选,默认空数组)
    • status_checks(可选,默认 ["Claude Code Review", "CodeRabbit", "Lint"])
    • category(可选,可推断)
  • 根据名称推断分类:名称以 -spec 结尾归为 Specs;以 -template 结尾归为 Templates;包含 infrastructure、ci-、org- 归为 Infrastructure;否则归为 Projects。
  • 克隆 github-infrastructure 仓库到临时目录并创建分支。
  • 在 tofu/repositories/<repo-name>/ 下生成三个模块文件:main.tf、outputs.tf、versions.tf。
  • 更新根模块 tofu/main.tf 与 tofu/outputs.tf,在对应分类注释下加入模块与输出。
  • 格式化、提交、推送并创建 PR,最后清理临时目录并向用户报告 PR URL。

使用方式

流程由技能按步骤执行:

  1. 收集参数。
  2. 克隆到临时目录:
    WORK_DIR=$(mktemp -d)
    git clone --depth 1 git@github.com:OlechowskiMichal/github-infrastructure.git "$WORK_DIR/github-infrastructure"
    cd "$WORK_DIR/github-infrastructure"
    git checkout -b feat/add-<repo-name>
    
  3. 创建模块文件,使用 references/templates.md 中的模板。
  4. 更新根模块。在 tofu/main.tf 对应分类注释下添加:
    module "<module_name>" {
      source = "./repositories/<repo-name>"
    }
    
    模块名由仓库名替换连字符和点为下划线、去掉前导点得到。 在 tofu/outputs.tf 的 repositories 与 branch_protections 输出中对应分类下添加:
    # In repositories:
    "<repo-name>" = module.<module_name>.repository
    
    # In branch_protections:
    "<repo-name>" = module.<module_name>.branch_protection
    
  5. 提交、推送并创建 PR:
    cd "$WORK_DIR/github-infrastructure"
    tofu -chdir=tofu fmt -recursive
    git add tofu/
    git commit -m "feat: add <repo-name> repository"
    git push -u origin feat/add-<repo-name>
    gh pr create --title "feat: add <repo-name> repository" --body "Add OpenTofu module for <repo-name>"
    
  6. 清理:
    [ -n "$WORK_DIR" ] && [ -d "$WORK_DIR" ] && rm -rf "$WORK_DIR"
    
    并向用户报告 PR URL。

OPA 策略要求

CI 会拒绝违规。以下为强制要求:

  • vulnerability_alerts = true
  • delete_branch_on_merge = true
  • allow_merge_commit = false
  • allow_squash_merge = true
  • has_wiki = false
  • has_projects = false
  • has_issues = true
  • 分支保护需包含 required_status_checks 与 required_pull_request_reviews
  • allows_force_pushes = false, allows_deletions = false
  • 至少 1 个批准审查
  • github_branch_protection.extra 块始终存在(即使 for_each = toset([]))
  • 仓库资源上使用 lifecycle { prevent_destroy = true }
  • archive_on_destroy = true

适用场景

  • 需要按组织基础设施规范批量或标准化创建 GitHub 仓库。
  • 希望通过 OpenTofu 与 PR 流程管理仓库配置,并自动应用分支保护与状态检查。
  • 需要在创建时就满足 OPA 策略合规要求。

限制

  • 依赖可访问的 github-infrastructure 仓库与相应凭据(git clone、gh CLI)。
  • 参数、模板与策略要求以资料提供为准,其他行为与收费、价格、平台支持等暂未提供。