FishEye Twitter Integration Plugin Tutorial

About the tutorial

The plugin created in this tutorial sends each of your commit messages to your Twitter account.

In this tutorial you will:

  1. Create a FishEye/Crucible plugin.
  2. Add an entry in settings menu for users to set properties.
  3. Set up a git repository.
  4. Create a listener that will post to Twitter on every commit.

 The tutorial teaches you how to:

  • Get the output of your servlet 'decorated' so that it has the correct headers and footers.
  • Use a servlet to add a new pane to the user settings dialog.
  • Render your servlet output using template engine.
  • Write an event listener to listen for commit events.
  • Use a Spring Component module to provide a service to several of your plugin modules.
  • Depend on a 3rd party library.


We encourage you to work through this tutorial.
If you want to skip ahead or check your work when you are done, you can find the plugin source code on Atlassian Bitbucket. Bitbucket serves a public Git repository containing the tutorial's code. To clone the repository, run:

$ git clone

Alternatively, you can download the source using the 'Downloads' page here:

Stage 1: Create plugin structure

For more detail on the initial setup of the SDK, and the first steps below, see Developing with the Atlassian Plugin SDK.


First, create your plugin skeleton:

$ atlas-create-fecru-plugin
Define value for groupId: : com.example.ampstutorial
Define value for artifactId: : fecrutwitter
Define value for version:  1.0-SNAPSHOT: :               # just accept the default
Define value for package:  com.example.ampstutorial: :   # again, just press enter for the default

Fisheye/Crucible version

atlas-create-fecru-plugin might create a skeleton with a different version of Fisheye/Crucible than you want to use. Update the properties in pom.xml


Java version

Update to use Java 8  (also in  pom.xml ) :


To make sure your maven uses the correct Java version run:

$ atlas-mvn -version | grep Java

slf4j dependency

Fix the following dependency (also in pom.xml):


Stage 2: Run FishEye/Crucible

Run FishEye/Crucible

Now run FishEye/Crucible with the skeleton plugin:

$ cd fecrutwitter
$ atlas-run

The first time you run it, you'll need to wait a while for files to download. When the application is ready, you'll see in the console:

[INFO] Type Ctrl-D to shutdown gracefully
[INFO] Type Ctrl-C to exit

You can use atlas-debug to enable remote debugging.

Add a user in the FishEye/Crucible admin panel

Go to http://localhost:3990/fecru/admin/, giving the administrator password 'password'. You should see the administrative panel. Go to User Settings > Users on the left, then click Add user on the right. Add yourself as a user to your FishEye/Crucible instance. The e-mail address must match the one you set in Git, so that we can match commits with Fisheye/Crucible users.

Now click Log in on the right to access your account.

Reload the plugin

The Atlassian Plugin SDK allows you to reload your plugin without restarting FishEye/Crucible, which will speed up our work in the following sections:

  1. Open a new terminal window and cd to fecrutwitter
  2. Run atlas-cli
  3. Type pi at the atlas-cli maven2> prompt
  4. Refresh your browser. 

Keep your atlas-cli console open.

Stage 3: Profile settings servlet setup

At this point open the project in your IDE.

Servlet skeleton

Create a new class TwitterSettingServlet which will let you set Twitter credentials from the admin panel.
package com.example.ampstutorial;

import javax.servlet.http.HttpServlet;

public class TwitterSettingsServlet extends HttpServlet {

Http GET: Hello world

Let's start with very simple output. Override method doGet:
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Hello world!");

New entry in Profile settings

To make your servlet visible in the Profile settings menu, add it to atlassian-plugin.xml

<!-- Load TwitterSettingsServlet -->
<servlet key="twitterSettingsServlet" class="com.example.ampstutorial.TwitterSettingsServlet">

<!-- Make twitter-settings servlet visible in profile settings menu -->
<web-item key="config-link" section="">
	<label key="Twitter Configuration"/>

Install the servlet with pi and go to http://localhost:3990/fecru/. Expand the top-rightmost menu and choose Profile settings. You should now see Twitter Configuration on the list. Click it to get your "Hello world!" message. Notice that the menu disappeared.

Decorate servlet with decorator

Add the following lines at the beginning of the servlet's  doGet  method:
request.setAttribute("decorator", "");

Remember to use pi each time to reload the servlet.

Refresh the page and check that after clicking Twitter Configuration the menu on the left no longer disappears.

Menu item highlighting

The Twitter Configuration item is not highlighted correctly when selected. That's because the decorator can't identify the link belonging to the active tab. We need to set the  attribute to the key of the Web Item. Replace the plain "Hello world!" string with:
	"<html><head>" +
	"<meta name='' content='com.example.ampstutorial.fecrutwitter:config-link'/>" +
	"</head><body><div>Hello world!</div></body></html>");

