commits.rb 1.06 KB
Newer Older
1 2 3 4
# frozen_string_literal: true

module ReleaseTools
  class Commits
5
    MAX_COMMITS_TO_CHECK = 100
6

7 8 9
    attr_reader :project

    def initialize(project, ref: 'master', client: ReleaseTools::GitlabClient)
10
      @project = project
11
      @ref = ref
12
      @client = client
13 14 15
    end

    def latest_successful
16 17 18 19 20
      commit_list.detect(&method(:success?))
    end

    # Find a commit with a passing build on production that also exists on dev
    def latest_dev_green_build_commit
21
      commit_list.detect do |commit|
22 23 24 25
        next unless success?(commit)

        begin
          # Hit the dev API with the specified commit to see if it even exists
26
          ReleaseTools::GitlabDevClient.commit(project, ref: commit.id)
27
        rescue Gitlab::Error::Error
28
          false
29 30
        end
      end
31 32 33 34 35
    end

    private

    def commit_list
36 37
      @commit_list ||= @client.commits(
        @project.path,
38 39
        per_page: MAX_COMMITS_TO_CHECK,
        ref_name: @ref
John Jarvis's avatar
John Jarvis committed
40
      )
41 42
    end

43 44
    def success?(commit)
      @client.commit(@project, ref: commit.id).status == 'success'
45 46 47
    end
  end
end