Skip to content

WebHook Scripting GitHub

Gabriel Engel edited this page Mar 8, 2016 · 5 revisions

We can do 2 types of integrations with GitHub:

  1. Receive alerts from GitHub (Incoming WebHook)
  2. Send commands to GitHub and optionally receive a response (Outgoing WebHook)

Receive alerts

  • Create a new incoming webhook
  • Select the channel were you will receive the alerts
  • Use this script to receive alerts from new issues, closed issues and comments
if (headers['x-github-event'] === 'ping') {
  return {
    content: {
      text: ':thumbsup: ' + content.zen
    }
  };
}

if (headers['x-github-event'] === 'issues' && content.action === 'opened') {
  var attachment = {
    color: 'red',
    author_name: 'by ' + content.issue.user.login,
    author_link: content.issue.user.html_url,
    author_icon: content.issue.user.avatar_url,
    fields: []
  };
  if (content.issue.labels) {
    var labels = [];
    content.issue.labels.forEach(function(label) {
      labels.push(label.name);
    });
    labels = labels.join(', ');
    attachment.fields.push({
      title: 'Labels',
      value: labels,
      short: labels.length <= 40
    });
  }
  if (content.issue.assignee) {
    attachment.fields.push({
      title: 'Assignee',
      value: content.issue.assignee.login,
      short: true
    });
  }
  return {
    content: {
      text: ':triangular_flag_on_post: Issue created [#' + content.issue.number + ' - ' + content.issue.title + '](' + content.issue.html_url + ')',
      attachments: [attachment]
    }
  };
}

if (headers['x-github-event'] === 'issues' && content.action === 'closed') {
  var attachment = {
    author_name: 'by ' + content.sender.login,
    author_link: content.sender.html_url,
    author_icon: content.sender.avatar_url
  };
  return {
    content: {
      text: ':white_check_mark: Issue closed [#' + content.issue.number + ' - ' + content.issue.title + '](' + content.issue.html_url + ')',
      attachments: [attachment]
    }
  };
}

if (headers['x-github-event'] === 'issue_comment' && content.action === 'created') {
  var attachment = {
    text: content.comment.body,
    author_name: 'by ' + content.comment.user.login,
    author_link: content.comment.user.html_url,
    author_icon: content.comment.user.avatar_url
  };
  return {
    content: {
      text: ':speech_balloon: Comment on issue [#' + content.issue.number + ' - ' + content.issue.title + '](' + content.issue.html_url + ')',
      attachments: [attachment]
    }
  };
}

return {
  error: {
    success: false,
    message: 'Unsupported method'
  }
};
  • Save the integration
  • Go to your repository settings -> webhooks & services -> add webhook
  • Paste your Webhook URL from Rocket.Chat into Payload URL
  • Keep Contenty type as application/json
  • Leave Secret empty and save

Send commands to GitHub

This script only works for public repositories

  • Create a new outgoing webhook
  • Select the channel where you will use the commands and receive the responses
  • Set URLs as https://api.github.com/repos/User-Or-Org-Name/Repo-Name like https://api.github.com/repos/RocketChat/Rocket.Chat
  • Use this Prepare Outgoing Request script to listen for commands pr ls, pr list and help
var match = request.data.text.match(/^pr\s(ls|list)\s*(open|closed|all)?$/);
if (match) {
  var u = request.url + '/pulls';
  if (match[2]) {
    u += '?state='+match[2];
  }
  return {
    url: u,
    headers: request.headers,
    method: 'GET'
  };
}

var match = request.data.text.match(/^help$/);
if (match) {
  return {
    message: {
      text: [
        '**GitHub commands**',
        '```',
          '  pr ls|list [open|closed|all]  List Pull Requests',
        '```'
      ].join('\n')
    }
  };
}
  • And this Process Outgoing Response to get the response and format for Rocket.Chat
var text = [];
response.content.forEach(function(pr) {
	text.push('> '+pr.state+' [#'+pr.number+']('+pr.html_url+') - '+pr.title);
});

return {
	content: {
		text: text.join('\n'),
		parseUrls: false
	}
};
  • Save your integration
Clone this wiki locally