Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get the status of already created Jira ticket by API

aliva dash July 7, 2022

I am creating a ticket using rest api in the jira instance  with the status as Open based on the input from the source database. If a user update the jira ticket status ,how can i get the status on the specific ticket so that the same i can update in the database.

2 answers

0 votes
aliva dash July 7, 2022

This is my code where i create the ticket in jira and update the jira id in the table .Can you tell me where can i read the status and update the data in table?

public int createDQTasks(Properties pty, ResultSet rs)
throws AuthenticationException, ClientHandlerException,
IOException, SQLException, Exception {

String task = "";
String story = "";
String exceptionFilePath = "";
String element1 = "";
String domain1 = "";
String table = "";


Client client = Client.create();
String auth = new String(Base64.encode(jiraUser + ":" + jiraPasswd));
auth = pty.getProperty("JIRAAuth");
System.err.println("***auth is: " + auth);

try {

String createResponseStr = "";
String createData = "";

while (rs.next()) {

story = rs.getString("STORY_ID");
task = rs.getString("EXCEPTION_ID");
exceptionFilePath = rs.getString(pty.getProperty("DQBadDataFileColumnName"));
element1 = rs.getString("DATA_ELEMENT");
domain1 = rs.getString("DOMAIN");
table = rs.getString("TABLE_NAME");



System.err.println("***"+rs.getString("PRIORITY")+"***");

createData = "{\"fields\":{" + "\"project\":{\"key\":\"" + pty.getProperty("DQDefaultProjectKey") + "\"},"
+ "\"issuetype\":{\"name\":\"Production Bug\"},"
+ "\"versions\":[{\"name\":\"DQ_SCI_PPS\"}],"
//+ "\"summary\":\"Data Domain: " + domain1 + " & Data Element: " + element1 + ""
+ "\"summary\":\"Issue with: " + element1 + " for table: " + table + " in domain: " + domain1 + ""
//+ "\"summary\":\"" + rs.getString("BIZ_FIELD_NAME") + ": " + rs.getString("BIZ_FIELD_NAME")
+ "\"," + "\"description\":\""
+ "DataElement: " + rs.getString("BIZ_FIELD_NAME")
+ " failed DQ check for rule: " + rs.getString("RULE_NAME") + ". "
+ "Current DQ score is: " + rs.getString("DQ_SCORE") + ". The bad record file is available at: " + pty.getProperty("DQExceptionFilePath") + exceptionFilePath
+ "\","
+ "\"priority\":{\"name\":\"" + getPriority(rs.getString("PRIORITY")) + "\"},"
+ "\"reporter\":{\"name\":\""
+ pty.getProperty("DQTaskDefaultReporter") + "\"},"
+"\"environment\":\"" + "TST" + "\","
//+"\"Data domain\":{\"name\":\"Product\"},"
+ "\"assignee\":{\"name\":\""
// + getTaskAssignee(rs.getString("STEWARD"), pty).replace(pty.getProperty("LenovoDomain"), "") + "\"}";
+ pty.getProperty("DQTaskDefaultReporter") + "\"}";

createData = createData + "}}";
System.out.format("%s: createData for Task is: %s\n", thisClass, createData);
logger.debug("createData for Task is: " + createData);

WebResource webResource = client
.resource(jiraRestCreateUri);
disableCertificateValidation();
ClientResponse createResponse = webResource
.header("Authorization", "Basic " + auth)
.type("application/json")
.accept("application/json")
.post(ClientResponse.class, createData);

int statusCode = createResponse.getStatus();
if (statusCode == 401) {

System.err.format("%s: Authentication Exception. Invalid Username or Password", thisClass);
logger.fatal("Authentication Exception. Invalid Username or Password");
errorMessage = errorMessage + encodeHTMLNewLine("Authentication Exception. Invalid Username or Password");

throw new AuthenticationException(
"Invalid Username or Password");
}

createResponseStr = createResponse.getEntity(String.class);
System.out
.format("%s: Task create request's response createResponseStr: %s\n",
thisClass, createResponseStr);
logger.debug("Task create request's response createResponseStr: " + createResponseStr);

Response createJSONResponse = gson.fromJson(
createResponseStr, Response.class);

if (statusCode == 201) {
System.err
.format("%s: SUCCESS: JIRA task created for EXCEPTION_ID %s is: %s\n",
thisClass, task,
createJSONResponse.getKey());
logger.info("SUCCESS: JIRA task created for EXCEPTION_ID: " + task + ". Task ID is: " + createJSONResponse.getKey());

String updateTask = "update " + dqExceptionStgTbl
+ " set JIRA_ID = '"
+ createJSONResponse.getKey() + "', TKT_STATUS = 'Open', "
+ "TKT_OPEN_DT = current_timestamp, TKT_UPDATE_DT = current_timestamp"
+ " where EXCEPTION_ID = '" + task + "'";

System.out.format(
"%s: Updated task ticket number for the corresponding EXCEPTION_ID using query: %s \n",
thisClass, updateTask);
logger.info("Updated task ticket number for the corresponding EXCEPTION_ID using query: " + updateTask);

executeUpdateInDB(updateTask, pty);

} else {

System.err.format("%s: ERROR: Creating JIRA task for EXCEPTION_ID: %s.\nCreate Request is: %s\n Response is: %s\n", thisClass, task,
createData, createResponseStr);

logger.error("Creating JIRA task for EXCEPTION_ID: " + task + "\n" + "Create Request is: " + createData + "\n"
+ "Response is: " + "\n" + createResponseStr + "\n");

errorMessage = errorMessage + encodeHTMLNewLine("ERROR: Creating JIRA task for EXCEPTION_ID: " + task) + encodeHTMLNewLine("Create Request is: " + createData)
+ encodeHTMLNewLine("Response is: " + createResponseStr);

}

}

} catch (SQLException sqle) {
stackTrace = ExceptionUtils.getStackTrace(sqle);
System.err.format("%s: ERROR: Exception updating task data into DQ table %s. \nException:\n %s", thisClass, dqExceptionTbl, sqle.toString());
logger.error("Exception updating task data into DQ table: " + dqExceptionTbl + "\nException:\n" + sqle.toString());
errorMessage = errorMessage + encodeHTMLNewLine("ERROR: Exception updating task data into DQ table: " + dqExceptionTbl) + encodeHTMLNewLine("Exception: ") + encodeHTMLNewLine(sqle.toString()) + encodeHTMLNewLine("stackTrace is: ") + encodeHTMLNewLine(stackTrace);
logger.error("Exception is: \n" + sqle.toString());
logger.error("stackTrace is: " + stackTrace);
sqle.printStackTrace();

} catch (Exception ex) {
stackTrace = ExceptionUtils.getStackTrace(ex);
System.err.format("%s: ERROR: Exception in createDQTasks method. \nException:\n %s", thisClass, ex.toString());
errorMessage = errorMessage + encodeHTMLNewLine("ERROR: Exception in createDQTasks method.") + encodeHTMLNewLine("Exception is: ") + encodeHTMLNewLine(ex.toString()) + encodeHTMLNewLine("stackTrace is: ") + encodeHTMLNewLine(stackTrace);
logger.error("ERROR: Exception in createDQTasks method.");
logger.error("Exception is: " + ex);
logger.error("stackTrace is: " + stackTrace);
ex.printStackTrace();

} finally {

client.destroy();
releaseConnectionToDB();
}

return 0;
}

0 votes
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 7, 2022

Assuming by "get", you mean "read the issue over the REST API", then all you need to do is read the issue, the response will contain all the fields, including the status.

aliva dash July 7, 2022

yes i need the status of the already created jira ticket. what is here read the issue?

Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 7, 2022

Um, you said you are creating issues with the REST API.  You use that to read them as well.

Like Craig Nodwell likes this
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 7, 2022

That was a bit short, sorry.

Your call will have got a success/failure message back from Jira.  The success message includes the key of the issue that was created.

You should make another call to Jira with that key to read the issue.  The response to that call will contain the issue status.  (Inside all the other fields, I'd recommend only asking for the fields you need, not everything every time)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events