I am making a Java based CLI application in which i can search Github repository and their latest tags. For this I built an program which calls Github API and captures the highest stars repository name and its owner. This functionality is properly working when you run below code it returns "cbeust".
My second step is to capture the latest tags using owner and repository name(url2) but when I call my program to capture it does run but does not produce expected output.
Please look at my program and let me know what i am doing wrong?
package com.github_search;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import com.jayway.jsonpath.JsonPath;
import org.apache.log4j.BasicConfigurator;
import org.json.JSONException;
import org.json.JSONObject;
public class github_search
{
public static void main(String[] args) {
try {
BasicConfigurator.configure();
String Name = "TESTNG";
String Tags = "6.4.0";
String url1 = "https://api.github.com/search/repositories?q="+ Name +"&sort=stars&order=desc";
List authors = JsonPath.read(github_search.search(url1).toString(), "$.items[*].owner.login");
System.out.println("Owner: (" + authors.get(0) +")");
String url2 = "https://api.github.com/repos/"+authors.get(0)+"/"+Name+"/tags";
List latest_tags = JsonPath.read(github_search.search(url2).toString(), "$..name");
System.out.println("first tag: (" + latest_tags.get(0) +")");
} catch (Exception e) {
// TODO: handle exception
}
}
public static String search(String url) throws IOException, JSONException {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
JSONObject obj_JSONObject = new JSONObject(response.toString());
return obj_JSONObject.toString();
}
}
Current Output
0 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['items'][*]['owner']['login']
Owner: (cbeust)
Expected Output
0 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['items'][*]['owner']['login']
Owner: (cbeust)
first tag: (testng-6.9.5)
JSON response for the tags request
[
{
"name": "testng-6.9.5",
"zipball_url": "https://api.github.com/repos/cbeust/testng/zipball/testng-6.9.5",
"tarball_url": "https://api.github.com/repos/cbeust/testng/tarball/testng-6.9.5",
"commit": {
"sha": "ef2d1199abff4e1b8fa4b1148c1314e776d7a044",
"url": "https://api.github.com/repos/cbeust/testng/commits/ef2d1199abff4e1b8fa4b1148c1314e776d7a044"
},
"node_id": "MDM6UmVmNzQ1NzQ5OnRlc3RuZy02LjkuNQ=="
},
{
...
}
]