Skip to content

Commit 4355ca6

Browse files
Adding JSon rest JIRA
1 parent 008701d commit 4355ca6

File tree

3 files changed

+101
-7
lines changed

3 files changed

+101
-7
lines changed

pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1717
<maven.compiler.source>1.7</maven.compiler.source>
1818
<maven.compiler.target>1.7</maven.compiler.target>
19+
<json-p.spec.version>1.0-alpha-1</json-p.spec.version>
20+
<johnzon.version>1.1.7</johnzon.version>
1921
</properties>
2022

2123
<dependencies>
@@ -30,6 +32,22 @@
3032
<artifactId>org.eclipse.jgit</artifactId>
3133
<version>4.8.0.201705170830-rc1</version>
3234
</dependency>
35+
<dependency>
36+
<groupId>com.squareup.okhttp3</groupId>
37+
<artifactId>okhttp</artifactId>
38+
<version>3.10.0</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.apache.johnzon</groupId>
42+
<artifactId>johnzon-core</artifactId>
43+
<version>${johnzon.version}</version>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.apache.geronimo.specs</groupId>
48+
<artifactId>geronimo-json_1.1_spec</artifactId>
49+
<version>1.0</version>
50+
</dependency>
3351
</dependencies>
3452

3553
<build>

src/main/java/org/clebert/ArtemisParser.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,21 @@ public class ArtemisParser {
2929

3030
public static void main(String arg[]) {
3131
try {
32-
if (arg.length < 4) {
33-
System.err.println("use Parser <repository> <reportOutput> <from> <to>");
32+
if (arg.length != 5) {
33+
System.err.println("use Parser <repository> <reportOutput> <from> <to> <rest : true|false>");
3434
System.exit(-1);
3535
}
3636

37+
boolean rest = Boolean.parseBoolean(arg[4]);
38+
3739
GitParser parser = new GitParser(new File(arg[0]), "ARTEMIS-", "https://issues.apache.org/jira/browse/", "https://github.com/apache/activemq-artemis/").
3840
setSourceSuffix(".java", ".md", ".c", ".sh", ".groovy").
3941
setSampleJQL("https://issues.apache.org/jira/issues/?jql=project%20%3D%20ARTEMIS%20AND%20key%20in%20");
42+
43+
if (rest) {
44+
parser.setRestLocation("https://issues.apache.org/jira/rest/api/2/issue/");
45+
}
46+
4047
parser.addInterestingfolder("test").addInterestingfolder("docs/").addInterestingfolder("examples/");
4148
PrintStream stream = new PrintStream(new FileOutputStream(arg[1]));
4249
parser.parse(stream, arg[2], arg[3]);

src/main/java/org/clebert/GitParser.java

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717

1818
package org.clebert;
1919

20+
import javax.json.Json;
21+
import javax.json.JsonObject;
22+
import javax.json.JsonString;
2023
import java.io.ByteArrayOutputStream;
2124
import java.io.File;
2225
import java.io.IOException;
2326
import java.io.InputStream;
2427
import java.io.OutputStream;
2528
import java.io.PrintStream;
29+
import java.net.URL;
2630
import java.text.DateFormat;
2731
import java.text.SimpleDateFormat;
2832
import java.util.ArrayList;
@@ -48,6 +52,8 @@
4852
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
4953
import org.eclipse.jgit.util.io.DisabledOutputStream;
5054

55+
import org.apache.johnzon.core.*;
56+
5157
/**
5258
* @author Clebert Suconic
5359
*/
@@ -59,9 +65,11 @@ public class GitParser {
5965
final String jira;
6066
final String jiraBrowseURI;
6167
final String githubURI;
68+
String restLocation;
6269
String[] sourceSuffix;
6370
// JQL used to list all JIRAs here
6471
String sampleJQL;
72+
String[] currentJiras;
6573

6674
final HashSet<String> totalJiras = new HashSet<>();
6775

@@ -101,6 +109,15 @@ public GitParser setSourceSuffix(String... sourceSuffix) {
101109
return this;
102110
}
103111

112+
public String getRestLocation() {
113+
return restLocation;
114+
}
115+
116+
public GitParser setRestLocation(String restLocation) {
117+
this.restLocation = restLocation;
118+
return this;
119+
}
120+
104121
private String makeALink(String text, String uri) {
105122
return "<a href='" + uri + "'>" + text + "</a>";
106123
}
@@ -132,10 +149,10 @@ public static String[] extractJIRAs(String jira, String message) {
132149
}
133150

134151
public String prettyCommitMessage(String message) {
135-
String[] jiras = extractJIRAs(jira, message);
136-
for (int i = 0; i < jiras.length; i++) {
137-
totalJiras.add(jiras[i]);
138-
message = message.replace(jiras[i], makeALink(jiras[i], jiraBrowseURI + jiras[i]));
152+
currentJiras = extractJIRAs(jira, message);
153+
for (int i = 0; i < currentJiras.length; i++) {
154+
totalJiras.add(currentJiras[i]);
155+
message = message.replace(currentJiras[i], makeALink(currentJiras[i], jiraBrowseURI + currentJiras[i]));
139156
}
140157

141158
return message;
@@ -159,6 +176,35 @@ private String readString(String fileName) throws Exception {
159176
return new String(out.toByteArray());
160177
}
161178

179+
JsonObject lastJIRAObject;
180+
String lastJIRA;
181+
182+
private JsonObject restJIRA(String JIRA) throws Exception {
183+
184+
System.out.println("Inspecting " + JIRA);
185+
if (lastJIRA != null && lastJIRA.equals(JIRA)) {
186+
return lastJIRAObject;
187+
}
188+
if (restLocation != null) {
189+
URL url = new URL(restLocation + JIRA);
190+
InputStream stream = url.openStream();
191+
192+
lastJIRA = JIRA;
193+
lastJIRAObject = Json.createReader(stream).readObject();
194+
return lastJIRAObject;
195+
}
196+
return null;
197+
}
198+
199+
private String getField(JsonObject object, String name) {
200+
201+
try {
202+
return object.getJsonObject("fields").getJsonObject(name).getString("name");
203+
} catch (Throwable e) {
204+
return " ";
205+
}
206+
}
207+
162208
public void parse(PrintStream output, String from, String to) throws Exception {
163209
Git git = Git.open(folder);
164210
RevWalk walk = new RevWalk(git.getRepository());
@@ -196,7 +242,7 @@ public void parse(PrintStream output, String from, String to) throws Exception {
196242

197243
StringBuffer interestingChanges[] = new StringBuffer[interestingFolder.size()];
198244

199-
output.print("<thead><tr><th>#</th><th>Commit</th><th>Date</th><th>Author</th><th>Short Message</th><th>Adds</th><th>Updates</th><th>Deletes</th>");
245+
output.print("<thead><tr><th>#</th><th>Commit</th><th>Date</th><th>Author</th><th>Short Message</th><th>Jira Status</th><th>Adds</th><th>Updates</th><th>Deletes</th>");
200246

201247
for (int i = 0; i < interestingFolder.size(); i++) {
202248
output.print("<th>" + interestingFolder.get(i) + "</th>");
@@ -225,6 +271,29 @@ public void parse(PrintStream output, String from, String to) throws Exception {
225271
output.print("<td>" + commit.getAuthorIdent().getName() + "</td>");
226272
output.print("<td>" + prettyCommitMessage(commit.getShortMessage()) + "</td>");
227273

274+
StringBuffer bufferJIRA = new StringBuffer();
275+
if (currentJiras != null) {
276+
for (int i = 0; i < currentJiras.length; i++) {
277+
278+
String jiraIteration = currentJiras[i];
279+
if (restLocation != null) {
280+
JsonObject object = restJIRA(jiraIteration);
281+
String issuetype = getField(object, "issuetype");
282+
String status = getField(object, "status");
283+
String resolution = getField(object, "resolution");
284+
bufferJIRA.append(makeALink(issuetype + "/" + resolution + "/" + status, jiraBrowseURI + jiraIteration));
285+
} else {
286+
bufferJIRA.append(makeALink(jiraIteration, jiraBrowseURI + jiraIteration));
287+
}
288+
289+
if (i < currentJiras.length -1) {
290+
bufferJIRA.append(",");
291+
}
292+
}
293+
294+
}
295+
output.println("<td>" + bufferJIRA.toString() + "</td>");
296+
228297
oldTreeIter.reset(reader, commit.getParent(0).getTree());
229298
newTreeIter.reset(reader, commit.getTree());
230299

0 commit comments

Comments
 (0)