From 4df3e248c7321f2f396753c8b43d94d1c45e2ce6 Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Wed, 5 Apr 2023 11:47:57 +0300 Subject: [PATCH] feat: allow specifying endpoint in yaml config --- spec/coverage_reporter/api/jobs_spec.cr | 2 +- spec/coverage_reporter/api/webhook_spec.cr | 2 +- src/coverage_reporter/api.cr | 19 ------------------- src/coverage_reporter/api/jobs.cr | 4 +++- src/coverage_reporter/api/webhook.cr | 2 +- src/coverage_reporter/config.cr | 15 +++++++++++++++ src/coverage_reporter/yaml_config.cr | 1 + 7 files changed, 22 insertions(+), 23 deletions(-) diff --git a/spec/coverage_reporter/api/jobs_spec.cr b/spec/coverage_reporter/api/jobs_spec.cr index 6330c1f1..50b0402b 100644 --- a/spec/coverage_reporter/api/jobs_spec.cr +++ b/spec/coverage_reporter/api/jobs_spec.cr @@ -19,7 +19,7 @@ Spectator.describe CoverageReporter::Api::Jobs do ] end - let(endpoint) { "#{CoverageReporter::Api::DEFAULT_DOMAIN}/api/#{CoverageReporter::Api::API_VERSION}/jobs" } + let(endpoint) { "#{CoverageReporter::Config::DEFAULT_ENDPOINT}/api/#{CoverageReporter::Api::Jobs::API_VERSION}/jobs" } before_each do ENV["COVERALLS_RUN_AT"] = Time::Format::RFC_3339.format(Time.local) diff --git a/spec/coverage_reporter/api/webhook_spec.cr b/spec/coverage_reporter/api/webhook_spec.cr index 49413b2c..7adf3154 100644 --- a/spec/coverage_reporter/api/webhook_spec.cr +++ b/spec/coverage_reporter/api/webhook_spec.cr @@ -4,7 +4,7 @@ Spectator.describe CoverageReporter::Api::Webhook do subject { described_class.new(config, "flag1,flag2") } let(config) { CoverageReporter::Config.new("token") } - let(endpoint) { "#{CoverageReporter::Api::DEFAULT_DOMAIN}/webhook" } + let(endpoint) { "#{CoverageReporter::Config::DEFAULT_ENDPOINT}/webhook" } after_each { WebMock.reset } diff --git a/src/coverage_reporter/api.cr b/src/coverage_reporter/api.cr index bda54918..3077afce 100644 --- a/src/coverage_reporter/api.cr +++ b/src/coverage_reporter/api.cr @@ -5,28 +5,9 @@ module CoverageReporter module Api extend self - API_VERSION = "v1" - DEFAULT_DOMAIN = "https://coveralls.io" - LOCAL_DOMAIN = "http://localhost:3000" - def show_response(res) # TODO: include info about account status Log.info "---\n✅ API Response: #{res.body}\n- 💛, Coveralls" end - - def uri(path) - if ENV["COVERALLS_ENDPOINT"]?.presence - domain = ENV["COVERALLS_ENDPOINT"] - else - domain = - if ENV["COVERALLS_DEVELOPMENT"]?.presence - LOCAL_DOMAIN - else - DEFAULT_DOMAIN - end - end - - "#{domain}/#{path}" - end end end diff --git a/src/coverage_reporter/api/jobs.cr b/src/coverage_reporter/api/jobs.cr index 96e2fd05..51ebb0c2 100644 --- a/src/coverage_reporter/api/jobs.cr +++ b/src/coverage_reporter/api/jobs.cr @@ -8,6 +8,8 @@ module CoverageReporter class Api::Jobs @source : Array(Hash(Symbol, String | Array(Int64?) | Array(Int64))) + API_VERSION = "v1" + def initialize( @config : Config, @parallel : Bool, @@ -24,7 +26,7 @@ module CoverageReporter def send_request(dry_run : Bool = false) data = build_request - api_url = Api.uri("api/#{API_VERSION}/jobs") + api_url = "#{@config.endpoint}/api/#{API_VERSION}/jobs" Log.info " ·job_flag: #{@config.flag_name}" if @config.flag_name Log.info "🚀 Posting coverage data to #{api_url}" diff --git a/src/coverage_reporter/api/webhook.cr b/src/coverage_reporter/api/webhook.cr index a589b4f3..beb7d569 100644 --- a/src/coverage_reporter/api/webhook.cr +++ b/src/coverage_reporter/api/webhook.cr @@ -7,7 +7,7 @@ module CoverageReporter end def send_request(dry_run : Bool = false) - webhook_url = Api.uri("webhook") + webhook_url = "#{@config.endpoint}/webhook" Log.info "⭐️ Calling parallel done webhook: #{webhook_url}" diff --git a/src/coverage_reporter/config.cr b/src/coverage_reporter/config.cr index 22c269e1..8070f544 100644 --- a/src/coverage_reporter/config.cr +++ b/src/coverage_reporter/config.cr @@ -35,6 +35,9 @@ module CoverageReporter CI::Local, } + DEFAULT_ENDPOINT = "https://coveralls.io" + LOCAL_ENDPOINT = "http://localhost:3000" + def initialize( repo_token : String?, path : String? = "", @@ -71,6 +74,18 @@ module CoverageReporter }.compact) end + def endpoint + if ENV["COVERALLS_ENDPOINT"]?.presence + return ENV["COVERALLS_ENDPOINT"] + end + + if ENV["COVERALLS_DEVELOPMENT"]?.presence + return LOCAL_ENDPOINT + end + + @yaml.endpoint.presence || DEFAULT_ENDPOINT + end + private def ci_options : Hash(Symbol, String) CI_OPTIONS.each do |ci| res = ci.options diff --git a/src/coverage_reporter/yaml_config.cr b/src/coverage_reporter/yaml_config.cr index 7f488f34..5da41cd1 100644 --- a/src/coverage_reporter/yaml_config.cr +++ b/src/coverage_reporter/yaml_config.cr @@ -9,6 +9,7 @@ module CoverageReporter property repo_token : String? property repo_secret_token : String? property repo_name : String? + property endpoint : String? DEFAULT_LOCATION = ".coveralls.yml"