huginn_github_notifications_agent 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 0d6aee74e2adca3a10f5ab9279365077842e4a2c
4
- data.tar.gz: 32156bfb2ba69eeebc9a6fa96c659aa28b38f456
2
+ SHA256:
3
+ metadata.gz: 32de69b831360c89b3502b200d49f099cc3c9a7616c34fcb6bdc2fc6deb11747
4
+ data.tar.gz: 07dd68125abca5710eb4b0d80f303c91220a328cc426df118821fd7d7ce4d8f4
5
5
  SHA512:
6
- metadata.gz: 12e52d39209d83e739ad87633b2b538f10f5331f27573f2c62980930b186cc683e1c0aff71763b327d83ed9704551bb3888bf2db27fd1292f1a752789413321c
7
- data.tar.gz: f2c6f6bbe30297e82f415439a310f2051d4ec50c8acaa771a98b6d2819d7aabba12415a6420e3a789a9b971ca9d4cdbea233c688e39ca2514c88432ccef0aa02
6
+ metadata.gz: 6ce0a3c77a6120b6403879c428484fb2755c586bb7a9769c1f1e5b1b9ec6342cbb743dc29b4af16cd483cd24bcf4b6cb3968c845491b3fbf6bc791aa64c6f23b
7
+ data.tar.gz: b1a643a836c5350baa9eeef1dc2fb15c5ebf7ef6a3659187ab0a9b74c25fbdb2e8e5e2f8141529c0640bd17df89a3f52bdb5d979c81138ade8438b0dde3d90a2
@@ -1,4 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Layout/LineLength
4
+
1
5
  require 'huginn_agent'
6
+ require 'virtus'
7
+ require 'huginn_github_notifications_agent/subject'
2
8
 
3
- #HuginnAgent.load 'huginn_github_notifications_agent/concerns/my_agent_concern'
9
+ # HuginnAgent.load 'huginn_github_notifications_agent/concerns/my_agent_concern'
4
10
  HuginnAgent.register 'huginn_github_notifications_agent/github_notifications_agent'
11
+ # rubocop:enable Layout/LineLength
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Agents
2
4
  class GithubNotificationsAgent < Agent
3
-
4
5
  cannot_receive_events!
5
6
  can_dry_run!
6
7
 
7
- default_schedule "every_10m"
8
+ default_schedule 'every_10m'
8
9
 
9
10
  description <<-MD
10
11
  The GithubNotificationsAgent fetches your notifications from Github.
@@ -17,6 +18,14 @@ module Agents
17
18
 
