From 086e9f303090d2c316c49b29537ed695f98e4afd Mon Sep 17 00:00:00 2001 From: Kevin Perez Date: Sun, 31 Dec 2017 12:53:24 -0600 Subject: [PATCH] fix rubocop offenses --- .rubocop.yml | 7 +++ .../api/v1/application_controller.rb | 3 - app/controllers/api/v1/comments_controller.rb | 2 - app/controllers/api/v1/diffs_controller.rb | 2 - .../api/v1/organizations_controller.rb | 2 - app/controllers/api/v1/pulls_controller.rb | 6 +- app/controllers/application_controller.rb | 2 - app/controllers/concerns/comment.rb | 2 - app/controllers/concerns/github_user.rb | 2 - app/controllers/concerns/organization.rb | 2 - app/controllers/concerns/pull_request.rb | 5 +- app/controllers/concerns/repository.rb | 2 - app/controllers/dashboard_controller.rb | 2 - app/controllers/sessions_controller.rb | 10 ++-- app/controllers/settings_controller.rb | 2 - app/controllers/welcome_controller.rb | 3 - app/models/user.rb | 2 - app/services/github_service.rb | 56 +++++++++---------- config/application.rb | 4 +- config/environments/test.rb | 2 +- config/initializers/analytics.rb | 6 +- config/initializers/backtrace_silencers.rb | 6 +- config/initializers/omniauth.rb | 1 - config/routes.rb | 1 - db/seeds.rb | 5 +- .../api/v1/comments_controller_spec.rb | 1 - .../api/v1/diffs_controller_spec.rb | 3 +- .../api/v1/organizations_controller_spec.rb | 1 - .../api/v1/pulls_controller_spec.rb | 1 - spec/controllers/dashboard_controller_spec.rb | 1 - spec/controllers/sessions_controller_spec.rb | 3 +- spec/controllers/welcome_controller_spec.rb | 1 - spec/services/github_service_spec.rb | 12 ++-- spec/spec_helper.rb | 32 +++++------ spec/support/authentication.rb | 1 - 35 files changed, 81 insertions(+), 112 deletions(-) create mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..e3f78cf --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,7 @@ +AllCops: + Exclude: + - db/schema.rb +Documentation: + Enabled: false +LineLength: + Max: 100 diff --git a/app/controllers/api/v1/application_controller.rb b/app/controllers/api/v1/application_controller.rb index 05170f1..d914bf7 100644 --- a/app/controllers/api/v1/application_controller.rb +++ b/app/controllers/api/v1/application_controller.rb @@ -1,7 +1,6 @@ module Api module V1 class ApplicationController < ::ApplicationController - def authenticate_user! if session[:user_id] current_user @@ -9,8 +8,6 @@ def authenticate_user! render json: { message: 'Invalid credentials' } end end - end end end - diff --git a/app/controllers/api/v1/comments_controller.rb b/app/controllers/api/v1/comments_controller.rb index 6d9fab7..d8c3de8 100644 --- a/app/controllers/api/v1/comments_controller.rb +++ b/app/controllers/api/v1/comments_controller.rb @@ -20,8 +20,6 @@ def create def comment_params params.permit(:repo, :pull, :text) end - end end end - diff --git a/app/controllers/api/v1/diffs_controller.rb b/app/controllers/api/v1/diffs_controller.rb index 8f78b6b..7ea9e91 100644 --- a/app/controllers/api/v1/diffs_controller.rb +++ b/app/controllers/api/v1/diffs_controller.rb @@ -12,8 +12,6 @@ def show def diff_params params.permit(:id, :repo) end - end end end - diff --git a/app/controllers/api/v1/organizations_controller.rb b/app/controllers/api/v1/organizations_controller.rb index c2763ef..a192c5d 100644 --- a/app/controllers/api/v1/organizations_controller.rb +++ b/app/controllers/api/v1/organizations_controller.rb @@ -14,8 +14,6 @@ def index def organizations GithubService.new(session[:user_token]).organizations end - end end end - diff --git a/app/controllers/api/v1/pulls_controller.rb b/app/controllers/api/v1/pulls_controller.rb index 021f599..21f172e 100644 --- a/app/controllers/api/v1/pulls_controller.rb +++ b/app/controllers/api/v1/pulls_controller.rb @@ -18,7 +18,9 @@ def update end def mergeable - render json: { mergeable: GithubService.new(session[:user_token]).pull_mergeable?(pull_params) } + render json: { + mergeable: GithubService.new(session[:user_token]).pull_mergeable?(pull_params) + } end private @@ -38,8 +40,6 @@ def pull_requests def organization params[:organization] || 'crowdint' end - end end end - diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6ba7afe..d1bb951 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -15,6 +15,4 @@ def authenticate_user! redirect_to root_path end end - end - diff --git a/app/controllers/concerns/comment.rb b/app/controllers/concerns/comment.rb index 9197ac6..8d78e9f 100644 --- a/app/controllers/concerns/comment.rb +++ b/app/controllers/concerns/comment.rb @@ -7,6 +7,4 @@ def initialize(params) @username = params[:user][:login] @avatar = params[:user][:avatar_url] end - end - diff --git a/app/controllers/concerns/github_user.rb b/app/controllers/concerns/github_user.rb index bad53bc..1cba18c 100644 --- a/app/controllers/concerns/github_user.rb +++ b/app/controllers/concerns/github_user.rb @@ -7,6 +7,4 @@ def initialize(params) @avatar = params[:avatar_url] @url = params[:html_url] end - end - diff --git a/app/controllers/concerns/organization.rb b/app/controllers/concerns/organization.rb index 31bbee7..bf6d689 100644 --- a/app/controllers/concerns/organization.rb +++ b/app/controllers/concerns/organization.rb @@ -6,6 +6,4 @@ def initialize(params) @name = params[:login] @avatar = params[:avatar_url] end - end - diff --git a/app/controllers/concerns/pull_request.rb b/app/controllers/concerns/pull_request.rb index bf5751b..5bcdd5e 100644 --- a/app/controllers/concerns/pull_request.rb +++ b/app/controllers/concerns/pull_request.rb @@ -1,5 +1,6 @@ class PullRequest - attr_accessor :id, :title, :url, :user, :created_at, :repository, :comments_count, :number, :is_private, :mergeable + attr_accessor :id, :title, :url, :user, :created_at, :repository, :comments_count, :number, + :is_private, :mergeable def initialize(params) @id = params[:id] @@ -13,6 +14,4 @@ def initialize(params) @is_private = params[:base][:repo][:private] @mergeable = true end - end - diff --git a/app/controllers/concerns/repository.rb b/app/controllers/concerns/repository.rb index 529a971..930dba7 100644 --- a/app/controllers/concerns/repository.rb +++ b/app/controllers/concerns/repository.rb @@ -8,6 +8,4 @@ def initialize(params) @description = params[:description] @private = params[:private] end - end - diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index e8c4420..1296294 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -3,6 +3,4 @@ class DashboardController < ApplicationController before_action :authenticate_user! def index; end - end - diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index ea4ab92..8631a29 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,9 +1,7 @@ class SessionsController < ApplicationController - def create auth = request.env['omniauth.auth'] - user = User.find_by_provider_and_uid(auth['provider'], auth['uid']) || User.create_with_omniauth(auth) - session[:user_id] = user.id + session[:user_id] = find_user(auth).id session[:user_token] = auth['credentials']['token'] redirect_to dashboard_url, notice: 'Signed in!' @@ -15,5 +13,9 @@ def destroy redirect_to root_url, notice: 'Signed out!' end -end + protected + def find_user(auth) + User.find_by_provider_and_uid(auth['provider'], auth['uid']) || User.create_with_omniauth(auth) + end +end diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb index 5e86f3d..699f3d6 100644 --- a/app/controllers/settings_controller.rb +++ b/app/controllers/settings_controller.rb @@ -7,6 +7,4 @@ class SettingsController < ApplicationController def analytics_keys render json: ANALYTICS_KEYS, status: :ok end - end - diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index 2eab28c..4529e88 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -1,8 +1,5 @@ class WelcomeController < ApplicationController - def index redirect_to dashboard_path if current_user end - end - diff --git a/app/models/user.rb b/app/models/user.rb index 257569e..907785e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,6 +9,4 @@ def self.create_with_omniauth(auth) user.avatar = auth['info']['image'] end end - end - diff --git a/app/services/github_service.rb b/app/services/github_service.rb index 7999b51..8421efe 100644 --- a/app/services/github_service.rb +++ b/app/services/github_service.rb @@ -8,22 +8,7 @@ def initialize(token) def pull_requests(organization) Rails.cache.fetch("#{cache_key}/#{organization}/pulls", expires_in: 5.minutes) do - pulls = [] - - repos_for(organization).each do |repo| - if repo.issues_count > 0 - pull_requests = github.pull_requests.list(user: organization, repo: repo.name, auto_pagination: true) - - pull_requests.each do |pull| - pulls << PullRequest.new(pull) - end - end - end - - repositories = pulls.map(&:repository).uniq { |e| [e.id] } - users = pulls.map(&:user).uniq { |e| [e.id] } - - [pulls, repositories, users] + fetch_pull_requests(organization) end end @@ -41,7 +26,7 @@ def organizations end def diff(params) - Rails.cache.fetch("#{cache_key}/diff/#{params[:repo]}/#{params[:id]}}", expires_in: 5.minutes) do + Rails.cache.fetch(diff_cache_key(params), expires_in: 5.minutes) do uri = URI("https://api.github.com/repos/#{params[:repo]}/pulls/#{params[:id]}.diff") req = Net::HTTP::Get.new(uri) @@ -62,21 +47,18 @@ def create_pull_comment(params) end def pull_comments(params) - comments_list = [] user, repo = user_repo(params) - - comments = github.issues.comments.list(user, repo, issue_id: params[:pull], auto_pagination: true) - - comments.each do |comment| - comments_list << Comment.new(comment) - end - - comments_list + comments = github.issues.comments.list user, + repo, + issue_id: params[:pull], + auto_pagination: true + comments.each_with_object([]) { |comment, list| list << Comment.new(comment) } end def merge_pull_request(params, current_user) user, repo = user_repo(params) - github.pull_requests.merge user, repo, params[:id], commit_message: "Merged from PR Dashboard by #{current_user.nickname}" + message = "Merged from PR Dashboard by #{current_user.nickname}" + github.pull_requests.merge user, repo, params[:id], commit_message: message end def close_pull_request(params, _) @@ -93,6 +75,10 @@ def pull_mergeable?(params) private + def diff_cache_key(params) + "#{cache_key}/diff/#{params[:repo]}/#{params[:id]}}" + end + def repos_for(organization) repos = [] @@ -112,5 +98,19 @@ def user_repo(params) [info.first, info.last] end -end + def fetch_pull_requests(organization) + pulls = repos_for(organization).each_with_object([]) do |repo, array| + next unless repo.issues_count > 0 + + pull_requests = github.pull_requests.list user: organization, + repo: repo.name, + auto_pagination: true + pull_requests.each { |pull| array << PullRequest.new(pull) } + end + + repositories = pulls.map(&:repository).uniq { |e| [e.id] } + users = pulls.map(&:user).uniq { |e| [e.id] } + [pulls, repositories, users] + end +end diff --git a/config/application.rb b/config/application.rb index e7140b9..f098d1a 100644 --- a/config/application.rb +++ b/config/application.rb @@ -12,8 +12,8 @@ module Prdashboard class Application < Rails::Application config.assets.precompile += %w( - ember_application.js - analytics/analytics.js + ember_application.js + analytics/analytics.js ) end end diff --git a/config/environments/test.rb b/config/environments/test.rb index 6d2d1dc..035ed81 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -14,7 +14,7 @@ # Configure static asset server for tests with Cache-Control for performance. config.serve_static_assets = true - config.static_cache_control = "public, max-age=3600" + config.static_cache_control = 'public, max-age=3600' # Show full error reports and disable caching. config.consider_all_requests_local = true diff --git a/config/initializers/analytics.rb b/config/initializers/analytics.rb index eeb3fd5..a83db17 100644 --- a/config/initializers/analytics.rb +++ b/config/initializers/analytics.rb @@ -1,3 +1,3 @@ -Prdashboard::Application.config.analytics_id = Rails.env.production? ? ENV['ANALYTICS_ID'] : nil -Prdashboard::Application.config.analytics_domain = Rails.env.production? ? ENV['ANALYTICS_DOMAIN'] : nil - +config = Prdashboard::Application.config +config.analytics_id = Rails.env.production? ? ENV['ANALYTICS_ID'] : nil +config.analytics_domain = Rails.env.production? ? ENV['ANALYTICS_DOMAIN'] : nil diff --git a/config/initializers/backtrace_silencers.rb b/config/initializers/backtrace_silencers.rb index 59385cd..78c4f58 100644 --- a/config/initializers/backtrace_silencers.rb +++ b/config/initializers/backtrace_silencers.rb @@ -1,7 +1,9 @@ # Be sure to restart your server when you modify this file. -# You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. +# You can add backtrace silencers for libraries that you're using but don't wish to see in your +# backtraces. # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ } -# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code. +# You can also remove all the silencers if you're trying to debug a problem that might stem from +# framework code. # Rails.backtrace_cleaner.remove_silencers! diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb index f95eca5..f3d7a6e 100644 --- a/config/initializers/omniauth.rb +++ b/config/initializers/omniauth.rb @@ -1,4 +1,3 @@ Rails.application.config.middleware.use OmniAuth::Builder do provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET'], scope: 'user,repo,read:org' end - diff --git a/config/routes.rb b/config/routes.rb index 271d96a..aa13435 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,4 +21,3 @@ get '/analytics', to: 'settings#analytics_keys' end - diff --git a/db/seeds.rb b/db/seeds.rb index 4edb1e8..6eaa360 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,5 +1,6 @@ -# This file should contain all the record creation needed to seed the database with its default values. -# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). +# This file should contain all the record creation needed to seed the database with its default +# values. The data can then be loaded with the rake db:seed (or created alongside the db with +# db:setup). # # Examples: # diff --git a/spec/controllers/api/v1/comments_controller_spec.rb b/spec/controllers/api/v1/comments_controller_spec.rb index 3476cd3..5de3005 100644 --- a/spec/controllers/api/v1/comments_controller_spec.rb +++ b/spec/controllers/api/v1/comments_controller_spec.rb @@ -58,4 +58,3 @@ end end end - diff --git a/spec/controllers/api/v1/diffs_controller_spec.rb b/spec/controllers/api/v1/diffs_controller_spec.rb index f48c8bb..036443e 100644 --- a/spec/controllers/api/v1/diffs_controller_spec.rb +++ b/spec/controllers/api/v1/diffs_controller_spec.rb @@ -5,7 +5,7 @@ describe '#show' do context 'when user is signed in' do - let(:diff) { "le diff" } + let(:diff) { 'le diff' } before do GithubService.any_instance.stub(:diff).and_return diff @@ -31,4 +31,3 @@ end end end - diff --git a/spec/controllers/api/v1/organizations_controller_spec.rb b/spec/controllers/api/v1/organizations_controller_spec.rb index c260ec6..12d32b4 100644 --- a/spec/controllers/api/v1/organizations_controller_spec.rb +++ b/spec/controllers/api/v1/organizations_controller_spec.rb @@ -44,4 +44,3 @@ end end end - diff --git a/spec/controllers/api/v1/pulls_controller_spec.rb b/spec/controllers/api/v1/pulls_controller_spec.rb index ae2b4cb..c73620d 100644 --- a/spec/controllers/api/v1/pulls_controller_spec.rb +++ b/spec/controllers/api/v1/pulls_controller_spec.rb @@ -147,4 +147,3 @@ end end - diff --git a/spec/controllers/dashboard_controller_spec.rb b/spec/controllers/dashboard_controller_spec.rb index fe0eada..374c1ec 100644 --- a/spec/controllers/dashboard_controller_spec.rb +++ b/spec/controllers/dashboard_controller_spec.rb @@ -19,4 +19,3 @@ end end end - diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index 3dc1fec..4ff6f90 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -3,7 +3,7 @@ describe SessionsController do describe '#create' do before do - request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:github] + request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:github] post :create end @@ -32,4 +32,3 @@ end end end - diff --git a/spec/controllers/welcome_controller_spec.rb b/spec/controllers/welcome_controller_spec.rb index 9f7eec7..0eadf48 100644 --- a/spec/controllers/welcome_controller_spec.rb +++ b/spec/controllers/welcome_controller_spec.rb @@ -19,4 +19,3 @@ end end end - diff --git a/spec/services/github_service_spec.rb b/spec/services/github_service_spec.rb index a27e910..236f8b1 100644 --- a/spec/services/github_service_spec.rb +++ b/spec/services/github_service_spec.rb @@ -38,20 +38,20 @@ let(:pulls) do [ { - id: 13748176, + id: 13_748_176, html_url: 'https://github.com/crowdint/rails3-jquery-autocomplete/pull/264', number: 264, title: '1.0.13 broken for Mongoid, items is not an array', created_at: '2014-03-19T17:21:27Z', user: { login: 'danielfarrell', - id: 13850, + id: 13_850, avatar_url: 'https://avatars.githubusercontent.com/u/13850?', html_url: 'https://github.com/danielfarrell' }, base: { repo: { - id: 778055, + id: 778_055, name: 'rails3-jquery-autocomplete', full_name: 'crowdint/rails3-jquery-autocomplete', private: false, @@ -85,12 +85,12 @@ let(:body) do [ { - id: 12345, + id: 12_345, login: 'crowdint', avatar_url: 'gravatar.com/avatar.png' }, { - id: 98765, + id: 98_765, login: 'magmaconf', avatar_url: 'gravatar.com/avatar.png' } @@ -123,7 +123,6 @@ end end - describe '#diff' do before do Net::HTTP.stub(:start).and_return OpenStruct.new(body: 'le diff') @@ -209,4 +208,3 @@ end end end - diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a27ff4b..13492c0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,43 +1,41 @@ require 'coveralls' Coveralls.wear!('rails') - # This file is copied to spec/ when you run 'rails generate rspec:install' -ENV["RAILS_ENV"] ||= 'test' -require File.expand_path("../../config/environment", __FILE__) +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../../config/environment', __FILE__) require 'rspec/rails' require 'rspec/autorun' # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. -Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } +Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } # Checks for pending migrations before tests are run. # If you are not using ActiveRecord, you can remove this line. ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) OmniAuth.config.test_mode = true -OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new({ - provider: 'github', - uid: '123545', - info: { - name: 'John', - email: 'test@github.com', - nickname: 'johngit', - image: 'something.png' +OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new( + provider: 'github', + uid: '123545', + info: { + name: 'John', + email: 'test@github.com', + nickname: 'johngit', + image: 'something.png' }, - credentials: { - token: '3232322321cdd2' + credentials: { + token: '3232322321cdd2' } -}) +) RSpec.configure do |config| config.fixture_path = "#{::Rails.root}/spec/fixtures" config.use_transactional_fixtures = true config.infer_base_class_for_anonymous_controllers = false - config.order = "random" + config.order = 'random' config.include JsonSpec::Helpers end Rails.cache.clear - diff --git a/spec/support/authentication.rb b/spec/support/authentication.rb index 60dfe2c..4268851 100644 --- a/spec/support/authentication.rb +++ b/spec/support/authentication.rb @@ -7,4 +7,3 @@ def signin_user end end -