Descargando problemas de GitHub al file .csv

GitHub emite descargas usando eclipse. Egit no devuelve nada.

Recientemente, he estado intentando crear una aplicación de escritorio java (para Windows), que downloadá problemas de GitHub desde un repository de problemas de GitHub específico y los saveá en un file .csv.

Creé una GUI simple usando Swing para habilitar la input de nombres de repository. También estoy usando la biblioteca egit de eclipse para establecer una connection con GitHub para download problemas. Utilizo authentication, ingresé usando un file .properties para autenticar la connection de egit con GitHub.

Aquí está el código principal que usa mi aplicación para download los problemas y escribirlos en un file .csv:

package io.github.psgs.issuesdownload; import io.github.psgs.issuesdownload.gui.GUI; import org.eclipse.egit.github.core.Issue; import org.eclipse.egit.github.core.client.GitHubClient; import org.eclipse.egit.github.core.service.IssueService; import java.io.FileWriter; import java.io.IOException; public class IssuesDownload { public static void main(String[] args) { try { Config.loadConfiguration(); } catch(IOException ex) { } GUI.main(args); } public static String saveIssues(String repoDetails) { String[] repoInfo = repoDetails.split("/"); String repoOwner = repoInfo[0]; String repoName = repoInfo[1]; GitHubClient client = new GitHubClient(); client.setCnetworkingentials(Config.githubuser, Config.githubpass); IssueService issueService = new IssueService(client); try { FileWriter writer = new FileWriter("issues.csv"); //String[] header = {"Id", "Title", "Creator", "Assignee", "Milestone", "State", "Body Text"}; writer.append("Id, Title, Creator, Assignee, Milestone, State, Body Text"); writer.append("\n"); for (Issue issue : issueService.getIssues(repoOwner, repoName, null)) { //String[] data = {String.valueOf(issue.getId()), issue.getTitle(), issue.getUser().getName(), issue.getAssignee().getName(), issue.getMilestone().getTitle(), issue.getState(), issue.getBodyText()}; writer.append(String.valueOf(issue.getId()) + ","); writer.append(issue.getTitle() + ","); writer.append(issue.getUser().getName() + ","); writer.append(issue.getAssignee().getName() + ","); writer.append(issue.getMilestone().getTitle() + ","); writer.append(issue.getState() + ","); writer.append(issue.getBodyText()); writer.append("\n"); } writer.flush(); writer.close(); return "Download Complete!"; } catch (IOException ex) { System.out.println("An IOException has occunetworking!"); ex.printStackTrace(); if (ex.getMessage().equalsIgnoreCase("api.github.com")) { return "An error has occunetworking, reaching " + ex.getMessage() + "! Please check your network connection."; } } return "An error has occunetworking!"; } } 

Este código también está disponible en: https://gist.github.com/psgs/9048602

El repository completo se puede encontrar en: https://github.com/psgs/IssuesDownload

Cuando ejecuto este código, con el file .properties en el mismo directory que el file comstackr .jar, los problemas de GitHub no aparecen en el file .csv. Probé la salida del file .csv y los encabezados escriben correctamente cuando elimino el código de descarga.

¿Alguien sabría por qué está sucediendo esto? Tal vez es un problema de authentication que me he perdido?

Después de probar unos nuevos contenedores de API, encontré una biblioteca de API que funciona. Ahora estoy usando la API GitHub de Kohsuke Kawaguchi para Java para conectarme a GitHub.

 <dependency> <groupId>org.kohsuke</groupId> <artifactId>github-api</artifactId> <version>1.49</version> </dependency> 

Mi código ahora dice lo siguiente:

 package io.github.psgs.issuesdownload; import io.github.psgs.issuesdownload.gui.GUI; import org.kohsuke.github.GHIssue; import org.kohsuke.github.GHIssueState; import org.kohsuke.github.GHRepository; import org.kohsuke.github.GitHub; import java.io.FileWriter; import java.io.IOException; public class IssuesDownload { public static void main(String[] args) { try { Config.loadConfiguration(); } catch (IOException ex) { System.out.println("An IOException had occunetworking while loading the configuration!"); ex.printStackTrace(); } GUI.main(args); } public static String saveIssues(String repoDetails, GHIssueState issueState) { String[] repoInfo = repoDetails.split("/"); try { GitHub github = GitHub.connectUsingOAuth(Config.githubtoken); GHRepository repository = github.getUser(repoInfo[0]).getRepository(repoInfo[1]); FileWriter writer = new FileWriter("issues.csv"); writer.append("Id, Title, Creator, Assignee, Milestone, State, Body Text"); writer.append("\n"); for (GHIssue issue : repository.getIssues(issueState)) { writer.append(String.valueOf(issue.getNumber()) + ","); writer.append(issue.getTitle() + ","); writer.append(issue.getUser().getLogin() + ","); if (issue.getAssignee() != null) { writer.append(issue.getAssignee().getName() + ","); } else { writer.append(" ,"); } if (issue.getMilestone() != null) { writer.append(issue.getMilestone().getTitle() + ","); } else { writer.append(" ,"); } writer.append(issue.getState() + ","); writer.append(issue.getBody() + ","); writer.append("\n"); } writer.flush(); writer.close(); return "Download Complete!"; } catch (IOException ex) { System.out.println("An IOException has occunetworking!"); ex.printStackTrace(); if (ex.getMessage().equalsIgnoreCase("api.github.com")) { return "An error has occurnetworking reaching " + ex.getMessage() + "! Please check your network connection."; } } return "An error has occunetworking!"; } } 

Parece que el método que está utilizando debe utilizarse para recuperar los problemas del usuario autenticado actualmente .

Quizás esta testing (en el repository del proyecto) sería útil. La única diferencia que veo es que no pasas un singletonMap y no estoy seguro de que tengas que hacerlo.

 public static <T> void outputCsv(String fileName, String[] headerName, List<T> listResult, HttpServletRequest request) { HttpServletResponse response = ResponseUtil.getResponse(); OutputStream out; try { // Encode file name fileName = Util.encodeUnicodeFile(fileName, request); response.setStatus(HttpServletResponse.SC_OK); response.setContentType("application/octet-stream; charset=" + Constant.CONST_SJIS_ENCODING); response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\""); response.setCharacterEncoding(Constant.CONST_SJIS_ENCODING); out = response.getOutputStream(); CSVWriter writer = new CSVWriter(new OutputStreamWriter(out, Constant.CONST_WINDOW_JAPAN_ENCODING), Constant.CONST_CSV_SEPARATOR_COMMA); // Convert list result data to List <String[]> List<String[]> list = convertListDataToCsvArray(listResult, headerName); // Write list data writer.writeAll(list); writer.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } } private static <T> List<String[]> convertListDataToCsvArray( List<T> listResult, String[] headerName) { List<String[]> listCsv = new ArrayList<String[]>(); // First row is header listCsv.add(headerName); for (T object : listResult) { // Get all fields of object Field[] fields = object.getClass().getFields(); // Init row String[] row = new String[headerName.length]; int index = 0; for (Field field : fields) { try { if (field.getType().equals(Long.class)) { // Read field value and set to string array Long value = (Long) field.get(object); row[index] = value != null ? String.valueOf(value) : ""; } else if (field.getType().equals(String.class)) { // Read field value and set to string array String value = (String) field.get(object); row[index] = value != null ? value : ""; } else if (field.getType().equals(BigDecimal.class)) { // Read field value and set to string array BigDecimal value = (BigDecimal) field.get(object); row[index] = value != null ? String.valueOf(value) : ""; } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } index++; } // Add row listCsv.add(row); } return listCsv; }