Class: Entitlements::Backend::GitHubTeam::Controller

Inherits:
BaseController
  • Object
show all
Includes:
Contracts::Core
Defined in:
lib/entitlements/backend/github_team/controller.rb

Constant Summary collapse

C =
::Contracts

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group_name, config = nil) ⇒ Controller

Returns a new instance of Controller.



22
23
24
25
# File 'lib/entitlements/backend/github_team/controller.rb', line 22

def initialize(group_name, config = nil)
  super
  @provider = Entitlements::Backend::GitHubTeam::Provider.new(config: @config)
end

Class Method Details

.priorityObject

Controller priority and registration



8
9
10
# File 'lib/entitlements/backend/github_team/controller.rb', line 8

def self.priority
  40
end

Instance Method Details

#apply(action) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/entitlements/backend/github_team/controller.rb', line 80

def apply(action)
  unless action.updated.is_a?(Entitlements::Models::Group)
    logger.fatal "#{action.dn}: GitHub entitlements interface does not support removing a team at this point"
    raise RuntimeError, "Invalid Operation"
  end

  if provider.change_ignored?(action)
    logger.debug "SKIP: GitHub team #{action.dn} only changes organization non-members or pending members"
    return
  end

  if provider.commit(action.updated)
    logger.debug "APPLY: Updating GitHub team #{action.dn}"
  else
    logger.warn "DID NOT APPLY: Changes not needed to #{action.dn}"
    logger.debug "Old: #{action.existing.inspect}"
    logger.debug "New: #{action.updated.inspect}"
  end
end

#calculateObject



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
# File 'lib/entitlements/backend/github_team/controller.rb', line 41

def calculate
  added = []
  changed = []
  teams = Entitlements::Data::Groups::Calculated.read_all(group_name, config)
  teams.each do |team_slug|
    group = Entitlements::Data::Groups::Calculated.read(team_slug)

    # Anyone who is not a member of the organization is ignored in the diff calculation.
    # This avoids adding an organization membership for someone by virtue of adding them
    # to a team, without declaring them as an administrator or a member of the org. Also
    # this avoids having a pending member show up in diffs until they accept their invite.
    ignored_users = provider.auto_generate_ignored_users(group)

    # "diff" includes a call to GitHub API to read the team as it currently exists there.
    # Returns a hash { added: Set(members), removed: Set(members) }
    diff = provider.diff(group, ignored_users)

    if diff[:added].empty? && diff[:removed].empty? && diff[:metadata].nil?
      logger.debug "UNCHANGED: No GitHub team changes for #{group_name}:#{team_slug}"
      next
    end

    if diff[:metadata] && diff[:metadata][:create_team]
      added << Entitlements::Models::Action.new(team_slug, provider.read(group), group, group_name, ignored_users:)
    else
      changed << Entitlements::Models::Action.new(team_slug, provider.read(group), group, group_name, ignored_users:)
    end
  end
  print_differences(key: group_name, added:, removed: [], changed:)

  @actions = added + changed
end

#prefetchObject



27
28
29
30
31
32
33
# File 'lib/entitlements/backend/github_team/controller.rb', line 27

def prefetch
  teams = Entitlements::Data::Groups::Calculated.read_all(group_name, config)
  teams.each do |team_slug|
    entitlement_group = Entitlements::Data::Groups::Calculated.read(team_slug)
    provider.read(entitlement_group)
  end
end

#validate_config!(key, data) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/entitlements/backend/github_team/controller.rb', line 108

def validate_config!(key, data)
  spec = COMMON_GROUP_CONFIG.merge({
    "base"  => { required: true, type: String },
    "addr"  => { required: false, type: String },
    "org"   => { required: true, type: String },
    "token" => { required: true, type: String },
    "ignore_not_found" => { required: false, type: [FalseClass, TrueClass] },
  })
  text = "GitHub group #{key.inspect}"
  Entitlements::Util::Util.validate_attr!(spec, data, text)
end