Class: GitlabAddApprovalRulesBot::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/gitlab_add_approval_rules_bot.rb

Instance Method Summary collapse

Instance Method Details

#apply(*rulenames) ⇒ Object



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
# File 'lib/gitlab_add_approval_rules_bot.rb', line 25

def apply(*rulenames)
  config_filename = "config.yml" if config_filename.nil?
  config = YAML.load_file(config_filename)
  project_id = ENV['PROJECT_ID'] || ENV['CI_PROJECT_ID']
  merge_request_iid = ENV['MERGE_REQUEST_IID'] || ENV['CI_MERGE_REQUEST_IID']

  rulenames.each do |rulename|
    if not config['rules'].key? rulename
      puts "SKIP: rulename #{rulename} in not exists."
      next
    end
    rule = config['rules'][rulename]
    rule_options = {
      name: rule['name'],
      approvals_required: rule['approvals_required'],
      user_ids: [],
      group_ids: [],
    }

    rule_options[:approval_project_rule_id] = rule['approval_project_rule_id'] || nil
    rule_options[:user_ids] = rule['user_ids'] || []
    rule_options[:group_ids] = rule['group_ids'] || []

    (rule['usernames'] || []).each do |username|
      user = Gitlab.user_search(username)&.first
      pp user
      if not user.nil? and user['username'] == username.to_s
        rule_options[:user_ids] << user['id']
      end
    end

    (rule['groupnames'] || []).each do |groupname|
      group = Gitlab.group_search(groupname)&.first
      pp group
      if not group.nil? and group['groupname'] == groupname.to_s
        rule_options[:group_ids] << group['id']
      end
    end

    puts "project_id: #{project_id}"
    puts "merge_request_iid: #{merge_request_iid}"
    puts "options: #{rule_options}"
    if options[:dry_run]
      puts "THIS IS DRY RUN MODE!!"
      next
    end

    if project_id.nil?
      puts "project_id is nil"
      puts "exit"
      next
    end
    if merge_request_iid.nil?
      puts "merge_request_iid is nil"
      puts "exit"
      next
    end

    Gitlab.create_merge_request_level_rule(project_id, merge_request_iid, rule_options)
  end
end