Skip to content

Commit

Permalink
Merge pull request #16 from rwth-acis/15-support-github-issues-and-pu…
Browse files Browse the repository at this point in the history
…ll-requests

Support GitHub issues and pull requests
  • Loading branch information
pdolif authored Aug 15, 2022
2 parents 3bf74cc + d844964 commit b57c640
Show file tree
Hide file tree
Showing 9 changed files with 595 additions and 318 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package i5.las2peer.services.apiTestingBot;

import i5.las2peer.api.Context;
import i5.las2peer.services.apiTestingBot.chat.GHMessageHandler;
import i5.las2peer.services.apiTestingBot.chat.Intent;
import i5.las2peer.services.apiTestingBot.chat.MessageHandler;
import i5.las2peer.services.apiTestingBot.chat.RCMessageHandler;
import i5.las2peer.services.apiTestingBot.context.MessengerType;
import i5.las2peer.services.apiTestingBot.context.TestModelingContext;
import i5.las2peer.services.apiTestingBot.context.TestModelingState;
import io.swagger.annotations.Api;
Expand All @@ -16,6 +19,7 @@

import java.io.Serializable;

import static i5.las2peer.services.apiTestingBot.context.MessengerType.*;
import static i5.las2peer.services.apiTestingBot.context.TestModelingState.*;

