Hi, I want to delete a filter using mysql query...

Jonnada Kiran January 22, 2018

Hi, I want to delete a filter using mysql query... Even though it is deleting from database then filter still shows in  Favorite Filters. When I click on Admin clog and select delete it says it is already deleted and removed from the favorite lists. But I want that to happen automate this thing. How to resolve this issue.

 

import groovy.sql.Sql
import java.sql.*
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.ComponentManager
import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def log = Logger.getLogger("test.connector")
log.setLevel(Level.DEBUG)

def driver = Class.forName('com.mysql.jdbc.Driver').newInstance() as Driver

def props = new Properties()
props.setProperty("user", "xxxx")
props.setProperty("password", "xxxxx!")

def conn = driver.connect("xxxxxx/jiradb?useUnicode=true&characterEncoding=UTF8&sessionVariables=storage_engine=InnoDB", props)
def sql = new Sql(conn)
if(sql!=null){
log.debug("connected")
try {


List<String> filternames=[];
List<String> filtername_username=[];
List<String> filterid=[];
def usermanager = ComponentAccessor.getUserManager()
def flag = false

// returns if there is a empty filter
sql.eachRow("select * from searchrequest where reqcontent = ''") {row ->
log.debug("Empty Filter Name: "+row.filtername)
filternames.push((String)row.filtername)
filtername_username.push((String)row.username)
filterid.push((String)row.ID)
}
log.debug(filternames)
log.debug(filtername_username)
log.debug(filterid)

 

//executes if there is a empty
if(filternames.size()>0){
ComponentManager componentManager = ComponentManager.getInstance()
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()

for(int j=0;j<filternames.size();j++){
String table="";
def filter_user = usermanager.getUserByKey(filtername_username[j])
table+="Filter ID: "+filterid[j]+" FilterName: "+filternames[j]+ " Created By : "+filtername_username[j]+ " \n"
table+= "\n \n Hello "+filter_user.getDisplayName()+", \n \n You have created a empty filter named "+filternames[j]+" which will impact the jira performance. So, We are removing it. Please create a filter with some query in it.\n"
table+= "\n\nThanks and Regards, \nJira Admins \n \n"
try{
log.debug(filterid[j])
log.debug(filternames[j])

//deleting from favoritesassociations
sql.execute("delete from favouriteassociations where entitytype = 'SearchRequest' AND entityid = "+filterid[j]+"")

 

//deleeting from searchrequest
sql.execute("delete from searchrequest where reqcontent = '' and username = '"+filtername_username[j]+"' and filtername='"+filternames[j]+"'");
log.debug("filter deleted")
flag = false
}
catch (Exception e) {
log.debug("filter not deleted");
flag = true
}

if (mailServer&&flag==false) {
Email email = new Email(filter_user.getEmailAddress())
email.setSubject("Empty Filter Alert ")
String content = table
email.setBody(content)
mailServer.send(email)
log.debug("Email Sent")
log.debug(table)
List<String> admins = ["xxx","xxxx","xxxx"]


for(int i=0;i<admins.size();i++){
SMTPMailServer mailServer1 = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if(mailServer1){
try{
def admin = usermanager.getUserByKey(admins[i])
log.debug(admin)
def admin_msg=""
admin_msg+= "Hello "+admin.getDisplayName()+" , \n \n"
admin_msg+="We have a empty filter named '"+filternames[j]+"' created by '"+usermanager.getUserByKey(filtername_username[j]).getDisplayName()+"'("+filtername_username[j]+"). We have automatically deleted it and notified the user. \n \n "
admin_msg+="Thanks & Regards, \n"
admin_msg+="Jira Admins"
log.debug(admin_msg)
Email adminemail = new Email("xxxxxxxx")
adminemail.setSubject("Automatic Deletion of Empty Filter Alert -- JIRA TEST ")
String admincontent = admin_msg
adminemail.setBody(admincontent)
mailServer1.send(adminemail)
log.debug("Admin Email Sent")
}
catch (Exception e) {
log.debug("admin not found");
}
}



}
}
else {
log.error("No SMTP mail server defined")
}
}




}


} finally {
sql.close()
conn.close()
}
}

1 answer

1 accepted

0 votes
Answer accepted
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 5, 2018

Directly modifying the SQL database while Jira is running is not advised.   This is because there are a number of cached elements from the database that are loaded up when jira is started.  For you to change the SQL database without Jira knowing about this is the reason for leaving this 'favorite filter' artifact around.

There are two possible ways to get around this:

  1. Make SQL changes only when Jira is stopped.  This way when Jira starts up it can cache that data correctly and accurately. (provided that your SQL query is the same way Jira would delete this information from the database)
  2. Make these changes via the Jira REST API.

 

I would recommend using method #2, just because you don't have to stop Jira this way.  You can see how to do this via the rest API in DELETE /rest/api/2/filter/{id}.

Jonnada Kiran February 6, 2018

I did it using groovy api. It is working fine now. Thanks.

DTS Infra Admin January 23, 2019

@Andy Heinzer Can we use rest api for deleting private filters also ? 

How can we delete private filters ?  . I am not even able to access them using rest api .

Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 23, 2019

@DTS Infra Admin No you cannot.  The REST API will only let you delete filters that either you created or that have been shared with you as an editor.  Starting with Jira 7.12 there have been some expanded abilities for end users to quickly share their filters with other users to view and edit.  More details in the Jira Software 7.12 release notes

But for filters that have not been shared with other users, Jira is designed to keep those filters private.   Even Jira admins aren't able to edit/manage these in the web UI nor via the REST API.  There is an existing feature request over in https://jira.atlassian.com/browse/JRASERVER-41269 that requests that specific feature and has been looking like it might actually get more attention in the near future.

Suggest an answer

Log in or Sign up to answer