Adds proper structure

This commit is contained in:
Julien Lengrand-Lambert
2021-03-11 10:04:08 +01:00
parent 384eec43fa
commit 28d2a0588e
6 changed files with 106 additions and 33 deletions

View File

@@ -38,6 +38,11 @@
<artifactId>github-api</artifactId> <artifactId>github-api</artifactId>
<version>1.122</version> <version>1.122</version>
</dependency> </dependency>
<dependency>
<groupId>org.javatuples</groupId>
<artifactId>javatuples</artifactId>
<version>1.2</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -1,5 +1,6 @@
package com.amazon.ask.githubtemplates.data; package com.amazon.ask.githubtemplates.data;
import org.javatuples.Pair;
import org.kohsuke.github.*; import org.kohsuke.github.*;
import java.io.File; import java.io.File;
@@ -15,10 +16,10 @@ import java.util.stream.StreamSupport;
public class GithubDataGrabber { public class GithubDataGrabber {
private Map<String, String> languagesAndTemplates = public static final Map<String, Pair<String, String>> TEMPLATES =
Map.of( Map.of(
"java", "https://github.com/Spring-Boot-Framework/Spring-Boot-Application-Template", "java", new Pair<>("Spring-Boot-Framework", "Spring-Boot-Application-Template"),
"typescript", "https://github.com/carsonfarmer/ts-template" "typescript", new Pair<>("carsonfarmer", "ts-template")
); );
private GitHub githubApi; private GitHub githubApi;
@@ -27,26 +28,37 @@ public class GithubDataGrabber {
this.githubApi = gitHub; this.githubApi = gitHub;
} }
// public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
// System.out.println("Grabbing data from your GitHub profile!"); System.out.println("Grabbing data from your GitHub profile!");
//
// Path githubConfigPath = Paths.get(Paths.get(System.getProperty("user.dir")).toString(), ".github");
// if(!new File(githubConfigPath.toString()).exists()){
// System.out.println("No GitHub config file found, exiting.");
// System.exit(0);
// }
//
// GitHub github = GitHubBuilder.fromPropertyFile(githubConfigPath.toString()).build();
//
// GithubDataGrabber githubHello = new GithubDataGrabber(github);
//
// githubHello.getLanguages("jlengrand").forEach(System.out::println);
// System.out.println("------");
// githubHello.getRepositories("jlengrand").forEach(System.out::println);
// }
public Set<String> getLanguages(String username) throws IOException { Path githubConfigPath = Paths.get(Paths.get(System.getProperty("user.dir")).toString(), ".githubconfig");
PagedIterable<GHRepository> repos = this.githubApi.getUser(username).listRepositories(); if(!new File(githubConfigPath.toString()).exists()){
System.out.println("No GitHub config file found, exiting.");
System.exit(0);
}
GitHub github = GitHubBuilder.fromPropertyFile(githubConfigPath.toString()).build();
GithubDataGrabber githubHello = new GithubDataGrabber(github);
githubHello.getLanguages().forEach(System.out::println);
System.out.println("------");
githubHello.getRepositories().forEach(System.out::println);
System.out.println("------");
System.out.println(TEMPLATES.get("java").getValue0());
System.out.println(TEMPLATES.get("java").getValue1());
// github.createRepository("theoneandonlytest")
// .fromTemplateRepository(
// TEMPLATES.get("java").getValue0(),
// TEMPLATES.get("java").getValue1()
// ).create();
}
public Set<String> getLanguages() throws IOException {
PagedIterable<GHRepository> repos = this.githubApi.getMyself().listRepositories();
return StreamSupport.stream(repos.spliterator(), false) return StreamSupport.stream(repos.spliterator(), false)
.map(repo -> repo.getLanguage()) .map(repo -> repo.getLanguage())
@@ -54,8 +66,8 @@ public class GithubDataGrabber {
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }
public List<String> getRepositories(String username) throws IOException { public List<String> getRepositories() throws IOException {
PagedIterable<GHRepository> repos = this.githubApi.getUser(username).listRepositories(); PagedIterable<GHRepository> repos = this.githubApi.getMyself().listRepositories();
return StreamSupport.stream(repos.spliterator(), false) return StreamSupport.stream(repos.spliterator(), false)
.map(repo -> sanitize(repo.getName())) .map(repo -> sanitize(repo.getName()))

View File

@@ -12,10 +12,13 @@ package com.amazon.ask.githubtemplates.handlers;
import com.amazon.ask.dispatcher.request.handler.HandlerInput; import com.amazon.ask.dispatcher.request.handler.HandlerInput;
import com.amazon.ask.dispatcher.request.handler.RequestHandler; import com.amazon.ask.dispatcher.request.handler.RequestHandler;
import com.amazon.ask.githubtemplates.handlers.github.GithubAPI;
import com.amazon.ask.githubtemplates.handlers.github.GithubInitException;
import com.amazon.ask.model.IntentRequest; import com.amazon.ask.model.IntentRequest;
import com.amazon.ask.model.Response; import com.amazon.ask.model.Response;
import com.amazon.ask.model.Slot; import com.amazon.ask.model.Slot;
import java.io.IOException;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.logging.Level; import java.util.logging.Level;
@@ -34,6 +37,7 @@ public class CreateRepositoryIntentHandler implements RequestHandler {
public Optional<Response> handle(HandlerInput input) { public Optional<Response> handle(HandlerInput input) {
Logger logger = Logger.getAnonymousLogger(); Logger logger = Logger.getAnonymousLogger();
// Not recommended in prod :)
String username = System.getenv("GH_USERNAME"); String username = System.getenv("GH_USERNAME");
String key = System.getenv("GH_APIKEY"); String key = System.getenv("GH_APIKEY");
@@ -41,17 +45,29 @@ public class CreateRepositoryIntentHandler implements RequestHandler {
Map<String, Slot> slots = intentRequest.getIntent().getSlots(); Map<String, Slot> slots = intentRequest.getIntent().getSlots();
for (Map.Entry<String, Slot> entry : slots.entrySet()) { for (Map.Entry<String, Slot> entry : slots.entrySet()) {
logger.log(Level.SEVERE, entry.getKey() + ":" + entry.getValue().getValue() + ":" + entry.getValue().getName()); logger.log(Level.FINE, entry.getKey() + ":" + entry.getValue().getValue() + ":" + entry.getValue().getName());
} }
String speechText; String speechText;
if(username != null && key != null) {
try{
GithubAPI api = new GithubAPI();
String language = slots.get("language").getValue(); String language = slots.get("language").getValue();
String title = slots.get("title").getValue(); String title = slots.get("title").getValue();
speechText = "Let's go with " + language + " and " + title + " !"; api.getGithub().createRepository("test")
} .fromTemplateRepository(
else{ api.TEMPLATES.get(language).getValue0(),
speechText = "Sorry, you are not logged in! I cannot create new repositories"; api.TEMPLATES.get(language).getValue1()
).create();
speechText = "Great! I'm creating a " + language + " repository called " + title + " !";
} catch (GithubInitException e) {
speechText = "Sorry, you are not logged in! I cannot create new repositories";
} catch (IOException e) {
speechText = "Sorry, error when trying to create the repository. Please try again later";
logger.log(Level.SEVERE, e.getMessage());
} }
return input.getResponseBuilder() return input.getResponseBuilder()

View File

@@ -15,6 +15,8 @@ import com.amazon.ask.dispatcher.request.handler.RequestHandler;
import com.amazon.ask.model.Response; import com.amazon.ask.model.Response;
import java.util.Optional; import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.amazon.ask.request.Predicates.intentName; import static com.amazon.ask.request.Predicates.intentName;
@@ -27,10 +29,10 @@ public class LoggedInIntentHandler implements RequestHandler {
@Override @Override
public Optional<Response> handle(HandlerInput input) { public Optional<Response> handle(HandlerInput input) {
String username = System.getenv("GH_USERNAME"); String username = System.getenv("GITHUB_LOGIN");
String key = System.getenv("GH_APIKEY"); String key = System.getenv("GITHUB_OAUTH");
String speechText = username == null || key == null ? "Sorry, you are not logged in!" : "Logged in as " + username; String speechText = username == null || key == null ? "Sorry, you are currently not logged in." : "Yes! You are logged in as " + username;
return input.getResponseBuilder() return input.getResponseBuilder()
.withSpeech(speechText) .withSpeech(speechText)
.build(); .build();

View File

@@ -0,0 +1,31 @@
package com.amazon.ask.githubtemplates.handlers.github;
import org.javatuples.Pair;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import java.io.IOException;
import java.util.Map;
public class GithubAPI {
public static final Map<String, Pair<String, String>> TEMPLATES =
Map.of(
"java", new Pair<>("Spring-Boot-Framework", "Spring-Boot-Application-Template"),
"typescript", new Pair<>("carsonfarmer", "ts-template")
);
private GitHub github;
public GithubAPI() throws GithubInitException {
try {
github = GitHubBuilder.fromEnvironment().build();
} catch (IOException e) {
throw new GithubInitException("Impossible to login the Github API");
}
}
public GitHub getGithub() {
return github;
}
}

View File

@@ -0,0 +1,7 @@
package com.amazon.ask.githubtemplates.handlers.github;
public class GithubInitException extends Exception{
public GithubInitException(String errorMessage) {
super(errorMessage);
}
}