diff --git a/.github/workflows/reviewdog.yml b/.github/workflows/reviewdog.yml index 2ac36a4..b03ac6f 100644 --- a/.github/workflows/reviewdog.yml +++ b/.github/workflows/reviewdog.yml @@ -6,8 +6,9 @@ jobs: fail-fast: false matrix: ruby: - - 3.0 + - 3.1 - 3.2 + - 3.3 runs-on: ubuntu-latest steps: diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..011ca7c --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,16 @@ +require: + - rubocop-performance + +AllCops: + NewCops: enable + TargetRubyVersion: 3.1 + +Style/Documentation: + Enabled: false + +Gemspec/DevelopmentDependencies: + Enabled: false + +Naming/FileName: + Exclude: + - 'lib/appsignal-sourcemap.rb' \ No newline at end of file diff --git a/Gemfile b/Gemfile index be173b2..7f4f5e9 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ # frozen_string_literal: true -source "https://rubygems.org" +source 'https://rubygems.org' gemspec diff --git a/appsignal-sourcemap.gemspec b/appsignal-sourcemap.gemspec index 8ba79f7..a80167f 100644 --- a/appsignal-sourcemap.gemspec +++ b/appsignal-sourcemap.gemspec @@ -1,27 +1,28 @@ # frozen_string_literal: true -$LOAD_PATH.push File.expand_path("lib", __dir__) +$LOAD_PATH.push File.expand_path('lib', __dir__) # Maintain your gem's version: -require "appsignal/sourcemap/version" +require 'appsignal/sourcemap/version' # Describe your gem and declare its dependencies: Gem::Specification.new do |spec| - spec.name = "appsignal-sourcemap" + spec.name = 'appsignal-sourcemap' spec.version = Appsignal::Sourcemap::VERSION - spec.authors = ["Drieam"] - spec.email = ["dev@drieam.com"] - spec.homepage = "https://github.com/drieam/appsignal-sourcemap" - spec.summary = "Upload private sourcemaps to appsignal" + spec.authors = ['Drieam'] + spec.email = ['dev@drieam.com'] + spec.homepage = 'https://github.com/drieam/appsignal-sourcemap' + spec.summary = 'Upload private sourcemaps to appsignal' - spec.metadata["allowed_push_host"] = "https://rubygems.pkg.github.com/Drieam" + spec.metadata['allowed_push_host'] = 'https://rubygems.pkg.github.com/Drieam' - spec.required_ruby_version = ">= 2.7" + spec.required_ruby_version = '>= 3.1' - spec.files = Dir["lib/**/*", "README.md"] + spec.files = Dir['lib/**/*', 'README.md'] - spec.add_dependency "appsignal", "~> 3.0" - spec.add_dependency "parallel", "~> 1.0" + spec.add_dependency 'appsignal', '~> 3.0' + spec.add_dependency 'parallel', '~> 1.0' - spec.add_development_dependency "standard" + spec.add_development_dependency 'standard' + spec.metadata['rubygems_mfa_required'] = 'true' end diff --git a/lib/appsignal-sourcemap.rb b/lib/appsignal-sourcemap.rb index d96f0db..b630ab2 100644 --- a/lib/appsignal-sourcemap.rb +++ b/lib/appsignal-sourcemap.rb @@ -1,3 +1,3 @@ # frozen_string_literal: true -require "appsignal/sourcemap/hook" +require 'appsignal/sourcemap/hook' diff --git a/lib/appsignal/sourcemap/base.rb b/lib/appsignal/sourcemap/base.rb new file mode 100644 index 0000000..b8160b0 --- /dev/null +++ b/lib/appsignal/sourcemap/base.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module Appsignal + module Sourcemap + class Base + private + + def log_appsignal(message, type = 'info') + log_method = Appsignal.respond_to?(:internal_logger) ? 'internal_logger' : 'logger' + Appsignal.send(log_method).send(type, message) + end + end + end +end diff --git a/lib/appsignal/sourcemap/hook.rb b/lib/appsignal/sourcemap/hook.rb index 7401b9f..a0dcb02 100644 --- a/lib/appsignal/sourcemap/hook.rb +++ b/lib/appsignal/sourcemap/hook.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "appsignal/hooks" +require 'appsignal/hooks' module Appsignal module Sourcemap @@ -10,15 +10,15 @@ class SourcemapHook < Appsignal::Hooks::Hook def dependencies_present? defined?(::Rails) && defined?(::Rake::Task) && - Rake::Task.task_defined?("assets:precompile") && + Rake::Task.task_defined?('assets:precompile') && Appsignal.config && Appsignal.config[:upload_sourcemaps] end def install - require "appsignal/sourcemap/supervisor" + require 'appsignal/sourcemap/supervisor' - Rake::Task["assets:precompile"].enhance do + Rake::Task['assets:precompile'].enhance do Appsignal::Sourcemap::Supervisor.start end end diff --git a/lib/appsignal/sourcemap/supervisor.rb b/lib/appsignal/sourcemap/supervisor.rb index f9330e5..45ea523 100644 --- a/lib/appsignal/sourcemap/supervisor.rb +++ b/lib/appsignal/sourcemap/supervisor.rb @@ -1,11 +1,11 @@ # frozen_string_literal: true -require "parallel" -require "appsignal/sourcemap/uploader" +require 'parallel' +require 'appsignal/sourcemap/uploader' module Appsignal module Sourcemap - class Supervisor + class Supervisor < Base PARALLEL_THREADS = 10 def self.start @@ -15,27 +15,25 @@ def self.start def start return if invalid_preconditions - Appsignal.logger.info("Starting sourcemaps upload") + log_appsignal('Starting sourcemaps upload') Parallel.each(source_map_paths, in_threads: PARALLEL_THREADS) do |source_map_path| Uploader.upload(source_map_path) end - Appsignal.logger.info("Finished sourcemaps upload") + log_appsignal('Finished sourcemaps upload') end private def invalid_preconditions unless Appsignal.config.valid? - return Appsignal.logger.error("Skipping sourcemaps upload since Appsignal config is invalid") + return log_appsignal('Skipping sourcemaps upload since Appsignal config is invalid', 'error') end if asset_host.blank? - return Appsignal.logger.error("Skipping sourcemaps upload since Rails asset_host is not set") - end - if source_map_paths.empty? - return Appsignal.logger.info("Skipping sourcemaps upload since no javascript maps are found") + return log_appsignal('Skipping sourcemaps upload since Rails asset_host is not set', 'error') end + return log_appsignal('Skipping sourcemaps upload since no javascript maps are found') if source_map_paths.empty? false end @@ -45,7 +43,7 @@ def asset_host end def source_map_paths - Dir.glob("**/*.map", base: Rails.public_path) + Dir.glob('**/*.map', base: Rails.public_path) end end end diff --git a/lib/appsignal/sourcemap/uploader.rb b/lib/appsignal/sourcemap/uploader.rb index 113aeb8..be6a6f2 100644 --- a/lib/appsignal/sourcemap/uploader.rb +++ b/lib/appsignal/sourcemap/uploader.rb @@ -2,8 +2,8 @@ module Appsignal module Sourcemap - class Uploader - UPLOAD_URI = URI("https://appsignal.com/api/sourcemaps") + class Uploader < Base + UPLOAD_URI = URI('https://appsignal.com/api/sourcemaps') def self.upload(sourcemap_path) new(sourcemap_path).upload @@ -11,22 +11,29 @@ def self.upload(sourcemap_path) def initialize(sourcemap_path) @sourcemap_path = sourcemap_path + super end - def upload # rubocop:disable Metrics/AbcSize - Appsignal.logger.debug "Starting sourcemap upload '#{@sourcemap_path}' with parameters: #{request_form_data}" + def upload + log_appsignal("Starting sourcemap upload '#{@sourcemap_path}' with parameters: #{request_form_data}", 'debug') response = Net::HTTP.start(UPLOAD_URI.hostname, UPLOAD_URI.port, use_ssl: true) do |http| http.request(request) end if response.is_a?(Net::HTTPSuccess) - Appsignal.logger.debug("Finished sourcemap upload '#{@sourcemap_path}'") + log_appsignal("Finished sourcemap upload '#{@sourcemap_path}'", 'debug') File.delete(sourcemap_full_path) return end - Appsignal.logger.error <<~MESSAGE + log_appsignal(error_message(response), 'error') + end + + private + + def error_message(response) + <<~MESSAGE Uploading sourcemap #{@sourcemap_path} failed with message '#{response.message}'. Response: #{response.body} @@ -34,26 +41,24 @@ def upload # rubocop:disable Metrics/AbcSize MESSAGE end - private - def sourcemap_full_path Rails.public_path.join(@sourcemap_path) end def request Net::HTTP::Post.new(UPLOAD_URI).tap do |request| - request.set_form request_form_data, "multipart/form-data" + request.set_form request_form_data, 'multipart/form-data' end end def request_form_data [ - ["push_api_key", Appsignal.config[:push_api_key]], - ["app_name", Appsignal.config[:name]], - ["revision", Appsignal.config[:revision]], - ["environment", Appsignal.config.env], - ["name[]", source_url], - ["file", sourcemap_content] + ['push_api_key', Appsignal.config[:push_api_key]], + ['app_name', Appsignal.config[:name]], + ['revision', Appsignal.config[:revision]], + ['environment', Appsignal.config.env], + ['name[]', source_url], + ['file', sourcemap_content] ] end @@ -66,7 +71,7 @@ def source_url end def js_path - @sourcemap_path.delete_suffix(".map") + @sourcemap_path.delete_suffix('.map') end def asset_host diff --git a/lib/appsignal/sourcemap/version.rb b/lib/appsignal/sourcemap/version.rb index 67aac89..c5a8c6e 100644 --- a/lib/appsignal/sourcemap/version.rb +++ b/lib/appsignal/sourcemap/version.rb @@ -2,6 +2,6 @@ module Appsignal module Sourcemap - VERSION = "1.0.0.develop" + VERSION = '1.0.0.develop' end end