18
19
  More options might be added for the [API](https://developer.github.com/v3/activity/notifications/#list-your-notifications).
19
20
 
21
+ This agent also adds two more fields to `subject` in the response, `url_web` and `repo_name`. These are for convenience, if you want to link to the updated resource for example.
22
+ ```
23
+ "subject": {
24
+ "url_web": "https://github.com/joenas/huginn_github_notifications/pull/1234",
25
+ "repo_name": "joenas/huginn_github_notifications"
26
+ }
27
+ ```
28
+
20
29
  MD
21
30
 
22
31
  def default_options
@@ -28,32 +37,49 @@ module Agents
28
37
  end
29
38
 
30
39
  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
40
+ unless options['access_token'].present?
41
+ errors.add(:base, 'access_token is required ')
42
+ end
43
+ # rubocop:disable Style/GuardClause
33
44
  if last_modified.present? && boolify(last_modified).nil?
34
- errors.add(:base, "last_modified must be a boolean value")
45
+ errors.add(:base, 'last_modified must be a boolean value')
35
46
  end
47
+ # rubocop:enable Style/GuardClause
36
48
  end
37
49
 
38
50
  def working?
39
51
  !recent_error_logs?
40
52
  end
41
53
 
54
+ # TODO: Fix
55
+ # rubocop:disable Metrics/MethodLength
56
+ # rubocop:disable Metrics/AbcSize
42
57
  def check
43
58
  response = HTTParty.get base_url, request_options
44
- # If there are no new notifications, you will see a "304 Not Modified" response
59
+ # If there are no new notifications, you will get a "304 Not Modified"
45
60
  return if response.code == 304
46
- notifications = JSON.parse response.body
61
+
62
+ notifications = JSON.parse(response.body)
47
63
  if response.code > 400
48
64
  error("Error during http request: #{response.body}")
49
65
  return
50
- elsif emit_single_event?
51
- create_event payload: {notifications: notifications}
66
+ end
67
+ notifications.each do |notif|
68
+ data = notif['subject'].merge(
69
+ repo_name: notif['repository']['full_name']
70
+ )
71
+ subject = ::HuginnGithubNotificationsAgent::Subject.new(data)
72
+ notif['subject'] = subject.to_h
73
+ end
74
+ if emit_single_event?
75
+ create_event payload: { notifications: notifications }
52
76
  else
53
- notifications.each {|notification| create_event payload: notification}
77
+ notifications.each { |notification| create_event payload: notification }
54
78
  end
55
- memory[:last_modified] = response.headers["last-modified"]
79
+ memory[:last_modified] = response.headers['last-modified']
56
80
  end
81
+ # rubocop:enable Metrics/MethodLength
82
+ # rubocop:enable Metrics/AbcSize
57
83
 
58
84
  private
59
85
 
@@ -65,29 +91,33 @@ module Agents
65
91
  options['last_modified']
66
92
  end
67
93
 
94
+ def use_last_modified?
95
+ memory[:last_modified].present? && boolify(last_modified)
96
+ end
97
+
68
98
  def base_url
69
- "https://api.github.com/notifications"
99
+ 'https://api.github.com/notifications'
70
100
  end
71
101
 
72
102
  def request_options
73
103
  {
74
- headers: default_headers.merge(extra_headers),
75
- query: query_parameters
104
+ headers: default_headers.merge(extra_headers)
76
105
  }
77
106
  end
78
107
 
79
108
  def default_headers
80
- {"User-Agent" => "Huginn (https://github.com/cantino/huginn)"}
109
+ {
110
+ 'User-Agent' => 'Huginn (https://github.com/cantino/huginn)',
111
+ 'Authorization' => 'token ' + interpolated['access_token']
112
+ }
81
113
  end
82
114
 
83
115
  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
- }
116
+ if use_last_modified?
117
+ { 'If-Modified-Since' => memory[:last_modified] }
118
+ else
119
+ {}
120
+ end
91
121
  end
92
122
  end
93
123
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HuginnGithubNotificationsAgent
4
+ class Subject
5
+ include Virtus.model
6
+
7
+ attribute :title, String
8
+ attribute :url, String
9
+ attribute :latest_comment_url, String
10
+ attribute :type, String
11
+ attribute :url_web, String
12
+ attribute :repo_name, String
13
+
14
+ def url_web
15
+ matches = url.scan(%r{/(?<type>pull|issues)s?/(?<id>\d+)$})
16
+ (["https://github.com/#{repo_name}"] << matches.flatten).join('/')
17
+ end
18
+ end
19
+ end
@@ -1,90 +1,115 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails_helper'
2
4
  require 'huginn_agent/spec_helper'
3
5
 
4
6
  describe Agents::GithubNotificationsAgent do
5
7
  before do
6
8
  @valid_params = {
7
- name: "somename",
9
+ name: 'somename',
8
10
  options: {
9
- access_token: 'sometoken',
10
- events: "multiple",
11
+ access_token: '{% credential github_access_token %}',
12
+ events: 'multiple',
11
13
  last_modified: true
12
14
  }
13
15
  }
14
16
 
15
17
  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'}
18
+ body: File.read(File.join(__dir__, 'fixtures/github_notifications.json')),
19
+ status: 200,
20
+ headers: {
21
+ 'Content-Type' => 'text/json',
22
+ 'Last-Modified' => 'Thu, 25 Oct 2012 15:16:27 GMT'
23
+ }
19
24
  )
20
25
 
26
+ users(:jane).user_credentials.create!(
27
+ credential_name: 'github_access_token',
28
+ credential_value: 'something'
29
+ )
21
30
  @checker = Agents::GithubNotificationsAgent.new(@valid_params)
22
31
  @checker.user = users(:jane)
23
32
  @checker.save!
24
-
25
33
  end
