Created
          February 22, 2022 07:31 
        
      - 
      
 - 
        
Save nickname55/5698b6f108551b46ca6375c49d5ca430 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import com.atlassian.jira.component.ComponentAccessor | |
| import org.apache.log4j.Level | |
| import org.apache.log4j.Logger | |
| import com.atlassian.jira.user.util.UserManager | |
| import com.atlassian.jira.bc.user.search.UserSearchService | |
| import com.atlassian.jira.bc.user.search.UserSearchParams | |
| import com.atlassian.jira.bc.user.ApplicationUserBuilderImpl | |
| log.setLevel(Level.DEBUG) | |
| // let's define what domain we want to and what it should be afterwards | |
| final SOURCE_DOMAIN = '@acme.com' | |
| final TARGET_DOMAIN = '@scavengers.inc' | |
| // here's the definition of our search params, the domain name is not here yet | |
| def userSearchService = ComponentAccessor.getUserSearchService() | |
| def userSearchParams = new UserSearchParams.Builder() | |
| .allowEmptyQuery(true) | |
| .canMatchEmail(true) | |
| .includeActive(true) | |
| .includeInactive(false) | |
| .sorted(true) | |
| .build() | |
| // here is the actual search for the users who meet our search criteria | |
| // including the domain in their e-mail | |
| def userList = userSearchService.findUsers('', SOURCE_DOMAIN, userSearchParams) | |
| // let's print who we have found and what their e-mails will be after the change | |
| log.debug 'userList size: ' + userList.size() + | |
| ', emails found: ' + userList.collect { it.getEmailAddress() } + | |
| ', emails will be: ' + userList.collect { | |
| it.getEmailAddress().replace(SOURCE_DOMAIN, TARGET_DOMAIN) } | |
| // now all what's left to do is to update the existing users with | |
| // their new emails with the target domain instead of the source domain | |
| def userManager = ComponentAccessor.getUserManager() | |
| userList.each { | |
| userManager.updateUser( | |
| new ApplicationUserBuilderImpl(it) | |
| .emailAddress( | |
| it.getEmailAddress().replace(SOURCE_DOMAIN, TARGET_DOMAIN)).build()) | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment