...
Code Block | ||
---|---|---|
| ||
/* Script to create issues of a particular issue type when a new project of classic type is created in JIRA cloud */ //get the project key on creation of project def projectKey = project.key; logger.info("The key of the project is: " + projectKey); //get call for project data based on project key def getProjectStyle = get("/rest/api/3/project/" + projectKey) .header('Content-Type', 'application/json') .asObject(Map) //project style def projectStyle = getProjectStyle.body.style; logger.info("Project Style is: " + projectStyle); //project id def projectId = getProjectStyle.body.id; logger.info("Project Id is: " + projectId); //variable used to assign rest call, later in the code def postCreateIssue; //issue type id def issueTypeId = "10002"; //account id of assignee def accountIdOfAssignee = "5b4f068a3675c92cbbded6a2"; //project style classic def projectStyleClassic = "classic"; //condition to create issues only if it is classic project if(projectStyle == projectStyleClassic) { postCreateIssue = post("/rest/api/3/issue/bulk") .header("Content-Type", "application/json") .body( [ issueUpdates: [ [ fields: [ summary : "Create user profiles", description: [ type: "doc", version: 1, content: [ [ type: "paragraph", content: [ [ text: "Create profiles for students in directory", type: "text" ] ] ] ] ], project : [ id: projectId ], issuetype : [ id: issueTypeId ], assignee: [ id: accountIdOfAssignee ] ] ], [ fields: [ summary : "Create logo", description: [ type: "doc", version: 1, content: [ [ type: "paragraph", content: [ [ text: "Create logos for the customer", type: "text" ] ] ] ] ], project : [ id: projectId ], issuetype : [ id: issueTypeId ], assignee: [ id: accountIdOfAssignee ] ] ], [ fields: [ summary : "Create accounts", description: [ type: "doc", version: 1, content: [ [ type: "paragraph", content: [ [ text: "Create accounts for each student n staff", type: "text" ] ] ] ] ], project : [ id: projectId ], issuetype : [ id: issueTypeId ], assignee: [ id: accountIdOfAssignee ] ] ], [ fields: [ summary : "Create properties", description: [ type: "doc", version: 1, content: [ [ type: "paragraph", content: [ [ text: "Create properties for students and staff", type: "text" ] ] ] ] ], project : [ id: projectId ], issuetype : [ id: issueTypeId ], assignee: [ id: accountIdOfAssignee ] ] ], [ fields: [ summary : "Assign and implement", description: [ type: "doc", version: 1, content: [ [ type: "paragraph", content: [ [ text: "Assign and implement the case", type: "text" ] ] ] ] ], project : [ id: projectId ], issuetype : [ id: issueTypeId ], assignee: [ id: accountIdOfAssignee ] ] ] ] ]) .asString() } else { logger.info("Project doesn't belong to classic style"); } //success or failure of issue creation based on condition if(projectStyle == projectStyleClassic && postCreateIssue.status == 201) { logger.info("Successfully added issues to project with key: ", projectKey); } else { logger.error("Failed to bulk add issues to project with key: ", projectKey); } |
13. Script to deactivate users if they are inactive for long time and doesn't belong to “xyz“ group(s) (Server)
Code Block | ||
---|---|---|
| ||
import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.embedded.api.UserWithAttributes
import com.atlassian.crowd.embedded.impl.ImmutableUser
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.security.groups.GroupManager
//Number of days the user has not logged in
final NUMBER_OF_DAYS = 180;
//Counter to calculate amount of users deactivated starting 0
def COUNTER = 0;
//Set a date as a limit to validate against / the period in which the user is not validated against
Date dateLimit = (new Date()) - NUMBER_OF_DAYS;
final GROUP_ONE = "Watchers_Only";
final GROUP_TWO = "Essential Accounts";
UserUtil userUtil = ComponentAccessor.userUtil;
CrowdService crowdService = ComponentAccessor.crowdService;
UserService userService = ComponentAccessor.getComponent(UserService);
ApplicationUser updateUser;
UserService.UpdateUserValidationResult updateUserValidationResult;
def groupManager = ComponentAccessor.getComponent(GroupManager);
//Loop to check the condition and deactivate users from instance
userUtil.getUsers().findAll{it.isActive()}.each {
ApplicationUser userInGroup = ComponentAccessor.getUserManager().getUserByName(it.getName());
def isUserInGroup = groupManager.isUserInGroup(userInGroup, GROUP_ONE);
def isUserInSecondGroup = groupManager.isUserInGroup(userInGroup, GROUP_TWO);
if(isUserInGroup == true) {
log.error "${it.getName()} NOT DEACTIVATED as they belong to ${GROUP_ONE}";
}
else if(isUserInSecondGroup == true) {
log.error "${it.getName()} NOT DEACTIVATED as they belong to ${GROUP_TWO}";
}
else {
UserWithAttributes user = crowdService.getUserWithAttributes(it.getName());
//Get the last logged in value of users in miliseconds / lastLoginMillis
String lastLoginMillis = user.getValue('login.lastLoginMillis');
//Condition to check if last logged in value is a number or null
if (lastLoginMillis?.isNumber()) {
//Converts the timestamp value to [ Day Month Date HH:MM:SS Timezone Year ] format
Date convertTimeStampToDate = new Date(Long.parseLong(lastLoginMillis));
if (convertTimeStampToDate.before(dateLimit)) {
updateUser = ApplicationUsers.from(ImmutableUser.newUser(user).active(false).toUser());
updateUserValidationResult = userService.validateUpdateUser(updateUser);
if (updateUserValidationResult.isValid()) {
userService.updateUser(updateUserValidationResult);
log.error "${updateUser.name} DEACTIVATED as their last login is on ${convertTimeStampToDate}";
COUNTER++;
} else {
log.error "Update of ${user.name} failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}";
}
}
}
}
}
"${COUNTER} users deactivated.\n";
|
Learning:
Knowledge on how script-runner listener works for both cloud and server
Got to understand different scenarios for Jira based on which scripts were written
Basic knowledge on groovy
While setting the value for multi-select system fields always set the values based on “id’s of values” and not “values itself“ in behaviors
While setting the value for single select system fields we can directly give “values” instead of “id’s of values”
script-runner behaviors
While setting the value for single select custom field we have to give “id’s of values” only (can also be directly configured in field setting as these are custom fields)
...