简体   繁体   中英

Java Github repository search

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<String> 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<String> 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=="
  },
  {
  ...
  }
]

The issue here is that your second call to search() is throwing an Exception, you are swallowing the exception in your catch block so the main method fails and exits before it can print out latest_tags.get(0) hence you only see the output from this call: System.out.println(authors.get(0)) .

The response from your first call to the GitHub API looks like this:

{
  "total_count": 6345,
  "incomplete_results": false,
  "items": [...]
}

This is a single JSON 'object' so this invocation is successful: new JSONObject(response.toString()) .

However, the response to your second call to the GitHub API looks like this:

[
  {
    "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=="
  },
  ...
]

So, this is an array of JSON 'objects' (note the way it is enclosed by square brackets) and hence this invocation: new JSONObject(response.toString()) fails with ...

Value [...] of type org.json.JSONArray cannot be converted to JSONObject

So, that explains why you are only seeing your first expected output. Now, to fix the issue you could simply replace these lines ...

JSONObject obj_JSONObject = new JSONObject(response.toString());
return obj_JSONObject.toString();

... with this:

return response.toString()

And, for completeness, provide some implementation in the catch block in your main method so that you won't be mislead by a swallowed exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM