Sha256: 328bf8ceb244a9daa078b8c488bc8ca6e3671f379c57fe6e23ccec6e1cb6fb05

Contents?: true

Size: 1.81 KB

Versions: 19

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

module RubyCI
  class Configuration
    attr_accessor :run_key, :build_id, :commit_msg, :commit, :branch,
                  :api_url, :secret_key, :author, :rubyci_main_url, :rubyci_api_url, :orig_build_id

    def initialize
      # Settings defaults
      self.run_key = nil
      self.build_id = guess_build_id
      self.commit = guess_commit
      self.commit_msg = `git log -1 --pretty=%B`.chomp
      self.branch = guess_branch
      self.api_url = ENV["RUBY_CI_API_URL"] || "api.fast.ci"
      self.secret_key = ENV.fetch("RUBY_CI_SECRET_KEY")
      self.author = guess_author
      self.rubyci_main_url = ENV.fetch('RUBYCI_MAIN_URL', 'https://events.ruby.ci')
      self.rubyci_api_url = ENV.fetch('RUBYCI_API_RB_URL', 'https://fast.ruby.ci')
      self.orig_build_id = ENV['RBCI_ORIG_BUILD_ID']
    end

    def reset
      initialize
    end

    def run_key
      @run_key || raise("#run_key was not configured.")
    end

    def guess_author
      author_Line = `git show | grep Author:`
      name, email = author_Line.scan(/Author: (.*) <(.*)>/).first
      {
        name: name,
        email: email
      }
    end

    def guess_build_id
      %w[RBCI_BUILD_ID GITHUB_RUN_ID BUILD_ID CIRCLE_BUILD_NUM].find do |keyword|
        key = ENV.keys.find { |k| k[keyword] }
        break ENV[key] if key && ENV[key]
      end || guess_commit
    end

    def guess_commit
      %w[RBCI_COMMIT _COMMIT _SHA1 _SHA].find do |keyword|
        key = ENV.keys.find { |k| k[keyword] }
        break ENV[key] if key && ENV[key]
      end || `git rev-parse --short HEAD`.chomp
    end

    def guess_branch
      %w[RBCI_BRANCH _BRANCH _REF].find do |keyword|
        key = ENV.keys.find { |k| k[keyword] }
        break ENV[key] if key && ENV[key]
      end || `git rev-parse --abbrev-ref HEAD`.chomp
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
ruby_ci-0.2.24 lib/ruby_ci/configuration.rb
ruby_ci-0.2.23 lib/ruby_ci/configuration.rb
ruby_ci-0.2.22 lib/ruby_ci/configuration.rb
ruby_ci-0.2.21 lib/ruby_ci/configuration.rb
ruby_ci-0.2.20 lib/ruby_ci/configuration.rb
ruby_ci-0.2.19 lib/ruby_ci/configuration.rb
ruby_ci-0.2.18 lib/ruby_ci/configuration.rb
ruby_ci-0.2.17 lib/ruby_ci/configuration.rb
ruby_ci-0.2.16 lib/ruby_ci/configuration.rb
ruby_ci-0.2.15 lib/ruby_ci/configuration.rb
ruby_ci-0.2.14 lib/ruby_ci/configuration.rb
ruby_ci-0.2.13 lib/ruby_ci/configuration.rb
ruby_ci-0.2.12 lib/ruby_ci/configuration.rb
ruby_ci-0.2.11 lib/ruby_ci/configuration.rb
ruby_ci-0.2.10 lib/ruby_ci/configuration.rb
ruby_ci-0.2.9 lib/ruby_ci/configuration.rb
ruby_ci-0.2.8 lib/ruby_ci/configuration.rb
ruby_ci-0.2.7 lib/ruby_ci/configuration.rb
ruby_ci-0.2.6 lib/ruby_ci/configuration.rb