Module: Gitlab::Danger::Helper

Defined in:
lib/gitlab_roulette/danger/helper.rb

Constant Summary collapse

CATEGORY_LABELS =
{
  developer: "Random Developer",
  ecomm: '~"Ecomm"',
  tools: '~"Tools and MiniApps"',
  checkout: '~"Checkout"',
  navigation: '~"Navigation"',
  mpay: '~"Magalu Pay"',
}.freeze

Instance Method Summary collapse

Instance Method Details

#all_changed_filesArray<String>

Returns a list of all files that have been added, modified or renamed. ‘git.modified_files` might contain paths that already have been renamed, so we need to remove them from the list.

Considering these changes:

  • A new_file.rb

  • D deleted_file.rb

  • M modified_file.rb

  • R renamed_file_before.rb -> renamed_file_after.rb

it will return “‘

‘new_file.rb’, ‘modified_file.rb’, ‘renamed_file_after.rb’

“‘

Returns:

  • (Array<String>)


26
27
28
29
30
31
32
33
34
# File 'lib/gitlab_roulette/danger/helper.rb', line 26

def all_changed_files
  Set.new
    .merge(git.added_files.to_a)
    .merge(git.modified_files.to_a)
    .merge(git.renamed_files.map { |x| x[:after] })
    .subtract(git.renamed_files.map { |x| x[:before] })
    .to_a
    .sort
end

#categoriesObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gitlab_roulette/danger/helper.rb', line 36

def categories
  @categories ||=
    begin
     url = "#{ENV['CI_API_V4_URL']}/projects/#{ENV['CI_PROJECT_ID']}/variables/GITLAB_ROULETTE_CATEGORIES"
     response = Faraday.get(url) do |req|
       req.headers['Content-Type'] = 'application/json'
       req.headers['PRIVATE-TOKEN'] = ENV['DANGER_GITLAB_API_TOKEN']
     end

     result = JSON.parse(response.body)
     JSON.parse(result['value'])
    rescue => err
     puts "Failed to fetch categories from #{url}"
    end
end

#category_for_file(file) ⇒ Object

Determines the category a file @return



84
85
86
87
88
89
90
# File 'lib/gitlab_roulette/danger/helper.rb', line 84

def category_for_file(file)
  category, _ = categories.find { |category, regex_list|
    regex_list.any? { |regex_string| /#{regex_string}/.match?(file) }
  }
  result = category || "unknown"
  result.to_sym
end

#changes_by_categoryHash<String,Array<String>>

Returns:

  • (Hash<String,Array<String>>)


76
77
78
79
80
# File 'lib/gitlab_roulette/danger/helper.rb', line 76

def changes_by_category
  all_changed_files.each_with_object(Hash.new { |h, k| h[k] = [] }) do |file, hash|
    hash[category_for_file(file)] << file
  end
end

#gitlab_helperObject



52
53
54
55
56
57
58
59
# File 'lib/gitlab_roulette/danger/helper.rb', line 52

def gitlab_helper
  # Unfortunately the following does not work:
  # - respond_to?(:gitlab)
  # - respond_to?(:gitlab, true)
  gitlab
rescue NoMethodError
  nil
end

#label_for_category(category) ⇒ Object

Returns the GFM for a category label, making its best guess if it’s not a category we know about.

@return



96
97
98
# File 'lib/gitlab_roulette/danger/helper.rb', line 96

def label_for_category(category)
  CATEGORY_LABELS.fetch(category, "~#{category}")
end

#markdown_list(items) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/gitlab_roulette/danger/helper.rb', line 65

def markdown_list(items)
  list = items.map { |item| "* `#{item}`" }.join("\n")

  if items.size > 10
    "\n<details>\n\n#{list}\n\n</details>\n"
  else
    list
  end
end

#missing_database_labels(current_mr_labels) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/gitlab_roulette/danger/helper.rb', line 113

def missing_database_labels(current_mr_labels)
  labels = if has_database_scoped_labels?(current_mr_labels)
             ['database']
           else
             ['database', 'database::review pending']
           end

  labels - current_mr_labels
end

#new_teammates(usernames) ⇒ Object



109
110
111
# File 'lib/gitlab_roulette/danger/helper.rb', line 109

def new_teammates(usernames)
  usernames.map { |u| Gitlab::Danger::Teammate.new('username' => u) }
end

#project_nameObject



61
62
63
# File 'lib/gitlab_roulette/danger/helper.rb', line 61

def project_name
  ENV["CI_PROJECT_NAME"]
end