Create repository

This commit is contained in:
Julien Lengrand-Lambert
2021-03-05 15:17:18 +01:00
commit 384eec43fa
18 changed files with 1447 additions and 0 deletions

61
lambda/pom.xml Normal file
View File

@@ -0,0 +1,61 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>alexa-skills-kit-samples</groupId>
<artifactId>helloworld</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>helloworld</name>
<url>http://developer.amazon.com/ask</url>
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<developers>
<developer>
<name>Alexa Skills Kit</name>
<email>ask-sdk-java@amazon.com</email>
<organization>Alexa</organization>
<organizationUrl>http://developer.amazon.com/ask</organizationUrl>
</developer>
</developers>
<scm>
<connection>scm:git:https://github.com/amzn/alexa-skills-kit-java.git</connection>
<developerConnection>scm:git:https://github.com/amzn/alexa-skills-kit-java.git</developerConnection>
<url>https://github.com/amzn/alexa-skills-kit-java.git</url>
</scm>
<dependencies>
<dependency>
<groupId>com.amazon.alexa</groupId>
<artifactId>ask-sdk</artifactId>
<version>2.20.2</version>
</dependency>
<dependency>
<groupId>org.kohsuke</groupId>
<artifactId>github-api</artifactId>
<version>1.122</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>11</source>
<target>11</target>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@@ -0,0 +1,40 @@
/*
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
the specific language governing permissions and limitations under the License.
*/
package com.amazon.ask.githubtemplates;
import com.amazon.ask.Skill;
import com.amazon.ask.Skills;
import com.amazon.ask.SkillStreamHandler;
import com.amazon.ask.githubtemplates.handlers.*;
public class GithubTemplatesStreamHandler extends SkillStreamHandler {
private static Skill getSkill() {
return Skills.standard()
.addRequestHandlers(
new CancelandStopIntentHandler(),
new HelloWorldIntentHandler(),
new LoggedInIntentHandler(),
new HelpIntentHandler(),
new LaunchRequestHandler(),
new SessionEndedRequestHandler(),
new FallbackIntentHandler(),
new CreateRepositoryIntentHandler()
)
.withSkillId("amzn1.ask.skill.e88f24da-5860-4ff8-a674-338c62768016")
.build();
}
public GithubTemplatesStreamHandler() {
super(getSkill());
}
}

View File

@@ -0,0 +1,72 @@
package com.amazon.ask.githubtemplates.data;
import org.kohsuke.github.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
public class GithubDataGrabber {
private Map<String, String> languagesAndTemplates =
Map.of(
"java", "https://github.com/Spring-Boot-Framework/Spring-Boot-Application-Template",
"typescript", "https://github.com/carsonfarmer/ts-template"
);
private GitHub githubApi;
public GithubDataGrabber(GitHub gitHub){
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 Set<String> getLanguages(String username) throws IOException {
PagedIterable<GHRepository> repos = this.githubApi.getUser(username).listRepositories();
return StreamSupport.stream(repos.spliterator(), false)
.map(repo -> repo.getLanguage())
.filter(s -> s != null)
.collect(Collectors.toSet());
}
public List<String> getRepositories(String username) throws IOException {
PagedIterable<GHRepository> repos = this.githubApi.getUser(username).listRepositories();
return StreamSupport.stream(repos.spliterator(), false)
.map(repo -> sanitize(repo.getName()))
.filter(s -> s != null)
.collect(Collectors.toList());
}
public String sanitize(String value) {
return value
.replace("-", " ")
.replace("_", " ");
}
}

View File

@@ -0,0 +1,35 @@
/*
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
the specific language governing permissions and limitations under the License.
*/
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.model.Response;
import java.util.Optional;
import static com.amazon.ask.request.Predicates.intentName;
public class CancelandStopIntentHandler implements RequestHandler {
@Override
public boolean canHandle(HandlerInput input) {
return input.matches(intentName("AMAZON.StopIntent").or(intentName("AMAZON.CancelIntent")));
}
@Override
public Optional<Response> handle(HandlerInput input) {
String speechText = "Got you. Till next time!";
return input.getResponseBuilder()
.withSpeech(speechText)
.build();
}
}

View File

@@ -0,0 +1,62 @@
/*
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
the specific language governing permissions and limitations under the License.
*/
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.model.IntentRequest;
import com.amazon.ask.model.Response;
import com.amazon.ask.model.Slot;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.amazon.ask.request.Predicates.intentName;
public class CreateRepositoryIntentHandler implements RequestHandler {
@Override
public boolean canHandle(HandlerInput input) {
return input.matches(intentName("CreateRepositoryIntent"));
}
@Override
public Optional<Response> handle(HandlerInput input) {
Logger logger = Logger.getAnonymousLogger();
String username = System.getenv("GH_USERNAME");
String key = System.getenv("GH_APIKEY");
IntentRequest intentRequest = (IntentRequest) input.getRequestEnvelope().getRequest();
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());
}
String speechText;
if(username != null && key != null) {
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";
}
return input.getResponseBuilder()
.withSpeech(speechText)
.build();
}
}

