summaryrefslogtreecommitdiff
path: root/scripts/check-template-changes
blob: 1a3060fe1bbcb1154e72a828b365622627118982 (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
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'tmpdir'

@template = ARGV.first

if @template.nil?
  puts "Usage: #{__FILE__} <path_to_project_template>"
  exit 1
end

@name = File.basename(@template).delete_suffix('.tar.gz')
@extracted_template_dir = Dir.mktmpdir(@name)
@master_template_dir = Dir.mktmpdir(@name)

def extract(dest)
  system('tar', 'xf', @template, '-C', dest, exception: true)
end

def cleanup
  FileUtils.rm_rf(@extracted_template_dir)
  FileUtils.rm_rf(@master_template_dir)
end

def repo_details
  Dir.chdir(@extracted_template_dir) do
    system('git', 'clone', 'project.bundle', @name, exception: true)
  end

  Dir.chdir(File.join(@extracted_template_dir, @name)) do
    head_commit = `git cat-file -p HEAD`
    lines = head_commit.split("\n")

    repository = lines
      .find { |line| line.start_with?('Template repository: ') }
      .rpartition(' ').last

    commit_sha = lines
      .find { |line| line.start_with?('Commit SHA: ') }
      .rpartition(' ').last

    [repository, commit_sha]
  end
end

puts "Extracting template to: #{@extracted_template_dir}"

extract(@extracted_template_dir)
branch = `git rev-parse --abbrev-ref HEAD`.chomp
system('git', 'checkout', 'master', exception: true)
extract(@master_template_dir)
system('git', 'checkout', branch, exception: true)

puts
puts '🧐 Comparing new template with master'
puts

system('git', '--no-pager', 'diff', '--no-index', @master_template_dir, @extracted_template_dir)

puts
puts '--- end diff ---'

repository, commit_sha = repo_details

puts
puts "📝 Template is created from #{repository} at commit #{commit_sha}"

unless repository.start_with?('https://gitlab.com/gitlab-org/project-templates/')
  puts '❌ This template does not have the correct origin'
  cleanup
  exit 1
end

puts '🧐 Verifying that template repo matches remote'
puts

remote_repo_dir = Dir.mktmpdir(@name)

system('git', 'clone', repository, remote_repo_dir, exception: true)

Dir.chdir(remote_repo_dir) do
  system('git', 'checkout', commit_sha, exception: true)
  system('git', '--no-pager', 'show')
end

extracted_template_repo_dir = File.join(@extracted_template_dir, @name)

FileUtils.rm_rf(File.join(extracted_template_repo_dir, '.git'))
FileUtils.cp_r(File.join(remote_repo_dir, '.git'), extracted_template_repo_dir)

Dir.chdir(extracted_template_repo_dir) do
  status = `git status`
  puts status
  puts

  if status.include?('nothing to commit, working tree clean')
    puts "✅ Template is up to date with remote commit #{commit_sha}"
  else
    puts '❌ Template is not synced with remote'
  end
end

FileUtils.rm_rf(remote_repo_dir)
cleanup