Sha256: b108afe37ddbdf9e1da4cf465221ef90e0f5c790d428b71b05eae027aed3900d

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 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 = Set.new(pull_request? ? files_from_pull_request.sort! : files_from_git.sort!)
      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

5 entries across 5 versions & 1 rubygems

Version Path
buildkite-builder-2.4.1 lib/buildkite/builder/file_resolver.rb
buildkite-builder-2.4.0 lib/buildkite/builder/file_resolver.rb
buildkite-builder-2.3.0 lib/buildkite/builder/file_resolver.rb
buildkite-builder-2.2.0 lib/buildkite/builder/file_resolver.rb
buildkite-builder-2.1.0 lib/buildkite/builder/file_resolver.rb