View File

@@ -0,0 +1,31 @@
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.model.Response;
import java.util.Optional;
import static com.amazon.ask.request.Predicates.intentName;
// 2018-July-09: AMAZON.FallackIntent is only currently available in en-US locale.
// This handler will not be triggered except in that locale, so it can be
// safely deployed for any locale.
public class FallbackIntentHandler implements RequestHandler {
@Override
public boolean canHandle(HandlerInput input) {
return input.matches(intentName("AMAZON.FallbackIntent"));
}
@Override
public Optional<Response> handle(HandlerInput input) {
String speechText = "Sorry, I didn't get that. Care to try again ?";
String repromptText = "Still didn' get that, sorry.";
return input.getResponseBuilder()
.withSpeech(speechText)
.withReprompt(repromptText)
.build();
}
}

View File

@@ -0,0 +1,36 @@
/*
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
the specific language governing permissions and limitations under the License.
*/
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.model.Response;
import java.util.Optional;
import static com.amazon.ask.request.Predicates.intentName;
public class HelloWorldIntentHandler implements RequestHandler {
@Override
public boolean canHandle(HandlerInput input) {
return input.matches(intentName("HelloWorldIntent"));
}
@Override
public Optional<Response> handle(HandlerInput input) {
String speechText = "Hey there! Looking forward what you're going to ask me!";
return input.getResponseBuilder()
.withSpeech(speechText)
.build();
}
}

View File

@@ -0,0 +1,37 @@
/*
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
the specific language governing permissions and limitations under the License.
*/
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.model.Response;
import java.util.Optional;
import static com.amazon.ask.request.Predicates.intentName;
public class HelpIntentHandler implements RequestHandler {
@Override
public boolean canHandle(HandlerInput input) {
return input.matches(intentName("AMAZON.HelpIntent"));
}
@Override
public Optional<Response> handle(HandlerInput input) {
String speechText = "Hey there. Try asking me to create a Java repository!";
String repromptText = "You can also ask me if you are still logged in Github!";
return input.getResponseBuilder()
.withSpeech(speechText)
.withReprompt(repromptText)
.build();
}
}

View File

@@ -0,0 +1,38 @@
/*
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
the specific language governing permissions and limitations under the License.
*/
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.model.LaunchRequest;
import com.amazon.ask.model.Response;
import java.util.Optional;
import static com.amazon.ask.request.Predicates.requestType;
public class LaunchRequestHandler implements RequestHandler {
@Override
public boolean canHandle(HandlerInput input) {
return input.matches(requestType(LaunchRequest.class));
}
@Override
public Optional<Response> handle(HandlerInput input) {
String speechText = "Welcome to GitHub templates! Let's create a repository!";
return input.getResponseBuilder()
.withSpeech(speechText)
.withReprompt(speechText)
.build();
}
}

View File

@@ -0,0 +1,39 @@
/*
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
the specific language governing permissions and limitations under the License.
*/
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.model.Response;
import java.util.Optional;
import static com.amazon.ask.request.Predicates.intentName;
public class LoggedInIntentHandler implements RequestHandler {
@Override
public boolean canHandle(HandlerInput input) {
return input.matches(intentName("LoggedInIntent"));
}
@Override
public Optional<Response> handle(HandlerInput input) {
String username = System.getenv("GH_USERNAME");
String key = System.getenv("GH_APIKEY");
String speechText = username == null || key == null ? "Sorry, you are not logged in!" : "Logged in as " + username;
return input.getResponseBuilder()
.withSpeech(speechText)
.build();
}
}

View File

@@ -0,0 +1,35 @@
/*
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
the specific language governing permissions and limitations under the License.
*/
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.model.Response;
import com.amazon.ask.model.SessionEndedRequest;
import java.util.Optional;
import static com.amazon.ask.request.Predicates.requestType;
public class SessionEndedRequestHandler implements RequestHandler {
@Override
public boolean canHandle(HandlerInput input) {
return input.matches(requestType(SessionEndedRequest.class));
}
@Override
public Optional<Response> handle(HandlerInput input) {
// any cleanup logic goes here
return input.getResponseBuilder().build();
}
}