From 6e1a1ef1729435b03b48050952a2a3388d6d95df Mon Sep 17 00:00:00 2001 From: "R. Tyler Croy" Date: Mon, 4 Sep 2017 10:28:17 -0700 Subject: [PATCH] Add the remainder of the Sentry DAO calls needed --- app/canary.rb | 15 ++++----- app/canary/dao/sentry.rb | 36 ++++++++++++++++++++- spec/dao/sentry_spec.rb | 68 +++++++++++++++++++++++++++++++++++++++- views/index.haml | 7 +++-- 4 files changed, 112 insertions(+), 14 deletions(-) diff --git a/app/canary.rb b/app/canary.rb index fe3f57c..b183a7a 100644 --- a/app/canary.rb +++ b/app/canary.rb @@ -6,6 +6,7 @@ require 'sinatra/base' require 'sentry-api' require 'canary/dao/jenkins' +require 'canary/dao/sentry' module CodeValet module Canary @@ -34,7 +35,7 @@ module CodeValet haml :index, :layout => :_base, :locals => { - :projects => projects, + :sentry => DAO::Sentry.new, :jenkins => DAO::Jenkins.new, } end @@ -42,18 +43,14 @@ module CodeValet get '/issue/:id' do issue = nil events = [] - begin - issue = SentryApi.issue(params['id']) - events = SentryApi.issue_events(params['id']) - rescue StandardError => exc - Raven.capture_exception(exc) - end + issue_id = params['id'] + sentry = DAO::Sentry.new haml :issue, :layout => :_base, :locals => { - :issue => issue, - :events => events, + :issue => sentry.issue_by(issue_id), + :events => sentry.events_for_issue(issue_id), } end end diff --git a/app/canary/dao/sentry.rb b/app/canary/dao/sentry.rb index f47c2eb..eb9a1b0 100644 --- a/app/canary/dao/sentry.rb +++ b/app/canary/dao/sentry.rb @@ -44,7 +44,6 @@ module CodeValet::Canary::DAO def issues_for(project_key) return cache.get_or_set("SentryApi#project_issues(#{project_key})", :expires_in => CACHE_SECONDS) do - SentryApi.project_issues(project_key) end rescue *CodeValet::Canary::DAO::NET_ERRORS => e @@ -56,6 +55,41 @@ module CodeValet::Canary::DAO return [] end + + # Return the issue by the given Sentry issue ID + # + # This response will be cached + # + # @return [Struct] + # @return [nil] in case of error + def issue_by(id) + return cache.get_or_set("SentryApi#issue(#{id})", + :expires_in => 5 * CACHE_SECONDS) do + SentryApi.issue(id) + end + rescue *CodeValet::Canary::DAO::NET_ERRORS => e + @error = e + return nil + rescue StandardError => e + @error = e + Raven.capture_exception(e) + return nil + end + + def events_for_issue(id) + return cache.get_or_set("SentryApi#issue_events(#{id})", + :expires_in => CACHE_SECONDS) do + SentryApi.issue_events(id) + end + rescue *CodeValet::Canary::DAO::NET_ERRORS => e + @error = e + return nil + rescue StandardError => e + @error = e + Raven.capture_exception(e) + return nil + end + private def cache diff --git a/spec/dao/sentry_spec.rb b/spec/dao/sentry_spec.rb index 4d38acc..baad97b 100644 --- a/spec/dao/sentry_spec.rb +++ b/spec/dao/sentry_spec.rb @@ -72,11 +72,77 @@ describe CodeValet::Canary::DAO::Sentry do expect(dao).to be_errored end - it 'should cache the response from SentryApi#projects' do + it 'should cache the response from SentryApi#project_issues' do expect(SentryApi).to receive(:project_issues).and_return(response) 3.times do expect(dao.issues_for('rspec')).to eql(response) end end end + + it { should respond_to :issue_by } + describe '#issue_by' do + let(:dao) { described_class.new } + subject(:issue) { dao.issue_by('fake-id') } + let(:response) { Hash.new } + + it 'should return SentryApi#issue' do + expect(SentryApi).to receive(:issue).and_return(response) + expect(issue).to eql(response) + end + + it 'should gracefully handle network errors' do + expect(SentryApi).to receive(:issue).and_raise(Errno::ECONNRESET) + expect(issue).to be_nil + expect(dao).to be_errored + end + + it 'should gracefully handle and record unknown errors' do + expect(SentryApi).to receive(:issue).and_raise(JSON::ParserError) + expect(Raven).to receive(:capture_exception) + + expect(issue).to be_nil + expect(dao).to be_errored + end + + it 'should cache the response from SentryApi#issue' do + expect(SentryApi).to receive(:issue).and_return(response) + 3.times do + expect(dao.issue_by('fake-id')).to eql(response) + end + end + end + + it { should respond_to :events_for_issue } + describe '#events_for_issue' do + let(:dao) { described_class.new } + subject(:issue) { dao.events_for_issue('fake-id') } + let(:response) { Hash.new } + + it 'should return SentryApi#issue' do + expect(SentryApi).to receive(:issue_events).and_return(response) + expect(issue).to eql(response) + end + + it 'should gracefully handle network errors' do + expect(SentryApi).to receive(:issue_events).and_raise(Errno::ECONNRESET) + expect(issue).to be_nil + expect(dao).to be_errored + end + + it 'should gracefully handle and record unknown errors' do + expect(SentryApi).to receive(:issue_events).and_raise(JSON::ParserError) + expect(Raven).to receive(:capture_exception) + + expect(issue).to be_nil + expect(dao).to be_errored + end + + it 'should cache the response from SentryApi#issue' do + expect(SentryApi).to receive(:issue_events).and_return(response) + 3.times do + expect(dao.events_for_issue('fake-id')).to eql(response) + end + end + end end diff --git a/views/index.haml b/views/index.haml index bbc535c..afedc8c 100644 --- a/views/index.haml +++ b/views/index.haml @@ -38,15 +38,16 @@ %h3 Recent issues .container - - if projects.size == 0 + - projects = sentry.projects + - if sentry.errored? Something went wrong when querying the error service! - else - - projects.auto_paginate do |project| + - projects.each do |project| %h4= project.name %p - - events = SentryApi.project_issues(project.slug) + - events = sentry.issues_for(project.slug) - unless events.size > 0 No recent events (yay). - else