mirror of
https://github.com/jlengrand/github-templates.git
synced 2026-03-10 08:21:22 +00:00
Adds proper structure
This commit is contained in:
@@ -38,6 +38,11 @@
|
||||
<artifactId>github-api</artifactId>
|
||||
<version>1.122</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.javatuples</groupId>
|
||||
<artifactId>javatuples</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -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<String, String> languagesAndTemplates =
|
||||
public static final Map<String, Pair<String, String>> 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<String> getLanguages(String username) throws IOException {
|
||||
PagedIterable<GHRepository> 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<String> getLanguages() throws IOException {
|
||||
PagedIterable<GHRepository> 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<String> getRepositories(String username) throws IOException {
|
||||
PagedIterable<GHRepository> repos = this.githubApi.getUser(username).listRepositories();
|
||||
public List<String> getRepositories() throws IOException {
|
||||
PagedIterable<GHRepository> repos = this.githubApi.getMyself().listRepositories();
|
||||
|
||||
return StreamSupport.stream(repos.spliterator(), false)
|
||||
.map(repo -> sanitize(repo.getName()))
|
||||
|
||||
@@ -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<Response> 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<String, Slot> slots = intentRequest.getIntent().getSlots();
|
||||
|
||||
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;
|
||||
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()
|
||||
|
||||
@@ -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<Response> 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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.amazon.ask.githubtemplates.handlers.github;
|
||||
|
||||
public class GithubInitException extends Exception{
|
||||
public GithubInitException(String errorMessage) {
|
||||
super(errorMessage);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user