Dangerとは
GitHub等のPullRequestをCI上から検査し、問題点をコメントするツール
例
- プルリクエストの内容が長すぎると注意をコメントしてくれる
- AndroidLintの結果をコメントしてくれる
参考: https://qiita.com/noboru_i/items/011fd5ef1808bea3d6ba
学び1: KotlinのAndroidLintはコマンドラインでは動かないのでDangerもコメントしない
cf. https://developer.android.com/studio/preview/kotlin-issues.html
Lint checks with Kotlin don't work from command line Lint checks on Kotlin files only work from the Android Studio IDE.
The currently do not run when you run lint directly from Gradle.
なので頑張ってセットアップしたのにxml系しかコメントしてくれない(辛)
追記
AndroidStudio3.1からコマンドラインからでも動くようになりそうです
https://developer.android.com/studio/preview/features/index.html
学び2: 複数のモジュールを管理している場合の設定方法
状況
以下のようなスタンダードな構成ではなく
(Project Root)
|
|--app
以下のように複数のモジュールを管理している状況
(Project Root)
|
|--app1
|
|--app2
|
|--app3
問題点
DangerのAndroidLintプラグインが一つのlintレポートファイルしか参照しない
cf. https://github.com/loadsmart/danger-android_lint/blob/master/lib/android_lint/plugin.rb#L37
# Location of lint report file
# If your Android lint task outputs to a different location, you can specify it here.
# Defaults to "app/build/reports/lint/lint-result.xml".
# @return [String]
attr_accessor :report_file
# A getter for `report_file`.
# @return [String]
def report_file
return @report_file || 'app/build/reports/lint/lint-result.xml'
end
なので https://qiita.com/noboru_i/items/2ad7c07d8b4266c9eb5b などを参考にしてDangerfileを
android_lint.gradle_task = "app:lint"
android_lint.report_file = "app/build/reports/lint-results.xml"
android_lint.filtering = true
android_lint.lint(inline_mode: true)
と設定しても一つのモジュールに対してしか動作してくれない。
解決法
DangerFileをモジュールごとに作成し、CIからモジュールごとにDangerを動かすようにした。
CircleCI(2.0)での設定の例
.circle/config.yml (一部)
###
# コメントが上書きされないように別々のdanger_idを付与。danger_idがないとapp1やapp2に対するDangerのコメントが、app3に対するDangerのコメントで上書きされてしまう。
###
- run:
name: Run danger for app1
command: bundle exec danger --dangerfile="app1/Dangerfile" --danger_id=1
- run:
name: Run danger for app2
command: bundle exec danger --dangerfile="app2/Dangerfile" --danger_id=2
- run:
name: Run danger for app3
command: bundle exec danger --dangerfile="app3/Dangerfile" --danger_id=3
app1モジュールのDangerfileの例
# PRの箇所のみコメントする
github.dismiss_out_of_range_messages
# Android Lint
android_lint.gradle_task = ":app1:lintDebug"
android_lint.report_file = "app1/build/reports/lint-results-debug.xml"
android_lint.filtering = true
android_lint.lint(inline_mode: true)