@Api
Expand All @@ -34,6 +38,8 @@ public Response modelTest(String body) {
JSONObject bodyJSON = (JSONObject) JSONValue.parse(body);
String intent = (String) bodyJSON.get("intent");
String message = (String) bodyJSON.get("msg");
String messenger = (String) bodyJSON.get("messenger");
MessengerType messengerType = MessengerType.fromString(messenger);
String channel = (String) bodyJSON.get("channel");

// get current modeling context for this channel
Expand All @@ -48,27 +54,32 @@ public Response modelTest(String body) {

StringBuilder responseMessageSB = new StringBuilder();
APITestingBot service = (APITestingBot) Context.get().getService();
MessageHandler messageHandler = new MessageHandler(service.getCaeBackendURL());

// setup message handler
MessageHandler messageHandler;
if(messengerType == ROCKET_CHAT) messageHandler = new RCMessageHandler(service.getCaeBackendURL());
else if(messengerType == GITHUB_ISSUES || messengerType == GITHUB_PR) messageHandler = new GHMessageHandler();
else return Response.status(Response.Status.BAD_REQUEST).entity("Unsupported messenger type.").build();

do {
if (initialState == INIT && intent.equals(Intent.MODEL_TEST)) {
handleNextState = messageHandler.handleInit(responseMessageSB, context);
}

if (handleNextState && context.getState() == SELECT_PROJECT) {
handleNextState = messageHandler.handleProjectSelectionQuestion(responseMessageSB, context, channel);
if (handleNextState && context.getState() == RC_SELECT_PROJECT) {
handleNextState = ((RCMessageHandler) messageHandler).handleProjectSelectionQuestion(responseMessageSB, context, channel);
}

if (initialState == SELECT_PROJECT) {
handleNextState = messageHandler.handleProjectSelection(responseMessageSB, context, message);
if (initialState == RC_SELECT_PROJECT) {
handleNextState = ((RCMessageHandler) messageHandler).handleProjectSelection(responseMessageSB, context, message);
}

if (handleNextState && context.getState() == SELECT_MICROSERVICE) {
handleNextState = messageHandler.handleMicroserviceSelectionQuestion(responseMessageSB, context);
if (handleNextState && context.getState() == RC_SELECT_MICROSERVICE) {
handleNextState = ((RCMessageHandler) messageHandler).handleMicroserviceSelectionQuestion(responseMessageSB, context);
}

if (initialState == SELECT_MICROSERVICE) {
handleNextState = messageHandler.handleMicroserviceSelection(responseMessageSB, context, message);
if (initialState == RC_SELECT_MICROSERVICE) {
handleNextState = ((RCMessageHandler) messageHandler).handleMicroserviceSelection(responseMessageSB, context, message);
}

if (handleNextState && context.getState() == NAME_TEST_CASE) {
Expand All @@ -79,20 +90,36 @@ public Response modelTest(String body) {
handleNextState = messageHandler.handleTestCaseName(responseMessageSB, context, message);
}

if (handleNextState && context.getState() == SELECT_METHOD) {
handleNextState = messageHandler.handleMethodSelectionQuestion(responseMessageSB, context);
if (handleNextState && context.getState() == RC_SELECT_METHOD) {
handleNextState = ((RCMessageHandler) messageHandler).handleMethodSelectionQuestion(responseMessageSB, context);
}

if (initialState == RC_SELECT_METHOD) {
handleNextState = ((RCMessageHandler) messageHandler).handleMethodSelection(responseMessageSB, context, message);
}

if (handleNextState && context.getState() == RC_ENTER_PATH_PARAMS) {
handleNextState = ((RCMessageHandler) messageHandler).handlePathParamsQuestion(responseMessageSB, context);
}

if ((handleNextState && context.getState() == RC_ENTER_PATH_PARAMS) || initialState == RC_ENTER_PATH_PARAMS) {
handleNextState = ((RCMessageHandler) messageHandler).handlePathParams(responseMessageSB, context, message);
}

if (initialState == SELECT_METHOD) {
handleNextState = messageHandler.handleMethodSelection(responseMessageSB, context, message);
if(handleNextState && context.getState() == GH_METHOD_QUESTION) {
handleNextState = ((GHMessageHandler) messageHandler).handleMethodQuestion(responseMessageSB, context);
}

if (handleNextState && context.getState() == ENTER_PATH_PARAMS) {
handleNextState = messageHandler.handlePathParamsQuestion(responseMessageSB, context);
if(initialState == GH_ENTER_METHOD) {
handleNextState = ((GHMessageHandler) messageHandler).handleMethod(responseMessageSB, context, intent);
}

if ((handleNextState && context.getState() == ENTER_PATH_PARAMS) || initialState == ENTER_PATH_PARAMS) {
handleNextState = messageHandler.handlePathParams(responseMessageSB, context, message);
if(handleNextState && context.getState() == GH_PATH_QUESTION) {
handleNextState = ((GHMessageHandler) messageHandler).handlePathQuestion(responseMessageSB, context);
}

if(initialState == GH_ENTER_PATH) {
handleNextState = ((GHMessageHandler) messageHandler).handlePath(responseMessageSB, context, message);
}

if ((handleNextState && context.getState() == BODY_QUESTION)) {
Expand Down Expand Up @@ -171,18 +198,12 @@ public Response modelTest(String body) {
initialState = FINAL;

} while(handleNextState);
String responseMessage = responseMessageSB.toString();

if(responseMessage.isEmpty()) responseMessage = "Error!";

System.out.println("New state is: " + APITestingBot.channelModelingContexts.get(channel).getState().name());

JSONObject res = new JSONObject();
res.put("text", responseMessage);
res.put("closeContext", context.getState() == FINAL);

if(context.getState() == FINAL) {
if(context.getMicroserviceComponent() != null) {
if(messengerType == ROCKET_CHAT && context.getMicroserviceComponent() != null) {
// store test case (as a suggestion)
int versionedModelId = ((Long) context.getMicroserviceComponent().get("versionedModelId")).intValue();
try {
Expand All @@ -192,13 +213,34 @@ public Response modelTest(String body) {
} catch (Exception e) {
e.printStackTrace();
}
} else if(messengerType == GITHUB_ISSUES || messengerType == GITHUB_PR) {
try {
// generate code for the test case
String code = (String) Context.get().invoke("i5.las2peer.services.codeGenerationService.CodeGenerationService",
"generateTestMethod", new Serializable[]{ context.toTestModel().getTestCases().get(0) });

// post generated code as a comment
responseMessageSB.append("Here is the generated test method code:");
responseMessageSB.append("\n");
responseMessageSB.append("```java");
responseMessageSB.append(code);
responseMessageSB.append("```");
} catch (Exception e) {
e.printStackTrace();
}
}

APITestingBot.channelModelingContexts.remove(channel);
}

return Response.status(200).entity(res.toJSONString()).build();
}
String responseMessage = responseMessageSB.toString();

if(responseMessage.isEmpty()) responseMessage = "Error!";

JSONObject res = new JSONObject();
res.put("text", responseMessage);
res.put("closeContext", context.getState() == FINAL);

return Response.status(200).entity(res.toJSONString()).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package i5.las2peer.services.apiTestingBot.chat;

import i5.las2peer.services.apiTestingBot.context.TestModelingContext;

import static i5.las2peer.services.apiTestingBot.chat.Messages.*;
import static i5.las2peer.services.apiTestingBot.context.TestModelingState.*;

/**
* Handles messages if the bot is used in GitHub.
*/
public class GHMessageHandler extends MessageHandler {

/**
* Reacts to the initial message of the user that starts a test modeling conversation.
*
* @param responseMessageSB StringBuilder
* @param context Current test modeling context
* @return Whether the next state should be handled too.
*/
@Override
public boolean handleInit(StringBuilder responseMessageSB, TestModelingContext context) {
responseMessageSB.append(MODEL_TEST_CASE_INTRO);
context.setState(NAME_TEST_CASE);
return true;
}

/**
* Stores the previously entered test case name to the context.
*
* @param responseMessageSB StringBuilder
* @param context Current test modeling context
* @param message Message sent by the user.
* @return Whether the next state should be handled too.
*/
@Override
public boolean handleTestCaseName(StringBuilder responseMessageSB, TestModelingContext context, String message) {
context.setState(GH_METHOD_QUESTION);
return super.handleTestCaseName(responseMessageSB, context, message);
}

/**
* Asks the user to enter the request method.
*
* @param responseMessageSB StringBuilder
* @param context Current test modeling context
* @return Whether the next state should be handled too.
*/
public boolean handleMethodQuestion(StringBuilder responseMessageSB, TestModelingContext context) {
responseMessageSB.append(GH_ENTER_REQUEST_METHOD);
context.setState(GH_ENTER_METHOD);
return false;
}

/**
* Handles the request method entered by the user.
*
* @param responseMessageSB StringBuilder
* @param context Current test modeling context
* @param intent Intent
* @return Whether the next state should be handled too.
*/
public boolean handleMethod(StringBuilder responseMessageSB, TestModelingContext context, String intent) {
if(!Intent.REQUEST_METHOD_INTENTS.contains(intent)) {
responseMessageSB.append(ERROR_COULD_NOT_UNDERSTAND);
return false;
}

// set request method in context
context.setRequestMethod(Intent.toRequestMethod(intent));
context.setState(GH_PATH_QUESTION);
return true;
}

/**
* Asks the user to enter the request path.
*
* @param responseMessageSB StringBuilder
* @param context Current test modeling context
* @return Whether the next state should be handled too.
*/
public boolean handlePathQuestion(StringBuilder responseMessageSB, TestModelingContext context) {
responseMessageSB.append(GH_ENTER_REQUEST_PATH);
context.setState(GH_ENTER_PATH);
return false;
}

/**
* Handles the request path entered by the user.
*
* @param responseMessageSB StringBuilder
* @param context Current test modeling context
* @param message Message sent by the user.
* @return Whether the next state should be handled too.
*/
public boolean handlePath(StringBuilder responseMessageSB, TestModelingContext context, String message) {
context.setRequestPath(message);
responseMessageSB.append(GH_REQUEST_INFO(context.getRequestMethod(), context.getRequestPath()));
context.setState(BODY_QUESTION);
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
package i5.las2peer.services.apiTestingBot.chat;

import java.util.List;

/**
* Rasa intents related to the API testing bot.
*/
public class Intent {
public static String MODEL_TEST = "modeltest";
public static String YES = "yes";
public static String NO = "no";
public static final String MODEL_TEST = "modeltest";

public static final String YES = "yes";
public static final String NO = "no";

public static final String GET = "request_method_get";
public static final String POST = "request_method_post";
public static final String PUT = "request_method_put";
public static final String DELETE = "request_method_delete";

public static final List<String> REQUEST_METHOD_INTENTS = List.of(GET, POST, PUT, DELETE);

public static String toRequestMethod(String intent) {
switch(intent) {
case Intent.GET:
return "GET";
case Intent.POST:
return "POST";
case Intent.PUT:
return "PUT";
case Intent.DELETE:
return "DELETE";
default:
return "GET";
}
}
}
Loading

0 comments on commit b57c640

Please sign in to comment.