summaryrefslogtreecommitdiff
path: root/scripts/setup/find-jh-branch.rb
blob: 67e83423d9c67e6aee6a3e6486cb91142207f8f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env ruby
# frozen_string_literal: true

# In spec/scripts/setup/find_jh_branch_spec.rb we completely stub it
require 'gitlab' unless Object.const_defined?(:Gitlab)

require_relative '../api/default_options'

class FindJhBranch
  JH_DEFAULT_BRANCH = 'main-jh'
  JH_PROJECT_PATH = 'gitlab-org/gitlab-jh-mirrors/gitlab'
  BranchNotFound = Class.new(RuntimeError)

  def run
    return JH_DEFAULT_BRANCH unless merge_request?

    jh_merge_request_ref_name ||
      default_branch_merge_request_ref_name ||
      stable_branch_merge_request_ref_name ||
      default_branch_for_non_stable
  end

  private

  def merge_request?
    !!merge_request_id
  end

  def jh_merge_request_ref_name
    branch_exist?(JH_PROJECT_PATH, jh_ref_name) && jh_ref_name
  end

  def default_branch_merge_request_ref_name
    target_default_branch? && JH_DEFAULT_BRANCH
  end

  def stable_branch_merge_request_ref_name
    target_stable_branch? && begin
      jh_stable_branch_name = merge_request.target_branch.sub(/\-ee\z/, '-jh')

      branch_exist?(JH_PROJECT_PATH, jh_stable_branch_name) &&
        jh_stable_branch_name
    end
  end

  def default_branch_for_non_stable
    if target_stable_branch?
      raise(BranchNotFound, "Cannot find a suitable JH branch")
    else
      JH_DEFAULT_BRANCH
    end
  end

  def branch_exist?(project_path, branch_name)
    !!gitlab.branch(project_path, branch_name)
  rescue Gitlab::Error::NotFound
    false
  end

  def target_default_branch?
    merge_request.target_branch == default_branch
  end

  def target_stable_branch?
    merge_request.target_branch.match?(/\A(?:\d+\-)+\d+\-stable\-ee\z/)
  end

  def ref_name
    ENV['CI_COMMIT_REF_NAME']
  end

  def default_branch
    ENV['CI_DEFAULT_BRANCH']
  end

  def merge_request_project_id
    ENV['CI_MERGE_REQUEST_PROJECT_ID']
  end

  def merge_request_id
    ENV['CI_MERGE_REQUEST_IID']
  end

  def jh_ref_name
    "#{ref_name}-jh"
  end

  def merge_request
    @merge_request ||= gitlab.merge_request(merge_request_project_id, merge_request_id)
  end

  def gitlab
    @gitlab ||= Gitlab.client(
      endpoint: API::DEFAULT_OPTIONS[:endpoint],
      private_token: ENV['ADD_JH_FILES_TOKEN'] || ''
    )
  end
end

if $PROGRAM_NAME == __FILE__
  puts FindJhBranch.new.run
end