This is a Gradle Plugin to report Android Lint and Detekt result back to Github Pull Request. This is targeted for someone who's doing Android Development and using Github who wants to run Android Lint and Detekt on their pull request. (Currently, this plugin assumes the usage of both Android Lint and Detekt)
Here is how it works using Github Actions (it can be used in other CI as well).
( 2 ) This will produce this lint report, and it will be reported back in the Pull Request:
- then you can fix the lint warnings and push again
There are a couple of steps needed to set up everything.
First, we need to set up a Github Action trigger to run Detekt and Android Lint.
Add a file in .github/workflows/run-lint.yml
(you can name it anything) in the root of your project:
name: Android Pull Request & Master CI
on:
pull_request:
branches:
- 'master'
push:
branches:
- 'master'
jobs:
lint:
name: Run Lint
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v1
- name: setup JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Run detekt
run: ./gradlew detekt
continue-on-error: true
- name: Run Android Lint
run: ./gradlew lint
- name: Run Android Lint Reporter to report Lint and Detekt result to PR
env:
PR_NUMBER: ${{ github.event.number }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
./gradlew report -PgithubPullRequestId=$PR_NUMBER -PgithubToken=$GITHUB_TOKEN
How to get Github Token
- Go to Github's
Settings --> Developer settings --> Generate
new token.
- Go to Personal Access Token, and click
Generate new token
:
- Check for Repo (all) and workflow
- It's better to make a bot account and use the token of the bot account
build.gradle
:
Groovy
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.worker8.android_lint_reporter:android_lint_reporter:<latest_version>"
}
}
Kotlin
buildscript {
repositories {
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath("gradle.plugin.com.worker8.android_lint_reporter:android_lint_reporter:<latest_version>")
}
}
Note: latest_version
can be found here: https://plugins.gradle.org/plugin/com.worker8.android_lint_reporter
app/build.gradle
:
Groovy
plugins {
id "com.worker8.android_lint_reporter"
}
android_lint_reporter {
lintFilePath = "./app/build/reports/lint-results.xml"
detektFilePath = ".app/build/reports/detekt_reports.xml"
githubOwner = "worker8"
githubRepositoryName = "AndroidLintReporter"
showLog = true // optional - default to false, show extra information, will slow things down
}
Kotlin
plugins {
id("com.worker8.android_lint_reporter")
}
android_lint_reporter {
lintFilePath = "./app/build/reports/lint-results.xml"
detektFilePath = ".app/build/reports/detekt_reports.xml"
githubOwner = "worker8"
githubRepositoryName = "AndroidLintReporter"
showLog = true // optional - default to false, show extra information, will slow things down
}
Try making a pull request, and you should see the Github Actions running under "Check" tab. When it's done, you should see your lint report being posted back to your pull request.
For those who is interested to contribute or fork it. Here's a blog post I wrote explaining the source code of this repo: https://bloggie.io/@_junrong/the-making-of-android-lint-reporter
Suggested IDE for development:
Download Intellij Community Edition for free and open this project.
Setup Github Personal Access Token(PAT)
Prepare a file local.properties
based on local.properties.TEMPLATE in the root directory of this project, with the following content:
github_token=<replace with your Github Personal Access Token (PAT)>
github_owner=<replace with your Github Owner Name>
github_repository=<replace with your Github Repository Name>
You can test your Github API using your PAT using cURL:
curl -H "Authorization: token <github_token>" -H "Content-Type: application/json" --data '{"body":"test123abc"}' -X POST https://api.github.com/repos/<github_token>/<GITHUB_PROJECT>/issues/<PR_OR_ISSUE_NUMBER>/comments
run Functional Test
The gist of this plugin is located in the class of AndroidLintReporterPlugin.kt
.
After setting everything up, you can run the test using this command:
./gradlew functionalTest
To deploy:
- Download secrets from https://plugins.gradle.org/ after logging in.
- up version in
build.gradle.kts
- then run
./gradlew publishPlugin
Refer to https://github.com/worker8/AndroidLintReporter/releases
Copyright 2020 Tan Jun Rong
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.