Skip to content
This repository was archived by the owner on Jun 12, 2022. It is now read-only.

Commit e43edaa

Browse files
committed
Completed query diff and post comment to revision
1 parent 93a3227 commit e43edaa

File tree

9 files changed

+204
-21
lines changed

9 files changed

+204
-21
lines changed

pom.xml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,38 @@
5151
<version>2.6</version>
5252
</dependency>
5353

54-
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
5554
<dependency>
5655
<groupId>org.apache.httpcomponents</groupId>
5756
<artifactId>httpclient</artifactId>
5857
<version>4.5.3</version>
59-
<scope>provided</scope>
58+
<!--<scope>provided</scope>-->
6059
</dependency>
6160

62-
<!-- https://mvnrepository.com/artifact/org.json/json -->
6361
<dependency>
6462
<groupId>org.json</groupId>
6563
<artifactId>json</artifactId>
6664
<version>20170516</version>
6765
</dependency>
6866

67+
<dependency>
68+
<groupId>commons-beanutils</groupId>
69+
<artifactId>commons-beanutils</artifactId>
70+
<version>1.9.3</version>
71+
</dependency>
72+
73+
<dependency>
74+
<groupId>commons-io</groupId>
75+
<artifactId>commons-io</artifactId>
76+
<version>2.5</version>
77+
</dependency>
78+
79+
80+
<!-- Just for test, should be remove when submitting -->
81+
<dependency>
82+
<groupId>org.springframework.boot</groupId>
83+
<artifactId>spring-boot-starter-web</artifactId>
84+
<version>1.5.4.RELEASE</version>
85+
</dependency>
6986

7087
<dependency>
7188
<groupId>org.jetbrains.kotlin</groupId>

src/main/java/io/nthienan/phdiff/PhabricatorDifferentialBot.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.nthienan.phdiff;
22

