brysbra
brysbra

Reputation: 13

JIRA custom plugin develpment - creating custom field returns null

I'm currently developing a custom JIRA plugin and I'm having some trouble integrating my plugin to the JIRA project/issue management system. I have only been using the JIRA SDK for about 4 months now so I'm no expert.

Some notes:

Now, what currently happening with my code is that is appears the createCustomField method is returning null. When I run the atlas-debug command, I can see (in cmd) that there is null pointer exception thrown because of that method. The plugin is not installed because of that.

Here is my code:

public class PluginListener implements InitializingBean, DisposableBean {

private final IssueTypeManager issueTypeManager;
private final CustomFieldManager customFieldManager;
private final FieldScreenManager fieldScreenManager;

public PluginListener(IssueTypeManager issueTypeManager, CustomFieldManager customFieldManager, FieldScreenManager fieldScreenManager) {
    this.issueTypeManager = issueTypeManager;
    this.customFieldManager = customFieldManager;
    this.fieldScreenManager = fieldScreenManager;
}

@Override
public void destroy() throws Exception {
    // Handle plugin disabling or un-installation here
}

@Override
public void afterPropertiesSet() throws Exception {
    // Handle plugin enabling or installation here      

    // Create issue type:
    IssueType issueType = this.issueTypeManager.createIssueType("TheType", "TheDescription", "/images/icons/issuetypes/genericissue.png");

    // Create custom field:
    // Create a list of issue types for which the custom field needs to be available    
    List<GenericValue> issueTypes = new ArrayList<GenericValue>();
    issueTypes.add(null);

    // Create a list of project contexts for which the custom field needs to be available
    List<JiraContextNode> contexts = new ArrayList<JiraContextNode>();
    contexts.add(GlobalIssueContext.getInstance());

    CustomFieldType fieldType = this.customFieldManager.getCustomFieldType("com.atlassian.jira.plugin.system.customfieldtypes:textfield");        
    CustomFieldSearcher fieldSearcher = this.customFieldManager.getCustomFieldSearcher("com.atlassian.jira.plugin.system.customfieldtypes:textsearcher");

    // Add custom field
    final CustomField cField = this.customFieldManager.createCustomField("FOO", "BAR", fieldType, fieldSearcher, contexts, issueTypes);

    // Add field to default Screen
    FieldScreen defaultScreen = fieldScreenManager.getFieldScreen(FieldScreen.DEFAULT_SCREEN_ID);
    if (!defaultScreen.containsField(cField.getId())) {
        FieldScreenTab firstTab = defaultScreen.getTab(0);
        firstTab.addFieldScreenLayoutItem(cField.getId());
    }
}

}

Upvotes: 1

Views: 718

Answers (1)

Scott Dudley
Scott Dudley

Reputation: 3298

Running atlas-debug is effectively installing your plugin from a JIRA cold start; the likely issue is that JIRA plugins are installed (and their OSGi bundles activated) before JIRA is fully ready to operate and process certain types of requests.

To debug this, after your atlas-debug run fails, I would try manually navigating to Toolgear->Add-ons->Manage add-ons and try to re-upload your plugin .JAR again (find it in the target directory). If it works that time, it suggests that JIRA was simply not ready for your plugin at the time of initial system startup, so you will need to move the field creation step elsewhere.

You might try the onStart event. If that does not work for you, the most reliable method will probably be lazy initialization, so long as you can ensure that this step gets performed the first time your plugin is ever accessed by a user.

Upvotes: 0

Related Questions