Hooking Gradle build scans notifications to Gitter

Recently I posted on the benefits of enabling build scans on your project, as you may appreciate in the screenshots you get a link to the build scan’s URL as a result when the build is run. This URL is build specific and only those that have access to the build log will be able to see the link. What if you wanted to share the link with others? There are many ways to share information about a project hosted on GitHub/Gitlab/Bitbucket, one of them happens to be Gitter.im.

Gitter.im is a messaging system that connects with GitHub repositories, bringing the IRC experience to the era of social coding. Gitter already provides integration with popular services as shown in the next screenshot.

It also gives you the choice to register a Custom integration when a service is not available out of the box, and this is precisely the hook we’ll use to get build scan notifications on a Gitter channel. As it turns out, the Gradle build scan feature is configurable and extensible; of the several values you may query is the scan’s URL as shown here. We’ll use the value of the buildScanUri property to configure a new Custom webhook in Gitter, which as it happens, it quite straightforward:

  • Log into Gitter using your Github/Gitlab/Bitbucket credentials.
  • Locate the repository you’d like to connect.
  • Click on Settings -> Integrations.
  • Select a “Custom” integration.

This should result in a screen like the following one

This screen shows two things: how the service may be configured to send data to it, and the URL that must be used to talk to the service. With this information in hand we can add a few lines of code to a Gradle build file, for example

plugins {
    id 'com.gradle.build-scan' version '2.1'
}

buildScan {
    licenseAgreementUrl = 'https://gradle.com/terms-of-service'
    licenseAgree        = 'yes'

    buildFinished { buildResult ->
        buildScanPublished { scan ->
            ['curl', '-s', '-d', "message=Build scan: ${scan.buildScanUri}", 'https://webhooks.gitter.im/e/*************'].execute()
        }
    }
}

The link is obfuscated in this example, you just have to use the actual link as shown in the Gitter settings window. Once this step is done there’s only a matter of testing it out, by simply running a build (locally) or if you have your build setup on a CI service. Here’s a short video of this feature integrated on the JDeferred project

Now everyone on the room we’ll see the notifications when a new build scan is available. The link to the build scan shown above is https://gradle.com/s/qjuexgiedrb52 if you’re curious to find out what kind of information becomes available when build scans are enabled on a project.

Keep on coding!

Trackbacks/Pingbacks

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

ˆ Back To Top