JIRA 4.0 : Creating and Editing an Issue
This page last changed on May 27, 2007 by dylan@atlassian.com.
Creating a new Issue
Here is sniplet of code that creates a new issue: // Use this if you are using JIRA 3.6.5 and below //MutableIssue issueObject = (MutableIssue) JiraUtils.loadComponent(IssueImpl.class); // Use this if you are using JIRA 3.7 and above MutableIssue issueObject = issueFactory.getIssue(); // Regular Fields issueObject.setProject(projectManager.getProject(new Long(10000))); issueObject.setIssueType(constantsManager.getIssueType("1")); issueObject.setSummary("Test Issue"); issueObject.setReporter(UserUtils.getUser("admin")); issueObject.setAssignee(UserUtils.getUser("admin")); issueObject.setPriority(constantsManager.getPriority("1")); issueObject.setDescription("Test description"); issueObject.setAffectedVersions(EasyList.build(versionManager.getVersion(new Long(10000)), versionManager.getVersion(new Long(10001)))); issueObject.setFixVersions(EasyList.build(versionManager.getVersion(new Long(10002)))); issueObject.setComponents(EasyList.build(projectManager.getComponent(new Long(10000)), projectManager.getComponent(new Long(10001)))); // Custom Fields CustomField customField = customFieldManager.getCustomFieldObject(new Long(10020)); issueObject.setCustomFieldValue(customField, "Test Value"); Map params = new HashMap(); params.put("issue", issueObject); GenericValue issue = issueManager.createIssue(authenticationContext.getUser(), params); This code example uses a lot of Manager objects. You can get a reference to them by declaring a dependency in the constructor of your plugin. Classes not managed by Picocontainer (eg. workflow conditions / functions, Services and Listeners, or JSP scriptlets) can still get pico-instantiated objects statically using static methods of ManagerFactory or ComponentManager.getInstance(). For example: ComponentManager.getInstance().getProjectManager(); ComponentManager.getInstance().getIssueFactory(); ManagerFactory.getCustomFieldManager(); //or ManagerFactory.getApplicationProperties(); Using ComponentManager.getInstance() is prefered as ManagerFactory will become obsolete. However, if a method that you are after does not exist on the ComponentManager, use ManagerFactory. The code above also sets a value for a custom field on the issue. Please note that the value must be an object of the type that the Custom Field expects. As the above code was using a Text custom field, a simple java.lang.String is fine. For more information on working with custom fields please see Working with Custom Fields. Editing an existing IssueThe code below edits a Due Date field of an issue and sets it to 24 hours from now. A comment that is visible by everyone who has permission to see the issue is also added. MutableIssue issue = issueFactory.getIssue(issueGV); issue.setDueDate(new Timestamp(System.currentTimeMillis() + 24*60*60*1000)); Map actionParams = EasyMap.build("issue", issue.getGenericValue(), "issueObject", issue, "remoteUser", authenticationContext.getUser()); actionParams.put("comment", "Test Comment"); actionParams.put("commentLevel", null); ActionResult aResult = CoreFactory.getActionDispatcher().execute(ActionNames.ISSUE_UPDATE, actionParams); You can also specify a group name as the commentLevel parameter to restrict the visibility of comments. If you use the above code to update an issue all the relevant change history entries will be made, and an Updated Issue Event generated. The code above created an issue object from an issue GenericValue. You can retrieve the issue generic value using the following code: // By Id GenericValue issueGV1 = issueManager.getIssue(new Long(10000)); // By Issue Key GenericValue issueGV2 = issueManager.getIssue("TST-1"); To learn how to update custom fields please see Working with Custom Fields. EventsIt's worth noting that in the examples above, when the issues are created or modified events are fired, and any notifications that are associated with those events will be triggered. |
![]() |
Document generated by Confluence on Oct 06, 2009 00:31 |