3-
import io.nthienan.phdiff.differential.DifferentialRevision;
43
import io.nthienan.phdiff.issue.IssueComparator;
54
import io.nthienan.phdiff.report.GlobalReportBuilder;
65
import org.sonar.api.batch.postjob.PostJob;
@@ -36,8 +35,8 @@ public PhabricatorDifferentialBot(GlobalReportBuilder reportBuilder, Settings se
3635
@Override
3736
public void describe(PostJobDescriptor descriptor) {
3837
descriptor
39-
.name("Phabricator DifferentialRevision Issue Publisher")
40-
.requireProperty(PhabricatorDifferentialPlugin.PHID);
38+
.name("Phabricator Differential Issue Publisher")
39+
.requireProperty(PhabricatorDifferentialPlugin.DIFF_ID);
4140
}
4241

4342
@Override
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.nthienan.phdiff
2+
3+
import io.nthienan.phdiff.conduit.ConduitClient
4+
import io.nthienan.phdiff.conduit.DifferentialClient
5+
import io.nthienan.phdiff.differential.Diff
6+
import org.springframework.boot.SpringApplication
7+
import org.springframework.boot.autoconfigure.SpringBootApplication
8+
import org.springframework.context.annotation.Configuration
9+
import org.springframework.web.bind.annotation.PostMapping
10+
import org.springframework.web.bind.annotation.RestController
11+
12+
/**
13+
* Created on 19-Jul-17.
14+
* @author nthienan
15+
*/
16+
@SpringBootApplication
17+
@RestController
18+
open class Application {
19+
20+
private val conduitClient = ConduitClient("https://test-hn6cabpfte2a.phacility.com", "api-nyma35ds7tjetuz6rtcvuqvi3yro");
21+
private val differentialClient = DifferentialClient(conduitClient)
22+
23+
@PostMapping
24+
fun test(): Diff {
25+
differentialClient.postComment("1", "**SonarQube** analysis reported **5** issues" +
26+
"- 1 Blocker issue ({icon bug color=red})\\n- 1 Critical issue ({icon arrow-circle-up color=red})\\n- 1 Major issue ({icon chevron-circle-up color=red})\\n- 1 Minor issue ({icon info-circle color=green})\\n- 1 Info issue ({icon chevron-circle-down color=green})\\n**Details as below**:\\n{icon bug color=red} //Line 15// - `src/main/java/com/tma/dc4b/ci/MyBadImpl.java`: A \"NullPointerException\" could be thrown; \"str\" is nullable here. [[ http://192.168.88.57:9000/coding_rules#rule_key=squid%3AS2259 | View rule ]]\\n{icon arrow-circle-up color=red} //Line 15// - `src/main/java/com/tma/dc4b/ci/MyBadImpl.java`: A \"NullPointerException\" could be thrown; \"str\" is nullable here. [[ http://192.168.88.57:9000/coding_rules#rule_key=squid%3AS2259 | View rule ]]\\n{icon chevron-circle-up color=red} //Line 15// - `src/main/java/com/tma/dc4b/ci/MyBadImpl.java`: A \"NullPointerException\" could be thrown; \"str\" is nullable here. [[ http://192.168.88.57:9000/coding_rules#rule_key=squid%3AS2259 | View rule ]]\\n{icon info-circle color=green} //Line 15// - `src/main/java/com/tma/dc4b/ci/MyBadImpl.java`: A \"NullPointerException\" could be thrown; \"str\" is nullable here. [[ http://192.168.88.57:9000/coding_rules#rule_key=squid%3AS2259 | View rule ]]\\n{icon chevron-circle-down color=green} //Line 15// - `src/main/java/com/tma/dc4b/ci/MyBadImpl.java`: A \"NullPointerException\" could be thrown; \"str\" is nullable here. [[ http://192.168.88.57:9000/coding_rules#rule_key=squid%3AS2259 | View rule ]]")
27+
return differentialClient.fetchDiff("1")
28+
}
29+
30+
}
31+
32+
fun main(args: Array<String>) {
33+
SpringApplication.run(Application::class.java, *args)
34+
}

src/main/kotlin/io/nthienan/phdiff/conduit/ConduitClient.kt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,45 @@
11
package io.nthienan.phdiff.conduit
22

3+
import org.apache.commons.io.IOUtils
4+
import org.apache.http.HttpStatus
35
import org.apache.http.NameValuePair
46
import org.apache.http.client.entity.UrlEncodedFormEntity
57
import org.apache.http.client.methods.HttpPost
68
import org.apache.http.impl.client.HttpClientBuilder
79
import org.apache.http.message.BasicNameValuePair
810
import org.json.JSONObject
9-
import org.sonar.api.internal.google.gson.JsonObject
1011
import java.net.URL
11-
import java.util.*
12+
import java.util.ArrayList
1213

1314
/**
1415
* Created on 17-Jul-17.
1516
* @author nthienan
1617
*/
1718
class ConduitClient(var url: String, var token: String) {
1819

19-
fun perform(action: String, params: JSONObject): JsonObject? {
20+
fun perform(action: String, params: JSONObject): JSONObject {
2021
val httpClient = HttpClientBuilder.create().build()
2122
val postRequest = makeRequest(action, params)
22-
return null
23+
val response = httpClient.execute(postRequest)
24+
val responseBody = IOUtils.toString(response.entity.content, Charsets.UTF_8)
25+
if (response.statusLine.statusCode != HttpStatus.SC_OK) {
26+
throw ConduitException(responseBody, response.statusLine.statusCode)
27+
}
28+
val result = JSONObject(responseBody)
29+
val errorInfo = result.get("error_info").toString()
30+
if (!(result.get("error_code").toString().equals("null")
31+
&& errorInfo.equals("null"))) {
32+
throw ConduitException(errorInfo, response.statusLine.statusCode)
33+
}
34+
return result
2335
}
2436

2537
private fun makeRequest(action: String, params: JSONObject): HttpPost {
2638
val postRequest = HttpPost(URL(URL(URL(url), "/api/"), action).toURI())
2739

2840
val conduitMetadata = JSONObject()
29-
conduitMetadata.append("token", token)
30-
params.append("__conduit__", conduitMetadata)
41+
conduitMetadata.put("token", token)
42+
params.put("__conduit__", conduitMetadata)
3143

3244
val formData = ArrayList<NameValuePair>()
3345
formData.add(BasicNameValuePair("params", params.toString()))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.nthienan.phdiff.conduit
2+
3+
/**
4+
* Created on 19-Jul-17.
5+
* @author nthienan
6+
*/
7+
class ConduitException : Exception {
8+
val code: Int
9+
10+
constructor(message: String) : super(message) {
11+
this.code = 0
12+
}
13+
14+
constructor(message: String, code: Int) : super(message) {
15+
this.code = code
16+
}
17+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package io.nthienan.phdiff.conduit
2+
3+
import io.nthienan.phdiff.differential.Diff
4+
import org.json.JSONException
5+
import org.json.JSONObject
6+
import java.io.IOException
7+
import java.util.Date
8+
9+
/**
10+
* Created on 19-Jul-17.
11+
* @author nthienan
12+
*/
13+
class DifferentialClient(val conduitClient: ConduitClient) {
14+
15+
/**
16+
* Posts a comment to a differential
17+
* @param revisionID the revision ID (e.g. "D1234" without the "D")
18+
* @param message the content of the comment
19+
* @param silent whether or not to trigger an email
20+
* @param action phabricator comment action, e.g. 'resign', 'reject', 'none'
21+
* @return the Conduit API response
22+
* @throws IOException if there is a network error talking to Conduit
23+
* @throws ConduitException if any error is experienced talking to Conduit
24+
*/
25+
@Throws(IOException::class, ConduitException::class)
26+
fun postComment(revisionID: String, message: String, silent: Boolean, action: String): JSONObject {
27+
var params = JSONObject()
28+
params.put("revision_id", revisionID)
29+
params.put("action", action)
30+
params.put("message", message)
31+
params.put("silent", silent)
32+
return conduitClient.perform("differential.createcomment", params)
33+
}
34+
35+
/**
36+
* Fetch a differential from Conduit
37+
* @return the Conduit API response
38+
* @throws IOException if there is a network error talking to Conduit
39+
* @throws ConduitException if any error is experienced talking to Conduit
40+
*/
41+
@Throws(IOException::class, ConduitException::class)
42+
fun fetchDiff(diffID: String): Diff {
43+
val params = JSONObject().put("ids", arrayOf(diffID))
44+
val query = conduitClient.perform("differential.querydiffs", params)
45+
val response: JSONObject
46+
try {
47+
response = query.getJSONObject("result")
48+
} catch (e: JSONException) {
49+
throw ConduitException(
50+
String.format("No 'result' object found in conduit call: (%s) %s",
51+
e.message, query.toString(2)))
52+
}
53+
54+
val diffJsonObj = response.getJSONObject(diffID)
55+
val diff = Diff()
56+
diff.id = diffJsonObj.get("id").toString()
57+
diff.revisionId = diffJsonObj.getString("revisionID")
58+
// diff.branch = diffJsonObj.getString("branch")
59+
diff.dateCreated = Date(diffJsonObj.getLong("dateCreated"))
60+
diff.dateModified = Date(diffJsonObj.getLong("dateModified"))
61+
return diff
62+
63+
}
64+
65+
/**
66+
* Post a comment on the differential
67+
* @param revisionID the revision ID (e.g. "D1234" without the "D")
68+
* @param message the string message to post
69+
* @return the Conduit API response
70+
* @throws IOException if there is a network error talking to Conduit
71+
* @throws ConduitException if any error is experienced talking to Conduit
72+
*/
73+
@Throws(ConduitException::class, IOException::class)
74+
fun postComment(revisionID: String, message: String): JSONObject {
75+
return postComment(revisionID, message, true, "none")
76+
}
77+
78+
/**
79+
* Fetch the commit message for the revision. This isn't available on the diff, so it requires a separate query.
80+
* @param revisionID The ID of the revision, e.g. for "D123" this would be "123"
81+
* @return A \n-separated string of the commit message
82+
* @throws ConduitException
83+
* @throws IOException
84+
*/
85+
@Throws(ConduitException::class, IOException::class)
86+
fun getCommitMessage(revisionID: String): String {
87+
val params = JSONObject().put("revision_id", revisionID)
88+
val query = conduitClient.perform("differential.getcommitmessage", params)
89+
90+
// NOTE: When you run this with `arc call-conduit dfferential.getcommitmessage` (from the command-line),
91+
// it comes back as "response". But it's "result" when running through this conduit API.
92+
return query.getString("result")
93+
}
94+
95+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.nthienan.phdiff.differential
2+
3+
import java.util.*
4+
5+
/**
6+
* Created on 18/07/2017.
7+
* @author nthienan
8+
*/
9+
class Diff {
10+
var id: String = ""
11+
var revisionId: String = ""
12+
var dateCreated: Date? = null
13+
var dateModified: Date? = null
14+
var branch: String = ""
15+
var unitStatus: Int = 0
16+
var lintStatus: Int = 0
17+
}

src/main/kotlin/io/nthienan/phdiff/differential/DifferentialDiff.kt

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/main/kotlin/io/nthienan/phdiff/differential/DifferentialRevision.kt renamed to src/main/kotlin/io/nthienan/phdiff/differential/Revision.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import java.util.*
66
* Created on 17-Jul-17.
77
* @author nthienan
88
*/
9-
class DifferentialRevision(var id: String, var phid: String) {
9+
class Revision(var id: String, var phid: String) {
1010
var title: String = ""
1111
var uri: String = ""
1212
var dateCreated: Date? = null

0 commit comments

Comments
 (0)