Skip to content

Conversation

@harata-t
Copy link
Collaborator

対応Issue

resolve #0

概要

  • Rails API側のコード品質向上のため、RuboCopの導入とGitHub Actionsでの自動修正・チェック機能を実装

実装詳細

  • RuboCopの導入

    • api/GemfileにRuboCopを追加
    • .rubocop.ymlでRuboCopの設定をカスタマイズ
    • .rubocop_todo.ymlで既存の違反を一時的に無視
  • コードの自動修正

    • 全APIファイル(コントローラー、モデル、ビュー、テスト等)でRuboCopの自動修正を実行
    • Style/FrozenStringLiteralCommentStyle/SymbolArray等のスタイル違反を修正
    • dependent: :destroyの追加等の機能改善
  • GitHub Actionsワークフローの実装

    • api-code-quality.ymlでRuboCop自動修正・チェック機能を追加
    • トリガ
      • push: api/** 変更時に起動(全ブランチ)。
      • pull_request: ベース gm3/develop かつ api/** 変更時に起動。
    • 実行フロー
      • rubocop -A --no-server を実行(ここでは失敗させず、自動修正のみ適用)。
      • 差分があれば bot で自動コミット&push(同一リポ PR のみ)。コミットメッセージに [skip ci] を付与してループ抑止。
      • rubocop --parallel --format github で最終チェック(残違反があればジョブを fail。PR には行内アノテーションが表示される)。
    • 安全装置
      • concurrency により同一 ref の並行実行をキャンセル。
      • if: github.actor != 'github-actions[bot]' で bot の自動コミットによる再実行をスキップ。
      • 自動コミットの author は github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

画面スクリーンショット等

テスト項目

  • api/** を変更した PR / push でワークフローが起動する。
  • 自動修正が発生した場合、1コミットにまとまり自動で push される(同一リポ PR)。
  • 残違反がある場合、ジョブが 失敗し PR の差分行にアノテーション(違反内容)が表示される。
  • 自動修正が無い場合はコミット/push が発生しない。
  • フォーク PR では自動 push が実行されない。
  • 既存 API 機能に影響がない(主要エンドポイントの疎通確認)。

備考

@harata-t harata-t changed the title RuboCopの導入と既存違反修正、GitHub ActionsでCI実装 Lintの欲の秋 ~CIを添えて~ Sep 13, 2025
@harata-t harata-t changed the title Lintの欲の秋 ~CIを添えて~ Lint欲の秋 ~CIを添えて~ Sep 13, 2025
Comment on lines +29 to +32
# 同じブランチ(ref)の並行実行を抑止(後から来た実行を優先)
concurrency:
group: rubocop-${{ github.ref }}
cancel-in-progress: true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

書いたことなかった
学び

Comment on lines 67 to 84

# ② 自動修正の差分があればコミット&push
# - push のとき:そのまま
# - PR のとき:同一リポのブランチ(フォーク以外)のみ push 許可
# - [skip ci]: そのコミットで再度 CI を走らせない抑制用タグ
- name: Commit auto-fixed files
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.repository == github.event.pull_request.head.repo.full_name)
run: |
if ! git diff --quiet; then
git config user.name "github-actions[bot]" # bot としてコミット
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add api/
git commit -m "chore(rubocop): auto fix (-A) [skip ci]"
git push
else
echo "No changes to commit."
fi

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これ、短くできます
おそらく下の感じでいける

https://github.com/NUTFes/group-manager-2/blob/gm3/develop/.github/workflows/user-code-quality.yml

      # 変更があれば、コミット&プッシュする
      - name: Commit formatted files
        uses: EndBug/add-and-commit@v9
        with:
          author_name: GitHub Action
          author_email: action@github.com
          message: "chore: auto format code"
          push: true

Copy link
Contributor

@hikahana hikahana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多分おそらくメイビーLGTM

@harata-t harata-t merged commit 8a2d179 into gm3/develop Nov 3, 2025
2 checks passed
@harata-t harata-t deleted the feat/harata/rails-lint-and-ci branch November 3, 2025 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants