How to update change history after deleting attachment using groovy script.

Hi Everyone,

 I am able to delete the attachment using the groovy script. The Problem is I am not able to reflect the changed within the change history of an issue. 

 

Does anyone has come across such issue? 

My Jira Version is 6.4.12

Thanks,

Manish

3 answers

1 accepted

Accepted Answer
0 votes

I was able to do this over the weekend. Here is the code to delete the attachment as well as update the Change history for an issue.

 

 

import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.crowd.embedded.api.CrowdService;
import com.atlassian.jira.issue.AttachmentManager;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.project.Project;
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.builder.JqlQueryBuilder
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.query.Query
import java.util.*
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.issue.attachment.AttachmentService
import com.atlassian.jira.bc.JiraServiceContext


def log = Logger.getLogger("com.ultimate.deleteAttachment")
log.setLevel(Level.DEBUG)


CrowdService crowdService = ComponentAccessor.getCrowdService();
User user = crowdService.getUser("xxxx")

def userManager = ComponentAccessor.getUserManager();
ApplicationUser appUser = userManager.getUserByKey("xxxx");


SearchProvider searchProvider = ComponentAccessor.getComponentOfType(SearchProvider.class);
SearchService searchService = ComponentAccessor.getComponentOfType(SearchService.class);

JiraServiceContext jiraServiceContext = new JiraServiceContextImpl(user);
AttachmentService attachmentService = ComponentAccessor.getComponentOfType(AttachmentService.class);


def jqlargument = "project = CA and issuetype = Epic and issuekey =\"CA-42\"";

SearchService.ParseResult parseResult = searchService.parseQuery(user,jqlargument);

Query query = parseResult.getQuery();

SearchResults results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter());

def fetchedIssues= results.getIssues();


for(j=0;j<fetchedIssues.size(); j++)
{
IssueManager issueManager = ComponentAccessor.getComponentOfType(IssueManager);
MutableIssue issue = issueManager.getIssueObject(fetchedIssues.get(j).getKey())

def issueId = issue.getId()

for(i=0; i<issue.getAttachments().size();i++)
{
def authorKey = issue.getAttachments().get(i).getAuthorObject().getKey()
Attachment attachment = issue.getAttachments().get(i)

AttachmentManager attachmentManager = ComponentAccessor.getComponent(AttachmentManager)


if(authorKey=="xxxxx")//provide owner userid of the attachment.
{

attachmentService.delete(jiraServiceContext,attachment.getId() )


}

}

}

0 votes

Hi Manish,

Can you please post the code so we can help?

Cheers

Bhushan

Here you go @Bhushan Nagaraj

import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.crowd.embedded.api.CrowdService;
import com.atlassian.jira.issue.AttachmentManager;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.project.Project;
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.builder.JqlQueryBuilder
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.query.Query
import java.util.*
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.MutableIssue

 

CrowdService crowdService = ComponentAccessor.getCrowdService();
User user = crowdService.getUser("xxxx")

def userManager = ComponentAccessor.getUserManager();
ApplicationUser appUser = userManager.getUserByKey("xxxx");

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObjectByName("Number of Attachments")

SearchProvider searchProvider = ComponentAccessor.getComponentOfType(SearchProvider.class);
SearchService searchService = ComponentAccessor.getComponentOfType(SearchService.class);


IssueIndexManager issueIndexingService = ComponentAccessor.getComponent(IssueIndexManager)



def issueService = ComponentAccessor.getIssueService()


def jqlargument = "project = CA and issuetype = Epic and issuekey =\"CA-42\"";

SearchService.ParseResult parseResult = searchService.parseQuery(user,jqlargument);

Query query = parseResult.getQuery();

SearchResults results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter());

def fetchedIssues= results.getIssues();


for(j=0;j<fetchedIssues.size(); j++)
{
IssueManager issueManager = ComponentAccessor.getComponentOfType(IssueManager);
MutableIssue issue = issueManager.getIssueObject(fetchedIssues.get(j).getKey())

def issueId = issue.getId()

for(i=0; i<issue.getAttachments().size();i++)
{
def authorKey = issue.getAttachments().get(i).getAuthorObject().getKey()
Attachment attachment = issue.getAttachments().get(i)

AttachmentManager attachmentManager = ComponentAccessor.getComponent(AttachmentManager)


if(authorKey=="xxxx")
{
attachmentManager.deleteAttachment(attachment)

}

}


}

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Tuesday in Jira

Looking for anyone who made the switch to Data Center

The Jira Marketing team is putting together an ebook on migrating to Data Center. We're looking for pro tips on how you staffed your project team and organized your Proof of Concept. Share yo...

23 views 0 2
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you