2

I am trying to write a method to delete an issue in JIRA.

I already have methods to create and update issues, but I cannot find any documentation on how to delete an issue using Java.

How can I delete a JIRA issue from a Java application?

CCovey
  • 799
  • 1
  • 10
  • 17
Techen2
  • 83
  • 8

2 Answers2

0

You can try this:(here in this class I am getting specific user created issues by using JQL and after getting the response, I am deleting all of them one by one)

public class DeleteJiraIssuesHelper {
       private InputStream inputStream;
       private JsonReader jsonReader;

public void deleteJiraIssues() throws JiraDeleteIssueException, IOException {   
String JQL="your jql query";
try {
  URL url = new URL(
      "your jira url/rest/api/latest/search?jql="+JQL);
  String userpass = "UserName" + ":" + "Password";
  String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setRequestMethod("GET");
  conn.setRequestProperty("Authorization", basicAuth);
  conn.setRequestProperty("Content-Type", "application/json");
  if (conn.getResponseCode() != 200) {
    throw new JiraConnectionException("Failed : HTTP error code : " + conn.getResponseCode());
  }
  conn.getResponseMessage();
  inputStream = conn.getInputStream();
  jsonReader = Json.createReader(inputStream);

  JsonObject jsonObject = jsonReader.readObject();
  int no = jsonObject.getInt("total");

  JsonArray jsonArray = jsonObject.getJsonArray("issues");
  List<String> issueList = new ArrayList<>();
  for (int i = 0; i <= no - 1; i++) {
    JsonObject jsonObject2 = jsonArray.getJsonObject(i);
    String key = jsonObject2.getString("key");
    issueList.add(key);
  }
  conn.disconnect();

  HttpURLConnection httpCon = null;
  for (int i = 0; i < no; i++) {
    URL url2 = new URL(
        "Jira url/rest/api/latest/issue/" + issueList.get(i));
    httpCon = (HttpURLConnection) url2.openConnection();
    httpCon.setRequestProperty("Authorization", basicAuth);
    httpCon.setDoOutput(true);
    httpCon.setRequestProperty("Content-Type", "application/json");
    httpCon.setRequestMethod("DELETE");
    httpCon.connect();
    httpCon.getResponseCode();
    httpCon.disconnect();
  }

} catch (IOException ex) {
  throw new JiraDeleteIssueException("MalformedURLException: " + ex);
} finally {
  inputStream.close();
  jsonReader.close();
  }
 }
}