Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 5 commits
  • 3 files changed
  • 0 comments
  • 1 contributor
Apr 13, 2012
Nanda Kishore Adding Github-Serivce which alerts a URL when the repo visibility is …
…changed from `Private` to `Public`
af0d55b
Nanda Kishore Functionality related to "Notifying, when a repo visibility is switch…
…ed from Private to Public" is in place. So are the testcases for the total service code.

Testcases related to
017473f
Nanda Kishore Adding more tests ddc5ff7
Nanda Kishore Added more testcases for asserting negative cases d8a147b
Nanda Kishore Removed the unnecessary testcases. Commented the raise_config_error w…
…hen the POST url webservice call fails, as "Public" Event emailing functionality is also provided and that can still be done.
215b7cf
22  docs/public
... ...
@@ -0,0 +1,22 @@
  1
+Public
  2
+========
  3
+
  4
+Install Notes
  5
+-------------
  6
+
  7
+1. `remote_url` (Required) Remote Web-Service Url which listens to `post` sent from github
  8
+2. `auth_token` (Required) Authentication Token to be used by the POST URL
  9
+3. `repo_name` (Required) Repo Name which this Public Hook is associated with
  10
+4. `notify_email` (Optional) Email Address(1) to intimate that Public Event is generated
  11
+
  12
+Developer Notes
  13
+---------------
  14
+
  15
+data
  16
+  - remote_url
  17
+  - auth_token
  18
+  - repo_name
  19
+  - notify_email
  20
+
  21
+payload
  22
+  - empty (refer to http://developer.github.com/v3/events/types/#publicevent)
72  services/pubalert.rb
... ...
@@ -0,0 +1,72 @@
  1
+class Service::PubAlert < Service
  2
+  #
  3
+  # Post to URL when Repo toggles from Private to Public
  4
+  #
  5
+
  6
+  string :remote_url,
  7
+    # (Required) The remote URL to POST data towards (eg: "https://com.example/webhook/github"
  8
+    :auth_token,
  9
+    # (Required) Authentication Token to be used by the POST URL
  10
+    :repo_name,
  11
+    # (Required) Repo name which triggered this Event.
  12
+    :notify_email
  13
+    # (Optional) Email Address(1) to intimate that Public Event is generated
  14
+
  15
+  default_events :public
  16
+
  17
+  def receive_public
  18
+    # make sure we have what we need
  19
+    raise_config_error "Missing 'remote_url'" if data['remote_url'].to_s == ''
  20
+    raise_config_error "Missing 'auth_token'" if data['auth_token'].to_s == ''
  21
+    raise_config_error "Missing 'repo_name'" if data['repo_name'].to_s == ''
  22
+
  23
+    # Set our headers
  24
+    http.headers['X-GitHub-Event'] = event.to_s #this should be 'public' anyway
  25
+
  26
+    begin
  27
+      res = http_post "#{data['remote_url']}",
  28
+              {:auth_token => data['auth_token'],
  29
+               :repo_name => data['repo_name']}
  30
+      if res.status < 200 || res.status > 299
  31
+        raise_config_error
  32
+      end
  33
+    rescue URI::Error => e
  34
+      #Ideally this should be logged somewhere. Or the Error need to be
  35
+      #raised as a config_error but since emailing the 'Public' event
  36
+      #(if an email-address is specified) should also be done, this is supresessed.
  37
+      #raise_config_error "Not able to send a POST request to #{data['remote_url']}. Reason being:%s" % e.message
  38
+    end
  39
+
  40
+    notify_email = data['notify_email'].split(/[, ]/).compact.reject {|s| s.nil? or s.empty? }[0]
  41
+
  42
+    begin
  43
+      notify_event notify_email, data['repo_name'] if notify_email
  44
+    rescue Net::SMTPError => e
  45
+      raise_config_error "Not able to shoot email to #{notify_email}. Reason being:%s" % e.message
  46
+    end
  47
+  end
  48
+
  49
+  def notify_event(address, repo_name)
  50
+    my = self
  51
+    Mail.defaults do
  52
+      delivery_method :smtp, { :address   => my.email_config['address'],
  53
+                           :port      => my.email_config['port'],
  54
+                           :domain    => my.email_config['domain'],
  55
+                           :user_name => my.email_config['user_name'],
  56
+                           :password  => my.email_config['password'],
  57
+                           :authentication => my.email_config['authentication'],
  58
+                           :enable_starttls_auto => my.email_config['enable_starttls_auto']}
  59
+    end
  60
+    mail_message = Mail.deliver do
  61
+      to       address
  62
+      from     "GitHub <noreply@github.com>"
  63
+      reply_to "GitHub <noreply@github.com>"
  64
+      subject  "#{repo_name} is open-sourced."
  65
+      text_part do
  66
+        content_type 'text/plain; charset=UTF-8'
  67
+        body         "#{repo_name} is open-sourced."
  68
+      end
  69
+    end
  70
+    mail_message
  71
+  end
  72
+end
128  test/pubalert_test.rb
... ...
@@ -0,0 +1,128 @@
  1
+require File.expand_path('../helper', __FILE__)
  2
+
  3
+class PubAlertTest < Service::TestCase
  4
+  def setup
  5
+    @stubs = Faraday::Adapter::Test::Stubs.new
  6
+  end
  7
+
  8
+  def test_empty_remote_url_hook_param
  9
+    svc = service :public, {}
  10
+    exception = assert_raise(Service::ConfigurationError) { svc.receive_public}
  11
+    assert_match /Missing 'remote_url'/, exception.message
  12
+  end
  13
+
  14
+  def test_empty_auth_token_hook_param
  15
+    data = {'remote_url' => 'a'}
  16
+    svc = service :public, data
  17
+    exception = assert_raise(Service::ConfigurationError) { svc.receive_public}
  18
+    assert_match /Missing 'auth_token'/, exception.message
  19
+  end
  20
+
  21
+  def test_empty_repo_name_hook_param
  22
+    data = {'remote_url' => 'a',
  23
+            'auth_token' => 'b'}
  24
+    svc = service :public, data
  25
+    exception = assert_raise(Service::ConfigurationError) { svc.receive_public}
  26
+    assert_match /Missing 'repo_name'/, exception.message
  27
+  end
  28
+
  29
+
  30
+-
  31
+  def test_empty_notify_email_hook_param
  32
+    callback_for_public_event = 'handle_public_event/'
  33
+    data = {'remote_url'   => "http://127.0.0.1:8000/#{callback_for_public_event}",
  34
+            'auth_token' => 'at',
  35
+            'repo_name' => 'r',
  36
+            'notify_email' => ''}
  37
+    svc = service :public, data
  38
+
  39
+    @stubs.post "/#{callback_for_public_event}" do |env|
  40
+      assert_equal 'public', env[:request_headers]['x-github-event']
  41
+      assert_match /form/, env[:request_headers]['content-type']
  42
+      assert_equal '127.0.0.1', env[:url].host
  43
+      assert_nil env[:request_headers]['X-Hub-Signature']
  44
+      [200, {}, '']
  45
+    end
  46
+    assert_nothing_raised(Service::ConfigurationError) { svc.receive_public }
  47
+    assert_equal svc.messages.size, 0
  48
+  end
  49
+
  50
+  def test_public_alert_with_valid_hook_params
  51
+    callback_for_public_event = 'handle_public_event/'
  52
+    data = {'remote_url'   => "http://127.0.0.1:8000/#{callback_for_public_event}",
  53
+            'auth_token'   => 'sometoken',
  54
+            'repo_name'    => 'testrepo',
  55
+            'notify_email' => 'xyz@domain.com'}
  56
+
  57
+    svc = service :public, data
  58
+
  59
+    @stubs.post "/#{callback_for_public_event}" do |env|
  60
+      assert_equal 'public', env[:request_headers]['x-github-event']
  61
+      assert_match /form/, env[:request_headers]['content-type']
  62
+      assert_equal '127.0.0.1', env[:url].host
  63
+      assert_nil env[:request_headers]['X-Hub-Signature']
  64
+      [200, {}, '']
  65
+    end
  66
+
  67
+    svc.receive_public
  68
+
  69
+    assert svc.messages
  70
+    assert_equal svc.messages.size, 1
  71
+    msg = svc.messages.shift
  72
+    assert msg
  73
+    assert_equal msg['to'], data['notify_email']
  74
+    assert_equal msg['from'], "GitHub <noreply@github.com>"
  75
+    assert_equal msg['reply_to'], "GitHub <noreply@github.com>"
  76
+    assert_equal msg['subject'], "#{data['repo_name']} is open-sourced."
  77
+    assert_equal msg['body'], "#{data['repo_name']} is open-sourced."
  78
+  end
  79
+
  80
+  def test_public_alert_with_multiple_email_addresses_in_notify_email
  81
+    callback_for_public_event = 'handle_public_event/'
  82
+    data = {'remote_url'   => "http://127.0.0.1:8000/#{callback_for_public_event}",
  83
+            'auth_token'   => 'sometoken',
  84
+            'repo_name'    => 'testrepo',
  85
+            'notify_email' => 'xyz@domain.com domain@xyz.com'}
  86
+
  87
+    svc = service :public, data
  88
+
  89
+    @stubs.post "/#{callback_for_public_event}" do |env|
  90
+      assert_equal 'public', env[:request_headers]['x-github-event']
  91
+      assert_match /form/, env[:request_headers]['content-type']
  92
+      assert_equal '127.0.0.1', env[:url].host
  93
+      assert_nil env[:request_headers]['X-Hub-Signature']
  94
+      [200, {}, '']
  95
+    end
  96
+
  97
+    svc.receive_public
  98
+
  99
+    assert svc.messages
  100
+    assert_equal svc.messages.size, 1
  101
+    msg = svc.messages.shift
  102
+    assert msg
  103
+    assert_equal msg['to'], 'xyz@domain.com'
  104
+    assert_equal msg['from'], "GitHub <noreply@github.com>"
  105
+    assert_equal msg['reply_to'], "GitHub <noreply@github.com>"
  106
+    assert_equal msg['subject'], "#{data['repo_name']} is open-sourced."
  107
+    assert_equal msg['body'], "#{data['repo_name']} is open-sourced."
  108
+
  109
+    # Anymore addresses ? ignored.
  110
+    assert_nil svc.messages.shift
  111
+  end
  112
+
  113
+  def service(*args)
  114
+    svc = super Service::PubAlert, *args
  115
+    def svc.messages
  116
+      @messages ||= []
  117
+    end
  118
+
  119
+    def svc.notify_event(address, repo_name)
  120
+       messages << {'to' => address,
  121
+                     'from' => "GitHub <noreply@github.com>",
  122
+                     'reply_to' => "GitHub <noreply@github.com>",
  123
+                     'subject' => "#{repo_name} is open-sourced.",
  124
+                     'body' => "#{repo_name} is open-sourced."}
  125
+    end
  126
+    svc
  127
+  end
  128
+end

No commit comments for this range

Something went wrong with that request. Please try again.