Class: Entitlements::Service::GitHub
- Inherits:
-
Object
- Object
- Entitlements::Service::GitHub
- Includes:
- Contracts::Core
- Defined in:
- lib/entitlements/service/github.rb
Direct Known Subclasses
Constant Summary collapse
- C =
::Contracts
- MAX_GRAPHQL_RESULTS =
This is a limitation of the GitHub API
100
- MAX_GRAPHQL_RETRIES =
Retries to smooth over transient network blips
3
- WAIT_BETWEEN_GRAPHQL_RETRIES =
1
Instance Attribute Summary collapse
-
#addr ⇒ Object
readonly
Returns the value of attribute addr.
-
#ignore_not_found ⇒ Object
readonly
Returns the value of attribute ignore_not_found.
-
#org ⇒ Object
readonly
Returns the value of attribute org.
-
#ou ⇒ Object
readonly
Returns the value of attribute ou.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #enterprise? ⇒ Boolean
- #identifier ⇒ Object
-
#initialize(addr: nil, org:, token:, ou:, ignore_not_found: false) ⇒ GitHub
constructor
A new instance of GitHub.
- #invalidate_org_members_predictive_cache ⇒ Object
- #org_members ⇒ Object
- #org_members_from_predictive_cache? ⇒ Boolean
- #pending_members ⇒ Object
Constructor Details
#initialize(addr: nil, org:, token:, ou:, ignore_not_found: false) ⇒ GitHub
Returns a new instance of GitHub.
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/entitlements/service/github.rb', line 38 def initialize(addr: nil, org:, token:, ou:, ignore_not_found: false) # Save some parameters for the connection but don't actually connect yet. @addr = addr @org = org @token = token @ou = ou @ignore_not_found = ignore_not_found # This is a global cache across all invocations of this object. GitHub membership # need to be obtained only one time per organization, but might be used multiple times. Entitlements.cache[:github_pending_members] ||= {} Entitlements.cache[:github_org_members] ||= {} end |
Instance Attribute Details
#addr ⇒ Object (readonly)
Returns the value of attribute addr.
20 21 22 |
# File 'lib/entitlements/service/github.rb', line 20 def addr @addr end |
#ignore_not_found ⇒ Object (readonly)
Returns the value of attribute ignore_not_found.
20 21 22 |
# File 'lib/entitlements/service/github.rb', line 20 def ignore_not_found @ignore_not_found end |
#org ⇒ Object (readonly)
Returns the value of attribute org.
20 21 22 |
# File 'lib/entitlements/service/github.rb', line 20 def org @org end |
#ou ⇒ Object (readonly)
Returns the value of attribute ou.
20 21 22 |
# File 'lib/entitlements/service/github.rb', line 20 def ou @ou end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
20 21 22 |
# File 'lib/entitlements/service/github.rb', line 20 def token @token end |
Instance Method Details
#enterprise? ⇒ Boolean
96 97 98 99 |
# File 'lib/entitlements/service/github.rb', line 96 def enterprise? = octokit. .key? :installed_version end |
#identifier ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/entitlements/service/github.rb', line 58 def identifier @identifier ||= begin if addr.nil? "github.com" else u = URI(addr) u.host end end end |
#invalidate_org_members_predictive_cache ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/entitlements/service/github.rb', line 137 def invalidate_org_members_predictive_cache # If the entry was not from the predictive cache in the first place, just return. # This really should not get called if that's the case, but regardless, we don't # want to pointlessly hit the API twice. return unless org_members_from_predictive_cache? # The entry did come from the predictive cache. Invalidate the entry, clear local # caches, and re-read the data from the API. Entitlements.logger.debug "Invalidating cache entries for cn=(admin|member),#{ou}" Entitlements::Data::Groups::Cached.invalidate("cn=admin,#{ou}") Entitlements::Data::Groups::Cached.invalidate("cn=member,#{ou}") Entitlements.cache[:github_org_members].delete(org_signature) org_members nil end |
#org_members ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/entitlements/service/github.rb', line 77 def org_members Entitlements.cache[:github_org_members][org_signature] ||= begin roles = Entitlements::Backend::GitHubOrg::ORGANIZATION_ROLES.invert # Some basic stats are helpful for debugging data, cache = members_and_roles_from_graphql_or_cache result = data.map { |username, role| [username, roles.fetch(role)] }.to_h admin_count = result.count { |_, role| role == "admin" } member_count = result.count { |_, role| role == "member" } Entitlements.logger.debug "Currently #{org} has #{admin_count} admin(s) and #{member_count} member(s)" { cache:, value: result } end Entitlements.cache[:github_org_members][org_signature][:value] end |
#org_members_from_predictive_cache? ⇒ Boolean
125 126 127 128 |
# File 'lib/entitlements/service/github.rb', line 125 def org_members_from_predictive_cache? org_members # Force this to be read if for some reason it has not been yet. Entitlements.cache[:github_org_members][org_signature][:cache] || false end |
#pending_members ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/entitlements/service/github.rb', line 108 def pending_members Entitlements.cache[:github_pending_members][org_signature] ||= begin # ghes does not support org invites return Set.new if enterprise? pm = pending_members_from_graphql Entitlements.logger.debug "Currently #{org} has #{pm.size} pending member(s)" pm end end |