summaryrefslogtreecommitdiff
path: root/lib/gitlab/ee_compat_check.rb
blob: b1a6d5fe0f6715989d25ee8b2b5a0af7d31eb48f (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# rubocop: disable Rails/Output
module Gitlab
  # Checks if a set of migrations requires downtime or not.
  class EeCompatCheck
    EE_REPO = 'https://gitlab.com/gitlab-org/gitlab-ee.git'.freeze

    attr_reader :ce_branch, :check_dir, :ce_repo

    def initialize(branch:, check_dir:, ce_repo: nil)
      @ce_branch = branch
      @check_dir = check_dir
      @ce_repo = ce_repo || 'https://gitlab.com/gitlab-org/gitlab-ce.git'
    end

    def check
      ensure_ee_repo
      delete_patches

      generate_patch(ce_branch, ce_patch_full_path)

      Dir.chdir(check_dir) do
        step("In the #{check_dir} directory")

        step("Pulling latest master", %w[git pull --ff-only origin master])

        status = catch(:halt_check) do
          ce_branch_compat_check!

          delete_ee_branch_locally

          ee_branch_presence_check!

          ee_branch_compat_check!
        end

        delete_ee_branch_locally
        delete_patches

        if status.nil?
          true
        else
          false
        end
      end
    end

    private

    def ensure_ee_repo
      if Dir.exist?(check_dir)
        step("#{check_dir} already exists")
      else
        cmd = %W[git clone --branch master --single-branch --depth 1 #{EE_REPO} #{check_dir}]
        step("Cloning #{EE_REPO} into #{check_dir}", cmd)
      end
    end

    def ce_branch_compat_check!
      cmd = %W[git apply --check #{ce_patch_full_path}]
      status = step("Checking if #{ce_patch_name} applies cleanly to EE/master", cmd)

      if status.zero?
        puts ce_applies_cleanly_msg(ce_branch)
        throw(:halt_check)
      end
    end

    def ee_branch_presence_check!
      status = step("Fetching origin/#{ee_branch}", %W[git fetch origin #{ee_branch}])

      unless status.zero?
        puts
        puts ce_branch_doesnt_apply_cleanly_and_no_ee_branch_msg

        throw(:halt_check, :ko)
      end
    end

    def ee_branch_compat_check!
      step("Checking out origin/#{ee_branch}", %W[git checkout -b #{ee_branch} FETCH_HEAD])

      generate_patch(ee_branch, ee_patch_full_path)
      cmd = %W[git apply --check #{ee_patch_full_path}]
      status = step("Checking if #{ee_patch_name} applies cleanly to EE/master", cmd)

      unless status.zero?
        puts
        puts ee_branch_doesnt_apply_cleanly_msg

        throw(:halt_check, :ko)
      end

      puts
      puts ee_applies_cleanly_msg
    end

    def generate_patch(branch, filepath)
      FileUtils.rm(filepath, force: true)

      depth = 0
      loop do
        depth += 10
        step("Fetching origin/master", %W[git fetch origin master --depth=#{depth}])
        status = step("Finding merge base with master", %W[git merge-base FETCH_HEAD #{branch}])

        break if status.zero? || depth > 500
      end

      raise "#{branch} is too far behind master, please rebase it!" if depth > 500

      step("Generating the patch against master")
      output, status = Gitlab::Popen.popen(%w[git format-patch FETCH_HEAD --stdout])
      throw(:halt_check, :ko) unless status.zero?

      File.write(filepath, output)
      throw(:halt_check, :ko) unless File.exist?(filepath)
    end

    def delete_ee_branch_locally
      command(%w[git checkout master])
      step("Deleting the local #{ee_branch} branch", %W[git branch -D #{ee_branch}])
    end

    def delete_patches
      step("Deleting #{ce_patch_full_path}")
      FileUtils.rm(ce_patch_full_path, force: true)

      step("Deleting #{ee_patch_full_path}")
      FileUtils.rm(ee_patch_full_path, force: true)
    end

    def ce_patch_name
      @ce_patch_name ||= "#{ce_branch}.patch"
    end

    def ce_patch_full_path
      @ce_patch_full_path ||= File.expand_path(ce_patch_name, check_dir)
    end

    def ee_branch
      @ee_branch ||= "#{ce_branch}-ee"
    end

    def ee_patch_name
      @ee_patch_name ||= "#{ee_branch}.patch"
    end

    def ee_patch_full_path
      @ee_patch_full_path ||= File.expand_path(ee_patch_name, check_dir)
    end

    def step(desc, cmd = nil)
      puts "\n=> #{desc}\n"

      if cmd
        puts "\n$ #{cmd.join(' ')}"
        command(cmd)
      end
    end

    def command(cmd)
      output, status = Gitlab::Popen.popen(cmd)
      puts output

      status
    end

    def ce_applies_cleanly_msg(ce_branch)
      <<-MSG.strip_heredoc
        =================================================================
        🎉 Congratulations!! 🎉

        The #{ce_branch} branch applies cleanly to EE/master!

        Much ❤️!!
        =================================================================\n
      MSG
    end

    def ce_branch_doesnt_apply_cleanly_and_no_ee_branch_msg
      <<-MSG.strip_heredoc
        =================================================================
        💥 Oh no! 💥

        The #{ce_branch} branch does not apply cleanly to the current
        EE/master, and no #{ee_branch} branch was found in the EE repository.

        Please create a #{ee_branch} branch that includes changes from
        #{ce_branch} but also specific changes than can be applied cleanly
        to EE/master.

        There are different ways to create such branch:

        1. Create a new branch based on the CE branch and rebase it on top of EE/master

          # In the EE repo
          $ git fetch #{ce_repo} #{ce_branch}
          $ git checkout -b #{ee_branch} FETCH_HEAD

          # You can squash the #{ce_branch} commits into a single "Port of #{ce_branch} to EE" commit
          # before rebasing to limit the conflicts-resolving steps during the rebase
          $ git fetch origin
          $ git rebase origin/master

          At this point you will likely have conflicts.
          Solve them, and continue/finish the rebase.

          You can squash the #{ce_branch} commits into a single "Port of #{ce_branch} to EE".

        2. Create a new branch from master and cherry-pick your CE commits

          # In the EE repo
          $ git fetch origin
          $ git checkout -b #{ee_branch} FETCH_HEAD
          $ git fetch #{ce_repo} #{ce_branch}
          $ git cherry-pick SHA # Repeat for all the commits you want to pick

          You can squash the #{ce_branch} commits into a single "Port of #{ce_branch} to EE" commit.

        Don't forget to push your branch to #{EE_REPO}:

          # In the EE repo
          $ git push origin #{ee_branch}

        You can then retry this failed build, and hopefully it should pass.

        Stay 💪 !
        =================================================================\n
      MSG
    end

    def ee_branch_doesnt_apply_cleanly_msg
      <<-MSG.strip_heredoc
        =================================================================
        💥 Oh no! 💥

        The #{ce_branch} does not apply cleanly to the current
        EE/master, and even though a #{ee_branch} branch exists in the EE
        repository, it does not apply cleanly either to EE/master!

        Please update the #{ee_branch}, push it again to #{EE_REPO}, and
        retry this build.

        Stay 💪 !
        =================================================================\n
      MSG
    end

    def ee_applies_cleanly_msg
      <<-MSG.strip_heredoc
        =================================================================
        🎉 Congratulations!! 🎉

        The #{ee_branch} branch applies cleanly to EE/master!

        Much ❤️!!
        =================================================================\n
      MSG
    end
  end
end