├── .idea ├── .gitignore ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── ScriptRunner-Scripts.iml ├── bamboo_server └── README.md ├── bitbucket_server └── README.md ├── confluence_server └── README.md ├── jira_cloud ├── #createProject.groovy# ├── addWorklog.groovy ├── commentOnLinkedIssues.groovy ├── createIssue.groovy ├── createIssueLink.groovy ├── createLinkedIssue.groovy ├── createProject.groovy ├── deleteIssuesBulk.groovy ├── getIssue.groovy ├── getIssueLinks.groovy ├── getIssueTypes.groovy ├── getProjectList.groovy ├── lastComment.groovy ├── parseJqlResults.groovy └── searchJQL.groovy └── jira_server ├── #createIssue.groovy# ├── activateDeactivateUser.groovy ├── addUserToGroup.groovy ├── baseurl.groovy ├── behaviour-mandatory-issue-links.groovy ├── behaviour-set-original-estimate-based-on-priority.groovy ├── behaviourCommentRequiredTransitionScreen.groovy ├── cascadeReadOnly.groovy ├── concatenateIssueSummaryAndCreatedDate.groovy ├── copyCommentsLinkedIssues.groovy ├── countCustomFieldValues.groovy ├── createCommentLinkedIssues.groovy ├── createGroup.groovy ├── createIssue.groovy ├── createIssueType.groovy ├── createProject.groovy ├── createProjectComponent.groovy ├── createSubTask.groovy ├── createVersion.groovy ├── deleteIssue.groovy ├── deleteProjects.groovy ├── divideNumbersWSJF.groovy ├── dynamicFormsUserPicker.groovy ├── getAllChangeItems.groovy ├── getAllIssueTypeSchemes.groovy ├── getAllProjectPermissions.groovy ├── getArchivedProjects.groovy ├── getAssignToFirstAssignee.groovy ├── getChangeItemsForField.groovy ├── getCustomFieldObjectByName.groovy ├── getCustomFieldObjectsByName_userPicker.groovy ├── getFieldsOnScreens.groovy ├── getIssue.groovy ├── getIssueLinks.groovy ├── getMultiSelectCustomFieldValue.groovy ├── getPrioritySchemes.groovy ├── getProjects.groovy ├── getProjectsFromIssueTypes.groovy ├── getProjectsFromProjectCategory ├── getSprintState.groovy ├── getSubTaskObjects.groovy ├── getTimeSpent.groovy ├── getVoters.groovy ├── getWatchers.groovy ├── getWorkflowDetails.groovy ├── getWorkflows.groovy ├── hasAssociatedIssues.groovy ├── hideCustomFieldBasedOnCascadeSelectList.groovy ├── jqlParser.groovy ├── lastComment.groovy ├── listAllProjects.groovy ├── numberOfComments.groovy ├── postFunctionConditionSelectListMultipleChoice.groovy ├── postFunctionModifySummaryOnCreate.groovy ├── postFunctionUpdateMessageField.groovy ├── postFunctionUpdateProjectRoleFromUserPickerField.groovy ├── postFunction_addWatcher.groovy ├── projectRename.groovy ├── readCSVFile.groovy ├── removeAllChangeItems.groovy ├── removeUser.groovy ├── removeUserFromGroup.groovy ├── runSQL.groovy ├── runSelectQueryDatabase.groovy ├── sendEmail.groovy ├── setSecurityLevel.groovy ├── systemCommands.groovy ├── transitionIssue.groovy ├── updateSingleSelectList.groovy ├── updateValue.groovy ├── userList.groovy └── workflowCondition_checkSelectListValue.groovy /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Introduction 2 | This is a repository of some useful groovy scripts to make your life easier. There would be script on all applications supported by ScriptRunner. Of course you need ScriptRunner. 3 | 4 | The ScriptRunner for Jira video tutorials can be found here: https://go.ravi.pro/ScriptRunnerJira 5 | 6 | Enjoy and no need to say thanks :) 7 | 8 | ## Social 9 | 1. Blog: https://www.ravisagar.in 10 | 2. Linkedin: https://www.linkedin.com/in/ravisagar/ 11 | 3. Twitter: https://www.twitter.com/ravisagar 12 | 4. Instagram: https://www.instagram.com/ravisagar.in 13 | 14 | ## Sponsor 15 | [Sparxsys Solutions](https://www.sparxsys.com) 16 | An Atlassian and Drupal Consultancy company in London and Delhi. They provide [ScriptRunner for Jira services](https://www.sparxsys.com/services/atlassian-consultancy/scriptrunner-jira) 17 | -------------------------------------------------------------------------------- /ScriptRunner-Scripts.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /bamboo_server/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sparxsys/ScriptRunner-Scripts/d20d640d461f66ef2ea2a5df6557792527c071e0/bamboo_server/README.md -------------------------------------------------------------------------------- /bitbucket_server/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sparxsys/ScriptRunner-Scripts/d20d640d461f66ef2ea2a5df6557792527c071e0/bitbucket_server/README.md -------------------------------------------------------------------------------- /confluence_server/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sparxsys/ScriptRunner-Scripts/d20d640d461f66ef2ea2a5df6557792527c071e0/confluence_server/README.md -------------------------------------------------------------------------------- /jira_cloud/#createProject.groovy#: -------------------------------------------------------------------------------- 1 | def resp = post('/rest/api/3/project') 2 | .header('Content-Type', 'application/json') 3 | .body( 4 | [ 5 | key: "PROJXYZZ", 6 | name: "PROJXYZZ", 7 | projectTypeKey: "business", 8 | projectTemplateKey: "com.atlassian.jira-core-project-templates:jira-core-project-management", 9 | description: "Rest demo project", 10 | leadAccountId: "557058:f2a3043a-ebcb-4815-9277-e0e14c4d9776", 11 | url: "http://www.ravisagar.in", 12 | assigneeType: "PROJECT_LEAD", 13 | avatarId: 10200, 14 | notificationScheme: 10007 15 | ] 16 | 17 | 18 | ) 19 | .asString().body 20 | 21 | return resp 22 | 23 | -------------------------------------------------------------------------------- /jira_cloud/addWorklog.groovy: -------------------------------------------------------------------------------- 1 | post('/rest/api/3/issue/bt-2/worklog') 2 | .header('Content-Type', 'application/json') 3 | .body( 4 | [ 5 | timeSpent: "20m", 6 | comment: [ 7 | type: "doc", 8 | version: 1, 9 | content: [ 10 | [ 11 | type: "paragraph", 12 | content: [ 13 | [ 14 | text: "This is from SR4J on Cloud.", 15 | type: "text" 16 | ] 17 | ] 18 | ] 19 | ] 20 | ] 21 | 22 | ]) 23 | .asString().body 24 | -------------------------------------------------------------------------------- /jira_cloud/commentOnLinkedIssues.groovy: -------------------------------------------------------------------------------- 1 | def issueKey = issue.key 2 | 3 | def response = get('/rest/api/3/issue/'+issueKey) 4 | .header('Content-Type', 'application/json') 5 | .asObject(Map) 6 | 7 | logger.info("Issue id: " + response.body.id) 8 | logger.info("Priority name: "+ response.body.fields.priority.name) 9 | logger.info("Inward issue links: "+ response.body.fields.issuelinks*.inwardIssue.key) 10 | logger.info("Outward issue links: "+ response.body.fields.issuelinks*.outwardIssue.key) 11 | 12 | response.body.fields.issuelinks.each { it-> 13 | 14 | if(it.inwardIssue) { 15 | logger.info("Inward issue: "+ it.inwardIssue.key) 16 | //add a comment 17 | addComment(issueKey, it.inwardIssue.key) 18 | } else if (it.outwardIssue) { 19 | logger.info("Outward issue: "+ it.outwardIssue.key) 20 | //add a comment 21 | } 22 | 23 | } 24 | 25 | def addComment(orginalIssue, linkedIssue) { 26 | 27 | 28 | def comment = orginalIssue + ' issue moved to on Hold' 29 | 30 | def result = post("/rest/api/2/issue/${linkedIssue}/comment") 31 | .header('Content-Type', 'application/json') 32 | .body([ 33 | body: comment 34 | ]) 35 | .asString() 36 | 37 | } 38 | 39 | -------------------------------------------------------------------------------- /jira_cloud/createIssue.groovy: -------------------------------------------------------------------------------- 1 | //ScriptRunner for Jira on Cloud - Create an Issue 2 | def resp = post("/rest/api/3/issue") 3 | .header("Content-Type", "application/json") 4 | .body( 5 | fields: [ 6 | summary: "New issue from SR4J on cloud :)", 7 | issuetype: [ 8 | id: 10001 9 | ], 10 | project: [ 11 | id: 10024 12 | ] 13 | ]) 14 | .asObject(Map) 15 | 16 | 17 | // If the task created successfully return a success message along with its key 18 | if (resp.status == 201) { 19 | return 'Success - Created with the key of ' + resp.body.key.toString() 20 | } else { 21 | return "${resp.status}: ${resp.body}" 22 | } -------------------------------------------------------------------------------- /jira_cloud/createIssueLink.groovy: -------------------------------------------------------------------------------- 1 | //Bt-5 blocks Bt-2 >> Outward link 2 | //Bt-2 blocked by Bt-5 >> Inward link 3 | 4 | post('/rest/api/3/issueLink') 5 | .header('Content-Type', 'application/json') 6 | .body( 7 | 8 | [ 9 | outwardIssue: [ 10 | key: "bt-2" 11 | ], 12 | 13 | inwardIssue: [ 14 | key: "bt-5" 15 | ], 16 | type: [ 17 | name: "Blocks" 18 | ] 19 | ] 20 | 21 | 22 | ) 23 | .asString().body 24 | 25 | 26 | -------------------------------------------------------------------------------- /jira_cloud/createLinkedIssue.groovy: -------------------------------------------------------------------------------- 1 | //ScriptRunner for Jira on Cloud - Create an Issue 2 | def resp = post("/rest/api/3/issue") 3 | .header("Content-Type", "application/json") 4 | .body( 5 | fields: [ 6 | summary: "Issue created from On Hold transition post function", 7 | issuetype: [ 8 | id: 10009 9 | ], 10 | project: [ 11 | id: 10037 12 | ] 13 | ]) 14 | .asObject(Map) 15 | 16 | 17 | // If the task created successfully return a success message along with its key 18 | if (resp.status == 201) { 19 | logger.info( "Success - Created with the key of " + resp.body.key.toString() ) 20 | 21 | createIssueLink(issue.key, resp.body.key.toString()) 22 | 23 | } else { 24 | logger.info( "${resp.status}: ${resp.body}" ) 25 | } 26 | 27 | 28 | def createIssueLink(outwardIssueKey, inwardIssueKey) { 29 | 30 | post('/rest/api/3/issueLink') 31 | .header('Content-Type', 'application/json') 32 | .body( 33 | 34 | [ 35 | outwardIssue: [ 36 | key: outwardIssueKey 37 | ], 38 | 39 | inwardIssue: [ 40 | key: inwardIssueKey 41 | ], 42 | type: [ 43 | name: "Blocks" 44 | ] 45 | ] 46 | 47 | 48 | ) 49 | .asString().body 50 | 51 | } 52 | 53 | -------------------------------------------------------------------------------- /jira_cloud/createProject.groovy: -------------------------------------------------------------------------------- 1 | def resp = post('/rest/api/3/project') 2 | .header('Content-Type', 'application/json') 3 | .body( 4 | [ 5 | key: "PROJXYZZ", 6 | name: "PROJXYZZ", 7 | projectTypeKey: "business", 8 | projectTemplateKey: "com.atlassian.jira-core-project-templates:jira-core-project-management", 9 | description: "Rest demo project", 10 | leadAccountId: "557058:f2a3043a-ebcb-4815-9277-e0e14c4d9776", 11 | url: "http://www.ravisagar.in", 12 | assigneeType: "PROJECT_LEAD", 13 | avatarId: 10200, 14 | notificationScheme: 10007 15 | ] 16 | 17 | 18 | ) 19 | .asString().body 20 | 21 | return resp 22 | -------------------------------------------------------------------------------- /jira_cloud/deleteIssuesBulk.groovy: -------------------------------------------------------------------------------- 1 | def response = get('/rest/api/2/search?jql=filter=10047') 2 | .header('Content-Type', 'application/json') 3 | .asObject(Map) 4 | 5 | response.body.issues.each { it -> 6 | 7 | getIssueDetails(it) 8 | 9 | } 10 | 11 | def getIssueDetails(issue) { 12 | 13 | logger.info("Issue Key: " + issue.key +", "+ issue.fields.priority.name + ", " + issue.fields.status.name) 14 | 15 | //Delete the issues with a specific status 16 | if (issue.fields.status.name == "To Do" ) { 17 | deleteIssue(issue.key) 18 | } 19 | } 20 | 21 | 22 | def deleteIssue(issueKey) { 23 | 24 | logger.info("Issue to be deleted: " + issueKey) 25 | 26 | def responseDelete = delete('/rest/api/3/issue/'+issueKey+'?deleteSubtasks=true') 27 | .header('Content-Type', 'application/json') 28 | .asObject(Map) 29 | 30 | if(responseDelete.status==403) { 31 | 32 | logger.info("Error: " + responseDelete.body.errorMessages) 33 | 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /jira_cloud/getIssue.groovy: -------------------------------------------------------------------------------- 1 | def response = get('/rest/api/3/issue/cts-12') 2 | .header('Content-Type', 'application/json') 3 | .asObject(Map) 4 | 5 | logger.info("Issue id: " + response.body.id) 6 | logger.info("Priority name: "+ response.body.fields.priority.name) 7 | 8 | return response.body 9 | -------------------------------------------------------------------------------- /jira_cloud/getIssueLinks.groovy: -------------------------------------------------------------------------------- 1 | def response = get('/rest/api/3/issue/AN-4') 2 | .header('Content-Type', 'application/json') 3 | .asObject(Map) 4 | 5 | logger.info("Issue id: " + response.body.id) 6 | logger.info("Priority name: "+ response.body.fields.priority.name) 7 | logger.info("Inward issue links: "+ response.body.fields.issuelinks*.inwardIssue.key) 8 | logger.info("Outward issue links: "+ response.body.fields.issuelinks*.outwardIssue.key) 9 | 10 | return response.body 11 | -------------------------------------------------------------------------------- /jira_cloud/getIssueTypes.groovy: -------------------------------------------------------------------------------- 1 | def responseIssueTypes = get("/rest/api/3/issuetype").asObject(List) 2 | 3 | def issueTypes = responseIssueTypes.body as List 4 | 5 | //Get only standard issue type with name "Task" 6 | 7 | return issueTypes.findAll{it.name=="Task" && !it.subtask} 8 | -------------------------------------------------------------------------------- /jira_cloud/getProjectList.groovy: -------------------------------------------------------------------------------- 1 | def response = get('/rest/api/3/project/search?expand=lead') 2 | .header('Content-Type', 'application/json') 3 | .asObject(Map)srip 4 | 5 | return response.body.values.each{ it -> 6 | 7 | logger.info(it.key+","+it.name+","+it.lead.displayName) 8 | 9 | } 10 | -------------------------------------------------------------------------------- /jira_cloud/lastComment.groovy: -------------------------------------------------------------------------------- 1 | //ScriptRunner for Jira on Cloud - Get the last comment 2 | //def issueKey = "IOSTWO-32" 3 | def issueKey = issue.key 4 | 5 | def resp = get("/rest/api/3/issue/${issueKey}/comment") 6 | .header("Content-Type", "application/json") 7 | .asObject(Map) 8 | 9 | return resp.body.comments ? resp.body.comments.last().body.content.content.text[0][0] : "No comments" 10 | -------------------------------------------------------------------------------- /jira_cloud/parseJqlResults.groovy: -------------------------------------------------------------------------------- 1 | //Script to perform search to get list of issues and then iterate over them 2 | 3 | def response = get('/rest/api/2/search?jql=filter=10047') 4 | .header('Content-Type', 'application/json') 5 | .asObject(Map) 6 | 7 | response.body.issues.each { it -> 8 | 9 | getIssueDetails(it) 10 | 11 | } 12 | 13 | def getIssueDetails(issue) { 14 | 15 | logger.info("Issue Key: " + issue.key +", "+ issue.fields.priority.name) 16 | } 17 | -------------------------------------------------------------------------------- /jira_cloud/searchJQL.groovy: -------------------------------------------------------------------------------- 1 | //ScriptRunner for Jira on Cloud - fetch issue keys based on a jql 2 | def resp = get("/rest/api/3/search?jql=project=CT") 3 | .header("Content-Type", "application/json") 4 | .asObject(Map) 5 | 6 | 7 | '${resp.status}: ' + resp.body.issues*.key 8 | -------------------------------------------------------------------------------- /jira_server/#createIssue.groovy#: -------------------------------------------------------------------------------- 1 | 2 | import com.atlassian.jira.component.ComponentAccessor 3 | import com.atlassian.jira.issue.MutableIssue 4 | 5 | 6 | def project = ComponentAccessor.projectManager.getProjectObjByKey("SCRUM") 7 | def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser 8 | 9 | 10 | MutableIssue issue = ComponentAccessor.issueFactory.issue 11 | issue.projectObject = project 12 | issue.summary = "Demo issue created from the script" 13 | issue.issueTypeId = 10102 14 | issue.assignee = user 15 | ComponentAccessor.issueManager.createIssueObject(user, issue) 16 | 17 | -------------------------------------------------------------------------------- /jira_server/activateDeactivateUser.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.bc.user.UserService 3 | import org.apache.log4j.Level 4 | import org.apache.log4j.Logger 5 | 6 | //Log 7 | def log = Logger.getLogger("in.ravisagar.sr4j") //Not needed when binding variable is available 8 | log.setLevel(Level.DEBUG) 9 | 10 | //String builder for log 11 | def sb = new StringBuilder() 12 | 13 | activateDeactivateUser("kapil.m",false, log) 14 | 15 | static def activateDeactivateUser(String userName, Boolean activate, log) { 16 | def userToUpdate = ComponentAccessor.getUserManager().getUserByName(userName) 17 | if (userToUpdate) { 18 | def userService = ComponentAccessor.getComponent(UserService) 19 | def updateUser = userService.newUserBuilder(userToUpdate).active(activate).build() 20 | def updateUserValidationResult = userService.validateUpdateUser(updateUser) 21 | if (!updateUserValidationResult.valid) { 22 | log.error "Update of ${userToUpdate.name} failed. ${updateUserValidationResult.errorCollection}" 23 | return 24 | } 25 | 26 | switch (activate){ 27 | case true: 28 | userToUpdate.isActive() ? log.info("User already active"): log.info("Attempting to activate") 29 | userService.updateUser(updateUserValidationResult) 30 | log.info "${updateUser.name} Activated" 31 | break 32 | case false: 33 | !userToUpdate.isActive() ? log.info("User already inactive"): log.info("Attempting to inactivate") 34 | userService.updateUser(updateUserValidationResult) 35 | log.info "${updateUser.name} deactivated" 36 | break 37 | default: break 38 | } 39 | 40 | } else { 41 | log.info "User not found" 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /jira_server/addUserToGroup.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | 3 | def userName = "user2" 4 | def groupNametoAdd = "jira-servicedesk-users" 5 | def userToAdd = ComponentAccessor.getUserManager().getUserByName(userName) 6 | def groupName = ComponentAccessor.getGroupManager().getGroup(groupNametoAdd) 7 | 8 | 9 | if (!userToAdd) { 10 | log.error("User doesn't exist") 11 | return 12 | } 13 | 14 | if (ComponentAccessor.getGroupManager().getGroupsForUser(userName).contains(groupName)) { 15 | log.error("User: $userToAdd.username already in the group: $groupName.name") 16 | return 17 | } 18 | 19 | 20 | 21 | if(!groupName) { 22 | log.error("Group doesn't exist") 23 | } else { 24 | ComponentAccessor.groupManager.addUserToGroup(userToAdd, groupName) 25 | log.error("User: $userToAdd.username added to the group: $groupName.name") 26 | } 27 | -------------------------------------------------------------------------------- /jira_server/baseurl.groovy: -------------------------------------------------------------------------------- 1 | /** 2 | * How to fetch the base url of a Jira issue 3 | * Video link: https://youtu.be/Oym0h65qiLk 4 | */ 5 | 6 | import com.atlassian.jira.issue.IssueManager 7 | import com.atlassian.jira.issue.MutableIssue 8 | import com.atlassian.jira.component.ComponentAccessor 9 | import com.atlassian.jira.issue.util.DefaultIssueChangeHolder 10 | import com.atlassian.jira.issue.ModifiedValue 11 | 12 | IssueManager im = ComponentAccessor.getIssueManager() 13 | MutableIssue issue = im.getIssueObject("ANDROID-126") 14 | 15 | def baseurl = com.atlassian.jira.component.ComponentAccessor.getApplicationProperties().getString("jira.baseurl") 16 | 17 | 18 | if(issue){ 19 | 20 | 21 | return baseurl +"/browse/"+ issue.key 22 | 23 | 24 | 25 | 26 | }else { 27 | 28 | return "Issue doesn't exist" 29 | } 30 | -------------------------------------------------------------------------------- /jira_server/behaviour-mandatory-issue-links.groovy: -------------------------------------------------------------------------------- 1 | /** 2 | * Make the links field required when there are no existing link and 3 | * the link type is "is part of" 4 | * Video link: https://youtu.be/q4KfHSTdVeA 5 | */ 6 | 7 | import com.atlassian.jira.component.ComponentAccessor 8 | import com.atlassian.jira.issue.link.IssueLink 9 | 10 | def links = getFieldById("issuelinks") 11 | def linksValue = links.getValue() 12 | def linkType = getFieldById("issuelinks-linktype") 13 | def linkTypeValue = linkType.getValue() 14 | def linkField = getFieldById("issuelinks-issues") 15 | def linkFieldValue = linkField.getValue() 16 | 17 | log.debug("Links Value: "+ linksValue) 18 | log.debug("Link Type Value: "+ linkTypeValue) 19 | log.debug("Links Value: "+ linkFieldValue) 20 | 21 | def existingLinks = ComponentAccessor.getIssueLinkManager().getInwardLinks(underlyingIssue?.getId()) 22 | 23 | if (existingLinks?.size() == 0 && linkTypeValue == "is part of") { 24 | 25 | links.setRequired(true) 26 | 27 | } else { 28 | 29 | links.setRequired(false) 30 | 31 | } 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /jira_server/behaviour-set-original-estimate-based-on-priority.groovy: -------------------------------------------------------------------------------- 1 | /* Set original estimates based on priority */ 2 | 3 | import com.atlassian.jira.issue.IssueConstantImpl 4 | 5 | def priority = getFieldById(getFieldChanged()) 6 | def selectedPriority = ((IssueConstantImpl) priority.getValue()).getName() 7 | 8 | def OE = getFieldById("timetracking_originalestimate") 9 | 10 | 11 | log.debug("Logging Priority: " + selectedPriority) 12 | 13 | 14 | if ( selectedPriority ==("Highest") ) 15 | { OE.setFormValue("24h") 16 | } 17 | else if ( selectedPriority==("High") ) 18 | { OE.setFormValue("3d") 19 | } 20 | else if ( selectedPriority==("Medium") ) 21 | { OE.setFormValue("24d") 22 | } 23 | else if ( selectedPriority==("Lowest") ) 24 | { OE.setFormValue("48d") 25 | } 26 | else 27 | { 28 | OE.setFormValue("0h") 29 | } 30 | -------------------------------------------------------------------------------- /jira_server/behaviourCommentRequiredTransitionScreen.groovy: -------------------------------------------------------------------------------- 1 | import com.onresolve.jira.groovy.user.FieldBehaviours 2 | import groovy.transform.BaseScript 3 | import org.apache.log4j.Level 4 | import org.apache.log4j.Logger 5 | 6 | @BaseScript FieldBehaviours fieldBehaviours 7 | 8 | def log = Logger.getLogger("sr4j.behaviour.com") 9 | log.setLevel(Level.DEBUG) 10 | 11 | def commentField = getFieldById("comment") 12 | def changedFormField = getFieldById(fieldChanged) 13 | 14 | 15 | if (changedFormField.formValue.toString().contains("10001")) { 16 | log.debug("Required True") 17 | commentField.setRequired(true) 18 | } else 19 | { 20 | log.debug("Required False") 21 | commentField.setRequired(false) 22 | } 23 | 24 | 25 | -------------------------------------------------------------------------------- /jira_server/cascadeReadOnly.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | **In response to the question here 3 | https://community.atlassian.com/t5/Marketplace-Apps-Integrations/How-do-I-gray-out-the-second-level-of-cascading-select-field/qaq-p/1185971#U1187530 4 | */ 5 | import com.atlassian.jira.component.ComponentAccessor 6 | import com.atlassian.jira.issue.customfields.manager.OptionsManager 7 | import com.atlassian.jira.issue.fields.config.FieldConfig 8 | import com.atlassian.jira.issue.customfields.option.Options 9 | 10 | log.debug("******Logging starts********") 11 | 12 | def parentField = getFieldById("customfield_10802") 13 | def parentFieldValue = parentField.getValue() 14 | def childField = getFieldById("customfield_10802:1") 15 | def childFieldValue = childField.getValue() 16 | 17 | def customField = getCustomFieldManager().getCustomFieldObjectsByName("Country State City") 18 | 19 | FieldConfig config = customField[0].getRelevantConfig(getIssueContext()) 20 | 21 | def rootOptions_ = ComponentAccessor.getOptionsManager().getOptions(config) 22 | def childOptions_ = [:] 23 | 24 | rootOptions_.rootOptions.each { it -> 25 | 26 | childOptions_[it] = it.childOptions 27 | 28 | } 29 | def master = ["India","France","USA"] 30 | def slave = ["India"] 31 | 32 | log.debug ("True or false: " + master.containsAll(slave)) 33 | 34 | log.debug("Master: "+ master) 35 | log.debug("Slave: "+ slave) 36 | 37 | def nullcheck = [] 38 | nullcheck = childOptions_.findAll { it.value.size() == 0 }.keySet() 39 | 40 | log.debug("Outside NULL Check: " + nullcheck) 41 | log.debug("Outside Selected Parent outside if: " + parentFieldValue[0].toString()) 42 | log.debug(parentFieldValue.isEmpty()) 43 | log.debug("Contains true or false: " + nullcheck.findAll { it ==~ parentFieldValue[0].toString() }.size() ) 44 | 45 | def readOnlyYes = nullcheck.findAll { it ==~ parentFieldValue[0].toString() }.size() 46 | 47 | if( !parentFieldValue.isEmpty() && readOnlyYes >= 1) { 48 | 49 | log.debug("Inside NULL Check: " + nullcheck) 50 | log.debug("Inside Selected Parent outside if: " + parentFieldValue) 51 | 52 | childField.setReadOnly(true) 53 | 54 | } else { 55 | childField.setReadOnly(false) 56 | 57 | } 58 | 59 | /* 60 | childOptions_.findAll { it.value.size() == 0 }.keySet().each { item -> 61 | 62 | 63 | log.debug("Iterating outside if, item is: "+item) 64 | log.debug("Selected Parent outside if: " + parentFieldValue[0]) 65 | 66 | 67 | if (parentFieldValue[0] != null && parentFieldValue[0].toString() == item.toString()) { 68 | 69 | log.debug("Iterating inside if, item is: "+item) 70 | log.debug("Selected Parent inside if: " + parentFieldValue[0]) 71 | 72 | childField.setReadOnly(true) 73 | } 74 | 75 | else { 76 | 77 | childField.setReadOnly(false) 78 | } 79 | 80 | } 81 | */ 82 | 83 | 84 | /* 85 | log.debug("Selected Parent: " + parentFieldValue[0]) 86 | log.debug("Selected Child: " + childFieldValue) 87 | log.debug("Root Options: " + rootOptions_.rootOptions ) 88 | log.debug("Child all options: " + childOptions_) 89 | log.debug("Child all options: " + childOptions_.values()*.size()) 90 | log.debug("Null parents: " + childOptions_.findAll { it.value.size() == 0 }.keySet() ) 91 | log.debug("Index of null child: " + childOptions_.findIndexOf { it.value.size() == 0 }) 92 | //log.debug("Child Field: ") 93 | 94 | */ 95 | 96 | /* 97 | def salescolValue = getFieldById(getFieldChanged()).getValue() 98 | def salescolOwnValue = getFieldByName("Sales Collateral Owner").getValue() 99 | def salescolCom = getFieldByName("Sales Collateral Completed") 100 | 101 | if ( !salescolValue.toString().contains("null") && salescolOwnValue) { 102 | 103 | salescolCom.setReadOnly(false) 104 | 105 | } 106 | else { 107 | 108 | salescolCom.setReadOnly(true) 109 | 110 | } 111 | 112 | */ -------------------------------------------------------------------------------- /jira_server/concatenateIssueSummaryAndCreatedDate.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.issue.IssueManager 2 | import com.atlassian.jira.issue.MutableIssue 3 | import com.atlassian.jira.component.ComponentAccessor 4 | import com.atlassian.jira.issue.util.DefaultIssueChangeHolder 5 | import com.atlassian.jira.issue.ModifiedValue 6 | 7 | IssueManager im = ComponentAccessor.getIssueManager() 8 | MutableIssue issue = im.getIssueObject("SD-18") //Change Issue Key 9 | 10 | return issue.summary + " - " + issue.created 11 | -------------------------------------------------------------------------------- /jira_server/copyCommentsLinkedIssues.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.issue.IssueManager 2 | import com.atlassian.jira.issue.MutableIssue 3 | import com.atlassian.jira.component.ComponentAccessor 4 | import com.atlassian.jira.issue.comments.CommentManager 5 | 6 | CommentManager commentMgr = ComponentAccessor.getCommentManager() 7 | 8 | IssueManager im = ComponentAccessor.getIssueManager() 9 | MutableIssue issue = im.getIssueObject("ISD-6") 10 | 11 | 12 | //def links = ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getId()) 13 | def links = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId()) 14 | 15 | for( l in links) { 16 | 17 | 18 | commentMgr.getComments(issue).each { it -> 19 | 20 | commentMgr.create(l.getDestinationObject(), it.authorApplicationUser, it.updateAuthorApplicationUser, it.body, it.groupLevel, it.roleLevelId, it.created, it.updated, false) 21 | } 22 | 23 | } 24 | 25 | 26 | -------------------------------------------------------------------------------- /jira_server/countCustomFieldValues.groovy: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | */ 4 | 5 | 6 | 7 | import com.atlassian.jira.component.ComponentAccessor 8 | import com.atlassian.jira.bc.issue.search.SearchService 9 | import com.atlassian.jira.jql.parser.JqlQueryParser 10 | import com.atlassian.jira.web.bean.PagerFilter 11 | import com.atlassian.jira.issue.IssueManager 12 | import com.atlassian.jira.component.ComponentAccessor 13 | import com.atlassian.jira.issue.MutableIssue 14 | import org.apache.log4j.Logger 15 | import org.apache.log4j.Level 16 | 17 | def log = Logger.getLogger("com.test.count") 18 | log.setLevel(Level.DEBUG) 19 | 20 | def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser) 21 | def searchService = ComponentAccessor.getComponent(SearchService) 22 | def issueManager = ComponentAccessor.getIssueManager() 23 | def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser 24 | 25 | 26 | def query = jqlQueryParser.parseQuery("project = DEMO and assignee = currentUser()") 27 | 28 | def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter()) 29 | 30 | log.debug("Total issues: ${search.total}") 31 | 32 | def countOptions = [] 33 | 34 | search.results.each { documentIssue -> 35 | log.debug(documentIssue.key) 36 | 37 | IssueManager im = ComponentAccessor.getIssueManager() 38 | MutableIssue issue = im.getIssueObject(documentIssue.id) 39 | // do something to the issue... 40 | log.debug(issue.summary) 41 | 42 | def customFieldManager = ComponentAccessor.getCustomFieldManager() 43 | def cField = customFieldManager.getCustomFieldObjectsByName("Select List") 44 | 45 | def cFieldValue = issue.getCustomFieldValue(cField[0]) 46 | 47 | if(cFieldValue) { 48 | countOptions.add(cFieldValue.toString()) 49 | } 50 | 51 | // log.debug(cFieldValue) 52 | //IssueManager im = ComponentAccessor.getIssueManager() 53 | //MutableIssue issue = im.getIssueObject(issue.key) 54 | } 55 | 56 | log.debug(countOptions) 57 | log.debug(countOptions.count("Select 1")) 58 | log.debug(countOptions.count("Select 2")) 59 | log.debug(countOptions.count("Select 3")) 60 | -------------------------------------------------------------------------------- /jira_server/createCommentLinkedIssues.groovy: -------------------------------------------------------------------------------- 1 | /** 2 | * Create a comment on a linked issue 3 | * Video link: 4 | **/ 5 | import com.atlassian.jira.issue.IssueManager; 6 | import com.atlassian.jira.issue.MutableIssue; 7 | import com.atlassian.jira.component.ComponentAccessor; 8 | import com.atlassian.jira.issue.link.IssueLink; 9 | 10 | import com.atlassian.jira.issue.comments.CommentManager; 11 | import com.atlassian.jira.user.ApplicationUser; 12 | 13 | CommentManager commentMgr = ComponentAccessor.getCommentManager() 14 | ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().loggedInUser 15 | 16 | 17 | IssueManager im = ComponentAccessor.getIssueManager(); 18 | MutableIssue issue = im.getIssueObject("ANDROID-124"); 19 | 20 | 21 | def lastComment = commentMgr.getComments(issue).last().body 22 | 23 | //def links = ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getId()) 24 | def links = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId()) 25 | 26 | //return links[0].getSourceObject() 27 | 28 | def output = "" 29 | for( l in links) { 30 | 31 | //use this for Outward links 32 | output = output + l.issueLinkType.name + ": " + l.getDestinationObject() + "
" 33 | 34 | //use this for Inward links 35 | // output = output + l.issueLinkType.name + ": " + l.getSourceObject() + "
" 36 | 37 | commentMgr.create(l.getDestinationObject(), currentUser, lastComment, false) 38 | 39 | } 40 | 41 | return output 42 | 43 | 44 | -------------------------------------------------------------------------------- /jira_server/createGroup.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.crowd.exception.embedded.InvalidGroupException 2 | import com.atlassian.jira.component.ComponentAccessor 3 | 4 | def groupName = "Group123" 5 | //Call to add the group 6 | addGroup(groupName) 7 | 8 | def addGroup(String groupName) { 9 | 10 | try { 11 | ComponentAccessor.groupManager.createGroup(groupName) 12 | log.error("Group added") 13 | } 14 | catch (InvalidGroupException invalidGroupException) { 15 | 16 | log.error("Error creating group: " + invalidGroupException.message) 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /jira_server/createIssue.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.issue.MutableIssue 3 | 4 | 5 | def project = ComponentAccessor.projectManager.getProjectObjByKey("SCRUM") 6 | def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser 7 | 8 | 9 | MutableIssue issue = ComponentAccessor.issueFactory.issue 10 | issue.projectObject = project 11 | issue.summary = "Demo issue created from the script" 12 | issue.issueTypeId = 10102 13 | issue.assignee = user 14 | ComponentAccessor.issueManager.createIssueObject(user, issue) 15 | 16 | -------------------------------------------------------------------------------- /jira_server/createIssueType.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.config.IssueTypeManager 3 | 4 | IssueTypeManager issueTypeManager = ComponentAccessor.getComponent(IssueTypeManager) 5 | 6 | // Get all the existing avatar id 7 | // issueTypeManager.getIssueTypes()*.avatar*.id 8 | 9 | issueTypeManager.createIssueType("Test Campaign", "This is a bundle of test cases", 10316) 10 | issueTypeManager.getIssueTypes()*.name 11 | -------------------------------------------------------------------------------- /jira_server/createProject.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.bc.project.ProjectCreationData 2 | import com.atlassian.jira.bc.project.ProjectService 3 | import com.atlassian.jira.component.ComponentAccessor 4 | import com.atlassian.jira.project.AssigneeTypes 5 | import com.atlassian.jira.project.type.ProjectTypeKey 6 | 7 | def projectKey = "HR" 8 | def projectName = "Business Core HR" 9 | def projectDescription = "Business Core Project" 10 | def projectUrl = "https://www.ravisagar.in" 11 | 12 | def projectService = ComponentAccessor.getComponent(ProjectService) 13 | def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser 14 | def projectTypeKey = new ProjectTypeKey("business") 15 | 16 | def projectCreationData = new ProjectCreationData.Builder().with { 17 | withName(projectName) 18 | withKey(projectKey) 19 | withDescription(projectDescription) 20 | withLead(loggedInUser) 21 | withUrl(projectUrl) 22 | withAssigneeType(AssigneeTypes.PROJECT_LEAD) 23 | withType(projectTypeKey) 24 | }.build() 25 | 26 | ProjectService.CreateProjectValidationResult createProjectValidationResult = 27 | projectService.validateCreateProject( 28 | loggedInUser, 29 | projectCreationData) 30 | 31 | if(!createProjectValidationResult.getErrorCollection().errors) 32 | { 33 | projectService.createProject(createProjectValidationResult) 34 | 35 | } else { 36 | return ("Project cannot be created ${createProjectValidationResult.getErrorCollection().errors}" ) 37 | } 38 | 39 | 40 | -------------------------------------------------------------------------------- /jira_server/createProjectComponent.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | 3 | def component = ComponentAccessor.projectComponentManager.create("Component 2", 4 | "Component 2 description", 5 | "admin", 6 | 1, 7 | 10000) 8 | 9 | return component.name 10 | -------------------------------------------------------------------------------- /jira_server/createSubTask.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.issue.MutableIssue 3 | 4 | 5 | def project = ComponentAccessor.projectManager.getProjectObjByKey("SCRUM") 6 | def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser 7 | 8 | def parentIssue = ComponentAccessor.issueManager.getIssueByCurrentKey("SCRUM-38") 9 | 10 | 11 | //return project.issueTypes*.id 12 | MutableIssue issue = ComponentAccessor.issueFactory.issue 13 | issue.projectObject = project 14 | issue.summary = "This is a sub task" 15 | issue.issueTypeId = 10201 16 | issue.assignee = user 17 | ComponentAccessor.issueManager.createIssueObject(user, issue) 18 | 19 | ComponentAccessor.subTaskManager.createSubTaskIssueLink(parentIssue, issue, user) 20 | -------------------------------------------------------------------------------- /jira_server/createVersion.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | 3 | def version = ComponentAccessor.versionManager.createVersion("Version 3", 4 | null, 5 | null, 6 | "Version 3 description", 7 | 10000, 8 | null,false) 9 | 10 | return version.name 11 | -------------------------------------------------------------------------------- /jira_server/deleteIssue.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.event.type.EventDispatchOption 3 | import com.atlassian.jira.issue.IssueManager 4 | import com.atlassian.jira.issue.MutableIssue 5 | import com.atlassian.jira.user.ApplicationUser 6 | import org.apache.log4j.Level 7 | import org.apache.log4j.Logger 8 | 9 | 10 | def log = Logger.getLogger("in.ravisagar.sr4j") 11 | log.setLevel(Level.DEBUG) 12 | 13 | IssueManager im = ComponentAccessor.getIssueManager() 14 | def issue = im.getIssueObject("SCRUM-25") as MutableIssue 15 | 16 | ApplicationUser user = ComponentAccessor.getUserManager().getUserByName("admin") 17 | 18 | im.deleteIssue(user, issue, EventDispatchOption.ISSUE_DELETED, false) 19 | -------------------------------------------------------------------------------- /jira_server/deleteProjects.groovy: -------------------------------------------------------------------------------- 1 | /** 2 | * Script to delete a project 3 | * Video link: 4 | **/ 5 | package com.example 6 | import com.atlassian.jira.component.ComponentAccessor 7 | 8 | def projectManager = ComponentAccessor.getProjectManager() 9 | 10 | //These 2 lines can delete a project 11 | def delete_project = projectManager.getProjects()[1] 12 | projectManager.removeProject(delete_project) 13 | 14 | /* 15 | Better example can be found here: https://community.atlassian.com/t5/Jira-questions/Delete-Projects-using-Groovy-from-console/qaq-p/225337 16 | Basically you need to validate first 17 | 18 | */ 19 | 20 | -------------------------------------------------------------------------------- /jira_server/divideNumbersWSJF.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.issue.IssueManager 2 | import com.atlassian.jira.issue.MutableIssue 3 | import com.atlassian.jira.component.ComponentAccessor 4 | import com.atlassian.jira.issue.util.DefaultIssueChangeHolder 5 | import com.atlassian.jira.issue.ModifiedValue 6 | 7 | IssueManager im = ComponentAccessor.getIssueManager() 8 | MutableIssue issue = im.getIssueObject("ANDROID-48") 9 | 10 | if(issue){ 11 | 12 | //get the value 13 | def customFieldManager = ComponentAccessor.getCustomFieldManager() 14 | 15 | //CoD 16 | def codField = customFieldManager.getCustomFieldObjectsByName("Cost of Delay") 17 | def codFieldValue = issue.getCustomFieldValue(codField[0]) as Double 18 | 19 | //Job Size 20 | def jsField = customFieldManager.getCustomFieldObjectsByName("Job Size") 21 | def jsFieldValue = issue.getCustomFieldValue(jsField[0]) as Double 22 | 23 | if (codFieldValue == null || jsFieldValue == null) { 24 | 25 | return "Invalid/Cannot be null" 26 | } 27 | return codFieldValue / jsFieldValue 28 | 29 | 30 | 31 | 32 | 33 | }else { 34 | 35 | return "Issue doesn't exist" 36 | } 37 | -------------------------------------------------------------------------------- /jira_server/dynamicFormsUserPicker.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.user.ApplicationUser 2 | import com.onresolve.scriptrunner.parameters.annotation.UserPicker 3 | 4 | @UserPicker(label = "Select user", description = "Add user to group") 5 | ApplicationUser user 6 | -------------------------------------------------------------------------------- /jira_server/getAllChangeItems.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.issue.IssueManager; 2 | import com.atlassian.jira.issue.MutableIssue; 3 | import com.atlassian.jira.issue.link.IssueLink 4 | 5 | import com.atlassian.jira.component.ComponentAccessor; 6 | 7 | IssueManager im = ComponentAccessor.getIssueManager(); 8 | MutableIssue issue = im.getIssueObject("ANDROID-38"); 9 | 10 | 11 | def changeHistoryManager = ComponentAccessor.getChangeHistoryManager() 12 | def created 13 | def fromvalue 14 | def tovalue 15 | def changefield 16 | def output = "" 17 | 18 | for (change in changeHistoryManager.getAllChangeItems(issue) ) { 19 | 20 | 21 | created = change.created.toString() 22 | fromvalue = change.getFrom().toString() 23 | tovalue = change.getTo().toString() 24 | changefield = change.field 25 | 26 | output = output + created + ': ' + changefield + ': ' + fromvalue + ' -> ' + tovalue + '
' 27 | 28 | } 29 | 30 | 31 | 32 | return output 33 | -------------------------------------------------------------------------------- /jira_server/getAllIssueTypeSchemes.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.issue.fields.config.manager.IssueTypeSchemeManager 3 | import com.atlassian.jira.project.Project 4 | 5 | IssueTypeSchemeManager issueTypeSchemeManager = ComponentAccessor.getIssueTypeSchemeManager() 6 | issueTypeSchemeManager.getAllSchemes()*.name 7 | 8 | Project project = ComponentAccessor.getProjectManager().getProjectByCurrentKey("SCRUM") 9 | //Get issue types of a project 10 | //issueTypeSchemeManager.getIssueTypesForProject(project)*.name 11 | -------------------------------------------------------------------------------- /jira_server/getAllProjectPermissions.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import org.apache.log4j.Level 3 | import org.apache.log4j.Logger 4 | 5 | def log = Logger.getLogger("in.ravisagar.sr4j") 6 | log.setLevel(Level.DEBUG) 7 | 8 | def permissionManager = ComponentAccessor.permissionManager 9 | 10 | permissionManager.getAllProjectPermissions().each { 11 | 12 | log.debug(it.getNameI18nKey()+","+it.getKey()) 13 | 14 | } 15 | -------------------------------------------------------------------------------- /jira_server/getArchivedProjects.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import org.apache.log4j.Level 3 | import org.apache.log4j.Logger 4 | 5 | 6 | def log = Logger.getLogger("in.ravisagar.sr4j") 7 | log.setLevel(Level.DEBUG) 8 | 9 | def projectManager = ComponentAccessor.getProjectManager() 10 | 11 | projectManager.getArchivedProjects().each { it -> 12 | 13 | log.debug(it.key) 14 | log.debug(it.getIssueTypes()*.name) 15 | log.debug(it.getComponents()*.name) 16 | log.debug(it.getProjectLead()*.username) 17 | log.debug(it.getVersions()*.name) 18 | log.debug(it.getProjectCategoryObject()?.name) 19 | log.debug(it.archived) 20 | 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /jira_server/getAssignToFirstAssignee.groovy: -------------------------------------------------------------------------------- 1 | /** 2 | * Code to retrieve the first assignee of the issue from the 3 | * Change History and assignee the issue back to that user 4 | **/ 5 | import com.atlassian.jira.issue.IssueManager 6 | import com.atlassian.jira.issue.MutableIssue 7 | import com.atlassian.jira.issue.link.IssueLink 8 | import com.atlassian.jira.component.ComponentAccessor 9 | import com.atlassian.jira.user.ApplicationUser 10 | 11 | 12 | IssueManager im = ComponentAccessor.getIssueManager(); 13 | MutableIssue issue = im.getIssueObject("ANDROID-41"); 14 | 15 | 16 | def changeHistoryManager = ComponentAccessor.getChangeHistoryManager() 17 | def firstAssignee=changeHistoryManager.getChangeItemsForField(issue, "assignee") 18 | def firstNotNullAssignee = firstAssignee[0]?.from ?: firstAssignee[0]?.to 19 | 20 | //return firstNotNullAssignee 21 | 22 | ApplicationUser user = ComponentAccessor.getUserManager().getUserByName(firstNotNullAssignee.toString()) 23 | 24 | def issueService = ComponentAccessor.getIssueService() 25 | 26 | def validateAssignResult = issueService.validateAssign(user, issue.id, issue.reporterId) 27 | issueService.assign(user, validateAssignResult) -------------------------------------------------------------------------------- /jira_server/getChangeItemsForField.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.issue.IssueManager 2 | import com.atlassian.jira.issue.MutableIssue 3 | import com.atlassian.jira.issue.link.IssueLink 4 | import com.atlassian.jira.component.ComponentAccessor 5 | import com.atlassian.jira.user.ApplicationUser 6 | import org.apache.log4j.Logger 7 | import org.apache.log4j.Level 8 | 9 | def log = Logger.getLogger("com.sagar.ravi") 10 | log.setLevel(Level.DEBUG) 11 | 12 | log.debug("Script started") 13 | 14 | IssueManager im = ComponentAccessor.getIssueManager(); 15 | MutableIssue issue = im.getIssueObject("ANDROID-38"); 16 | 17 | def changeHistoryManager = ComponentAccessor.getChangeHistoryManager() 18 | changeHistoryManager.getAllChangeItems(issue).each { it -> 19 | 20 | 21 | log.debug(it.created.toString() + ": " + it.field + " - " + it.from + "->" + it.to) 22 | 23 | } 24 | 25 | log.debug("ONLY PRIORITY") 26 | 27 | changeHistoryManager.getChangeItemsForField(issue, "priority").each { it -> 28 | 29 | log.debug(it.created.toString() + ": " + it.field + " - " + it.fromString + "->" + it.toString) 30 | 31 | } 32 | 33 | 34 | -------------------------------------------------------------------------------- /jira_server/getCustomFieldObjectByName.groovy: -------------------------------------------------------------------------------- 1 | /** 2 | * Fetch the value of a custom field by its name 3 | * 4 | **/ 5 | 6 | import com.atlassian.jira.issue.IssueManager 7 | import com.atlassian.jira.issue.MutableIssue 8 | import com.atlassian.jira.component.ComponentAccessor 9 | import com.atlassian.jira.issue.util.DefaultIssueChangeHolder 10 | import com.atlassian.jira.issue.ModifiedValue 11 | 12 | IssueManager im = ComponentAccessor.getIssueManager() 13 | MutableIssue issue = im.getIssueObject("ANDROID-126") 14 | 15 | if(issue){ 16 | 17 | //get the value 18 | def customFieldManager = ComponentAccessor.getCustomFieldManager() 19 | //def cField = customFieldManager.getCustomFieldObject("customfield_10500") 20 | 21 | def cField = customFieldManager.getCustomFieldObjectByName("Text Field") 22 | def cFieldValue = issue.getCustomFieldValue(cField) 23 | 24 | 25 | 26 | 27 | }else { 28 | 29 | return "Issue doesn't exist" 30 | } -------------------------------------------------------------------------------- /jira_server/getCustomFieldObjectsByName_userPicker.groovy: -------------------------------------------------------------------------------- 1 | 2 | import com.atlassian.jira.component.ComponentAccessor 3 | import com.atlassian.jira.issue.CustomFieldManager 4 | import com.atlassian.jira.issue.IssueManager 5 | import com.atlassian.jira.issue.MutableIssue 6 | import com.atlassian.jira.user.ApplicationUser 7 | import org.apache.log4j.Level 8 | import org.apache.log4j.Logger 9 | 10 | IssueManager im = ComponentAccessor.getIssueManager() 11 | MutableIssue issue = im.getIssueObject("SCRUM-25") 12 | 13 | def log = Logger.getLogger("com.debug.url") 14 | log.setLevel(Level.DEBUG) 15 | 16 | CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager() 17 | def customField = customFieldManager.getCustomFieldObjectsByName("User Picker (multiple users)") 18 | 19 | log.debug(customField[0].getCustomFieldType()) 20 | 21 | if(customField) { 22 | 23 | def customFieldValue = issue.getCustomFieldValue(customField[0]) as ArrayList 24 | return customFieldValue*.getKey() 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /jira_server/getFieldsOnScreens.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import org.apache.log4j.Level 3 | import org.apache.log4j.Logger 4 | 5 | def log = Logger.getLogger("in.ravisagar.sr4j") 6 | log.setLevel(Level.DEBUG) 7 | 8 | ComponentAccessor.getFieldScreenManager().getFieldScreens().each { it -> 9 | 10 | log.debug("Screen: " + it.name) 11 | log.debug("Tabs: " + it.getTabs()*.name) 12 | 13 | it.getTab(0).getFieldScreenLayoutItems().each { fsli -> 14 | 15 | log.debug(fsli.fieldId) 16 | 17 | 18 | } 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /jira_server/getIssue.groovy: -------------------------------------------------------------------------------- 1 | /** 2 | * Code to fetch issue details 3 | * Video: https://youtu.be/3570FeZ7pqQ 4 | * Tested on version: Jira: 8.22.2, ScriptRunner for Jira: 6.50.0 5 | */ 6 | import com.atlassian.jira.component.ComponentAccessor 7 | import com.atlassian.jira.issue.IssueManager 8 | 9 | IssueManager im = ComponentAccessor.getIssueManager() 10 | def issue = im.getIssueObject("DEMO-1") 11 | 12 | return "Summary: ${issue.summary}, Assignee: ${issue.assignee?.displayName}, Status: $issue.status.name" 13 | -------------------------------------------------------------------------------- /jira_server/getIssueLinks.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.issue.IssueManager; 2 | import com.atlassian.jira.issue.MutableIssue; 3 | import com.atlassian.jira.component.ComponentAccessor; 4 | import com.atlassian.jira.issue.link.IssueLink; 5 | 6 | 7 | IssueManager im = ComponentAccessor.getIssueManager(); 8 | MutableIssue issue = im.getIssueObject("ANDROID-124"); 9 | 10 | 11 | //def links = ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getId()) 12 | def links = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId()) 13 | 14 | //return links[0].getSourceObject() 15 | 16 | def output = "" 17 | for( l in links) { 18 | 19 | //use this for Outward links 20 | output = output + l.issueLinkType.name + ": " + l.getDestinationObject() + "
" 21 | 22 | //use this for Inward links 23 | // output = output + l.issueLinkType.name + ": " + l.getSourceObject() + "
" 24 | 25 | } 26 | 27 | return output 28 | 29 | 30 | -------------------------------------------------------------------------------- /jira_server/getMultiSelectCustomFieldValue.groovy: -------------------------------------------------------------------------------- 1 | /** 2 | * Fetch the value of a custom field by its name 3 | * 4 | **/ 5 | 6 | import com.atlassian.jira.issue.IssueManager 7 | import com.atlassian.jira.issue.MutableIssue 8 | import com.atlassian.jira.component.ComponentAccessor 9 | 10 | IssueManager im = ComponentAccessor.getIssueManager() 11 | MutableIssue issue = im.getIssueObject("SCRUM-38") 12 | 13 | if(issue){ 14 | 15 | //get the value 16 | def customFieldManager = ComponentAccessor.getCustomFieldManager() 17 | 18 | def cField = customFieldManager.getCustomFieldObjectsByName("Countries") 19 | 20 | def cFieldValue = cField ? issue.getCustomFieldValue(cField[0]) : "Field doesn't exist" 21 | 22 | log.debug "Custom field value type or class" + cFieldValue.getClass() 23 | 24 | cFieldValue.eachWithIndex { it, i -> 25 | log.debug "Country name $i: $it" 26 | } 27 | 28 | return cFieldValue 29 | 30 | 31 | }else { 32 | 33 | return "Issue doesn't exist" 34 | } 35 | -------------------------------------------------------------------------------- /jira_server/getPrioritySchemes.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.config.PriorityManager 3 | import com.atlassian.jira.issue.fields.config.manager.PrioritySchemeManager 4 | import org.apache.log4j.Level 5 | import org.apache.log4j.Logger 6 | 7 | 8 | def log = Logger.getLogger("in.ravisagar.sr4j") 9 | log.setLevel(Level.DEBUG) 10 | 11 | ComponentAccessor.getComponent(PrioritySchemeManager).getAllSchemes() 12 | 13 | ComponentAccessor.getComponent(PriorityManager).getPriorities()*.name 14 | -------------------------------------------------------------------------------- /jira_server/getProjects.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import org.apache.log4j.Level 3 | import org.apache.log4j.Logger 4 | 5 | 6 | def log = Logger.getLogger("in.ravisagar.sr4j") 7 | log.setLevel(Level.DEBUG) 8 | 9 | def projectManager = ComponentAccessor.getProjectManager() 10 | 11 | projectManager.getProjects().each { it -> 12 | 13 | log.debug(it.key) 14 | log.debug(it.getIssueTypes()*.name) 15 | log.debug(it.getComponents()*.name) 16 | log.debug(it.getProjectLead()*.username) 17 | log.debug(it.getVersions()*.name) 18 | log.debug(it.getProjectCategoryObject()?.name) 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /jira_server/getProjectsFromIssueTypes.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.issue.fields.config.manager.IssueTypeSchemeManager 3 | import org.apache.log4j.Level 4 | import org.apache.log4j.Logger 5 | 6 | 7 | def log = Logger.getLogger("in.ravisagar.sr4j") 8 | log.setLevel(Level.DEBUG) 9 | 10 | IssueTypeSchemeManager issueTypeSchemeManager = ComponentAccessor.getIssueTypeSchemeManager() 11 | def issueTypeToSearch = "Bug" 12 | def projectList = [] 13 | issueTypeSchemeManager.getAllSchemes()*.getAssociatedProjectObjects().each { projects -> 14 | 15 | projects.each { project -> 16 | 17 | if (project.getIssueTypes()*.name.contains(issueTypeToSearch)) { 18 | projectList.add(project.name) 19 | 20 | } 21 | } 22 | } 23 | 24 | log.debug(projectList) 25 | -------------------------------------------------------------------------------- /jira_server/getProjectsFromProjectCategory: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import org.apache.log4j.Level 3 | import org.apache.log4j.Logger 4 | 5 | 6 | def log = Logger.getLogger("in.ravisagar.sr4j") 7 | log.setLevel(Level.DEBUG) 8 | 9 | def projectManager = ComponentAccessor.getProjectManager() 10 | 11 | projectCategory = projectManager.getProjectCategoryObjectByName("Internal") 12 | 13 | log.debug("Project Category: " + projectCategory.name) 14 | 15 | def projects = projectManager.getProjectsFromProjectCategory(projectCategory) 16 | 17 | 18 | projects.each { it -> 19 | 20 | log.debug(it.getComponents()*.name) 21 | log.debug(it.getProjectLead()*.username) 22 | log.debug(it.getVersions()*.name) 23 | log.debug(it.getProjectCategoryObject()?.name) 24 | 25 | } 26 | 27 | -------------------------------------------------------------------------------- /jira_server/getSprintState.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.issue.IssueManager 3 | import com.atlassian.jira.issue.CustomFieldManager 4 | 5 | IssueManager im = ComponentAccessor.getIssueManager() 6 | 7 | def issue = im.getIssueObject("KEY-1234") 8 | 9 | CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager() 10 | def customField = customFieldManager.getCustomFieldObjectsByName("Sprint").first() 11 | 12 | def sprint = issue.getCustomFieldValue(customField) 13 | 14 | sprint.findAll{it.active == true || it.future == true}.last().id 15 | //sprint.findAll{it.closed == true}.last() 16 | 17 | //Active Sprint 18 | sprint.findAll { it.active == true }*.name 19 | 20 | //Closed Sprint 21 | sprint.findAll { it.closed == true }*.name 22 | 23 | //Future Sprint 24 | sprint.findAll { it.future == true }*.name -------------------------------------------------------------------------------- /jira_server/getSubTaskObjects.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.issue.IssueManager; 2 | import com.atlassian.jira.issue.MutableIssue; 3 | import com.atlassian.jira.component.ComponentAccessor; 4 | 5 | IssueManager im = ComponentAccessor.getIssueManager(); 6 | MutableIssue issue = im.getIssueObject("ANDROID-38"); 7 | 8 | def output = "" 9 | for (e in issue.getSubTaskObjects()) { 10 | 11 | output = output + e.getSummary() +"," + e.issueType.name + "
" 12 | 13 | } 14 | 15 | return output 16 | 17 | //MutableIssue sub_issue = im.getIssueObject(issue.getSubTaskObjects()[1].toString()); 18 | 19 | //return sub_issue -------------------------------------------------------------------------------- /jira_server/getTimeSpent.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.issue.IssueManager 3 | import org.apache.log4j.Level 4 | import org.apache.log4j.Logger 5 | 6 | 7 | def log = Logger.getLogger("pro.ravi.sr4j") 8 | log.setLevel(Level.DEBUG) 9 | 10 | IssueManager im = ComponentAccessor.getIssueManager() 11 | def issue = im.getIssueObject("SCRUM-14") 12 | 13 | log.debug(issue.getTimeSpent()) 14 | 15 | log.debug(issue.getOriginalEstimate()) 16 | -------------------------------------------------------------------------------- /jira_server/getVoters.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.issue.Issue 3 | import com.atlassian.jira.issue.IssueManager 4 | import com.atlassian.jira.issue.MutableIssue 5 | import org.apache.log4j.Level 6 | import org.apache.log4j.Logger 7 | 8 | 9 | //def log = Logger.getLogger("in.ravisagar.sr4j") 10 | log.setLevel(Level.DEBUG) 11 | 12 | IssueManager im = ComponentAccessor.getIssueManager() 13 | def issue = im.getIssueObject("SCRUM-14") 14 | 15 | def voteCount = ComponentAccessor.getVoteManager().getVoteCount(issue) 16 | log.debug("Voter count: " + voteCount) 17 | 18 | def english = new Locale("en") 19 | def voters = ComponentAccessor.getVoteManager().getVotersFor(issue, english) 20 | log.debug("Voters: " + voters*.name) 21 | -------------------------------------------------------------------------------- /jira_server/getWatchers.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.issue.Issue 3 | import com.atlassian.jira.issue.IssueManager 4 | import com.atlassian.jira.issue.MutableIssue 5 | import org.apache.log4j.Level 6 | import org.apache.log4j.Logger 7 | 8 | 9 | //def log = Logger.getLogger("in.ravisagar.sr4j") 10 | log.setLevel(Level.DEBUG) 11 | 12 | IssueManager im = ComponentAccessor.getIssueManager() 13 | def issue = im.getIssueObject("SCRUM-14") 14 | 15 | Locale english = new Locale("en") 16 | 17 | def watcherCount = ComponentAccessor.watcherManager.getWatcherCount(issue) 18 | log.debug("Watcher Count: " + watcherCount) 19 | 20 | def watchers = ComponentAccessor.watcherManager.getWatchers(issue, english) 21 | 22 | log.debug("Watchers: " + watchers*.name) 23 | -------------------------------------------------------------------------------- /jira_server/getWorkflowDetails.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.workflow.WorkflowManager 3 | import org.apache.log4j.Level 4 | import org.apache.log4j.Logger 5 | 6 | 7 | def log = Logger.getLogger("in.ravisagar.sr4j") 8 | log.setLevel(Level.DEBUG) 9 | WorkflowManager workflowManager = ComponentAccessor.getWorkflowManager() 10 | 11 | workflowManager.getWorkflows().each { it -> 12 | 13 | log.debug("Workflow: " + it.name) 14 | log.debug(it.getLinkedStatusObjects()*.name) 15 | 16 | it.getLinkedStatusObjects().each { status -> 17 | log.debug(it.getLinkedStep(status).name) 18 | log.debug(it.getLinkedStep(status).getActions().id) 19 | 20 | } 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /jira_server/getWorkflows.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.issue.IssueManager 3 | import com.atlassian.jira.issue.MutableIssue 4 | import com.atlassian.jira.workflow.WorkflowManager 5 | 6 | WorkflowManager workflowManager = ComponentAccessor.getWorkflowManager() 7 | 8 | //Get all workflows 9 | // workflowManager.getWorkflows() 10 | 11 | IssueManager im = ComponentAccessor.getIssueManager() 12 | MutableIssue issue = im.getIssueObject("PM-1") 13 | workflowManager.getWorkflow(issue).name 14 | -------------------------------------------------------------------------------- /jira_server/hasAssociatedIssues.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.config.IssueTypeManager 3 | import org.apache.log4j.Level 4 | import org.apache.log4j.Logger 5 | 6 | def log = Logger.getLogger("in.ravisagar.sr4j") 7 | log.setLevel(Level.DEBUG) 8 | 9 | IssueTypeManager issueTypeManager = ComponentAccessor.getComponent(IssueTypeManager) 10 | issueTypeManager.getIssueTypes().each { 11 | 12 | log.debug(it.name + ": " + issueTypeManager.hasAssociatedIssues(it) ) 13 | 14 | } 15 | 16 | -------------------------------------------------------------------------------- /jira_server/hideCustomFieldBasedOnCascadeSelectList.groovy: -------------------------------------------------------------------------------- 1 | /** 2 | Hide a field based on the values selected in either parent or child of a cascading select list. 3 | 4 | Cascading field: Country and city 5 | Parent options: India, UK 6 | Child options: [Delhi, Mumbai], [Londo, Reading, Slough] 7 | 8 | **/ 9 | import com.atlassian.jira.component.ComponentAccessor 10 | import com.atlassian.jira.issue.customfields.manager.OptionsManager 11 | import com.atlassian.jira.issue.fields.config.FieldConfig 12 | import com.atlassian.jira.issue.customfields.option.Options 13 | 14 | log.debug("******Logging starts********") 15 | 16 | def parentField = getFieldById("customfield_10401") 17 | def parentFieldValue = parentField.getValue() 18 | def childField = getFieldById("customfield_10401:1") 19 | def childFieldValue = childField.getValue() 20 | def fieldToHide = getFieldByName("Review Date") 21 | 22 | log.debug("Parent Field: " + parentFieldValue) 23 | log.debug("Child Field: " + childFieldValue) 24 | 25 | /**Hide based on parent field **/ 26 | if( parentFieldValue.containsAll("UK")) { 27 | 28 | fieldToHide.setHidden(true) 29 | 30 | } else { 31 | 32 | fieldToHide.setHidden(false) 33 | } 34 | 35 | /**Hide based on child field **/ 36 | 37 | /* 38 | if( childFieldValue == "Delhi") { 39 | 40 | fieldToHide.setHidden(true) 41 | 42 | } else { 43 | 44 | fieldToHide.setHidden(false) 45 | } 46 | */ 47 | -------------------------------------------------------------------------------- /jira_server/jqlParser.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.bc.issue.search.SearchService 2 | import com.atlassian.jira.component.ComponentAccessor 3 | import com.atlassian.jira.web.bean.PagerFilter 4 | 5 | def searchService = ComponentAccessor.getComponentOfType(SearchService) 6 | def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser 7 | 8 | def filter = "issuetype = Story" 9 | SearchService.ParseResult parseResult = searchService.parseQuery(user, filter) 10 | def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter) 11 | 12 | //def issues = results.issues //Jira 7 13 | def issues = results.results //Jira 8 14 | log.debug("Total issues: " + issues.size()) 15 | issues.each { it -> 16 | 17 | log.debug(it.key + ", " + it.issueType.name) 18 | 19 | } 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /jira_server/lastComment.groovy: -------------------------------------------------------------------------------- 1 | 2 | import com.atlassian.jira.issue.comments.CommentManager 3 | import com.atlassian.jira.issue.IssueManager 4 | import com.atlassian.jira.issue.MutableIssue 5 | import com.atlassian.jira.component.ComponentAccessor 6 | 7 | 8 | CommentManager commentMgr = ComponentAccessor.getCommentManager() 9 | //IssueManager im = ComponentAccessor.getIssueManager() 10 | //MutableIssue issue = im.getIssueObject("SCRUM-318") 11 | 12 | 13 | issue ? commentMgr.getComments(issue).last().getBody(): log.debug("Issue doesn't exist") 14 | -------------------------------------------------------------------------------- /jira_server/listAllProjects.groovy: -------------------------------------------------------------------------------- 1 | package com.example 2 | import com.atlassian.jira.component.ComponentAccessor 3 | 4 | def projectManager = ComponentAccessor.getProjectManager() 5 | def i 6 | def output="" 7 | 8 | 9 | for (i=0; i" 13 | 14 | } 15 | 16 | //Write to a file 17 | File file = new File("/Users/spareuser/IdeaProjects/scriptrunner-samples/jira/target/jira/home/export/file.csv") 18 | file.write(output) 19 | 20 | //Display on the console 21 | return output 22 | -------------------------------------------------------------------------------- /jira_server/numberOfComments.groovy: -------------------------------------------------------------------------------- 1 | 2 | import com.atlassian.jira.issue.comments.CommentManager 3 | import com.atlassian.jira.issue.IssueManager 4 | import com.atlassian.jira.issue.MutableIssue 5 | import com.atlassian.jira.component.ComponentAccessor 6 | 7 | 8 | CommentManager commentMgr = ComponentAccessor.getCommentManager() 9 | //IssueManager im = ComponentAccessor.getIssueManager() 10 | //MutableIssue issue = im.getIssueObject("SCRUM-318") 11 | 12 | 13 | def numberOfComments = issue ? commentMgr.getComments(issue).size(): log.debug("Issue doesn't exist") 14 | 15 | -------------------------------------------------------------------------------- /jira_server/postFunctionConditionSelectListMultipleChoice.groovy: -------------------------------------------------------------------------------- 1 | /** 2 | * Post function condition to allow creation of sub task based on value 3 | * selected in Select List multiple choice 4 | * Video link: 5 | **/ 6 | def values = cfValues['My Multi List']*.value 7 | if ( values.contains('Option 1') || values.contains('Option 2') || values.contains('Option 3')) { 8 | return true 9 | } else { 10 | return false 11 | } -------------------------------------------------------------------------------- /jira_server/postFunctionModifySummaryOnCreate.groovy: -------------------------------------------------------------------------------- 1 | /** 2 | * Post function to Append value from a user picker field to summary on issue creation. 3 | * Video link: 4 | */ 5 | import com.atlassian.jira.component.ComponentAccessor 6 | import com.atlassian.jira.user.ApplicationUser 7 | 8 | def customFieldManager = ComponentAccessor.getCustomFieldManager() 9 | def cField = customFieldManager.getCustomFieldObjectsByName("User Picker Field") 10 | def cFieldValue = issue.getCustomFieldValue(cField[0]) as ApplicationUser 11 | 12 | if (cFieldValue) { 13 | issue.summary = issue.summary + ": " + cFieldValue.displayName } 14 | -------------------------------------------------------------------------------- /jira_server/postFunctionUpdateMessageField.groovy: -------------------------------------------------------------------------------- 1 | /** 2 | * Post function to update the message field (Text field read only) with a custom message 3 | * This field can only be updated while importing or using code 4 | * Refer to this link: https://community.atlassian.com/t5/Jira-Core-questions/Text-Field-read-only/qaq-p/182646 5 | * Video link: https://youtu.be/gJfoB2q4LIk 6 | 7 | **/ 8 | 9 | import com.atlassian.jira.component.ComponentAccessor 10 | import com.atlassian.jira.issue.util.DefaultIssueChangeHolder 11 | import com.atlassian.jira.issue.ModifiedValue 12 | 13 | 14 | //get the value of Message Field 15 | def customFieldManager = ComponentAccessor.getCustomFieldManager() 16 | def cField = customFieldManager.getCustomFieldObjectsByName("Message Field") 17 | def cFieldValue = issue.getCustomFieldValue(cField[0]) 18 | 19 | //Prepare the message with system field or custom message 20 | def message = "*Issue status*: " + 21 | issue.getStatus().name + "\n" + 22 | "*This is a message that can be displayed*" 23 | 24 | //update the value 25 | def changeHolder = new DefaultIssueChangeHolder() 26 | cField[0].updateValue(null, issue, new ModifiedValue(cFieldValue, message),changeHolder) 27 | -------------------------------------------------------------------------------- /jira_server/postFunctionUpdateProjectRoleFromUserPickerField.groovy: -------------------------------------------------------------------------------- 1 | /** 2 | * Post function to update project role from user picker field 3 | * Video link: https://youtu.be/0--2IoU6KCA 4 | **/ 5 | //import com.atlassian.jira.issue.IssueManager 6 | //import com.atlassian.jira.issue.MutableIssue 7 | import com.atlassian.jira.component.ComponentAccessor 8 | import com.atlassian.jira.issue.util.DefaultIssueChangeHolder 9 | import com.atlassian.jira.bc.user.UserService 10 | import com.atlassian.jira.bc.projectroles.ProjectRoleService 11 | import com.atlassian.jira.component.ComponentAccessor 12 | import com.atlassian.jira.security.roles.ProjectRoleActor 13 | import com.atlassian.jira.security.roles.ProjectRoleManager 14 | import com.atlassian.jira.util.SimpleErrorCollection 15 | import com.atlassian.jira.user.ApplicationUser 16 | 17 | 18 | //IssueManager im = ComponentAccessor.getIssueManager() 19 | //MutableIssue issue = im.getIssueObject("ANDROID-34") 20 | 21 | //get the value of Project Manager 22 | def customFieldManager = ComponentAccessor.getCustomFieldManager() 23 | def projectManagerField = customFieldManager.getCustomFieldObjectsByName("Project Manager") 24 | def projectManagerValue = issue.getCustomFieldValue(projectManagerField[0]) as ApplicationUser 25 | 26 | //get the value of Project Manager 27 | def projectSponsorField = customFieldManager.getCustomFieldObjectsByName("Project Sponsor") 28 | def projectSponsorValue = issue.getCustomFieldValue(projectSponsorField[0]) as ApplicationUser 29 | 30 | def pmActors = [] 31 | def psActors = [] 32 | pmActors.add(projectManagerValue.getUsername().toString()) 33 | psActors.add(projectSponsorValue.getUsername().toString()) 34 | 35 | def projectManager = ComponentAccessor.getProjectManager() 36 | def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService) 37 | def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager) 38 | def errorCollection = new SimpleErrorCollection() 39 | //def project = projectManager.getProjectObjByKey("ANDROID") 40 | def project = issue.getProjectObject() 41 | 42 | def projectManagerRole = projectRoleManager.getProjectRole("Project Managers") 43 | def projectSponsorsRole = projectRoleManager.getProjectRole("Project Sponsors") 44 | 45 | //Add to Project Managers role 46 | projectRoleService.addActorsToProjectRole(pmActors, 47 | projectManagerRole, 48 | project, 49 | ProjectRoleActor.USER_ROLE_ACTOR_TYPE, 50 | errorCollection) 51 | //Add to Project Sponsors role 52 | projectRoleService.addActorsToProjectRole(psActors, 53 | projectSponsorsRole, 54 | project, 55 | ProjectRoleActor.USER_ROLE_ACTOR_TYPE, 56 | errorCollection) 57 | -------------------------------------------------------------------------------- /jira_server/postFunction_addWatcher.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.issue.IssueManager 3 | import com.atlassian.jira.issue.watchers.WatcherManager 4 | 5 | 6 | IssueManager im = ComponentAccessor.getIssueManager() 7 | WatcherManager watcherManager = ComponentAccessor.getWatcherManager() 8 | 9 | def issue = im.getIssueObject("SSP-23") 10 | def watcherUser = ComponentAccessor.userManager.getUserByName("admin") 11 | 12 | watcherManager.startWatching(watcherUser, issue) 13 | watcherManager.stopWatching(watcherUser,issue) 14 | 15 | 16 | -------------------------------------------------------------------------------- /jira_server/projectRename.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.project.UpdateProjectParameters 3 | import org.apache.log4j.Level 4 | import org.apache.log4j.Logger 5 | 6 | 7 | def log = Logger.getLogger("in.ravisagar.sr4j") 8 | log.setLevel(Level.DEBUG) 9 | 10 | def projectManager = ComponentAccessor.getProjectManager() 11 | def project = projectManager.getProjectObjByName("Old project") 12 | 13 | if(project) { 14 | def updateProjectParameters = UpdateProjectParameters.forProject(project.id).name("New project") 15 | projectManager.updateProject(updateProjectParameters) 16 | log.debug("Project renamed") 17 | } 18 | else { 19 | log.debug("Project doesn't exist") 20 | } 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /jira_server/readCSVFile.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | CSV Format 3 | userName,userEmail,userName 4 | user1,user1@example.com,"User 1 Name" 5 | user2,user2@example.com, "User 2 Name" 6 | user3,user3@example.com, "User 3 Name" 7 | 8 | CSV parsing code copied from: https://stackoverflow.com/questions/49675423/read-csv-file-and-put-result-in-a-map-using-groovy-without-using-any-external-l 9 | */ 10 | 11 | 12 | File file = new File("/opt/jira/home/scripts/file.csv") 13 | 14 | def csvMapList = [] 15 | 16 | file.eachLine { line -> 17 | def columns = line.split(",") 18 | def tmpMap = [:] 19 | 20 | tmpMap.putAt("userName", columns[0]) 21 | tmpMap.putAt("userEmail", columns[1]) 22 | tmpMap.putAt("userFullName", columns[2]) 23 | 24 | csvMapList.add(tmpMap) 25 | } 26 | 27 | // csvMapList.getAt("userName") //return all usernames 28 | // return csvMapList.getAt("userEmail") //return all emails 29 | //csvMapList.getAt("userFullName") //return all usernames 30 | 31 | return csvMapList 32 | 33 | -------------------------------------------------------------------------------- /jira_server/removeAllChangeItems.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.issue.IssueManager; 2 | import com.atlassian.jira.issue.MutableIssue; 3 | import com.atlassian.jira.issue.link.IssueLink 4 | 5 | import com.atlassian.jira.component.ComponentAccessor; 6 | 7 | IssueManager im = ComponentAccessor.getIssueManager(); 8 | MutableIssue issue = im.getIssueObject("ANDROID-126"); 9 | 10 | 11 | def changeHistoryManager = ComponentAccessor.getChangeHistoryManager() 12 | 13 | changeHistoryManager.removeAllChangeItems(issue) -------------------------------------------------------------------------------- /jira_server/removeUser.groovy: -------------------------------------------------------------------------------- 1 | 2 | import com.atlassian.jira.bc.user.UserService 3 | import com.atlassian.jira.component.ComponentAccessor 4 | 5 | def userService = ComponentAccessor.getComponent(UserService) 6 | def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser 7 | 8 | UserService.DeleteUserValidationResult result = userService.validateDeleteUser(currentUser, "michelle.williams") 9 | 10 | //return result.isValid() 11 | 12 | //return result.errorCollection 13 | 14 | //userService.removeUser(currentUser, result) 15 | 16 | 17 | -------------------------------------------------------------------------------- /jira_server/removeUserFromGroup.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | 3 | def userName = "user2" 4 | def groupNametoRemove = "jira-software-userss" 5 | def userToRemove = ComponentAccessor.getUserManager().getUserByName(userName) 6 | def groupName = ComponentAccessor.getGroupManager().getGroup(groupNametoRemove) 7 | 8 | if (!userToRemove) { 9 | log.error("User doesn't exist") 10 | return 11 | } 12 | 13 | if (!ComponentAccessor.getGroupManager().getGroupsForUser(userName).contains(groupName)) { 14 | log.error("User: $userToRemove.username not in the group:" + groupName?.name) 15 | return 16 | } 17 | 18 | if(!groupName) { 19 | log.error("Group doesn't exist") 20 | } else { 21 | 22 | ComponentAccessor.userUtil.removeUserFromGroup(groupName, userToRemove) 23 | log.error("User: $userToRemove.username removed from the group: $groupName.name") 24 | } 25 | 26 | -------------------------------------------------------------------------------- /jira_server/runSQL.groovy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sparxsys/ScriptRunner-Scripts/d20d640d461f66ef2ea2a5df6557792527c071e0/jira_server/runSQL.groovy -------------------------------------------------------------------------------- /jira_server/runSelectQueryDatabase.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import groovy.sql.Sql 3 | import org.ofbiz.core.entity.ConnectionFactory 4 | import org.ofbiz.core.entity.DelegatorInterface 5 | import org.apache.log4j.Logger 6 | import org.apache.log4j.Level 7 | 8 | def log = Logger.getLogger("com.sagar.ravi") 9 | log.setLevel(Level.DEBUG) 10 | 11 | import java.sql.Connection 12 | 13 | def delegator = (DelegatorInterface) ComponentAccessor.getComponent(DelegatorInterface) 14 | String helperName = delegator.getGroupHelperName("default") 15 | 16 | //Enclose the SQL statement in triple quotes so it can span multiple lines for better visibility 17 | def sqlStmt = """ 18 | SELECT pname, pkey, lead, projecttype 19 | FROM project 20 | """ 21 | 22 | Connection conn = ConnectionFactory.getConnection(helperName) 23 | Sql sql = new Sql(conn) 24 | 25 | //Use try and finally blocks for database connections because if something is wrong with the database connection 26 | //the connection can atleast be closed. finally block always gets executed. 27 | 28 | try { 29 | StringBuffer sb = new StringBuffer() 30 | sql.eachRow(sqlStmt) { it -> 31 | 32 | sb.append( it.getAt("pname") ) 33 | sb.append(",") 34 | sb.append( it.getAt("pkey") ) 35 | sb.append(",") 36 | sb.append( it.getAt("lead") ) 37 | sb.append(",") 38 | sb.append( it.getAt("projecttype") ) 39 | sb.append("\n") 40 | 41 | 42 | 43 | 44 | } 45 | log.debug sb.toString() 46 | } 47 | finally { 48 | sql.close() 49 | } 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /jira_server/sendEmail.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.mail.Email 2 | import com.atlassian.mail.server.SMTPMailServer 3 | import com.atlassian.jira.component.ComponentAccessor 4 | 5 | def emailSubject = "Test Email Subject" 6 | def emailBody = "Test Email Body" 7 | def emailAddress = "ravisagar@gmail.com" 8 | 9 | 10 | SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer() 11 | 12 | 13 | if (mailServer) { 14 | Email email = new Email(emailAddress) 15 | email.setSubject(emailSubject) 16 | email.setBody(emailBody) 17 | mailServer.send(email) 18 | } else {} 19 | -------------------------------------------------------------------------------- /jira_server/setSecurityLevel.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.event.type.EventDispatchOption 3 | 4 | def issue = ComponentAccessor.getIssueManager().getIssueObject("ISD-2") 5 | issue.setSecurityLevelId(10000) 6 | 7 | def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() 8 | def issueManager = ComponentAccessor.getIssueManager() 9 | 10 | issueManager.updateIssue(user,issue,EventDispatchOption.DO_NOT_DISPATCH,true) 11 | -------------------------------------------------------------------------------- /jira_server/systemCommands.groovy: -------------------------------------------------------------------------------- 1 | def standardOutput = new StringBuilder() 2 | def standardError = new StringBuilder() 3 | def command = "mpc toggle" 4 | def process = command.execute() 5 | 6 | process.consumeProcessOutput(standardOutput, standardError) 7 | return standardOutput 8 | -------------------------------------------------------------------------------- /jira_server/transitionIssue.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.issue.IssueManager; 2 | import com.atlassian.jira.issue.MutableIssue; 3 | import com.atlassian.jira.component.ComponentAccessor; 4 | import com.atlassian.jira.user.ApplicationUser; 5 | 6 | //Workflow imports 7 | import com.atlassian.jira.issue.IssueInputParametersImpl 8 | import com.atlassian.jira.bc.issue.IssueService 9 | import com.atlassian.jira.workflow.JiraWorkflow 10 | import com.atlassian.jira.workflow.WorkflowManager 11 | 12 | 13 | ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().loggedInUser 14 | 15 | 16 | IssueManager im = ComponentAccessor.getIssueManager(); 17 | MutableIssue issue = im.getIssueObject("HT-36"); 18 | 19 | 20 | //Workflow 21 | WorkflowManager workflowManager = ComponentAccessor.getWorkflowManager() 22 | JiraWorkflow workflow = workflowManager.getWorkflow(issue) 23 | 24 | //return workflow.getLinkedStep(issue.getStatus()).getActions().id 25 | 26 | //[921, 851, 761, 891, 901, 871] [781, 761, 901, 911] 27 | 28 | 29 | 30 | def actionId = 911 31 | IssueService issueService = ComponentAccessor.getIssueService() 32 | 33 | def transitionValidationResult = issueService.validateTransition(currentUser, issue.id, actionId, new IssueInputParametersImpl()) 34 | 35 | //return transitionValidationResult.isValid() 36 | 37 | def transitionResult = issueService.transition(currentUser, transitionValidationResult) 38 | return transitionResult 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /jira_server/updateSingleSelectList.groovy: -------------------------------------------------------------------------------- 1 | /** 2 | Video: https://youtu.be/lkc7mpF0vZY 3 | ScriptRunner Consulting: www.sparxsys.com 4 | **/ 5 | 6 | import com.atlassian.jira.component.ComponentAccessor 7 | import com.atlassian.jira.issue.Issue 8 | import com.atlassian.jira.issue.MutableIssue 9 | import com.atlassian.jira.issue.ModifiedValue 10 | import com.atlassian.jira.issue.index.IssueIndexingService 11 | import com.atlassian.jira.issue.util.DefaultIssueChangeHolder 12 | import com.atlassian.jira.issue.IssueManager 13 | import com.atlassian.jira.issue.CustomFieldManager 14 | import com.atlassian.jira.user.ApplicationUser 15 | import com.atlassian.jira.util.ImportUtils 16 | import com.atlassian.jira.event.type.EventDispatchOption 17 | 18 | 19 | IssueManager im = ComponentAccessor.getIssueManager() 20 | 21 | def issue = im.getIssueObject("DEMO-24") 22 | 23 | updateSingleSelectList(issue, "Impact", "High") 24 | 25 | def updateSingleSelectList(MutableIssue issue, String fieldName, String optionToUpdate) { 26 | 27 | IssueManager im = ComponentAccessor.getIssueManager() 28 | //def issueToUpdate = im.getIssueObject(issueKey) 29 | CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager() 30 | def customField = customFieldManager.getCustomFieldObjectsByName(fieldName).first() 31 | def customFieldFieldConfig = customField.getRelevantConfig(issue) 32 | def newOptionToUpdate = 33 | ComponentAccessor.optionsManager.getOptions(customFieldFieldConfig)?.find { 34 | it.value == optionToUpdate 35 | } 36 | //Do not update history 37 | customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), newOptionToUpdate), new DefaultIssueChangeHolder()) 38 | 39 | //Update history 40 | //ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() 41 | //issue.setCustomFieldValue(customField, newOptionToUpdate) 42 | //ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false) 43 | 44 | } 45 | -------------------------------------------------------------------------------- /jira_server/updateValue.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.issue.IssueManager 2 | import com.atlassian.jira.issue.MutableIssue 3 | import com.atlassian.jira.component.ComponentAccessor 4 | import com.atlassian.jira.issue.util.DefaultIssueChangeHolder 5 | import com.atlassian.jira.issue.ModifiedValue 6 | 7 | IssueManager im = ComponentAccessor.getIssueManager() 8 | MutableIssue issue = im.getIssueObject("ANDROID-126") 9 | 10 | if(issue){ 11 | 12 | //get the value 13 | def customFieldManager = ComponentAccessor.getCustomFieldManager() 14 | def cField = customFieldManager.getCustomFieldObject("customfield_10500") 15 | def cFieldValue = issue.getCustomFieldValue(cField) 16 | 17 | //return cFieldValue 18 | 19 | //update the value 20 | def changeHolder = new DefaultIssueChangeHolder() 21 | cField.updateValue(null, issue, new ModifiedValue(cFieldValue, "Life is great"),changeHolder) 22 | 23 | 24 | 25 | }else { 26 | 27 | return "Issue doesn't exist" 28 | } 29 | 30 | -------------------------------------------------------------------------------- /jira_server/userList.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.bc.JiraServiceContext 2 | import com.atlassian.jira.bc.JiraServiceContextImpl 3 | import com.atlassian.jira.bc.user.search.UserSearchService 4 | import com.atlassian.jira.component.ComponentAccessor 5 | 6 | def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser 7 | def query = ComponentAccessor.getUserSearchService() as UserSearchService 8 | 9 | JiraServiceContext jiraServiceContext = new JiraServiceContextImpl(user) 10 | 11 | return query.findUsersAllowEmptyQuery(jiraServiceContext, "")*.name -------------------------------------------------------------------------------- /jira_server/workflowCondition_checkSelectListValue.groovy: -------------------------------------------------------------------------------- 1 | import com.atlassian.jira.component.ComponentAccessor 2 | import com.atlassian.jira.issue.IssueManager 3 | import com.atlassian.jira.issue.MutableIssue 4 | import org.apache.log4j.Level 5 | import org.apache.log4j.Logger 6 | 7 | def log = Logger.getLogger("in.ravisagar.sr4j") 8 | //log.setLevel(Level.DEBUG) 9 | 10 | //IssueManager im = ComponentAccessor.getIssueManager() 11 | //MutableIssue issue = im.getIssueObject("SCRUM-31") 12 | 13 | def country = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Country") 14 | def countryValue = country?issue.getCustomFieldValue(country[0]): "Not found" 15 | log.debug("Country selected: " + countryValue) 16 | 17 | 18 | passesCondition = false 19 | 20 | if (countryValue.toString() == "India") { 21 | 22 | passesCondition = true 23 | } 24 | --------------------------------------------------------------------------------