huginn_github_notifications_agent 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0d6aee74e2adca3a10f5ab9279365077842e4a2c
4
+ data.tar.gz: 32156bfb2ba69eeebc9a6fa96c659aa28b38f456
5
+ SHA512:
6
+ metadata.gz: 12e52d39209d83e739ad87633b2b538f10f5331f27573f2c62980930b186cc683e1c0aff71763b327d83ed9704551bb3888bf2db27fd1292f1a752789413321c
7
+ data.tar.gz: f2c6f6bbe30297e82f415439a310f2051d4ec50c8acaa771a98b6d2819d7aabba12415a6420e3a789a9b971ca9d4cdbea233c688e39ca2514c88432ccef0aa02
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2017 joenas
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_github_notifications_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_github_notifications_agent/github_notifications_agent'
@@ -0,0 +1,93 @@
1
+ module Agents
2
+ class GithubNotificationsAgent < Agent
3
+
4
+ cannot_receive_events!
5
+ can_dry_run!
6
+
7
+ default_schedule "every_10m"
8
+
9
+ description <<-MD
10
+ The GithubNotificationsAgent fetches your notifications from Github.
11
+
12
+ You need to create a [personal access token](https://github.com/settings/tokens) to use this, only the `notifications` scope is necessary.
13
+
14
+ To emit all new notifications as a single event, change `events` to `single`. The event key will be `notifications`.
15
+
16
+ To fetch all (unread) notifications, change `last_modified` to `false`. Default behaviour is to only fetch notifications that are updated since last run.
17
+
18
+ More options might be added for the [API](https://developer.github.com/v3/activity/notifications/#list-your-notifications).
19
+
20
+ MD
21
+
22
+ def default_options
23
+ {
24
+ 'access_token' => 'my_gh_access_token',
25
+ 'events' => 'multiple',
26
+ 'last_modified' => true
27
+ }
28
+ end
29
+
30
+ def validate_options
31
+ errors.add(:base, "access_token is required ") unless options['access_token'].present?
32
+ errors.add(:base, "interval needs to be a positive integer") if options['interval'].present? && options['interval'].to_i <= 0
33
+ if last_modified.present? && boolify(last_modified).nil?
34
+ errors.add(:base, "last_modified must be a boolean value")
35
+ end
36
+ end
37
+
38
+ def working?
39
+ !recent_error_logs?
40
+ end
41
+
42
+ def check
43
+ response = HTTParty.get base_url, request_options
44
+ # If there are no new notifications, you will see a "304 Not Modified" response
45
+ return if response.code == 304
46
+ notifications = JSON.parse response.body
47
+ if response.code > 400
48
+ error("Error during http request: #{response.body}")
49
+ return
50
+ elsif emit_single_event?
51
+ create_event payload: {notifications: notifications}
52
+ else
53
+ notifications.each {|notification| create_event payload: notification}
54
+ end
55
+ memory[:last_modified] = response.headers["last-modified"]
56
+ end
57
+
58
+ private
59
+
60
+ def emit_single_event?
61
+ options['events'] == 'single'
62
+ end
63
+
64
+ def last_modified
65
+ options['last_modified']
66
+ end
67
+
68
+ def base_url
69
+ "https://api.github.com/notifications"
70
+ end
71
+
72
+ def request_options
73
+ {
74
+ headers: default_headers.merge(extra_headers),
75
+ query: query_parameters
76
+ }
77
+ end
78
+
79
+ def default_headers
80
+ {"User-Agent" => "Huginn (https://github.com/cantino/huginn)"}
81
+ end
82
+
83
+ def extra_headers
84
+ (memory[:last_modified].present? && boolify(last_modified)) ? {'If-Modified-Since' => memory[:last_modified]} : {}
85
+ end
86
+
87
+ def query_parameters
88
+ {
89
+ access_token: interpolated['access_token']
90
+ }
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,90 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::GithubNotificationsAgent do
5
+ before do
6
+ @valid_params = {
7
+ name: "somename",
8
+ options: {
9
+ access_token: 'sometoken',
10
+ events: "multiple",
11
+ last_modified: true
12
+ }
13
+ }
14
+
15
+ stub_request(:get, /github\.com/).to_return(
16
+ :body => File.read(File.join(__dir__,"fixtures/github_notifications.json")),
17
+ :status => 200,
18
+ :headers => {"Content-Type" => "text/json", "Last-Modified" => 'Thu, 25 Oct 2012 15:16:27 GMT'}
19
+ )
20
+
21
+ @checker = Agents::GithubNotificationsAgent.new(@valid_params)
22
+ @checker.user = users(:jane)
23
+ @checker.save!
24
+
25
+ end
26
+
27
+ describe "#check" do
28
+ it "checks if it can handle multiple events" do
29
+ expect {
30
+ @checker.check()
31
+ }.to change { Event.count }.by(2)
32
+ end
33
+ end
34
+
35
+ describe "helpers" do
36
+ it "should generate a correct request options hash on the first run" do
37
+ expect(@checker.send(:request_options)).to eq({
38
+ headers: {"User-Agent" => "Huginn (https://github.com/cantino/huginn)"},
39
+ query: {access_token: @checker.options['access_token']}
40
+ })
41
+ end
42
+
43
+ it "should generate a correct request options hash on consecutive runs" do
44
+ time = (Time.now-1.minute).iso8601
45
+ @checker.memory[:last_modified] = time
46
+ @checker.save
47
+ expect(@checker.reload.send(:request_options)).to eq({
48
+ headers: {"User-Agent" => "Huginn (https://github.com/cantino/huginn)", "If-Modified-Since" => time},
49
+ query: {access_token: @checker.options['access_token']}
50
+ })
51
+ end
52
+
53
+ it "should generate a correct request options hash on consecutive runs with last_modified == false" do
54
+ @checker.options['last_modified'] = 'false'
55
+ @checker.memory[:last_modified] = Time.now
56
+ @checker.save
57
+ expect(@checker.reload.send(:request_options)).to eq({
58
+ headers: {"User-Agent" => "Huginn (https://github.com/cantino/huginn)"},
59
+ query: {access_token: @checker.options['access_token']}
60
+ })
61
+ end
62
+
63
+ end
64
+
65
+ describe "validation" do
66
+ before do
67
+ expect(@checker).to be_valid
68
+ end
69
+
70
+ it "should validate presence of access_token key" do
71
+ @checker.options[:access_token] = nil
72
+ expect(@checker).not_to be_valid
73
+ end
74
+
75
+ it "should validate last_modified is boolean" do
76
+ @checker.options[:last_modified] = 'test'
77
+ expect(@checker).not_to be_valid
78
+ end
79
+
80
+ it "should validate interval is positive integer, if present" do
81
+ @checker.options[:interval] = "asdf"
82
+ expect(@checker).not_to be_valid
83
+ end
84
+
85
+ it "should validate interval is positive integer, if present" do
86
+ @checker.options[:interval] = "-1"
87
+ expect(@checker).not_to be_valid
88
+ end
89
+ end
90
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huginn_github_notifications_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - joenas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: huginn_agent
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - jon@jonnev.se
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE.txt
63
+ - lib/huginn_github_notifications_agent.rb
64
+ - lib/huginn_github_notifications_agent/github_notifications_agent.rb
65
+ - spec/github_notifications_agent_spec.rb
66
+ homepage: https://github.com/joenas/huginn_github_notifications_agent
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.5.1
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Huginn agent to fetch Github notifications
90
+ test_files:
91
+ - spec/github_notifications_agent_spec.rb