-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/poet 61 query issues based on status #30
base: master
Are you sure you want to change the base?
Changes from all commits
358b5cb
2dde489
624871f
c430870
57c35fe
e330e2c
4f5586e
4e3030c
cd2dc88
1da2ed6
31632e5
4248f29
134cb8a
7d94b2c
30c92ed
883e583
e8530a9
271f98c
6014ea7
66abf37
deac5dc
95ad00c
1633bad
9438738
5fa79d4
319111d
b5c321d
4102c81
23908ac
0435f19
f5e9180
df09ff2
c9ed870
42969cf
1089ed0
c3e5110
00e2ea3
0792eee
d1476c1
9602389
52b00ee
15cc526
b1a0939
e1aca71
7ac83d5
a36c21e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package models; | ||
|
||
/** | ||
* @author dxli | ||
*/ | ||
|
||
public class JiraAuth { | ||
public String username; | ||
public String password; | ||
|
||
public JiraAuth(String username, String password) { | ||
this.username = username; | ||
this.password = password; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package models; | ||
|
||
/** | ||
* @author sabinapokhrel | ||
* | ||
*/ | ||
|
||
public class LuisResponse { | ||
public String intent; | ||
public String entityType; | ||
public String entityName; | ||
|
||
public LuisResponse(String intent, String entityType, String entityName) { | ||
this.intent = intent; | ||
this.entityType = entityType; | ||
this.entityName = entityName; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package models; | ||
|
||
/** | ||
* @author sabinapokhrel | ||
* | ||
*/ | ||
|
||
public class ResponseToClient { | ||
public String status; | ||
public String message; | ||
|
||
public ResponseToClient(String status, String message) { | ||
this.status = status; | ||
this.message = message; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package modules; | ||
|
||
/** | ||
* Created by dxli on 30/03/17. | ||
*/ | ||
public class PoetModule { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,264 @@ | ||
package services; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import models.JiraAuth; | ||
import models.ResponseToClient; | ||
import play.libs.Json; | ||
import play.libs.ws.WSAuthScheme; | ||
import play.libs.ws.WSClient; | ||
import play.libs.ws.WSRequest; | ||
import play.libs.ws.WSResponse; | ||
import utils.ConfigUtilities; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
/** | ||
* The class is for all "reading actions" on JIRA. | ||
* Extracting fields from raw response on need. | ||
*/ | ||
public class JiraReaderService { | ||
private String messageToReturn; | ||
private WSClient ws; | ||
private JiraAuth jiraAuth; | ||
|
||
|
||
public JiraReaderService(WSClient ws, JiraAuth jiraAuth) { | ||
this.ws = ws; | ||
this.jiraAuth = jiraAuth; | ||
} | ||
|
||
/** | ||
* To request for JIRA ticket info page via REST API. A non-blocking call. | ||
* | ||
* @param ticketId ticket ID in string. | ||
* @return info page encoded in JSON. | ||
*/ | ||
public CompletionStage<JsonNode> fetchTicketByApi(String ticketId) { | ||
|
||
System.out.println(ConfigUtilities.getString("jira.baseUrl") | ||
+ ConfigUtilities.getString("jira.issueEndpoint") | ||
+ ticketId); | ||
|
||
WSRequest request = ws.url(ConfigUtilities.getString("jira.baseUrl") | ||
+ ConfigUtilities.getString("jira.issueEndpoint") | ||
+ ticketId); | ||
WSRequest complexRequest = request.setAuth(jiraAuth.username, jiraAuth.password, WSAuthScheme.BASIC); | ||
|
||
return complexRequest.get().thenApply(WSResponse::asJson); | ||
} | ||
|
||
/** | ||
* To request information based on username via REST API. A non-blocking call. | ||
* | ||
* @param jiraUsername jira username in string. | ||
* @return info page encoded in JSON. | ||
*/ | ||
public CompletionStage<JsonNode> fetchAssigneeInfoByApi(String jiraUsername) { | ||
|
||
WSRequest request = ws.url("https://jira.agiledigital.com.au/rest/api/2/search") | ||
.setQueryParameter("jql", "assignee=" + jiraUsername + " and status='in progress'"); | ||
WSRequest complexRequest = request.setAuth(jiraAuth.username, jiraAuth.password, WSAuthScheme.BASIC); | ||
return complexRequest.get().thenApply(WSResponse::asJson); | ||
} | ||
|
||
/** | ||
* To request information based on status via REST API. A non-blocking call. | ||
* | ||
* @param status status of issue in string. | ||
* @return info page encoded in JSON. | ||
*/ | ||
public CompletionStage<JsonNode> fetchIssuesForStatusByApi(String status) { | ||
|
||
WSRequest request = ws.url("https://jira.agiledigital.com.au/rest/api/2/search") | ||
.setQueryParameter("jql", "status='" + status +"'"); | ||
WSRequest complexRequest = request.setAuth(jiraAuth.username, jiraAuth.password, WSAuthScheme.BASIC); | ||
return complexRequest.get().thenApply(WSResponse::asJson); | ||
} | ||
|
||
/** | ||
* //TODO : comment this. | ||
* | ||
* @param response | ||
* @param intent | ||
* @param entity | ||
* @return | ||
*/ | ||
public JsonNode read(JsonNode response, String intent, String entity) { | ||
Boolean isSuccess = false; | ||
System.out.println(intent + " " + entity); | ||
switch (intent) { | ||
case "IssueDescription": | ||
isSuccess = readDescription(entity, response); | ||
break; | ||
case "IssueAssignee": | ||
isSuccess = readAssignee(entity, response); | ||
break; | ||
case "IssueStatus": | ||
isSuccess = readStatus(entity, response); | ||
break; | ||
case "AssigneeIssues": | ||
isSuccess = readIssues(entity, response); | ||
break; | ||
case "IssuesForStatus": | ||
isSuccess = readIssuesForStatus(entity, response); | ||
break; | ||
} | ||
|
||
if (isSuccess) { | ||
return Json.toJson(new ResponseToClient(JiraServiceProvider.REQUEST_SUCCESS, messageToReturn)); | ||
} else { | ||
return Json.toJson(new ResponseToClient(JiraServiceProvider.REQUEST_FAILURE, | ||
ConfigUtilities.getString("error-message.issue-not-found"))); | ||
} | ||
} | ||
|
||
/** | ||
* This method reads assignee of the ticket. | ||
* | ||
* @param ticketNo is the IssueID of type string which was mentioned in the query by the user. | ||
* @param responseBody is the JSON object received from JIRA Rest API. | ||
* @return true if success, otherwise if no such ticket exists, false. | ||
*/ | ||
private Boolean readAssignee(String ticketNo, JsonNode responseBody) { | ||
if (responseBody.get("errorMessages") != null) { | ||
return false; | ||
} else { | ||
this.messageToReturn = responseBody.get("fields").get("assignee").get("displayName").textValue() | ||
+ " is working on " | ||
+ ticketNo + "."; | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* This method reads description of the ticket. | ||
* | ||
* @param responseBody is the JSON object received from JIRA Rest API. | ||
* @return true if success, otherwise if no such ticket exists, false. | ||
*/ | ||
private Boolean readDescription(String ticketNo, JsonNode responseBody) { | ||
if (responseBody.get("errorMessages") != null) { | ||
return false; | ||
} else { | ||
this.messageToReturn = hyperlinkTicketNo("Description of " | ||
// + '<' + "http://google.com" +'|' +ticketNo+ '>' // replace google by the actual ticket page | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this commented code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is the same file in another PR. Please make sure the other PR merged first, then rebase this branch's changes on top of master. |
||
+ ticketNo | ||
+ " is as follows: \n" | ||
+ responseBody.get("fields").get("description").textValue()); | ||
|
||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* This method reads status of the ticket. | ||
* | ||
* @param ticketNo is the IssueID of type string which was mentioned in the query by the user. | ||
* @param responseBody is the JSON object received from JIRA Rest API. | ||
* @return true if success, otherwise if no such ticket exists, false. | ||
*/ | ||
private Boolean readStatus(String ticketNo, JsonNode responseBody) { | ||
if (responseBody.get("errorMessages") != null) { | ||
return false; | ||
} else { | ||
this.messageToReturn = hyperlinkTicketNo("The status of " | ||
+ ticketNo | ||
+ " is " | ||
+ responseBody.get("fields").get("status").get("name").textValue()); | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* This method reads status of the ticket. | ||
* | ||
* @param assignee is the username of assignee of type string which was mentioned in the query by the user. | ||
* @param responseBody is the JSON object received from JIRA Rest API. | ||
* @return true if success, otherwise if no such ticket exists, false. | ||
*/ | ||
private Boolean readIssues(String assignee, JsonNode responseBody) { | ||
if (responseBody.get("errorMessages") != null) { | ||
return false; | ||
} else { | ||
int issueCount = Integer.parseInt(responseBody.get("total").toString()); | ||
if (issueCount > 0) { | ||
StringBuffer issues = new StringBuffer(); | ||
for (int i = 0, j = 0; i < issueCount; i++) { | ||
String string = responseBody.get("issues").findValues("key").get(j).textValue(); | ||
StringBuffer tmp; | ||
if (i < issueCount - 1) | ||
tmp = new StringBuffer(string + ", "); | ||
else | ||
tmp = new StringBuffer(string + "."); | ||
issues.append(tmp); | ||
j = j + 6; | ||
} | ||
this.messageToReturn = hyperlinkTicketNo(assignee + " is working on " + issues.toString()); | ||
} else { | ||
this.messageToReturn = assignee + " is currently not working on any issue."; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* This method reads status of the ticket. | ||
* | ||
* @param assignee is the username of assignee of type string which was mentioned in the query by the user. | ||
* @param responseBody is the JSON object received from JIRA Rest API. | ||
* @return true if success, otherwise if no such ticket exists, false. | ||
*/ | ||
private Boolean readIssuesForStatus(String status, JsonNode responseBody) { | ||
System.out.println(responseBody); | ||
if (responseBody.get("errorMessages") != null) { | ||
return false; | ||
} else { | ||
//System.out.println(responseBody); | ||
int issueCount = Integer.parseInt(responseBody.get("total").toString()); | ||
if (issueCount > 0) { | ||
StringBuffer issues = new StringBuffer(); | ||
for (int i = 0, j = 0; i < issueCount; i++) { | ||
String string = responseBody.get("issues").findValues("key").get(j).textValue(); | ||
StringBuffer tmp; | ||
if (i < issueCount - 1) | ||
tmp = new StringBuffer(string + ", "); | ||
else | ||
tmp = new StringBuffer(string + "."); | ||
issues.append(tmp); | ||
j = j + 6; | ||
} | ||
this.messageToReturn = hyperlinkTicketNo(status + " issues are " + issues.toString()); | ||
} else { | ||
this.messageToReturn = "There are no issues " + status + "."; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
|
||
|
||
|
||
/** | ||
* The methods hyperlinks the ticket number appearing in the | ||
* description, or actually, any string fed into. This is implemented | ||
* by first checking the project name configured in the application.conf | ||
* and then use the slack formatting to wrap around the ticket number. | ||
* | ||
* @param issueDescription the description of the ticket queried. | ||
* This can also be applied to any other strings. | ||
* @return the issue description after formatting. This | ||
* should hyperlink the ticket numbers appearing in | ||
* the input string. | ||
* @Reference: https://api.slack.com/docs/message-formatting | ||
*/ | ||
private static String hyperlinkTicketNo(String issueDescription) { | ||
String jiraIssueName = ConfigUtilities.getString("jira.issueName"); | ||
String pattern = "((?i)" + jiraIssueName + "-\\d+)"; | ||
String issueUrl = ConfigUtilities.getString("jira.baseUrl") + "/browse/"; | ||
String hyperlink = "<" + issueUrl + "$1|$1>"; | ||
|
||
issueDescription = issueDescription.replaceAll(pattern, hyperlink); | ||
return issueDescription; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment this?