Stage 6: Listen for commits and post to Twitter
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.
DisposableBean interfaces to register/unregister your listener at the right point of application startup/shut down.
To install the listener, configure a new component:
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:
Work with CommitEvent
New components for working with commits
We need two components to work with
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.
handleEvent method to get user login from (repository, committer) tuple. Check if user has configured Twitter access. If yes, post to Twitter:
Post to Twitter
Add a dependency on external library that provides Twitter API:
See http://twitter4j.org/ for library docs.
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!