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

This widget could not be displayed.

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() )


}

}

}

This widget could not be displayed.

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)

}

}


}

This widget could not be displayed.

Hi @Jamie Echlin [Adaptavist]@Jamie Echlin [Adaptavist]

Do you guys have any idea about this?

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

112 views 2 0
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