Sha256: e9e27ad1bebbc0a079ce00b7a13669beee8df55346af8eaab0a3646ee9ddfae0

Contents?: true

Size: 1.34 KB

Versions: 17

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

require 'open3'
require 'set'

module Buildkite
  module Builder
    class FileResolver
      @cache = true

      attr_reader :modified_files

      class << self
        attr_accessor :cache

        def resolve(reset = false)
          @resolve = nil if !cache || reset
          @resolve ||= new
        end
      end

      def initialize
        @modified_files = SortedSet.new(pull_request? ? files_from_pull_request : files_from_git)
      end

      private

      def files_from_pull_request
        Github.pull_request_files.map { |f| f.fetch('filename') }
      end

      def files_from_git
        if Buildkite.env
          changed_files = command("git diff-tree --no-commit-id --name-only -r #{Buildkite.env.commit}")
        else
          default_branch = command('git symbolic-ref refs/remotes/origin/HEAD').strip
          changed_files = command("git diff --name-only #{default_branch}")
          changed_files << command('git diff --name-only')
        end

        changed_files.split.uniq.sort
      end

      def pull_request?
        Buildkite.env&.pull_request
      end

      def command(cmd)
        output, status = Open3.capture2(*cmd.split)

        if status.success?
          output
        else
          raise "Command failed (exit #{status.exitstatus}): #{cmd}"
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
buildkite-builder-2.0.0 lib/buildkite/builder/file_resolver.rb
buildkite-builder-2.0.0.beta4 lib/buildkite/builder/file_resolver.rb
buildkite-builder-2.0.0.beta3 lib/buildkite/builder/file_resolver.rb
buildkite-builder-2.0.0.beta2 lib/buildkite/builder/file_resolver.rb
buildkite-builder-2.0.0.beta1 lib/buildkite/builder/file_resolver.rb
buildkite-builder-1.4.1 lib/buildkite/builder/file_resolver.rb
buildkite-builder-1.4.0 lib/buildkite/builder/file_resolver.rb
buildkite-builder-1.3.1 lib/buildkite/builder/file_resolver.rb
buildkite-builder-1.3.0 lib/buildkite/builder/file_resolver.rb
buildkite-builder-1.2.0 lib/buildkite/builder/file_resolver.rb
buildkite-builder-1.1.0 lib/buildkite/builder/file_resolver.rb
buildkite-builder-1.0.0 lib/buildkite/builder/file_resolver.rb
buildkite-builder-1.0.0.beta.5 lib/buildkite/builder/file_resolver.rb
buildkite-builder-1.0.0.beta.4 lib/buildkite/builder/file_resolver.rb
buildkite-builder-1.0.0.beta.3 lib/buildkite/builder/file_resolver.rb
buildkite-builder-1.0.0.beta.2 lib/buildkite/builder/file_resolver.rb
buildkite-builder-1.0.0.beta.1 lib/buildkite/builder/file_resolver.rb