コンテンツにスキップ

カスタムコードルールを追加する

Custom Code Rulesを使用すると、チーム固有のコーディング標準や組織特有のパターンを強制できます。デフォルトのチェックではカバーされない特定のコードパターンを検出したり、命名規則を強制したり、アーキテクチャ違反にフラグを立てたりするルールを作成できます。

Custom Code Rulesは、チームの特定の要件に合わせて定義する AI駆動のチェックです。一般的なベストプラクティスを適用するデフォルトのチェックとは異なり、カスタムルールはあなたのコードベース、アーキテクチャ、またはビジネスロジックに固有の標準を強制します。

  1. Code Quality > Checksタブに移動します
  2. Add Custom Code Rule をクリックします
  3. ルールが検出すべき内容を明確に記述します:

例:

Allow only Alpine base images in Dockerfiles as base images

このルールを適用するプログラミング言語を選択します

  • Generate Examples をクリックして、AIに初期コードサンプルを生成させます
  • 生成された例を確認し、必要に応じて修正します
  • 準拠している例と違反している例の両方を用意します:

準拠している例:

# ✅ Code that follows the rule
FROM alpine:3.18
RUN apk add --no-cache nodejs

違反している例:

# ❌ Code that violates the rule
FROM ubuntu:latest
RUN apt-get update && apt-get install nodejs
  1. Validate Rule をクリックして、作成した例をテストします
  2. システムは以下を検証します:
    • 準拠している例がルールに合格すること
    • 違反している例が正しくフラグ付けされること
    • ルールのロジックに一貫性があり、明確であること
  3. 検証が失敗した場合は例を調整します

検証が完了したら、ルールに関する追加情報を入力します:

  1. Title: ルールに明確で分かりやすい名前を付けます

    Use Alpine base images in Docker containers
  2. TL;DR: 問題の簡単な要約を記述します

    Non-Alpine base images increase container size and attack surface
  3. How to fix: 開発者向けに実行可能なガイダンスを提供します

    Replace your base image with an Alpine Linux variant. For example, change 'FROM node:18' to 'FROM node:18-alpine'. You may need to adjust package installation commands from apt-get to apk.
    
  4. Save Rule をクリックして有効化します

これでカスタムルールが Checksタブに表示され、有効化されているリポジトリの新しいプルリクエストのスキャンを開始します。

曖昧すぎる例:

Use proper error handling

具体的で実行可能な例:

All API endpoints must wrap database calls in try-catch blocks and return
standardized error responses with status codes

スタイルではなくパターンに焦点を当てる

Section titled “スタイルではなくパターンに焦点を当てる”

カスタムルールは、フォーマットよりも論理的なパターンを検出する場合に最も効果を発揮します。

カスタムルールに適した例:

  • API認証の要件
  • データベーストランザクションのパターン
  • セキュリティヘッダーの実装
  • ビジネスロジックの検証

リンターの方が適している例:

  • インデントとスペーシング
  • 括弧の配置
  • 変数の命名スタイル
All SQL queries must use parameterized statements. Direct string
concatenation in SQL queries is not allowed.
Controllers should not directly access the database. All database
operations must go through a service or repository layer.
All REST API endpoints must include rate limiting headers
(X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset).
Every exported function must have at least one corresponding test
in the __tests__ directory with the same file name pattern.
All public API methods must include JSDoc comments with @param,
@returns, and @throws annotations.