Class: Entitlements::Backend::GitHubTeam::Provider

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

Constant Summary collapse

C =
::Contracts

Instance Method Summary collapse

Constructor Details

#initialize(config:) ⇒ Provider

Returns a new instance of Provider.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/entitlements/backend/github_team/provider.rb', line 21

def initialize(config:)
  @github = Entitlements::Backend::GitHubTeam::Service.new(
    org: config.fetch("org"),
    addr: config.fetch("addr", nil),
    token: config.fetch("token"),
    ou: config.fetch("base"),
    ignore_not_found: config.fetch("ignore_not_found", false)
  )

  @github_team_cache = {}
end

Instance Method Details

#auto_generate_ignored_users(entitlement_group) ⇒ Object



146
147
148
149
150
# File 'lib/entitlements/backend/github_team/provider.rb', line 146

def auto_generate_ignored_users(entitlement_group)
  org_members = github.org_members.keys.map(&:downcase)
  group_members = entitlement_group.member_strings.map(&:downcase)
  Set.new(group_members - org_members)
end

#change_ignored?(action) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
# File 'lib/entitlements/backend/github_team/provider.rb', line 112

def change_ignored?(action)
  return false if action.existing.nil?

  result = diff_existing_updated(action.existing, action.updated, action.ignored_users)
  result[:added].empty? && result[:removed].empty? && result[:metadata].nil?
end

#commit(entitlement_group) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/entitlements/backend/github_team/provider.rb', line 125

def commit(entitlement_group)
  github_team = github.read_team(entitlement_group)

  # Create the new team and invalidate the cache
  if github_team.nil?
    team_name = entitlement_group.cn.downcase
    github.create_team(entitlement_group:)
    github.invalidate_predictive_cache(entitlement_group)
    @github_team_cache.delete(team_name)
    github_team = github.read_team(entitlement_group)
  end
  github.sync_team(entitlement_group, github_team)
end

#diff(entitlement_group, ignored_users = Set.new) ⇒ Object



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

def diff(entitlement_group, ignored_users = Set.new)
  # The current value of the team from `read` might be based on the predictive cache
  # or on an actual API call. At this stage we don't care.
  team_identifier = entitlement_group.cn.downcase
  github_team_group = read(entitlement_group)
  if github_team_group.nil?
    github_team_group = create_github_team_group(entitlement_group)
  end

  result = diff_existing_updated(github_team_group, entitlement_group, ignored_users)

  # If there are no differences, return. (If we read from the predictive cache, we just saved ourselves a call
  # to the API. Hurray.)
  return result unless result[:added].any? || result[:removed].any? || result[:metadata]

  # If the group doesn't exist yet, we know we're not using the cache and we can save on any further API calls
  unless github_team_group.("team_id") == -999
    # There are differences so we don't want to use the predictive cache. Call to `from_predictive_cache?`
    # to determine whether our source of "current state" came from the predictive cache or from the API.
    # If it returns false, it came from the API, and we should just return what we got
    # (since pulling the data from the API again would be pointless).
    return result unless github.from_predictive_cache?(entitlement_group)

    # If `from_predictive_cache?` returned true, the data came from the predictive cache. We need
    # to invalidate the predictive cache entry, clean up the instance variable and re-read the refreshed data.
    github.invalidate_predictive_cache(entitlement_group)
    @github_team_cache.delete(team_identifier)
    github_team_group = read(entitlement_group)
  end

  # And finally, we have to calculate a new diff, which this time uses the fresh data from the API as
  # its basis, rather than the predictive cache.
  diff_existing_updated(github_team_group, entitlement_group, ignored_users)
end

#diff_existing_updated(existing_group, group, ignored_users = Set.new) ⇒ Object



101
102
103
# File 'lib/entitlements/backend/github_team/provider.rb', line 101

def diff_existing_updated(existing_group, group, ignored_users = Set.new)
  (existing_group, group, super)
end

#read(entitlement_group) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/entitlements/backend/github_team/provider.rb', line 40

def read(entitlement_group)
  slug = Entitlements::Util::Util.any_to_cn(entitlement_group.cn.downcase)
  return @github_team_cache[slug] if @github_team_cache[slug]

  github_team = github.read_team(entitlement_group)

  # We should not cache a team which does not exist
  return nil if github_team.nil?

  Entitlements.logger.debug "Loaded #{github_team.team_dn} (id=#{github_team.team_id}) with #{github_team.member_strings.count} member(s)"
  @github_team_cache[github_team.team_name] = github_team
end