com.example.ampstutorial.fecrutwitter:config-link  is the plugin key (from the  <atlassian-plugin>  element) and the key of the Web Item, separated by a colon.

Now the correct text should be highlighted when you click on the Twitter Configuration link.


Your servlet can use a templating library to produce HTML. FishEye/Crucible provide some utility classes to help you use  Velocity , but you could use other libraries too.

Add TemplateRenderer dependency


Add TemplateRenderer component

<!-- Import the template renderer -->
<component-import key="templateRenderer" interface="com.atlassian.templaterenderer.TemplateRenderer" />

Create basic template

In the resources directory create the "Hello world!" template:

	<meta name='' content='com.example.ampstutorial.fecrutwitter:config-link'/>
Hello world!

Inject template renderer
import com.atlassian.templaterenderer.TemplateRenderer;
	private final TemplateRenderer templateRenderer;
	public TwitterSettingsServlet(TemplateRenderer templateRenderer) {
		this.templateRenderer = templateRenderer;

Use template

In doGet replace the response you had so far with:
	templateRenderer.render("/templates/twitterSettings.vm", ImmutableMap.of(), response.getWriter());

Stage 4 Twitter access configuration

Register the app in Twitter

To access the Twitter Platform, you need to obtain a Consumer Key/Secret pair and an Access Token/Secret pair. Every user who wants his commits to be tweeted needs to do this.

Go to and select Create new app. Once done, open the Keys and Access Tokens tab, then click Create my access token.

Debug: check Twitter access

Once you have the 4 authentication strings, you might want to check if they are correct and if they truly let you post to Twitter. You can do so with this debug code:
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.conf.PropertyConfiguration;

import java.util.Properties;

 * Little app to ensure authorisation works and you can publish tweets to Twitter.
 * First use {@link TwitterTokenGenerator} to obtain access token/secret pair. Then provide this script with the consumer
 * key/secret and access token/secret. Lastly, type in the twitter message you want to post.
public class TwitterStatusUpdater {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(;

        // Consumer key + consumer secret
        print("Enter consumer key:");
        String consumerKey = br.readLine();
        print("Enter consumer secret:");
        String consumerSecret = br.readLine();

        // Access token + access token secret
        print("Enter access token:");
        String token = br.readLine();
        print("Enter access token secret:");
        String tokenSecret = br.readLine();

        TwitterFactory twitterFactory = new TwitterFactory(new PropertyConfiguration(new Properties() {{
            put("oauth.consumerKey", consumerKey);
            put("oauth.consumerSecret", consumerSecret);
            put("oauth.accessToken", token);
            put("oauth.accessTokenSecret", tokenSecret);

        Twitter twitter = twitterFactory.getInstance();

        print("Enter your Twitter status update:");
        String twitterUpdate = br.readLine();
        print("No exceptions, so your update seems successful");

    private static void print(String x) {

Bean of Twitter data

We'd like to store the 4 authentication strings as one object:
public class TwitterLoginRecord {

    private final String consumerKey;
    private final String consumerSecret;
    private final String token;
    private final String tokenSecret;

    public TwitterLoginRecord(String consumerKey,
                              String consumerSecret,
                              String token,
                              String tokenSecret) {
        this.consumerKey = consumerKey;
        this.consumerSecret = consumerSecret;
        this.token = token;
        this.tokenSecret = tokenSecret;

    public String getConsumerKey() {
        return consumerKey;

    public String getConsumerSecret() {
        return consumerSecret;

    public String getTokenSecret() {
        return tokenSecret;

    public String getToken() {
        return token;

Servlet for Twitter data input

We need to change the "Hello world!" servlet to become a form for typing in the data that will become TwitterLoginRecord. First declare the variable-class mapping on the first line of the template:

#* @vtlvariable name="loginRecord" type="com.example.ampstutorial.TwitterAccessTokenRecord" *#

Then change the body to become a form:

    <form action="./twitter-settings" method="post">
        <table class="dialog-prefs" cellspacing="0">
                <th colspan="2"><h3>Twitter Settings</h3></th>
                <td class="tdLabel"><label for="consumerKey" class="label">Consumer key:</label></td>
                <td><input type="text" name="consumerKey" value="${loginRecord.consumerKey}" id="consumerKey"/></td>
                <td class="tdLabel"><label for="consumerSecret" class="label">Consumer secret:</label></td>
                <td><input type="password" name="consumerSecret" value="${loginRecord.consumerSecret}" id="consumerSecret"/></td>
                <td class="tdLabel"><label for="token" class="label">Token:</label></td>
                <td><input type="text" name="token" value="${loginRecord.token}" id="token"/></td>
                <td class="tdLabel"><label for="tokenSecret" class="label">Token secret:</label></td>
                <td><input type="password" name="tokenSecret" value="${loginRecord.tokenSecret}" id="tokenSecret"/></td>
                <td class="action"><input type="submit" value="Save"/></td>

Note that action="./token-servlet" specifies where the form will post the collected data.

Save data from the servlet post

To accept the data posted from the form, override the doPost method:
import static;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String consumerKey = request.getParameter("consumerKey");
        String consumerSecret = request.getParameter("consumerSecret");
        String token = request.getParameter("token");
        String tokenSecret = request.getParameter("tokenSecret");

        if (       !isNullOrEmpty(consumerKey)
                && !isNullOrEmpty(consumerSecret)
                && !isNullOrEmpty(token)
                && !isNullOrEmpty(tokenSecret)) {
            storeLoginRecord(consumerKey, consumerSecret, token, tokenSecret);

Note how the parameter names match those in the twitterSettings.vm form.

Store TwitterLoginRecords

For the purposes of this tutorial, the username and password are just stored in a  Map  in a field of the servlet – this is not persistent. To learn how to save configuration data for your plugins see  Storing Plugin Settings for FishEye .

We'll use TwitterLoginRecordStore interface, so that we can easily switch to a persistent store in the future.
public interface TwitterLoginRecordStore {

    TwitterLoginRecord get(String userName);
    void put(String userName, TwitterLoginRecord record);
import java.util.HashMap;
import java.util.Map;

public class TwitterLoginRecordStoreImpl implements TwitterLoginRecordStore {

    private final Map<String, TwitterLoginRecord> store = new HashMap<>();

    public TwitterLoginRecord get(String userName) {
        return store.get(userName);

    public void put(String userName, TwitterLoginRecord record) {
        store.put(userName, record);

Add the TwitterLoginRecordStore component

<component key="twitterLoginRecordStore" class="com.example.ampstutorial.TwitterLoginRecordStoreImpl" public="true">

Inject and use TwitterLoginRecordStore
private final TwitterLoginRecordStore twitterLoginRecordStore;
private final TemplateRenderer templateRenderer;

public TwitterSettingsServlet(TwitterLoginRecordStore twitterLoginRecordStore, TemplateRenderer templateRenderer) {
	this.twitterLoginRecordStore = twitterLoginRecordStore;
	this.templateRenderer = templateRenderer;
private void storeLoginRecord(String consumerKey, String consumerSecret, String token, String tokenSecret) {
    new TwitterLoginRecord(consumerKey, consumerSecret, token, tokenSecret));

Get the current user login

We use the user login as the key in the store. To get the current user login, we use EffectiveUserProvider. Add a new dependency:


Inject the user provider to the servlet and use it:
import com.atlassian.fecru.user.EffectiveUserProvider;

	private final EffectiveUserProvider effectiveUserProvider;

	public TwitterSettingsServlet(EffectiveUserProvider effectiveUserProvider,
                                  TwitterLoginRecordStore twitterLoginRecordStore,
                                  TemplateRenderer templateRenderer) {
		this.effectiveUserProvider = effectiveUserProvider;
		this.twitterLoginRecordStore = twitterLoginRecordStore;
		this.templateRenderer = templateRenderer;
	private String getCurrentUser() {
		return effectiveUserProvider.getEffectiveUserLogin().getUserName();

Load TwitterLoginRecord to fill the form

Once saved, we want to show the data in the form. To fill the form template, we need to modify the get method:
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("decorator", "");

		TwitterLoginRecord loginRecord = twitterLoginRecordStore.get(getCurrentUser());
			loginRecord != null ? ImmutableMap.of("loginRecord", loginRecord) : ImmutableMap.of(),

Stage 5: Set up the Git repository

New Git repository

The goal of this tutorial is to send tweets for every commit. To do that, we need a repository to commit to. We will use Git for this tutorial. We only need a local repository.

$ mkdir repo_for_tutorial
$ cd repo_for_tutorial
$ git init
Initialized empty Git repository in .../repo_for_tutorial/.git/

Add a repo to FishEye/Crucible

Open the admin panel (http://localhost:3990/fecru/admin), go to Repository Settings > Repositories. Click Add repository, select Git and provide the local path to your Git repository. Once added, select the new repository and click Browse repository. You should see your activity, such as changes to the a.txt file (see below).

Hint: quick command to generate a commit

To speed up the flow of your testing, you can use this command to modify your file and commit in one go:

$ echo $(date) >> a.txt && git add a.txt && git commit -m "tweet $(date)"

Stage 6: Listen for commits and post to Twitter

CommitListener skeleton

To listen for commits, you need to register yourself at EventPublisher and annotate at least one method with @EventListener. Choose the type of events you are interested in by specifying the type of the annotated method parameter.

Use InitializingBean and DisposableBean interfaces to register/unregister your listener at the right point of application startup/shut down.
import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.fisheye.event.CommitEvent;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class CommitListener implements InitializingBean, DisposableBean {

    public CommitListener(EventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
    public void afterPropertiesSet() throws Exception {

    public void destroy() throws Exception {

    public void handleEvent(CommitEvent ce) {

To install the listener, configure a new component:

    <component key="commit-listener" class="com.example.ampstutorial.CommitListener" public="true">

Use logger

When something goes wrong, it's a good idea to log, so that it's easier to investigate the root cause. Let's add a logger:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
    private static final Logger LOGGER = LoggerFactory.getLogger(CommitListener.class); 

Work with CommitEvent

New components for working with commits

We need two components to work with CommitEvents: CommitterUserMappingManager to map from git committer to FishEye/Crucible user and RevisionDataService to get changeset data from changeset id. Both of these are available by default and you don't need to install them in atlassian-plugin.xml. We also need a third component - TwitterLoginRecordStore that we already have.
import com.cenqua.fisheye.model.manager.CommitterUserMappingManager;
    private final EventPublisher eventPublisher;
    private final CommitterUserMappingManager committerUserMappingManager;
    private final RevisionDataService revisionDataService;
    private final TwitterLoginRecordStore twitterLoginRecordStore;

    public CommitListener(EventPublisher eventPublisher,
                          CommitterUserMappingManager committerUserMappingManager,
                          RevisionDataService revisionDataService,
                          TwitterLoginRecordStore twitterLoginRecordStore) {
        this.eventPublisher = eventPublisher;
        this.committerUserMappingManager = committerUserMappingManager;
        this.revisionDataService = revisionDataService;
        this.twitterLoginRecordStore = twitterLoginRecordStore;

Handle events

Update handleEvent method to get user login from (repository, committer) tuple. Check if user has configured Twitter access. If yes, post to Twitter:
    public void handleEvent(CommitEvent ce) {
        String repositoryName = ce.getRepositoryName();
        ChangesetDataFE csData = revisionDataService.getChangeset(repositoryName, ce.getChangeSetId());
        if (csData != null) {
            String commitAuthor = csData.getAuthor();
            User user = committerUserMappingManager.getUserForCommitter(repositoryName, commitAuthor);
            if (user != null) {
                TwitterLoginRecord loginRecord = twitterLoginRecordStore.getForUser(user.getUsername());
                if (loginRecord != null) {
                    postToTwitter(loginRecord, csData);
                } else {
          "Twitter account not set for user {}", user);
            } else {
      "Can't find a user name for commit author {} in repository {}", commitAuthor, repositoryName);
        } else {
            LOGGER.warn("Failed to find changeset data for {} in repository {}", changeSetId, repositoryName);

Post to Twitter

Add a dependency on external library that provides Twitter API:


See for library docs.

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.PropertyConfiguration;
    private void postToTwitter(TwitterLoginRecord loginRecord, ChangesetDataFE csData) {
        try {
            // TwitterFactory can be configured only via PropertyConfiguration
            Properties props = new Properties();
            props.put("oauth.consumerKey", loginRecord.getConsumerKey());
            props.put("oauth.consumerSecret", loginRecord.getConsumerSecret());
            props.put("oauth.accessToken", loginRecord.getToken());
            props.put("oauth.accessTokenSecret", loginRecord.getTokenSecret());
            TwitterFactory twitterFactory = new TwitterFactory(new PropertyConfiguration(props));

            Twitter twitter = twitterFactory.getInstance();
        } catch (TwitterException exception) {
            LOGGER.error("Failed posting to Twitter", exception);

Test your plugin

Commit something to the test repository (you can use the short script given in the previous section) and watch your plugin post the commit message to Twitter. Congratulations, you finished the task!

Was this page helpful?

Have a question about this article?

See questions about this article

Powered by Confluence and Scroll Viewport