Skip to content

Instantly share code, notes, and snippets.

@fancyremarker
Created November 21, 2014 19:41
Show Gist options
  • Save fancyremarker/c46ea88a876efcd9be1c to your computer and use it in GitHub Desktop.
Save fancyremarker/c46ea88a876efcd9be1c to your computer and use it in GitHub Desktop.
Sync all an org's repos with Slack notifications, specifying individual events to subscribe to
#!/usr/bin/env ruby
# Usage: ruby slack-notifications-sync.rb aptible event [event event ...]
# Supported events:
# * commit_comment
# * create
# * delete
# * deployment
# * deployment_status
# * download
# * follow
# * fork
# * fork_apply
# * gist
# * gollum
# * issue_comment
# * issues
# * member
# * public
# * pull_request
# * pull_request_review_comment
# * push
# * release
# * status
# * team_add
# * watch
require 'octokit'
org_login = ARGV[0]
events = ARGV[1..-1]
print 'Username: '
username = $stdin.gets.chomp
print 'Password: '
password = $stdin.gets.chomp
print 'Hook URL: '
hook_url = $stdin.gets.chomp
Octokit.configure do |c|
c.login = username
c.password = password
end
Octokit.auto_paginate = true
Octokit.org_repos(org_login).each do |repo|
existing_hooks = Octokit.hooks(repo.full_name)
slack_hook = existing_hooks.find { |h| h.config[:url] =~ /hooks.slack.com/ }
next if slack_hook &&
slack_hook.config[:url] == hook_url &&
slack_hook.events.sort == events.sort
# Remove existing hook since it doesn't match
if slack_hook
puts "Removing misconfigured hook on #{repo.full_name}..."
Octokit.remove_hook(repo.full_name, slack_hook.id)
end
# Create new hook
puts "Adding hook on #{repo.full_name}..."
Octokit.create_hook(
repo.full_name,
'web',
{ url: hook_url, content_type: 'json' },
{ events: events, active: true }
)
end
@fancyremarker
Copy link
Author

Hi @aruzmeister, you also need content_type: 'json', as well as the second hash argument:

{ events: events, active: true }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment