Page tree

이 문서는 Scriptrunner 활용하여 기존 첨부파일(attahment), 댓글(comment) 소유주(creator) 변경하여 새로 생성하기  가이드를 공유하기 위해 작성되었다.


도구명Jira, scriptrunner
비고




Scriptrunner 활용하여 기존 첨부파일, 댓글 소유주 변경하여 새로 생성하기 

ScriptRunner Console 들어가기

  • Scriptrunner → Console


프로젝트 지정하여 변경하기

  • Console 창에 다음 코드 입력
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.FileSystemAttachmentDirectoryAccessor
import org.apache.log4j.Level
import org.apache.log4j.Logger
import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean
import java.io.File
import com.atlassian.jira.issue.comments.MutableComment
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption
import com.onresolve.scriptrunner.parameters.annotation.*
import com.atlassian.jira.project.Project

def log = Logger.getLogger("com.curvc")
log.setLevel(Level.DEBUG)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def userManager = ComponentAccessor.getUserManager()
def projectManager = ComponentAccessor.getProjectManager()
def commentManager = ComponentAccessor.getCommentManager()

//프로젝트 명 입력
@ProjectPicker(
    label = 'Project', description = 'Pick a project', placeholder = 'Pick a project', includeArchived = false
)
Project project 
String projectName = "SERVICE"
//찾을 유저 명 입력
@UserPicker(label = "Original User", description = "Select a user")
ApplicationUser user
log.info("본래 유저: " + user.displayName)
// 변경할 유저명 입력
@UserPicker(label = "Change User", description = "Select a user")
ApplicationUser changeUser
log.info("변경할 유저: " + changeUser.displayName)
log.info(project.getId())

// admin user
@UserPicker(label = "Admin User", description = "Select a admin user")
ApplicationUser adminuser
 
def issues = issueManager.getIssueObjects(issueManager.getIssueIdsForProject(project.getId()))
 
for(int i = 0;i < issues.size();i++){
    def issue = issues[i] as MutableIssue
    log.info(issue.getKey())
    if(issue.reporter == user){
        // 첨부파일들 변경
        def attachments = ComponentAccessor.attachmentManager.getAttachments(issue)
        log.info("첨부파일 수: " + attachments.size())
        for (attachment in attachments) {
            String type = attachment.getMimetype()
            def attachmentname = attachment.getFilename().toString()
            log.info(attachmentname)
            def attachmentUser = userManager.getUserByKey(attachment.getAuthorKey())
            log.info(attachmentUser)
            //변경될 첨부파일명
            String fileName = attachmentname.toString()
            def attachmentid = attachment.getId().toString()
            // 첨부파일 위치 파악
            def filelocate = ComponentAccessor.getComponent(FileSystemAttachmentDirectoryAccessor.class).getAttachmentDirectory(issue)
            def filepath = filelocate.toString() + '/' + attachmentid
            File oriFile = new File(filepath)
            log.info(filepath)
            // 기존첨부파일 신규 등록
            def bean = new CreateAttachmentParamsBean.Builder()
            .file(oriFile)
            .filename(fileName)
            .contentType(type)
            .author(changeUser)
            .issue(issue)
            .createdTime(attachment.getCreated())
            .build()
            ComponentAccessor.attachmentManager.createAttachment(bean)
            log.info("create attachment " + fileName)
            //Jira 이슈에서 첨부파일 삭제
            ComponentAccessor.attachmentManager.deleteAttachment(attachment)
            log.info("attahment: " + attachmentname + " recreated")
        }
        //댓글 변경
        def comments = commentManager.getCommentsForUser(issue, user)
        log.info(issue.getKey() + " 댓글 수: " + comments.size())
        for (int j = 0; j < comments.size(); j++){
            def mutableComment = commentManager.getMutableComment(comments[j].getId())
            log.info(mutableComment.getAuthorFullName())
            if(mutableComment.getAuthorApplicationUser().getKey() == user.getKey()){
                commentManager.create(issue, changeUser, changeUser, mutableComment.getBody(), mutableComment.getGroupLevel(), mutableComment.getRoleLevelId(), mutableComment.getCreated(), mutableComment.getUpdated(), false)
                commentManager.delete(mutableComment)               
                log.info("comment recreated")
                
            }
        }
        // 이슈 갱신
        issueManager.updateIssue(adminuser, issue, EventDispatchOption.ISSUE_UPDATED, false)
    }
}


  • Project: 변경할 프로젝트 선택
    Original User: 기존 유저 선택
    Change User: 변경할 유저 선택
    Admin User: 관리자 유저 선택

