سير العمل لنشر الإنتاج

خطوط أنابيب CI/CD للكود المُنشأ بالذكاء الاصطناعي

5 دقيقة للقراءة

نهج الثقة مع التحقق

الكود المُنشأ بالذكاء الاصطناعي يتطلب خطوات تحقق إضافية في خطوط أنابيب CI/CD. بينما ينتج مساعدو الذكاء الاصطناعي كودًا عالي الجودة، التحقق الآلي يوفر شبكة أمان أساسية.

هيكل Pipeline المُحسَّن

# .github/workflows/ai-code-validation.yml
name: AI Code Validation

on:
  push:
    branches: [main, develop]
  pull_request:

jobs:
  # بوابات الجودة القياسية
  quality-gates:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: فحص الأنواع
        run: npm run typecheck

      - name: Lint
        run: npm run lint

      - name: اختبارات الوحدة
        run: npm run test:unit

      - name: اختبارات التكامل
        run: npm run test:integration

  # التحقق الخاص بالذكاء الاصطناعي
  ai-code-validation:
    runs-on: ubuntu-latest
    needs: quality-gates
    steps:
      - name: فحص الأمان
        uses: github/codeql-action/analyze@v3

      - name: تدقيق الاعتماديات
        run: npm audit --audit-level=high

      - name: فحص الأسرار المشفرة
        uses: trufflesecurity/trufflehog@main
        with:
          path: ./
          base: ${{ github.event.repository.default_branch }}

      - name: الامتثال للرخص
        run: npx license-checker --onlyAllow "MIT;Apache-2.0;BSD-3-Clause"

  # التحقق من الأداء
  performance-check:
    runs-on: ubuntu-latest
    needs: quality-gates
    steps:
      - name: فحص حجم الحزمة
        run: |
          npm run build
          npx bundlesize

      - name: Lighthouse CI
        uses: treosh/lighthouse-ci-action@v10
        with:
          budgetPath: ./lighthouse-budget.json

بوابات الجودة لكود الذكاء الاصطناعي

فرض أمان الأنواع

// tsconfig.strict.json - أقصى أمان للأنواع لكود الذكاء الاصطناعي
{
  "compilerOptions": {
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictBindCallApply": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "useUnknownInCatchVariables": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "exactOptionalPropertyTypes": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true
  }
}

قواعد ESLint مخصصة

// .eslintrc.js - قواعد خاصة بكود الذكاء الاصطناعي
module.exports = {
  rules: {
    // منع مشاكل كود الذكاء الاصطناعي الشائعة
    'no-console': 'error',
    'no-debugger': 'error',
    'no-alert': 'error',

    // ضمان معالجة الأخطاء المناسبة
    'no-throw-literal': 'error',
    '@typescript-eslint/no-floating-promises': 'error',
    '@typescript-eslint/no-misused-promises': 'error',

    // التركيز على الأمان
    'no-eval': 'error',
    'no-implied-eval': 'error',
    'no-new-func': 'error',

    // الذكاء الاصطناعي أحيانًا يولد كود معقد جدًا
    'complexity': ['error', { max: 15 }],
    'max-depth': ['error', { max: 4 }],
    'max-lines-per-function': ['error', { max: 100 }],

    // ضمان التوثيق لـ APIs العامة
    'jsdoc/require-jsdoc': ['error', {
      publicOnly: true,
      require: {
        FunctionDeclaration: true,
        ClassDeclaration: true,
        MethodDefinition: true
      }
    }]
  }
};

مراجعة الكود الآلية

مراجعة PR بمساعدة الذكاء الاصطناعي

# .github/workflows/ai-review.yml
name: AI Code Review

on:
  pull_request:
    types: [opened, synchronize]

jobs:
  ai-review:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: الحصول على الملفات المتغيرة
        id: changed
        run: |
          echo "files=$(git diff --name-only origin/main...HEAD | tr '\n' ' ')" >> $GITHUB_OUTPUT

      - name: مراجعة Claude Code
        run: |
          claude "راجع هذه الملفات المتغيرة لـ:
          1. الثغرات الأمنية
          2. مشاكل الأداء
          3. فجوات معالجة الأخطاء
          4. تغطية الاختبار
          5. تناسق أسلوب الكود

          الملفات: ${{ steps.changed.outputs.files }}

          أخرج مراجعة منظمة مع مستويات الخطورة."

أتمتة قائمة المراجعة

claude "أنشئ قائمة مراجعة PR لهذا الفرق.
تحقق من:
- [ ] لا أسرار أو بيانات اعتماد مشفرة
- [ ] جميع الدوال الجديدة لها معالجة أخطاء
- [ ] العمليات غير المتزامنة لها await مناسب
- [ ] استعلامات قاعدة البيانات مُعلمة
- [ ] إدخال المستخدم مُتحقق منه
- [ ] APIs الجديدة لها تحديد معدل
- [ ] الاختبارات تغطي المسار السعيد وحالات الحافة
- [ ] لا عبارات console.log في كود الإنتاج"

متطلبات تغطية الاختبار

# jest.config.js
module.exports = {
  coverageThreshold: {
    global: {
      branches: 80,
      functions: 80,
      lines: 80,
      statements: 80
    },
    // معيار أعلى للمسارات الحرجة
    './src/services/payment/': {
      branches: 95,
      functions: 95,
      lines: 95,
      statements: 95
    },
    './src/services/auth/': {
      branches: 95,
      functions: 95,
      lines: 95,
      statements: 95
    }
  }
};

Pre-commit Hooks

# .pre-commit-config.yaml
repos:
  - repo: local
    hooks:
      - id: typecheck
        name: فحص الأنواع
        entry: npm run typecheck
        language: system
        types: [typescript]
        pass_filenames: false

      - id: lint
        name: Lint
        entry: npm run lint:fix
        language: system
        types: [typescript, javascript]

      - id: test-related
        name: اختبار الملفات ذات الصلة
        entry: npm run test:related
        language: system
        types: [typescript]

      - id: check-secrets
        name: فحص الأسرار
        entry: git secrets --scan
        language: system

حماية الفرع

claude "أنشئ تكوين حماية فرع يـ:
1. يتطلب نجاح جميع فحوصات CI
2. يتطلب موافقة واحدة على الأقل
3. يرفض المراجعات القديمة عند commits جديدة
4. يفرض التاريخ الخطي
5. يمنع force pushes"
# حماية الفرع عبر GitHub API أو الإعدادات
branch_protection:
  main:
    required_status_checks:
      strict: true
      checks:
        - context: "quality-gates"
        - context: "ai-code-validation"
        - context: "performance-check"
    required_pull_request_reviews:
      required_approving_review_count: 1
      dismiss_stale_reviews: true
      require_code_owner_reviews: true
    enforce_admins: true
    required_linear_history: true
    allow_force_pushes: false

الدرس التالي

سنغطي استراتيجيات النشر الآمن للكود المُنشأ بالذكاء الاصطناعي، بما في ذلك النشر التدريجي ونشر الكناري. :::

اختبار

الوحدة 5: سير العمل لنشر الإنتاج

خذ الاختبار