Sha256: 7bb838b9ef7732313fb4b06d3c673ae8e442b464655ae3f95a95ee59ed18e0a6

Contents?: true

Size: 1.81 KB

Versions: 6

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

module FastCI
  class Configuration
    attr_accessor :run_key, :build_id, :commit_msg, :commit, :branch,
                  :api_url, :secret_key, :author, :fastci_main_url, :fastci_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["FAST_CI_API_URL"] || "apx.fast.ci"
      self.secret_key = ENV.fetch("FAST_CI_SECRET_KEY")
      self.author = guess_author
      self.fastci_main_url = ENV.fetch('FASTCI_MAIN_URL', 'https://events.ruby.ci')
      self.fastci_api_url = ENV.fetch('FASTCI_API_RB_URL', 'https://ruby.fast.ci')
      self.orig_build_id = ENV['FSCI_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[FSCI_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[FSCI_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[FSCI_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

6 entries across 6 versions & 1 rubygems

Version Path
fast_ci-1.0.5 lib/fast_ci/configuration.rb
fast_ci-1.0.4 lib/fast_ci/configuration.rb
fast_ci-1.0.3 lib/fast_ci/configuration.rb
fast_ci-1.0.2 lib/fast_ci/configuration.rb
fast_ci-1.0.1 lib/fast_ci/configuration.rb
fast_ci-1.0.0 lib/fast_ci/configuration.rb