カスタムコードルールを追加する
Custom Code Rulesを使用すると、チーム固有のコーディング標準や組織特有のパターンを強制できます。デフォルトのチェックではカバーされない特定のコードパターンを検出したり、命名規則を強制したり、アーキテクチャ違反にフラグを立てたりするルールを作成できます。
Custom Code Rulesとは?
Section titled “Custom Code Rulesとは?”Custom Code Rulesは、チームの特定の要件に合わせて定義する AI駆動のチェックです。一般的なベストプラクティスを適用するデフォルトのチェックとは異なり、カスタムルールはあなたのコードベース、アーキテクチャ、またはビジネスロジックに固有の標準を強制します。
Custom Code Ruleを作成する
Section titled “Custom Code Ruleを作成する”1. ルールを定義する
Section titled “1. ルールを定義する”- Code Quality > Checksタブに移動します
- Add Custom Code Rule をクリックします
- ルールが検出すべき内容を明確に記述します:
例:
Allow only Alpine base images in Dockerfiles as base images2. 対象言語を選択する
Section titled “2. 対象言語を選択する”このルールを適用するプログラミング言語を選択します
3. 例を生成・調整する
Section titled “3. 例を生成・調整する”- Generate Examples をクリックして、AIに初期コードサンプルを生成させます
- 生成された例を確認し、必要に応じて修正します
- 準拠している例と違反している例の両方を用意します:
準拠している例:
# ✅ Code that follows the ruleFROM alpine:3.18RUN apk add --no-cache nodejs違反している例:
# ❌ Code that violates the ruleFROM ubuntu:latestRUN apt-get update && apt-get install nodejs4. ルールを検証する
Section titled “4. ルールを検証する”- Validate Rule をクリックして、作成した例をテストします
- システムは以下を検証します:
- 準拠している例がルールに合格すること
- 違反している例が正しくフラグ付けされること
- ルールのロジックに一貫性があり、明確であること
- 検証が失敗した場合は例を調整します
5. ルールの詳細を設定する
Section titled “5. ルールの詳細を設定する”検証が完了したら、ルールに関する追加情報を入力します:
-
Title: ルールに明確で分かりやすい名前を付けます
Use Alpine base images in Docker containers -
TL;DR: 問題の簡単な要約を記述します
Non-Alpine base images increase container size and attack surface -
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. -
Save Rule をクリックして有効化します
これでカスタムルールが Checksタブに表示され、有効化されているリポジトリの新しいプルリクエストのスキャンを開始します。
効果的なCustom Ruleの書き方
Section titled “効果的なCustom Ruleの書き方”具体的かつ明確にする
Section titled “具体的かつ明確にする”❌ 曖昧すぎる例:
Use proper error handling✅ 具体的で実行可能な例:
All API endpoints must wrap database calls in try-catch blocks and returnstandardized error responses with status codesスタイルではなくパターンに焦点を当てる
Section titled “スタイルではなくパターンに焦点を当てる”カスタムルールは、フォーマットよりも論理的なパターンを検出する場合に最も効果を発揮します。
✅ カスタムルールに適した例:
- API認証の要件
- データベーストランザクションのパターン
- セキュリティヘッダーの実装
- ビジネスロジックの検証
❌ リンターの方が適している例:
- インデントとスペーシング
- 括弧の配置
- 変数の命名スタイル
よくあるCustom Ruleの例
Section titled “よくあるCustom Ruleの例”セキュリティルール
Section titled “セキュリティルール”All SQL queries must use parameterized statements. Direct stringconcatenation in SQL queries is not allowed.アーキテクチャルール
Section titled “アーキテクチャルール”Controllers should not directly access the database. All databaseoperations must go through a service or repository layer.APIの標準
Section titled “APIの標準”All REST API endpoints must include rate limiting headers(X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset).テストの要件
Section titled “テストの要件”Every exported function must have at least one corresponding testin the __tests__ directory with the same file name pattern.ドキュメントの標準
Section titled “ドキュメントの標準”All public API methods must include JSDoc comments with @param,@returns, and @throws annotations.