Skip to content

Commit 45b6195

Browse files
authored
Merge pull request #10 from worker8/add-support-for-detekt
Add support for Detekt
2 parents 2708c15 + 24ce534 commit 45b6195

File tree

13 files changed

+370
-207
lines changed

13 files changed

+370
-207
lines changed

README.md

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,43 @@
11

22

3-
# AndroidLintReporter [![](https://img.shields.io/badge/latest-2.0.0-blue)](https://plugins.gradle.org/plugin/com.worker8.android_lint_reporter) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
3+
# AndroidLintReporter [![](https://img.shields.io/badge/latest-2.1.0-blue)](https://plugins.gradle.org/plugin/com.worker8.android_lint_reporter) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
44

55
<img width="300" alt="Android Lint Reporter Logo" src="https://user-images.githubusercontent.com/1988156/79091365-fc968000-7d87-11ea-997d-2a0fa1f6ec5a.png">
66

7-
This is a Gradle Plugin to parse, format, report Android Lint result back to Github Pull Request using Github Actions. This is targetted for someone who's doing Android Development and using Github who wants to run lint on their pull request.
7+
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.
8+
(Currently, this plugin assumes the usage of both Android Lint and Detekt)
89

9-
Here is how it works.
10+
Here is how it works using Github Actions (it can be used in other CI as well).
1011

1112
<details>
1213
<summary>
13-
1. When a Pull Request is created:
14+
( 1 ) When a Pull Request is created, a Github Actions will be triggered:
1415
</summary>
1516
<br>
16-
<img width="1057" src="https://user-images.githubusercontent.com/1988156/77041343-13ea8380-69fd-11ea-9c94-2935aff4f542.png">
17+
<img width="1057" src="https://user-images.githubusercontent.com/1988156/115521839-83006e80-a2c6-11eb-9960-53fe714cbfee.png"/>
1718
</details>
18-
19-
<details>
20-
<summary>
21-
2. Github Actions will be triggered:
22-
</summary>
2319
<br>
24-
<img width="1057" src="https://user-images.githubusercontent.com/1988156/77041423-3a102380-69fd-11ea-8aa8-8026b4d1375c.png">
25-
</details>
26-
27-
<details>
28-
<summary>
29-
3. The Github Actions will run:
30-
</summary>
3120
<br>
32-
<code>
33-
./gradlew lint && ./gradlew parseAndSendLintResult -PgithubPullRequestId=<PR number> -PgithubToken=<Github Access Token>
34-
</code>
35-
36-
Note: The task `parseAndSendLintResult` is provided by this plugin!
37-
</details>
38-
3921
<details>
4022
<summary>
41-
4. This will produce this lint report and it will be reported back in the Pull Request:
23+
( 2 ) This will produce this lint report, and it will be reported back in the Pull Request:
4224

4325
- then you can fix the lint warnings and push again
4426
</summary>
27+
28+
<img width="1057" src="https://user-images.githubusercontent.com/1988156/114693261-6313fc80-9d54-11eb-8dda-431c5adf9ca5.png"/>
4529
<br>
46-
<img width="1057" src="https://user-images.githubusercontent.com/1988156/114693261-6313fc80-9d54-11eb-8dda-431c5adf9ca5.png">
30+
<img width="1057" src="https://user-images.githubusercontent.com/1988156/115523163-c27b8a80-a2c7-11eb-99f9-ff7c108863c8.png"/>
4731
</details>
4832

4933
## How to Setup
50-
There are a couple of steps needed to setup everything.
34+
There are a couple of steps needed to set up everything.
5135

5236
#### 1. Github Actions
5337

54-
First, we need to setup a a Github Action trigger to run lint and the gradle task provided by this plugin.
38+
First, we need to set up a Github Action trigger to run Detekt and Android Lint.
5539

56-
Add a file in `.github/workflows/run-lint.yml` in the root of your project:
40+
Add a file in `.github/workflows/run-lint.yml` (you can name it anything) in the root of your project:
5741

