Commits

Cheryl Jerozal committed c3f7163

initial commit

Comments (0)

Files changed (5)

+This code accompanies the tutorial at:
+
+https://developer.atlassian.com/display/JIRADEV/Plugin+Tutorial+-+Adding+a+JQL+Function+to+JIRA
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>com.example.plugins.tutorial</groupId>
+  <artifactId>tutorial-jira-simple-jql-function</artifactId>
+  <packaging>atlassian-plugin</packaging>
+  <name>tutorial-jira-simple-jql-function</name>
+  <version>1.0</version>
+  <description>This is the com.example.plugins.tutorial:tutorial-jira-simple-jql-function plugin for Atlassian JIRA.</description>
+  <organization>
+    <name>Example Company</name>
+    <url>http://www.example.com/</url>
+  </organization>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>com.atlassian.maven.plugins</groupId>
+        <artifactId>maven-jira-plugin</artifactId>
+        <version>3.7</version>
+        <extensions>true</extensions>
+        <configuration>
+          <productVersion>${jira.version}</productVersion>
+          <productDataVersion>${jira.version}</productDataVersion>
+        </configuration>
+      </plugin>
+      <plugin>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <source>1.6</source>
+          <target>1.6</target>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  <dependencies>
+    <dependency>
+      <groupId>com.atlassian.jira</groupId>
+      <artifactId>jira-api</artifactId>
+      <version>${jira.version}</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>com.atlassian.jira</groupId>
+      <artifactId>jira-core</artifactId>
+      <version>${jira.version}</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.8.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>com.atlassian.jira</groupId>
+      <artifactId>jira-tests</artifactId>
+      <version>${jira.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>com.atlassian.jira</groupId>
+      <artifactId>jira-func-tests</artifactId>
+      <version>${jira.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-all</artifactId>
+      <version>1.8.5</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+  <properties>
+    <amps.version>3.7</amps.version>
+    <jira.version>5.0-beta1</jira.version>
+  </properties>
+</project>
+

src/main/java/com/example/plugins/tutorial/jira/jql/RecentProjectFunction.java

+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.user.UserHistoryItem;
+import com.atlassian.jira.user.UserProjectHistoryManager;
+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 java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * A handler for the "recentHistory" function. This function will return all the projects within the user's history.
+ */
+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;
+    }
+
+    /**
+     * This method validates the passed in args. In this case the function accepts no args, so let's validate that were none.
+     */
+    public MessageSet validate(User searcher, FunctionOperand operand, TerminalClause terminalClause)
+    {
+        return validateNumberOfArgs(operand, 0);
+    }
+
+    
+    public List<QueryLiteral> getValues(QueryCreationContext queryCreationContext, FunctionOperand operand, TerminalClause terminalClause)
+    {
+        final List<QueryLiteral> literals = new LinkedList<QueryLiteral>();
+
+        /*
+        * We do not need to do a security check here as the search will do the security checks for us.
+        */
+        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;
+    }
+
+   /**
+    * This method returns the min number of args the function takes. In this case - 0
+    */
+    public int getMinimumNumberOfExpectedArguments()
+    {
+        return 0;
+    }
+
+   /**
+    * This method needs to return the type of objects the function deals with. In this case - Projects
+    */
+    public JiraDataType getDataType()
+    {
+        return JiraDataTypes.PROJECT;
+    }
+}

src/main/resources/atlassian-plugin.properties

+#
+#Wed Nov 09 22:03:36 GMT 2011
+recent-project-function.description=Provides a JQL function to restrict the search to recently visited projects.
+recent-project-function.name=Recent Project Function

src/main/resources/atlassian-plugin.xml

+<?xml version="1.0" encoding="UTF-8"?>
+
+<atlassian-plugin key="${project.groupId}.${project.artifactId}" name="${project.name}" plugins-version="2">
+  <plugin-info>
+    <description>${project.description}</description>
+    <version>${project.version}</version>
+    <vendor name="${project.organization.name}" url="${project.organization.url}"/>
+  </plugin-info>
+
+  <jql-function name="Recent Project Function" i18n-name-key="recent-project-function.name" key="recent-project-function" class="com.example.plugins.tutorial.jira.jql.RecentProjectFunction">
+    <fname>recentProjects</fname>
+    <description key="recent-project-function.description">Provides a JQL function to restrict the search to recently visited projects.</description>
+    <list>true</list>
+  </jql-function>
+    
+  <resource type="i18n" name="i18n" location="atlassian-plugin"/>
+</atlassian-plugin>