Skip to end of metadata
Go to start of metadata

The source code of the plugin used in this tutorial is available in the Atlassian public source repository. You can check out the source code here.

Level of experience: Beginner

Icon

Our tutorials are classified as 'beginner', 'intermediate' and 'advanced'. This one is at 'beginner' level, so you can follow it even if you have never developed a plugin or gadget before.

On this page:

Overview

This tutorial shows you how to write a gadget that lists the most recent changes which have been committed to the repositories monitored by a Fisheye instance.

What You Will Learn

After completing this tutorial you will know how to:

  • Create a gadget to be hosted by Fisheye and displayed on the JIRA dashboard.
  • Use FishEye and Crucible's built in REST services from a Gadget.

Assumed Knowledge

  • Understanding of what a gadget is and the relationship between the dashboard a gadget is displayed on and the host the gadget is served from.
  • Familiarity with writing Javascript.
  • Familiarity with the jQuery library.

Your gadget will be a 'plugin' gadget. That means that it will be embedded within an Atlassian plugin. The plugin will consist of the following parts:

  • An Atlassian plugin descriptor, that is an XML file enabling the plugin in FishEye.
  • A gadget specification, that is an XML file containing the gadget's HTML, CSS and Javascript.

All these components will be contained within a single JAR file. We will discuss each component in the examples below.

If you are interested, you can compare standalone gadgets and gadgets embedded in plugins.

Step 1. Create the Plugin Project

Use the appropriate atlas-create-application-plugin command to create your plugin. For example, atlas-create-jira-plugin or atlas-create-confluence-plugin.

When you run atlas-create-fecru-plugin you'll be asked to supply a groupId and an artifactId – use com.atlassian.tutorial.fisheye and recentchangesgadget respectively. When you create a plugin of your own, you should use a groupId based on your company's domain.

Icon

You need to make sure that your project is using FishEye 2.2.0 or later. Edit your pom.xml and set the fecru.version property to the build number of the version of FishEye you want to use, e.g.:

Step 2. Customise the plugin Vendor and Description in your pom.xml

Here are the parts of pom.xml which you may wish to change:

You should change the organization name and url attributes to those of your company, and the description to a description of your plugin.

These values are copied to your atlassian-plugin.xml plugin descriptor file, and are displayed on the plugin administration page of FishEye when your plugin is loaded.

Step 3. Create the Gadget Specification

Here is a basic gadget specification using the Atlassian Gadgets JavaScript Framework:

Copy the XML code from the above gadget specification and put it in a new file named gadget.xml in the src/main/resources directory.

Step 4. Register the Plugin Module in the Plugin Descriptor

  1. Add the <gadget> the plugin module to your plugin descriptor, atlassian-plugin.xml.
  2. Follow these steps to build and install your plugin, so that you can test your code. If you have not already started the application, start it now:
    • Open a command window and go to the plugin root folder (where the pom.xml is located).
    • Run atlas-run (or atlas-debug if you might want to launch the debugger in your IDE).

    From this point onwards, you can use FastDev to reinstall your plugin behind the scenes as you work.

    To trigger the reinstallation of your plugin:
    1. Make the changes to your plugin module.
    2. Open the Developer Toolbar.
    3. Press the FastDev icon.

      The system rebuilds and reloads your plugin:

    Use live reload to view real-time updates to templates and other resources:

    1. Open the Developer Toolbar.
    2. Press the live reload icon.
      The  icon starts to revolve indicating it is on.
    3. Edit your project resources.
    4. Save your changes:
      Back in the host application, your plugin displays any user visible changes you make. 

    Go back to the browser. The updated plugin has been installed into the application, and you can test your changes.

    The full instructions are in the SDK guide.

Step 5. Add the Gadget to a Dashboard for Testing

The gadget you are creating in this tutorial will be 'served' by a FishEye instance, but 'hosted' on a JIRA dashboard. That is, the HTTP requests which serve the plugin descriptor you created above, and the plugin's data and resources will go to a FishEye instance in which the plugin is installed, but the gadget will be displayed on a JIRA dashboard.

You will need an instance of JIRA on which you have administrative privileges. This JIRA instance must also be able to 'see' the FishEye instance serving the plugin. A JIRA instance outside your firewall will not be able to see a FishEye instance running on your desktop development machine without special network configuration, for instance.

To add the plugin you will need to:

  1. Start up your FishEye instance with the plugin SDK command atlas-run. This builds your plugin and starts a FishEye instance with the plugin installed in it.
  2. Go to http://localhost:3990/fecru/admin/viewplugins.do and check that your plugin has loaded successfully. You should see this:
  3. Add the plugin to your JIRA dashboard:
    1. Log in as an administrator.
    2. Select Tools, Create Dashboard... and create a new, blank dashboard.
    3. When your new dashboard displays, click 'add a new gadget'
    4. Click the 'Add Gadget to Directory' button and type http://localhost:3990/fecru/rest/gadgets/1.0/g/com.atlassian.tutorial.fisheye.recentchangesgadget:gadget/gadget.xml and click 'Add Gadget'.
    5. Your gadget will appear highlighted in yellow. Clock the 'Add it Now' button, and then the 'Finished' button.
  4. You should see a new gadget containing the text 'Hello World' appear on your dashboard.
Icon

If you see the error message Error loading gadget: org.apache.shindig.gadgets.GadgetException: Unable to retrieve gadget xml. HTTP error 504 try stopping and starting JIRA.

Icon

JIRA sometimes caches your gadget, so you'll need to restart JIRA when you change your gadget.xml file, otherwise you won't see your changes.

Step 6. Make the Gadget Do Something Useful

Now you will write the JavaScript and HTML code to retrieve recent changes data from FishEye and display the information in the gadget. As you can see below, using the built-in REST interfaces of FishEye to retrieve the information we want for this gadget involves many REST calls. If this did not perform satisfactorily you would need to consider writing your own REST endpoint to retrieve exactly the data you need in a single call. See this tutorial for an example of creating your own REST endpoint.

Because your gadget is embedded in a plugin, you can use the Atlassian Gadgets JavaScript Framework in addition to the OpenSocial JavaScript API.

Clear the Current Contents of the Gadget

We are going to build up the HTML displayed in the gadget as we go, so first we will remove its contents:

gadget.getView() returns a jQuery object representing the body of the Gadget. We replace any existing contents with a div which will contain all our content, and a h1 containing our heading.

Get the list of repositories

First the gadget retrieves a list of all the repositories in the FishEye instance.

Some things to notice in the code above:

  1. We are retrieving data from the REST endpoint as XML, and using jQuery to parse it. The Atlassian Gadgets Javascript framework prefixes jQuery with AJS, so the jQuery $ function name becomes AJS.$.
  2. The reportError function uses the Firebug console to report errors.
  3. Each repository name is passed to the loadChangesetsForRepo function, which we will write in the next step.

Find the Recent Changesets for Each Repository

Now we make another REST call for each repository, getting the recent changesets from the repository:

  1. The first thing this function does is to use jQuery to create a div to hold the changesets from this repository. The div is added as a child element to the div with the class main which was created at the start.
  2. The function uses jQuery to find each csid element in the XML response. Each csid value is passed to the addChangesetData function to load the details for that changeset.

Add the HTML for a Changeset

For each changeset, we make a REST call to get its details – in this tutorial we only display the id and the comment.

Step 7. Build, Install and Run the Plugin

Follow these steps to build and install your plugin, so that you can test your code. If you have not already started the application, start it now:
  • Open a command window and go to the plugin root folder (where the pom.xml is located).
  • Run atlas-run (or atlas-debug if you might want to launch the debugger in your IDE).

From this point onwards, you can use FastDev to reinstall your plugin behind the scenes as you work.

To trigger the reinstallation of your plugin:
  1. Make the changes to your plugin module.
  2. Open the Developer Toolbar.
  3. Press the FastDev icon.

    The system rebuilds and reloads your plugin:

Use live reload to view real-time updates to templates and other resources:

  1. Open the Developer Toolbar.
  2. Press the live reload icon.
    The  icon starts to revolve indicating it is on.
  3. Edit your project resources.
  4. Save your changes:
    Back in the host application, your plugin displays any user visible changes you make. 

Go back to the browser. The updated plugin has been installed into the application, and you can test your changes.

The full instructions are in the SDK guide.

Congratulations, that's it

Icon

Your gadget is complete. Have a chocolate!