Requirement: Display a list of all the users on the project dashboard.

Source code available at https://bitbucket.org/jiradev-tutorials/project-tab-panel

  1. Create a plugin skeleton and clean up the atlassian-plugin.xml file. Again, we only need the i18n resource module.

    • groupId: com.jiradev.jira.plugins
    • artifactId: user-role-project-tab
    • version: 1.0
    • Define value for package: com.jiradev.jira.plugins.panels.project ( this is the first time we are defining a value for a package. We are using this package because we are going to bundle another plugin within the same project in the next tutorial. You will know why when you go through the next tutorial)

  2. Run the atlas-create-jira-plugin-module command on the project. Select option 13 for Project Tab Panel module.

    • Use Generic Project Tab Panel: N (we need our own class to retrieve all users and implement the functionality we require)
    • Enter new class name: UserRoleProjectTabPanel
    • Enter package name: com.jiradev.jira.plugins.panels.project
    • Show advanced setup: N
    • Add another plugin module: N

  3. Open the project in your IDE and take a look at the atlassian-plugin.xml file. Notice that the project-tabpanel has a resource of type velocity defined. This file is located in your project's resource/templates/tabpanels folder. Edit this file and enter the text <h1>Tweet for JIRADEV.com</h1>

    Also notice you src directory. The SDK has created a new java class named UserRoleProjectTabPanel.java in the package com.jiradev.jira.plugins.panels.project. This is where we will be writing code to implement our functionality.

  4. Run JIRA using atlas-run command and create a demo project. Navigate to the project dashboard. And you should now see the output displayed in the below screenshot.



  5. Update the user-role-project-tab-panel.label property in resources/user-role-project-tab.properties files to "Roles".
  6. Great. Let us start writing some code. Update the UserRoleProjectTabPanel.java file. We need to write code to get all the users in the project roles. Take a look at the API https://developer.atlassian.com/static/javadoc/jira/6.0.8/reference/com/atlassian/jira/plugin/projectpanel/impl/AbstractProjectTabPanel.html
  7. Our UserRoleProjectTabPanel.java file already extends the AbstractProjectTabPanel class. We will implement the createVelocityParams method. Below is the code for the UserRoleProjectTabPanel class. I have tried to add as many comments as possible to make it easier for you to understand.
    package com.jiradev.jira.plugins.panels.project;
    
    import com.atlassian.crowd.embedded.api.User;
    import com.atlassian.jira.component.ComponentAccessor;
    import com.atlassian.jira.project.Project;
    import com.atlassian.jira.security.roles.ProjectRole;
    import com.atlassian.jira.security.roles.ProjectRoleActors;
    import com.atlassian.jira.security.roles.ProjectRoleManager;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import com.atlassian.jira.plugin.projectpanel.impl.AbstractProjectTabPanel;
    import com.atlassian.jira.plugin.projectpanel.ProjectTabPanel;
    import com.atlassian.jira.plugin.projectpanel.ProjectTabPanelModuleDescriptor;
    import com.atlassian.jira.project.browse.BrowseContext;
    
    import java.util.Collection;
    import java.util.Map;
    import java.util.TreeMap;
    
    
    public class UserRoleProjectTabPanel extends AbstractProjectTabPanel implements ProjectTabPanel
    {
        private static final Logger log = LoggerFactory.getLogger(UserRoleProjectTabPanel.class);
        private ProjectRoleManager projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class);
        private TreeMap people = new TreeMap();
    
        public Map createVelocityParams (BrowseContext ctx)
        {
            //Get the params object. This will hold all the values that can be accessed in the user-role-project-tab.properties file
            Map params = super.createVelocityParams(ctx);
            //Get the project object
            Project project = ctx.getProject();
            //Get all the project roles
            Collection projectRoles = projectRoleManager.getProjectRoles();
            //Iterate through each role and get the users associated with the role
            for (ProjectRole projectRole : projectRoles){
                ProjectRoleActors roleActors = projectRoleManager.getProjectRoleActors(projectRole, project);
                people.put(projectRole.getName(),roleActors.getUsers());
            }
            params.put("people",people);
            params.put("avatarService",ComponentAccessor.getAvatarService());
            return params;
        }
    
        public boolean showPanel(BrowseContext context)
        {
            return true;
        }
    }
    
    				
  8. Now that we have added all the required objects to the params map, we will have access to it in the velocity file. Let us update the view for the panel. Below is the code in the update-role-project-tab-panel.vm file.
    <div class="module">
    <div class="mod-content">
        <table class="aui">
        #foreach( $entry in $people.entrySet() )
        #if($entry.getValue().size() > 0)
            <tr>
                <td>$entry.getKey()
                <td>
                    <table>
                    #foreach($user in $entry.getValue())
                        <tr>
                            <td>
                                <img height="48px" src="$avatarService.getAvatarAbsoluteURL($user,$user.getName(), $Avatar.Size.SMALL)"/>
                            </td>
                            <td style="padding: 3px;">
                                <a href="${requestContext.baseUrl}/secure/ViewProfile.jspa?name=$user.getName()">$user.getDisplayName()</a>
                                <br />
                                <a href="mailto:$user.getEmailAddress()">$user.getEmailAddress()
                            </td>
                        </tr>
                    #end
                    </table>
                </td>
            </tr>
        #end
        #end
        </table>
    </div>
    </div>
    

Debugging your project

  1. To debug your code, we need to run the plugin in debug mode. Stop the current instance of JIRA (Ctrl+C)
  2. Run JIRA on debug mode using the command atlas-debug
  3. Once JIRA has started, from your IntelliJ Idea IDE, select Run -> Edit Configurations from the top menu.
  4. Click on the plus (+) icon and select "Remote".



  5. Enter a name for your configuration and select Apply and click OK. You have the debugger setup. Add a breakpoint in your UserRoleProjectTabPanel.java file within the createVelocityParams method. Start the debugger by clicking on the Debug icon.



  6. Access your local instance of JIRA and navigate to the project dashboard. Your debugger will be activated. Step through the code and understand what it is doing. Refer to the API for each classes used.

Create a new user and associate him to a role in a project. You should see the list of users in your project tab panel now.




Share


Show your support by tweeting about this tutorial. Is Jiradev something you would reommend? Let me know.

Contribute


Do you have a Jira plugin tutorial that can be used on this site? Please do share it with me and I can add it along with the ones available.

Contribute

Feedback


Your feedback can help improve the content on this site. If you have anything that you would like me to change/implement on the site.