Custom Event Listener

Adding a custom listener

  1. Navigate to General Configuration > ScriptRunner > Event Listeners
  2. Choose the Custom Event Listener to use your own scripts to respond to events.

  3. Enter an optional Name to identify your script. 

  4. Enter a Script, either Inline or from a File.
  5. Select Event(s) for the listener to fire on.
  6. Select Add.
    You can also Preview to view your results before saving the listener.

Writing code for custom event listeners

Script binding

The script binding is a set of variables you can use in your custom script. An event variable in the script binding corresponds to the event which triggered the listener. For example, if you are listening for PageCreateEvent the event variable will be a PageCreateEvent object.

Multiple Events

You can choose to have your handler listen for multiple events. If you need to do different things depending on the type of event, you can check that with instanceof. Alternatively, you can type the event variable to the most specific superclass. In the previous example, that would be PageEvent.

In the following example, the event is getting page content for both PageCreateEvent and PageUpdateEvent. Since PageEvent is a common superclass for both PageCreateEvent and PageUpdateEvent, you could cover both events by using the following code:

Include Bitbucket for Confluence: An error occured

Failed to connect to Bitbucket. Check the Application Link for Bitbucket Data Center or the permissions of the app password for Bitbucket Cloud. Error details: 503:

Examples

Add a comment when a banned word is used

Some organizations have a particular style guide or would like to enforce specific rules. This event listener example looks at the content of new pages for banned words. If the page content contains any on a list of banned words, a comment is automatically added with an alternative suggestion.

Follow these steps to create the listener:

  1. Navigate to General Configuration > ScriptRunner > Event Listeners
  2. Select Custom Event Listener.
  3. Enter Add a comment when a banned word is used for Name
  4. Enter the following Script

    Include Bitbucket for Confluence: An error occured

    Failed to connect to Bitbucket. Check the Application Link for Bitbucket Data Center or the permissions of the app password for Bitbucket Cloud. Error details: 503:
  5. Leave Events blank because the event is defined in the code.
  6. Select Add.

Result

The following image contains a comment generated in response to a banned word:

In practice, you would also want to watch page updates and only look at the diff between old and new versions.

Add an inline comment when a banned word is used

Similar to the previous example, you can configure your listener to add inline comments instead. As you get these comments, you can dismiss them.

Follow these steps to create the listener:

  1. Navigate to General Configuration > ScriptRunner > Event Listeners
  2. Select Custom Event Listener.
  3. Enter Add an inline comment when a banned word is used for Name
  4. Enter the following Script

    Include Bitbucket for Confluence: An error occured

    Failed to connect to Bitbucket. Check the Application Link for Bitbucket Data Center or the permissions of the app password for Bitbucket Cloud. Error details: 503:
  5. Leave Events blank because the event is defined in the code.
  6. Select Add.

Result

The following image is an example of an inline comment:

Create a page in a space when a user is created

This event listener example automatically creates a user profile page in the Team space. You can use this profile page to list their skills and profile.

Follow these steps to create the listener:

  1. Navigate to General Configuration > ScriptRunner > Event Listeners
  2. Select Custom Event Listener.
  3. Enter Create a page in space when a user is created for Name
  4. Enter the following Script

    Include Bitbucket for Confluence: An error occured

    Failed to connect to Bitbucket. Check the Application Link for Bitbucket Data Center or the permissions of the app password for Bitbucket Cloud. Error details: 503:
  5. Pick UserCreateEvent for Events.
  6. Select Add.

Result

The following image is an example of a profile page created when the jbloggs user is created:

Collect stats

This event listener example automatically sends statistics to statsd for page views, space views, and users/pages views.

Follow these steps to create the listener:

  1. Navigate to General Configuration > ScriptRunner > Event Listeners
  2. Select Custom Event Listener.
  3. Enter Collect stats for Name
  4. Enter the following Script

    Include Bitbucket for Confluence: An error occured

    Failed to connect to Bitbucket. Check the Application Link for Bitbucket Data Center or the permissions of the app password for Bitbucket Cloud. Error details: 503:
  5. Pick PageViewEvent for Events.
  6. Select Add.

Result

The following image is an example of the statistics for page views:

In the example, Grafana is used to visualize page views metrics.
On this page