Step 1. Create the plugin project
- If you have not already set up the Atlassian Plugin SDK, do that now: Set up the Atlassian Plugin SDK and Build a Project.
- Enter the following command to create a plugin skeleton:
atlas-create-jira-plugin
- Choose 1 for JIRA 5 when asked which version of JIRA you want to create the plugin for.
- As prompted, enter the following information to identify your plugin:
group-id |
com.example.plugins.tutorial.jira |
artifact-id |
jira-simple-jql-function |
version |
1.0-SNAPSHOT |
package |
com.example.plugins.tutorial.jira |
- Confirm your entries when prompted.
Step 2. Review and tweak the POM
- Change to the new
jira-simple-jql-function
directory and open the pom.xml
file for editing.
- Add your company or organisation name and your website to the
<organization>
element (the following code blocks show how it looks in plain text): 
- Update the
<description>
element: 
- Save and close the file.
Step 3. Add your plugin modules
- From the plugin root folder (where the
pom.xml
is located), enter the following command:
atlas-create-jira-plugin-module
- Choose the option labelled
JQL Function
.
- Supply the following information as prompted:
Enter New Classname |
RecentProjectFunction |
Package Name |
com.example.plugins.tutorial.jira.jql |
- Choose
N
for Show Advanced Setup.
- Choose
N
for Add Another Plugin Module.
- Confirm your choices.
Step 4. Review and tweak the plugin descriptor
- Change to the src/main/resources/ and open
atlassian-plugin.xml
for editing.
- Find the
jql-function
element and add two elements, fname
and list
, after the description, as shown here: 
- Save and close the file.
Step 5. Write the plugin code
- Change to the source code directory for our function,
src/main/java/com/example/plugins/tutorial/jira/jql/
- Open the
RecentProjectFunction.java
file for editing.
package com.example.plugins.tutorial.jira.jql;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.atlassian.jira.JiraDataType;
import com.atlassian.jira.JiraDataTypes;
import com.atlassian.jira.jql.operand.QueryLiteral;
import com.atlassian.jira.jql.query.QueryCreationContext;
import com.atlassian.jira.plugin.jql.function.AbstractJqlFunction;
import com.atlassian.jira.util.MessageSet;
import com.atlassian.jira.util.NotNull;
import com.atlassian.query.clause.TerminalClause;
import com.atlassian.query.operand.FunctionOperand;
import com.google.common.collect.Iterables;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.user.UserHistoryItem;
import com.atlassian.jira.user.UserProjectHistoryManager;
import java.util.LinkedList;
import java.util.Collections;
import java.util.List;
import com.atlassian.jira.user.ApplicationUser; //NOW COMPATIBLE WITH JIRA 6 & 7
public class RecentProjectFunction extends AbstractJqlFunction
{
private static final Logger log = LoggerFactory.getLogger(RecentProjectFunction.class);
private final UserProjectHistoryManager userProjectHistoryManager;
public RecentProjectFunction(UserProjectHistoryManager userProjectHistoryManager)
{
this.userProjectHistoryManager = userProjectHistoryManager;
}
public MessageSet validate(ApplicationUser searcher, FunctionOperand operand, TerminalClause terminalClause)
{
//NOW COMPATIBLE WITH JIRA 6 & 7
return validateNumberOfArgs(operand, 0);
}
public List<QueryLiteral> getValues(QueryCreationContext queryCreationContext, FunctionOperand operand, TerminalClause terminalClause)
{
final List<QueryLiteral> literals = new LinkedList<QueryLiteral>();
final List<UserHistoryItem> projects = userProjectHistoryManager.getProjectHistoryWithoutPermissionChecks(queryCreationContext.getUser());
for (final UserHistoryItem userHistoryItem : projects)
{
final String value = userHistoryItem.getEntityId();
try
{
literals.add(new QueryLiteral(operand, Long.parseLong(value)));
}
catch (NumberFormatException e)
{
log.warn(String.format("User history returned a non numeric project ID '%s'.", value));
}
}
return literals;
}
public int getMinimumNumberOfExpectedArguments()
{
return 0;
}
public JiraDataType getDataType()
{
return JiraDataTypes.PROJECT;
}
}
Step 6. Start JIRA and try out the plugin
- Run
atlas-run
More info in Atlassian
Like this:
Like Loading...
Related