26
34
 
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)
35
+ describe '#check' do
36
+ it "emits a single event with options['events'] = single" do
37
+ @checker.options[:events] = 'single'
38
+ expect { @checker.check }.to change { Event.count }.by(1)
39
+ end
40
+
41
+ it "emits multiple events with options['events'] = multiple" do
42
+ expect { @checker.check }.to change { Event.count }.by(2)
43
+ end
44
+
45
+ it 'creates an Events with the received data' do
46
+ @checker.check
47
+ expect(Event.last.payload['url']).to eq 'https://api.github.com/notifications/threads/1'
48
+
49
+ # Adding subject.url_web
50
+ subject_first = @checker.events.first.payload['subject']
51
+ subject_last = @checker.events.last.payload['subject']
52
+ expect(subject_first['url_web']).to eq 'https://github.com/octocat/Hello-World/pull/10'
53
+ expect(subject_first['repo_name']).to eq 'octocat/Hello-World'
54
+ expect(subject_first['title']).to eq 'Greetings'
55
+ expect(subject_last['url_web']).to eq 'https://github.com/octocat/Hello-World/issues/123'
32
56
  end
33
57
  end
34
58
 
35
- describe "helpers" do
36
- it "should generate a correct request options hash on the first run" do
59
+ describe 'helpers' do
60
+ it 'should generate a correct request options hash on the first run' do
61
+ access_token = users(:jane).user_credentials.last.credential_value
37
62
  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']}
63
+ headers: {
64
+ 'User-Agent' => 'Huginn (https://github.com/cantino/huginn)',
65
+ 'Authorization' => "token #{access_token}"
66
+ }
40
67
  })
41
68
  end
42
69
 
43
- it "should generate a correct request options hash on consecutive runs" do
44
- time = (Time.now-1.minute).iso8601
70
+ it 'should generate a correct request options hash on consecutive runs' do
71
+ time = (Time.now - 1.minute).iso8601
45
72
  @checker.memory[:last_modified] = time
46
73
  @checker.save
74
+ access_token = users(:jane).user_credentials.last.credential_value
47
75
  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']}
76
+ headers: {
77
+ 'User-Agent' => 'Huginn (https://github.com/cantino/huginn)',
78
+ 'If-Modified-Since' => time,
79
+ 'Authorization' => "token #{access_token}"
80
+ }
50
81
  })
51
82
  end
52
83
 
53
- it "should generate a correct request options hash on consecutive runs with last_modified == false" do
84
+ # rubocop:disable Layout/LineLength
85
+ it 'should generate a correct request options hash on consecutive runs with last_modified == false' do
86
+ # rubocop:enable Layout/LineLength
54
87
  @checker.options['last_modified'] = 'false'
55
88
  @checker.memory[:last_modified] = Time.now
56
89
  @checker.save
90
+ access_token = users(:jane).user_credentials.last.credential_value
57
91
  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']}
92
+ headers: {
93
+ 'User-Agent' => 'Huginn (https://github.com/cantino/huginn)',
94
+ 'Authorization' => "token #{access_token}"
95
+ }
60
96
  })
61
97
  end
62
-
63
98
  end
64
99
 
65
- describe "validation" do
100
+ describe 'validation' do
66
101
  before do
67
102
  expect(@checker).to be_valid
68
103
  end
69
104
 
70
- it "should validate presence of access_token key" do
105
+ it 'should validate presence of access_token key' do
71
106
  @checker.options[:access_token] = nil
72
107
  expect(@checker).not_to be_valid
73
108
  end
74
109
 
75
- it "should validate last_modified is boolean" do
110
+ it 'should validate last_modified is boolean' do
76
111
  @checker.options[:last_modified] = 'test'
77
112
  expect(@checker).not_to be_valid
78
113
  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
114
  end
90
115
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: huginn_github_notifications_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - joenas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-15 00:00:00.000000000 Z
11
+ date: 2020-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -30,14 +30,28 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '13.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.80.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.80.1
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: huginn_agent
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +66,20 @@ dependencies:
52
66
  - - ">="
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: virtus
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
55
83
  description:
56
84
  email:
57
85
  - jon@jonnev.se
@@ -62,6 +90,7 @@ files:
62
90
  - LICENSE.txt
63
91
  - lib/huginn_github_notifications_agent.rb
64
92
  - lib/huginn_github_notifications_agent/github_notifications_agent.rb
93
+ - lib/huginn_github_notifications_agent/subject.rb
65
94
  - spec/github_notifications_agent_spec.rb
66
95
  homepage: https://github.com/joenas/huginn_github_notifications_agent
67
96
  licenses:
@@ -82,8 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
111
  - !ruby/object:Gem::Version
83
112
  version: '0'
84
113
  requirements: []
85
- rubyforge_project:
86
- rubygems_version: 2.5.1
114
+ rubygems_version: 3.0.3
87
115
  signing_key:
88
116
  specification_version: 4
89
117
  summary: Huginn agent to fetch Github notifications