From 28d2a0588eced3c5da68e1ca24a159cdd74402c6 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Thu, 11 Mar 2021 10:04:08 +0100 Subject: [PATCH] Adds proper structure --- lambda/pom.xml | 5 ++ .../data/GithubDataGrabber.java | 60 +++++++++++-------- .../CreateRepositoryIntentHandler.java | 28 +++++++-- .../handlers/LoggedInIntentHandler.java | 8 ++- .../handlers/github/GithubAPI.java | 31 ++++++++++ .../handlers/github/GithubInitException.java | 7 +++ 6 files changed, 106 insertions(+), 33 deletions(-) create mode 100644 lambda/src/com/amazon/ask/githubtemplates/handlers/github/GithubAPI.java create mode 100644 lambda/src/com/amazon/ask/githubtemplates/handlers/github/GithubInitException.java diff --git a/lambda/pom.xml b/lambda/pom.xml index 014469f..4247124 100644 --- a/lambda/pom.xml +++ b/lambda/pom.xml @@ -38,6 +38,11 @@ github-api 1.122 + + org.javatuples + javatuples + 1.2 + diff --git a/lambda/src/com/amazon/ask/githubtemplates/data/GithubDataGrabber.java b/lambda/src/com/amazon/ask/githubtemplates/data/GithubDataGrabber.java index e83f77c..28fea2c 100644 --- a/lambda/src/com/amazon/ask/githubtemplates/data/GithubDataGrabber.java +++ b/lambda/src/com/amazon/ask/githubtemplates/data/GithubDataGrabber.java @@ -1,5 +1,6 @@ package com.amazon.ask.githubtemplates.data; +import org.javatuples.Pair; import org.kohsuke.github.*; import java.io.File; @@ -15,10 +16,10 @@ import java.util.stream.StreamSupport; public class GithubDataGrabber { - private Map languagesAndTemplates = + public static final Map> TEMPLATES = Map.of( - "java", "https://github.com/Spring-Boot-Framework/Spring-Boot-Application-Template", - "typescript", "https://github.com/carsonfarmer/ts-template" + "java", new Pair<>("Spring-Boot-Framework", "Spring-Boot-Application-Template"), + "typescript", new Pair<>("carsonfarmer", "ts-template") ); private GitHub githubApi; @@ -27,26 +28,37 @@ public class GithubDataGrabber { this.githubApi = gitHub; } -// public static void main(String[] args) throws IOException { -// 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 static void main(String[] args) throws IOException { + System.out.println("Grabbing data from your GitHub profile!"); - public Set getLanguages(String username) throws IOException { - PagedIterable repos = this.githubApi.getUser(username).listRepositories(); + Path githubConfigPath = Paths.get(Paths.get(System.getProperty("user.dir")).toString(), ".githubconfig"); + 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 getLanguages() throws IOException { + PagedIterable repos = this.githubApi.getMyself().listRepositories(); return StreamSupport.stream(repos.spliterator(), false) .map(repo -> repo.getLanguage()) @@ -54,8 +66,8 @@ public class GithubDataGrabber { .collect(Collectors.toSet()); } - public List getRepositories(String username) throws IOException { - PagedIterable repos = this.githubApi.getUser(username).listRepositories(); + public List getRepositories() throws IOException { + PagedIterable repos = this.githubApi.getMyself().listRepositories(); return StreamSupport.stream(repos.spliterator(), false) .map(repo -> sanitize(repo.getName())) diff --git a/lambda/src/com/amazon/ask/githubtemplates/handlers/CreateRepositoryIntentHandler.java b/lambda/src/com/amazon/ask/githubtemplates/handlers/CreateRepositoryIntentHandler.java index f9a6df4..3013db9 100644 --- a/lambda/src/com/amazon/ask/githubtemplates/handlers/CreateRepositoryIntentHandler.java +++ b/lambda/src/com/amazon/ask/githubtemplates/handlers/CreateRepositoryIntentHandler.java @@ -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.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.Response; import com.amazon.ask.model.Slot; +import java.io.IOException; import java.util.Map; import java.util.Optional; import java.util.logging.Level; @@ -34,6 +37,7 @@ public class CreateRepositoryIntentHandler implements RequestHandler { public Optional handle(HandlerInput input) { Logger logger = Logger.getAnonymousLogger(); + // Not recommended in prod :) String username = System.getenv("GH_USERNAME"); String key = System.getenv("GH_APIKEY"); @@ -41,17 +45,29 @@ public class CreateRepositoryIntentHandler implements RequestHandler { Map slots = intentRequest.getIntent().getSlots(); for (Map.Entry 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; - if(username != null && key != null) { + + try{ + GithubAPI api = new GithubAPI(); + String language = slots.get("language").getValue(); String title = slots.get("title").getValue(); - speechText = "Let's go with " + language + " and " + title + " !"; - } - else{ - speechText = "Sorry, you are not logged in! I cannot create new repositories"; + api.getGithub().createRepository("test") + .fromTemplateRepository( + api.TEMPLATES.get(language).getValue0(), + 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() diff --git a/lambda/src/com/amazon/ask/githubtemplates/handlers/LoggedInIntentHandler.java b/lambda/src/com/amazon/ask/githubtemplates/handlers/LoggedInIntentHandler.java index 1012b7b..81579c1 100644 --- a/lambda/src/com/amazon/ask/githubtemplates/handlers/LoggedInIntentHandler.java +++ b/lambda/src/com/amazon/ask/githubtemplates/handlers/LoggedInIntentHandler.java @@ -15,6 +15,8 @@ import com.amazon.ask.dispatcher.request.handler.RequestHandler; import com.amazon.ask.model.Response; import java.util.Optional; +import java.util.logging.Level; +import java.util.logging.Logger; import static com.amazon.ask.request.Predicates.intentName; @@ -27,10 +29,10 @@ public class LoggedInIntentHandler implements RequestHandler { @Override public Optional handle(HandlerInput input) { - String username = System.getenv("GH_USERNAME"); - String key = System.getenv("GH_APIKEY"); + String username = System.getenv("GITHUB_LOGIN"); + 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() .withSpeech(speechText) .build(); diff --git a/lambda/src/com/amazon/ask/githubtemplates/handlers/github/GithubAPI.java b/lambda/src/com/amazon/ask/githubtemplates/handlers/github/GithubAPI.java new file mode 100644 index 0000000..67bb617 --- /dev/null +++ b/lambda/src/com/amazon/ask/githubtemplates/handlers/github/GithubAPI.java @@ -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> 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; + } +} diff --git a/lambda/src/com/amazon/ask/githubtemplates/handlers/github/GithubInitException.java b/lambda/src/com/amazon/ask/githubtemplates/handlers/github/GithubInitException.java new file mode 100644 index 0000000..2954f11 --- /dev/null +++ b/lambda/src/com/amazon/ask/githubtemplates/handlers/github/GithubInitException.java @@ -0,0 +1,7 @@ +package com.amazon.ask.githubtemplates.handlers.github; + +public class GithubInitException extends Exception{ + public GithubInitException(String errorMessage) { + super(errorMessage); + } +}