5842
```yml
5943
name: Android Pull Request & Master CI
@@ -77,12 +61,17 @@ jobs:
7761
uses: actions/setup-java@v1
7862
with:
7963
java-version: 1.8
80-
- name: Run lint && parse lint-results.xml && send report to PR
64+
- name: Run detekt
65+
run: ./gradlew detekt
66+
continue-on-error: true
67+
- name: Run Android Lint
68+
run: ./gradlew lint
69+
- name: Run Android Lint Reporter to report Lint and Detekt result to PR
8170
env:
8271
PR_NUMBER: ${{ github.event.number }}
8372
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
8473
run: |
85-
./gradlew lint && ./gradlew parseAndSendLintResult -PgithubPullRequestId=$PR_NUMBER -PgithubToken=$GITHUB_TOKEN
74+
./gradlew report -PgithubPullRequestId=$PR_NUMBER -PgithubToken=$GITHUB_TOKEN
8675
```
8776
<details>
8877
<summary>
@@ -162,8 +151,10 @@ plugins {
162151
}
163152
android_lint_reporter {
164153
lintFilePath = "./app/build/reports/lint-results.xml"
165-
githubUsername = "worker8"
154+
detektFilePath = ".app/build/reports/detekt_reports.xml"
155+
githubOwner = "worker8"
166156
githubRepositoryName = "AndroidLintReporter"
157+
showLog = true // optional - default to false, show extra information, will slow things down
167158
}
168159
```
169160
@@ -175,8 +166,10 @@ plugins {
175166
}
176167
android_lint_reporter {
177168
lintFilePath = "./app/build/reports/lint-results.xml"
178-
githubUsername = "worker8"
169+
detektFilePath = ".app/build/reports/detekt_reports.xml"
170+
githubOwner = "worker8"
179171
githubRepositoryName = "AndroidLintReporter"
172+
showLog = true // optional - default to false, show extra information, will slow things down
180173
}
181174
```
182175

@@ -196,10 +189,12 @@ Download Intellij Community Edition for free and open this project.
196189

197190
**Setup Github Personal Access Token(PAT)**
198191