이슈 지정하여 변경하기

  • Console창에 다음 코드 입력
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.FileSystemAttachmentDirectoryAccessor
import org.apache.log4j.Level
import org.apache.log4j.Logger
import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean
import java.io.File
import com.atlassian.jira.issue.comments.MutableComment
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption
import com.onresolve.scriptrunner.parameters.annotation.*
import com.atlassian.jira.project.Project

def log = Logger.getLogger("com.curvc")
log.setLevel(Level.DEBUG)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def userManager = ComponentAccessor.getUserManager()
def projectManager = ComponentAccessor.getProjectManager()
def commentManager = ComponentAccessor.getCommentManager()

// 변경할 이슈 입력
@ShortTextInput(label = "Issue", description = "Enter a issueKey")
String issueKey

//찾을 유저 명 입력
@UserPicker(label = "Original User", description = "Select a user")
ApplicationUser user
log.info("본래 유저: " + user.displayName)
// 변경할 유저명 입력
@UserPicker(label = "Change User", description = "Select a Chnage user")
ApplicationUser changeUser
log.info("변경할 유저: " + changeUser.displayName)

// admin user
@UserPicker(label = "Admin User", description = "Select a admin user")
ApplicationUser adminuser

def issue = issueManager.getIssueByCurrentKey(issueKey)
if(issue instanceof MutableIssue){
    issue as MutableIssue
} else {
    log.info("해당 issueKey에 대한 Issue가 없습니다.")
    return
}

log.info(issue.getKey())
if(issue.reporter == user){
    // 담당자 변경
    issue.setReporter(changeUser)
    // 첨부파일들 변경
    def attachments = ComponentAccessor.attachmentManager.getAttachments(issue)
    log.info("첨부파일 수: " + attachments.size())
    for (attachment in attachments) {
        String type = attachment.getMimetype()
        def attachmentname = attachment.getFilename().toString()
        log.info(attachmentname)
        def attachmentUser = userManager.getUserByKey(attachment.getAuthorKey())
        log.info(attachmentUser)
        //변경될 첨부파일명
        String fileName = attachmentname.toString()
        def attachmentid = attachment.getId().toString()
        // 첨부파일 위치 파악
        def filelocate = ComponentAccessor.getComponent(FileSystemAttachmentDirectoryAccessor.class).getAttachmentDirectory(issue)
        def filepath = filelocate.toString() + '/' + attachmentid
        File oriFile = new File(filepath)
        log.info(filepath)
        // 기존첨부파일 신규 등록
        def bean = new CreateAttachmentParamsBean.Builder()
            .file(oriFile)
            .filename(fileName)
            .contentType(type)
            .author(changeUser)
            .issue(issue)
            .createdTime(attachment.getCreated())
            .build()
        ComponentAccessor.attachmentManager.createAttachment(bean)
        log.info("create attachment " + fileName)
        //Jira 이슈에서 첨부파일 삭제
        ComponentAccessor.attachmentManager.deleteAttachment(attachment)
        log.info("attahment: " + attachmentname + " recreated")
    }    
    //댓글 변경
    def comments = commentManager.getCommentsForUser(issue, user)
    log.info(issue.getKey() + " 댓글 수: " + comments.size())
    for (int i = 0; i < comments.size(); i++){
        def mutableComment = commentManager.getMutableComment(comments[i].getId())
        log.info(mutableComment.getAuthorFullName())
        if(mutableComment.getAuthorApplicationUser().getKey() == user.getKey()){
            commentManager.create(issue, changeUser, changeUser, mutableComment.getBody(), mutableComment.getGroupLevel(), mutableComment.getRoleLevelId(), mutableComment.getCreated(), mutableComment.getUpdated(), false)
            commentManager.delete(mutableComment)               
            log.info("comment recreated")
             
        }
    }
    // 이슈 갱신
    issueManager.updateIssue(adminuser, issue, EventDispatchOption.ISSUE_UPDATED, false)
}


  • Issue: 변경 할 IssueKey 입력
    Original User: 기존 유저 선택
    Change User: 변경할 유저 선택
    Admin User: 관리자 유저 선택





참조링크


  • No labels