# TravisCI

Add TravisCI notifications via a new WebHook in Rocket.Chat

## Receive alerts

1. Create a new **Incoming WebHook**.
2. Select the channel where you will receive the alerts. You may wish to create a dedicated channel for your notifications.
3. Select an account from which the alerts will be posted. You may wish to create a dedicated account just for notifications.
4. Set the "Enable Scripts" option to `True`.
5. Copy scripts below and paste it in Script field.
6. Save the integration. This will generate a webhook URL and secret for you.
7. In your `.travis.yml` file add WebHooks parameter with webhook URL generated.

```yaml
  notifications:
      webhooks: <webhook_url_generated>
```

This script will generate notifications for the following build events:

* Build finished status for any pushed code
* Build finished status for new Pull Request

![Screenshot of messages generated by TravisCI integration script](https://2853711081-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhHYdqf09DtGwAq3Pa3FU%2Fuploads%2Fgit-blob-377b412cca57df32ed09e6570c9bdd5bcf26df00%2Ftravis-ci.png?alt=media)

*Note:* If status is passed, message color is green otherwise it is red.

```javascript
const buildMessage = (obj) => {
  const min = Math.floor(obj.duration / 60);
  const sec = obj.duration - min * 60;

  let template = `Build [#${obj.number}](${obj.build_url})`;
  template += ` ([${obj.commit.substring(0, 7)}](${obj.compare_url})) of`
  template += ` ${obj.repository.owner_name}/${obj.repository.name}@${obj.branch}`;
  if(obj.pull_request) {
     let pr_url = `https://github.com/${obj.repository.owner_name}/`;
     pr_url += `${obj.repository.name}/pull/${obj.pull_request_number}`;
     template += ` in PR [#${obj.pull_request_number}](${pr_url})`;
  }
  template += ` by ${obj.author_name} ${obj.state} in ${min} min ${sec} sec`;

  let status_color = '#36A64F';
  if(obj.state !== 'passed') {
    status_color = '#A63636'
  }

  return {
    text: template,
    color: status_color
  };
};

class Script {
  process_incoming_request({ request }) {
    msg = buildMessage(request.content);

    return {
      content:{
        attachments: [
          {
            text: msg.text,
            color: msg.color
          }
        ]
      }
    };
  }
}
```

If you want more customizations, please look in TravisCI documentation: <https://docs.travis-ci.com/user/notifications/#Configuring-webhook-notifications>