199-
Prepare a file `local.properties` in the root directory of this project, with the following content:
192+
Prepare a file `local.properties` based on [local.properties.TEMPLATE](https://github.com/worker8/AndroidLintReporter/blob/master/local.properties.TEMPLATE) in the root directory of this project, with the following content:
200193

201194
```
202-
github_token="<obtain your personall access token from Github"
195+
github_token=<replace with your Github Personal Access Token (PAT)>
196+
github_owner=<replace with your Github Owner Name>
197+
github_repository=<replace with your Github Repository Name>
203198
```
204199

205200
You can test your Github API using your PAT using cURL:
@@ -208,15 +203,15 @@ curl -H "Authorization: token <github_token>" -H "Content-Type: application/json
208203
```
209204

210205
**run Functional Test**
211-
212-
To run the test, use this command:
206+
The gist of this plugin is located in the class of `AndroidLintReporterPlugin.kt`.
207+
After setting everything up, you can run the test using this command:
213208

214209
```
215210
./gradlew functionalTest
216211
```
217212

218213
To deploy:
219-
1. Download secrets from https://plugins.gradle.org/ after login.
214+
1. Download secrets from https://plugins.gradle.org/ after logging in.
220215
2. up version in `build.gradle.kts`
221216
3. then run `./gradlew publishPlugin`
222217

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object Constant {
1313
val pluginName = "AndroidLintReporterPlugin"
1414
val id = "com.worker8.android_lint_reporter"
1515
val implementationClass = "android_lint_reporter.AndroidLintReporterPlugin"
16-
val version = "2.0.0"
16+
val version = "2.1.0"
1717
val website = "https://github.com/worker8/AndroidLintReporter"
1818
val displayName = "Android Lint Reporter"
1919
val description = "Gradle Plugin to parse, format, report Android Lint result back to Github Pull Request using Github Actions"

local.properties.TEMPLATE

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## This file must *NOT* be checked into Version Control Systems,
2+
# as it contains information specific to your local configuration.
3+
#
4+
# Location of the SDK. This is only used by Gradle.
5+
# For customization when using a Version Control System, please read the
6+
# header note.
7+
#Tue May 19 17:00:16 JST 2020
8+
9+
# rename this file into local.properties by removing '.TEMPLATE' from the filename
10+
sdk.dir=<replace with your sdk location>
11+
12+
# you don't need quotes to surround the string, e.g. github_token=ASDF123123123
13+
github_token=<replace with your Github Personal Access Token>
14+
github_owner=<replace with your Github Owner Name>
15+
github_repository=<replace with your Github Repository Name>

src/functionalTest/kotlin/android_lint_reporter/AndroidLintReporterPluginFunctionalTest.kt

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import java.io.File
88
import org.gradle.testkit.runner.GradleRunner
99
import java.io.FileInputStream
1010
import java.io.FileNotFoundException
11+
import java.lang.Exception
1112
import java.lang.StringBuilder
1213
import java.util.*
1314
import kotlin.test.Test
@@ -18,7 +19,10 @@ class AndroidLintReporterPluginFunctionalTest {
1819
val sb = StringBuilder().apply {
1920
appendln("github_token property cannot be found in local.properties")
2021
appendln("please prepare local.properties in the root directory")
21-
appendln("and set 'github_token=abcdefgh123456'")
22+
appendln("and set the following content:")
23+
appendln(" github_token=\"abcdefgh123456\"")
24+
appendln(" github_owner=\"worker8(replace with your project username)\"")
25+
appendln(" github_repository_name=\"SampleProjectName\"")
2226
appendln("otherwise, this functional test will fail because it needs a github personal access token to work")
2327
}
2428
sb.toString()
@@ -35,36 +39,50 @@ class AndroidLintReporterPluginFunctionalTest {
3539
id('com.worker8.android_lint_reporter')
3640
}
3741
android_lint_reporter {
38-
lintFilePath = "${File("").absolutePath}/src/main/resources/lint-results.xml"
39-
githubUsername = "worker8"
40-
githubRepositoryName = "SimpleCurrency"
42+
lintFilePath = "${File("").absolutePath}/lint-results.xml"
43+
detektFilePath = "${File("").absolutePath}/detekt_report.xml"
44+
githubOwner= "${getProperty("github_owner")}"
45+
githubRepositoryName = "${getProperty("github_repository")}"
46+
showLog = true
4147
}
4248
""")
4349

4450
// Run the build
4551
val runner = GradleRunner.create()
4652
runner.forwardOutput()
4753
runner.withPluginClasspath()
48-
runner.withArguments(listOf("parseAndSendLintResult", "-PgithubToken=${getGithubToken()}", "-PgithubPullRequestId=360"))
54+
println("getProperty(\"github_token\"): ${getProperty("github_token")}")
55+
println("getProperty(\"github_owner\"): ${getProperty("github_owner")}")
56+
println("getProperty(\"github_repository\"): ${getProperty("github_repository")}")
57+
runner.withArguments(listOf("report", "-PgithubToken=${getProperty("github_token")}", "-PisDebug=true", "-PgithubPullRequestId=366", "--stacktrace"))
4958
runner.withProjectDir(projectDir)
50-
val result = runner.build()
51-
println("output: ${result.output}")
59+
try {
60+
val result = runner.build()
61+
println("function test ended with the following result: ${result.output}")
62+
} catch (e: Exception) {
63+
e.printStackTrace()
64+
}
65+
5266
assertTrue(true)
5367
}
5468

55-
private fun getGithubToken(): String {
69+
private fun getProperty(propertyName: String): String {
70+
val props = getProperties()
71+
if (props[propertyName] == null) {
72+
error(noLocalPropertiesErrorMessage)
73+
}
74+
return props[propertyName] as String
75+
}
76+
77+
private fun getProperties(): Properties {
5678
val props = Properties()
5779
val localPropertyFile: File
5880
try {
5981
localPropertyFile = File("local.properties")
6082
props.load(FileInputStream(localPropertyFile))
61-
if (props["github_token"] == null) {
62-
error(noLocalPropertiesErrorMessage)
63-
}
64-
6583
} catch (e: FileNotFoundException) {
6684
error(noLocalPropertiesErrorMessage)
6785
}
68-
return props["github_token"] as String
86+
return props
6987
}
7088
}

0 commit comments

Comments
 (0)