自定义Jira插件:无法更新问题

时间:2013-01-22 05:49:55

标签: java servlets jira jira-plugin atlassian-plugin-sdk

我正在尝试创建一个jira插件,可以使用issueKey参数更新任何问题的“受理人”。

它做的一切正确,就像我得到正确的issueId和用户,但它无法更新问题,它不会在日志中显示任何内容或打印出任何错误,但我放入servlet的条件失败。 我无法让它工作,任何建议。在这种情况下也让我知道doGet和doPost方法。谢谢。 这是代码:

package com.elixir.plugins.servlet;

import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.bc.project.ProjectService;
import com.atlassian.jira.security.PermissionManager;
import com.atlassian.jira.security.Permissions;

//import com.atlassian.jira.bc.issue;//added
import com.atlassian.jira.bc.ServiceResultImpl;//added
import com.atlassian.jira.bc.issue.IssueService.IssueResult;//added
import com.atlassian.jira.bc.issue.IssueService.IssueValidationResult;//added
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult;//added
import com.atlassian.jira.bc.issue.IssueService.TransitionValidationResult;//added

//import com.atlassian.jira.rest.v2.issue; //added
//import com.atlassian.jira.rest.api.util.*; //added
import com.atlassian.jira.util.ErrorCollection; //added

//import com.atlassian.jira.issue; //added
import com.atlassian.jira.issue.*; //added
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.issue.IssueInputParametersImpl;//added
import com.atlassian.jira.issue.worklog.Worklog;       //added
import com.atlassian.jira.issue.worklog.WorklogManager;//added
import com.atlassian.jira.workflow.*;//added
import com.atlassian.jira.ComponentManager;//added
import com.atlassian.jira.ManagerFactory;//added
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.config.properties.APKeys;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.event.issue.IssueEventManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.search.SearchException;
import com.atlassian.jira.jql.builder.JqlClauseBuilder;
import com.atlassian.jira.jql.builder.JqlQueryBuilder;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.web.bean.PagerFilter;
import com.atlassian.sal.api.user.UserManager;
import com.atlassian.templaterenderer.TemplateRenderer;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.atlassian.jira.config.properties.ApplicationProperties;    //added
import com.atlassian.jira.config.properties.ApplicationPropertiesImpl;//added
import com.atlassian.jira.util.ImportUtils;                           //added
import com.atlassian.jira.util.JiraUtils;                             //added
import com.atlassian.jira.workflow.WorkflowTransitionUtil;            //added
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;        //added

import com.atlassian.core.user.*;//added

/*import com.opensymphony.user.User;
import com.opensymphony.user.UserManager;*/
import com.opensymphony.workflow.*;
import com.opensymphony.workflow.InvalidActionException;
import com.opensymphony.workflow.WorkflowContext;
import com.opensymphony.workflow.loader.ActionDescriptor;
import com.opensymphony.workflow.loader.StepDescriptor;
import com.opensymphony.workflow.loader.AbstractDescriptor;    //added
import com.opensymphony.workflow.loader.WorkflowDescriptor;    //added
import com.atlassian.jira.workflow.ImmutableWorkflowDescriptor;//added
import com.opensymphony.module.propertyset.PropertySet;        //added
//import com.opensymphony.user.EntityNotFoundException;          //added
import com.opensymphony.util.TextUtils;                        //added
import com.opensymphony.workflow.loader.ValidatorDescriptor;   //added
import com.opensymphony.workflow.loader.DescriptorFactory;     //added
import com.opensymphony.workflow.spi.WorkflowEntry;            //added

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*; //added
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.ofbiz.core.entity.GenericEntityException;//added
import org.ofbiz.core.entity.GenericValue;          //added

public class Unassigner extends HttpServlet {
    private static final Logger log = LoggerFactory.getLogger(Unassigner.class);
    private IssueService issueService;
    private ProjectService projectService;
    private SearchService searchService;
    private UserManager userManager;
    private TemplateRenderer templaterenderer;
    private com.atlassian.jira.user.util.UserManager jiraUserManager;

    public Unassigner(IssueService issueService, ProjectService projectService, SearchService searchService,
                     UserManager userManager, TemplateRenderer templateRenderer,
                     com.atlassian.jira.user.util.UserManager jiraUserManager) {
        this.issueService = issueService;
        this.projectService = projectService;
        this.searchService = searchService;
        this.userManager = userManager;
        this.templaterenderer = templateRenderer;
        this.jiraUserManager = jiraUserManager;
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        String issueKey = req.getParameter("issue");
        Issue issue = ComponentAccessor.getIssueManager().getIssueObject(issueKey);
        User loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
        IssueInputParameters issueInputParameters = new IssueInputParametersImpl(); 
        issueInputParameters.setAssigneeId("Unassigned");

        issueInputParameters.setRetainExistingValuesWhenParameterNotProvided(false);

        resp.getWriter().write(loggedInUser +"<br>");
        resp.getWriter().write(issue.getId() +"<br>");
        resp.getWriter().write(issueInputParameters +"<br>");

        IssueService issueService = ComponentManager.getInstance().getIssueService();
        Long issueId = issue.getId();
        UpdateValidationResult updateValidationResult = issueService.validateUpdate(loggedInUser, issueId, issueInputParameters);

        resp.getWriter().write(updateValidationResult.getErrorCollection().getErrorMessages() +"<br>");

        if (updateValidationResult.isValid()) { 
            IssueResult updateResult = issueService.update(loggedInUser, updateValidationResult);
            MutableIssue updatedIssue = updateResult.getIssue();
            resp.getWriter().write("<p style='background:LightYellow; font-family:Arial,Helvetica,sans-serif;font-size:small;'>Issue Updated "+ updatedIssue +"</p>");
        }
        if (!updateValidationResult.isValid()) {
            //final Collection<String> errorMessages = updateValidationResult.getErrorCollection().getErrorMessages();
            resp.getWriter().write("<p style='background:Pink; font-family:Arial,Helvetica,sans-serif;font-size:small;'>Issue Updated Failed</p><br>"+ updateValidationResult.getErrorCollection().getErrorMessages());
        }


    }
}

1 个答案:

答案 0 :(得分:7)

尝试以下代码

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject(issueKey);
User loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
issue.setAssignee(loggedInUser);
issue.store();