I have a requirement where I want to archive certain EPICS across my JIRA instance. I know there is an inbuilt scriptrunner job to archive, but we don't want to use it.
Can I please get some help with this.
Thank you !
Hello @Aisha M ,
What do you understand by "archiving" the issues ? Do you mean to move them to another project ?
Hi @Aisha M
Thanks for summoning me
Did you try to filter those issues and archive each of them using Groovy script? I can't help you with the entire code but this might be a good starting point.
import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.archiving.ArchivedIssueService@StandardModuleArchivedIssueService archivedIssueServicedef NOTIFY_USERS = false // if you want to notify users change to truedef user = //.... the user who will archive the issuedef issues = [...] // filter those issues which are less than 500issues.eachWithIndex {it, index -> if (index == 500) return // another control for exceeding 500 limit def validationResult = archivedIssueService.validateArchiveIssue(user, it.getKey(), NOTIFY_USERS); if (validationResult.isValid()) { archivedIssueService.archiveIssue(validationResult); }}
I did not test the code, I just code it here, so please be aware
I hope I clearly understood your request, and this might help you well.
Please let me know if you have questions.
Cheers
EDIT: Here is the latest working code, thanks @Aisha M
import com.atlassian.jira.bc.issue.search.SearchServiceimport com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.archiving.ArchivedIssueServiceimport com.atlassian.jira.issue.search.SearchProviderimport com.atlassian.jira.jql.parser.JqlQueryParserimport com.atlassian.jira.web.bean.PagerFilterArchivedIssueService archivedIssueService = ComponentAccessor.getComponent(ArchivedIssueService)def NOTIFY_USERS = false // if you want to notify users change to truedef jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)def searchService = ComponentAccessor.getComponent(SearchService)def issueManager = ComponentAccessor.getIssueManager()def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()def query = jqlQueryParser.parseQuery("""project = ALTY2 AND issuetype = Epic""")def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())int counter = 0search.results.each { documentIssue -> if (++counter >= 500) return def issue = issueManager.getIssueObject(documentIssue.id) def validationResult = archivedIssueService.validateArchiveIssue(user, issue.getKey(), NOTIFY_USERS); if (validationResult.isValid()) { archivedIssueService.archiveIssue(validationResult); }}return null
FINAL SCRIPT :
import com.atlassian.jira.bc.issue.search.SearchServiceimport com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.archiving.ArchivedIssueServiceimport com.atlassian.jira.issue.search.SearchProviderimport com.atlassian.jira.jql.parser.JqlQueryParserimport com.atlassian.jira.web.bean.PagerFilter
ArchivedIssueService archivedIssueService = ComponentAccessor.getComponent(ArchivedIssueService)
def NOTIFY_USERS = false
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)def searchService = ComponentAccessor.getComponent(SearchService)def issueManager = ComponentAccessor.getIssueManager()def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def query = jqlQueryParser.parseQuery("""project = ALTY2 AND issuetype = Epic""")def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())
int counter = 0search.results.each { documentIssue ->if (++counter > 3) return
def issue = issueManager.getIssueObject(documentIssue.id)def validationResult = archivedIssueService.validateArchiveIssue(user, issue.getKey(), NOTIFY_USERS);log.error ("validationResult: $validationResult")if (validationResult.isValid()) {log.error ("Calling archive service for $issue.key")archivedIssueService.archiveIssue(validationResult);log.error ("Called archive service for $issue.key")}}return null
@Antoine Berry Hello !!! Hope you are well :)
I'm so sorry for bothering, but can you please me with this question . . I have run my wits end to figure out script . . . I know you the only one who is an expert on scripts :)
@Antoine Berry We archive issues just we archive projects right . . It takes it out of the jira index. Then you can restore it went needed.
https://confluence.atlassian.com/adminjiraserver/archiving-an-issue-968669980.html
Hi @Aisha M unfortunately this feature is available for DataCenter only (as you can see in this KB). I am afraid you are running server ?
Looks like we have Data center . . . Just verified now
I am able to archive issue in my instance using the scriptrunner job, but I want to set that up using a script instead of the inbuild job . . But we had a bad incident of when every issue in the instance got archived & it became a huge mess . . so don't want to use that
Or you think if there is any way of archiving those epics with the conditions using a script ?
Calling another script expert @Tuncay Senturk _Snapbytes_ to please pitch in & share some thoughts :) At this point, I'm super desperate for any help ;(
@Tuncay Senturk _Snapbytes_ Thank you for responding No I haven’t used a groovy script for archiving issues . . But I am looking for a groovy script job to achieve this. . .
I had initially set this up (almost) using the inbuilt archive functions available within scriptrunner , but it was given the red flag due to the probability of an error happening in production . . And to be honest , I’m not good with scripting at all . . So, looking for help here . .
Is this script something that is to be used in the script runner job ? Also, is it possible to get the issues through a query in the script ?
I will not be notifying the users , so I ll skip that . . I have an user AUTOMATION that ll perform this. . And the issue must be filtered based on my query . . So that’s okay I guess
But do I need another job if I want an email id to be notified that “this many issues are gonna be archived” in the next 4 days ?
Sorry, it wasn't clear.
The below code might help you even more.
It has the script to fetch those issues, control exceeding 500, and the code archiving the issue.
import com.atlassian.jira.bc.issue.search.SearchServiceimport com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.archiving.ArchivedIssueServiceimport com.atlassian.jira.issue.search.SearchProviderimport com.atlassian.jira.jql.parser.JqlQueryParserimport com.atlassian.jira.web.bean.PagerFilter@StandardModuleArchivedIssueService archivedIssueServicedef NOTIFY_USERS = false // if you want to notify users change to truedef jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)def searchService = ComponentAccessor.getComponent(SearchService)def issueManager = ComponentAccessor.getIssueManager()def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()def query = jqlQueryParser.parseQuery("YOUR JQL HERE to get those issues")def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())int counter = 0search.results.each { documentIssue -> if (counter++ > 500) return def issue = issueManager.getIssueObject(documentIssue.id) def validationResult = archivedIssueService.validateArchiveIssue(user, it.getKey(), NOTIFY_USERS); if (validationResult.isValid()) { archivedIssueService.archiveIssue(validationResult); }}
Please let me know if you are getting error and where!
@Tuncay Senturk _Snapbytes_ Wow, let me try . .
Update - I get an error in the line below
@StandardModule
error - unable to resolve class. Unable to find class for annotation
To put in in simpler terms, I want to do two things,
JOB 1
Run the JQL query
calculate the number results
send a mail to my email about the result count
(after 4 days)
JOB 2
run the JQL query
get the first 500 list
if due >30 then automatically archive
Not sure about how much this makes sense
@Tuncay Senturk _Snapbytes_ One quick question pleaseeeee
is it possible to use cron expression on different unique dates, example
6 July 2022
16 Oct 2022
23 Dec 2022
i tried,
0 0 5 6,16 JUL,OCT ? 2022 but realised this might run on 6,16 of JULY & 6,16 of Oct . . Or is it right
No, I'm afraid you can't do that. You can define recurrent dates but not multiple particular dates.
You can use this website to generate cron expressions.
https://www.freeformatter.com/cron-expression-generator-quartz.html
@Tuncay Senturk _Snapbytes_ Yea, that’s what I thought so too. . Actually, I had been using that site for it . . So wanted to know if it’s possible
@Tuncay Senturk _Snapbytes_ Hi ! Hope you are doing well ! I was looking back at old posts & just wanted to thank you again for how much you have helped me with my Jira questions Especially this question, can't believe the amount of patience you had to figure this out ! THANK YOU !!
Thank you for your kind words! I'm glad I could help. Your appreciation means a lot to me, and it was my pleasure to help.
I'm here if you ever need more assistance.
ps: Just had a look at the post again, wow! loads of messages. But I assure you that it was fun.
It looks like you're new here. Sign in or register to get started.