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

Manish Kumar September 21, 2017

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

0 votes
Answer accepted
Manish Kumar September 23, 2017

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
Manish Kumar September 22, 2017

Hi @JamieA@Jamie Echlin _ScriptRunner - The Adaptavist Group_

Do you guys have any idea about this?

0 votes
Bhushan Nagaraj
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 21, 2017

Hi Manish,

Can you please post the code so we can help?

Cheers

Bhushan

Manish Kumar September 22, 2017

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