Skip to content

feat: Add 'codei18n init' subcommand --story=1 #11

feat: Add 'codei18n init' subcommand --story=1

feat: Add 'codei18n init' subcommand --story=1 #11

Workflow file for this run

name: CI
on:
push:
branches: [ main, master, develop ]
pull_request:
branches: [ main, master, develop ]
jobs:
# 代码质量检查
lint:
name: 代码质量检查
runs-on: ubuntu-latest
steps:
- name: 检出代码
uses: actions/checkout@v4
- name: 设置 Go 环境
uses: actions/setup-go@v5
with:
go-version: '1.25.5'
cache: true
- name: 验证 go.mod 和 go.sum
run: |
go mod download
go mod verify
- name: 运行 gofmt 检查
run: |
if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then
echo "以下文件需要格式化:"
gofmt -s -l .
exit 1
fi
- name: 运行 go vet
run: go vet ./...
- name: 安装 staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest
- name: 运行 staticcheck
run: staticcheck ./...
# 构建测试(多平台)
build:
name: 构建测试
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
go-version: ['1.25.5']
steps:
- name: 检出代码
uses: actions/checkout@v4
- name: 设置 Go 环境
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
cache: true
- name: 下载依赖
run: go mod download
- name: 构建
run: go build -v -o bin/codei18n${{ matrix.os == 'windows-latest' && '.exe' || '' }} ./cmd/codei18n
- name: 验证构建产物
shell: bash
run: |
if [ "${{ matrix.os }}" = "windows-latest" ]; then
test -f bin/codei18n.exe
else
test -f bin/codei18n
fi
# 单元测试和覆盖率
test:
name: 单元测试
runs-on: ubuntu-latest
steps:
- name: 检出代码
uses: actions/checkout@v4
- name: 设置 Go 环境
uses: actions/setup-go@v5
with:
go-version: '1.25.5'
cache: true
- name: 运行测试
run: go test -v -race -coverprofile=coverage.out ./...
- name: 生成覆盖率报告
run: go tool cover -func=coverage.out
- name: 检查覆盖率要求
run: |
# 检查总体覆盖率 >= 60%
total_coverage=$(go tool cover -func=coverage.out | grep total | awk '{print $3}' | sed 's/%//')
echo "总体覆盖率: ${total_coverage}%"
if [ -z "$total_coverage" ]; then
echo "警告: 无法获取覆盖率数据(可能还没有测试)"
exit 0
fi
# 使用 awk 进行浮点数比较
if awk -v cov="$total_coverage" 'BEGIN {exit !(cov >= 60)}'; then
echo "✓ 总体覆盖率达标 (${total_coverage}% >= 60%)"
else
echo "✗ 总体覆盖率不达标 (${total_coverage}% < 60%)"
echo "注意: 这是一个警告,暂不阻断 CI"
# 暂时不阻断 CI,等项目成熟后再严格要求
# exit 1
fi
- name: 上传覆盖率报告到 Codecov
uses: codecov/codecov-action@v4
with:
file: ./coverage.out
flags: unittests
name: codecov-umbrella
fail_ci_if_error: false
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
# 集成测试
integration-test:
name: 集成测试
runs-on: ubuntu-latest
needs: [lint, build]
steps:
- name: 检出代码
uses: actions/checkout@v4
- name: 设置 Go 环境
uses: actions/setup-go@v5
with:
go-version: '1.25.5'
cache: true
- name: 构建
run: make build
- name: 运行 CLI 帮助命令
run: ./bin/codei18n --help
- name: 测试 scan 命令
run: |
# 创建测试文件
mkdir -p /tmp/test-codei18n
cat > /tmp/test-codei18n/test.go << 'EOF'
package main
// Calculate the sum of two numbers
func Add(a, b int) int {
return a + b
}
EOF
# 测试 scan 命令
./bin/codei18n scan --file /tmp/test-codei18n/test.go --format json || true
- name: 验证配置文件生成
run: |
# 测试 init 命令
cd /tmp/test-codei18n
/Users/devinzeng/go/src/github.com/studyzy/codei18n/bin/codei18n init --source-lang en --target-lang zh-CN || true
# 安全扫描
security:
name: 安全扫描
runs-on: ubuntu-latest
steps:
- name: 检出代码
uses: actions/checkout@v4
- name: 设置 Go 环境
uses: actions/setup-go@v5
with:
go-version: '1.25.5'
cache: true
- name: 运行 gosec 安全扫描
uses: securego/gosec@master
with:
args: '-no-fail -fmt json -out results.json ./...'
continue-on-error: true
- name: 运行 govulncheck
run: |
go install golang.org/x/vuln/cmd/govulncheck@latest
govulncheck ./...
continue-on-error: true
# 依赖审计
dependencies:
name: 依赖审计
runs-on: ubuntu-latest
steps:
- name: 检出代码
uses: actions/checkout@v4
- name: 设置 Go 环境
uses: actions/setup-go@v5
with:
go-version: '1.25.5'
cache: true
- name: 检查过时的依赖
run: |
go list -u -m -json all | jq -r 'select(.Update != null) | "\(.Path): \(.Version) -> \(.Update.Version)"'
continue-on-error: true
- name: 下载并验证依赖
run: |
go mod download
go mod verify
# 许可证检查
license:
name: 许可证检查
runs-on: ubuntu-latest
steps:
- name: 检出代码
uses: actions/checkout@v4
- name: 检查 LICENSE 文件
run: |
if [ ! -f LICENSE ]; then
echo "警告: 项目缺少 LICENSE 文件"
echo "建议添加开源许可证(如 MIT、Apache 2.0 等)"
else
echo "✓ LICENSE 文件存在"
cat LICENSE
fi
continue-on-error: true
# 最终状态检查
ci-success:
name: CI 通过
runs-on: ubuntu-latest
needs: [lint, build, test, integration-test, security, dependencies]
if: always()
steps:
- name: 检查所有任务状态
run: |
if [ "${{ needs.lint.result }}" != "success" ] || \
[ "${{ needs.build.result }}" != "success" ] || \
[ "${{ needs.test.result }}" != "success" ] || \
[ "${{ needs.integration-test.result }}" != "success" ]; then
echo "❌ CI 检查失败"
exit 1
fi
echo "✅ 所有 CI 检查通过"