forked from robinebers/openusage
-
Notifications
You must be signed in to change notification settings - Fork 0
Show Copilot Enterprise Usage Without GraphQL Listing #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mstallone
merged 3 commits into
main
from
fix/copilot-enterprise-billing-without-graphql-scope
Sep 5, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
Sources/Runway/Providers/Copilot/CopilotEnterpriseDirectBilling.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import Foundation | ||
|
|
||
| /// Billing for a Copilot seat assigned with an empty organization list. Membership orgs are not | ||
| /// the billing home. Prefer GraphQL enterprise listing when the token has `read:enterprise`; | ||
| /// otherwise derive candidate slugs from `/user/orgs` and read each enterprise's Copilot usage | ||
| /// with no organization filter. Never query those orgs' own billing endpoints — a 503 there used | ||
| /// to fail the whole card. | ||
| extension CopilotProvider { | ||
| func enterpriseDirectBillingLookup(token: String) async -> OrgBillingLookup { | ||
| if let cached = defaults.string(forKey: Self.billingEnterpriseDefaultsKey) { | ||
| do { | ||
| switch try await enterpriseWideUsageLookup(enterprise: cached, token: token) { | ||
| case .usage(let lines): | ||
| return .usage(lines) | ||
| case .empty(let lines): | ||
| // This slug was cached after it reported Copilot usage, so its own zero is the | ||
| // owning enterprise's report — the same verified empty the org-managed path uses. | ||
| return .empty(lines, enterpriseVerified: true) | ||
| case .forbidden, .inaccessible, .notFound: | ||
| defaults.removeObject(forKey: Self.billingEnterpriseDefaultsKey) | ||
| } | ||
| } catch { | ||
| AppLog.warn( | ||
| LogTag.plugin("copilot"), | ||
| "enterprise AI credit lookup failed for the remembered enterprise: \(error.localizedDescription)" | ||
| ) | ||
| return .temporarilyUnavailable | ||
| } | ||
| } | ||
|
|
||
| switch await CopilotEnterpriseDiscovery(client: orgBillingClient).lookupSlugs(token: token) { | ||
| case .slugs(let slugs): | ||
| return await probeEnterpriseWideSlugs(slugs, token: token, emptyAcceptance: .singleListedSlug) | ||
| case .noEnterprises: | ||
| return .managed(provenEnterpriseAssociation: false) | ||
| case .managed: | ||
| // GraphQL listing needs `read:enterprise`. Org owners can still read enterprise REST | ||
| // billing once the slug is known, so fall through to membership-derived candidates. | ||
| return await membershipDerivedEnterpriseBillingLookup(token: token) | ||
| case .temporarilyUnavailable: | ||
| return .temporarilyUnavailable | ||
| } | ||
| } | ||
|
|
||
| /// `/user/orgs` plus hyphen-prefix guesses, used only after GraphQL enterprise listing is denied. | ||
| private func membershipDerivedEnterpriseBillingLookup(token: String) async -> OrgBillingLookup { | ||
| let orgs: [String] | ||
| do { | ||
| let response = try await orgBillingClient.fetchUserOrgs(token: token) | ||
| guard response.statusCode == 200 else { | ||
| AppLog.info( | ||
| LogTag.plugin("copilot"), | ||
| "org list HTTP \(response.statusCode); skipping membership-derived enterprise billing" | ||
| ) | ||
| if response.isGitHubRateLimited || response.statusCode >= 500 { | ||
| return .temporarilyUnavailable | ||
| } | ||
| return .managed(provenEnterpriseAssociation: false) | ||
| } | ||
| orgs = CopilotOrgBillingMapper.orgLogins(response) | ||
| } catch { | ||
| AppLog.warn( | ||
| LogTag.plugin("copilot"), | ||
| "org list fetch failed during enterprise-direct discovery: \(error.localizedDescription)" | ||
| ) | ||
| return .temporarilyUnavailable | ||
| } | ||
|
|
||
| let slugs = CopilotOrgBillingMapper.candidateEnterpriseSlugs(fromOrgLogins: orgs) | ||
| guard !slugs.isEmpty else { | ||
| return .managed(provenEnterpriseAssociation: false) | ||
| } | ||
| return await probeEnterpriseWideSlugs(slugs, token: token, emptyAcceptance: .singleReadableSlug) | ||
|
mstallone marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private enum EnterpriseEmptyAcceptance: Equatable { | ||
| /// GraphQL listed these enterprises. Only a single listed slug's empty report can stand. | ||
| case singleListedSlug | ||
| /// Guessed from membership orgs. Extra 404s are expected; only one HTTP 200 empty can stand. | ||
| case singleReadableSlug | ||
| } | ||
|
|
||
| private func probeEnterpriseWideSlugs( | ||
| _ slugs: [String], | ||
| token: String, | ||
| emptyAcceptance: EnterpriseEmptyAcceptance | ||
| ) async -> OrgBillingLookup { | ||
| var sawTransientFailure = false | ||
| var emptyCandidate: [MetricLine]? | ||
| var readableCount = 0 | ||
| for slug in slugs { | ||
| do { | ||
| switch try await enterpriseWideUsageLookup(enterprise: slug, token: token) { | ||
| case .usage(let lines): | ||
| defaults.set(slug, forKey: Self.billingEnterpriseDefaultsKey) | ||
| return .usage(lines) | ||
| case .empty(let lines): | ||
| readableCount += 1 | ||
| emptyCandidate = emptyCandidate ?? lines | ||
| case .forbidden, .inaccessible, .notFound: | ||
| continue | ||
| } | ||
| } catch { | ||
| sawTransientFailure = true | ||
| AppLog.warn( | ||
| LogTag.plugin("copilot"), | ||
| "enterprise AI credit usage failed for one enterprise; trying the next: \(error.localizedDescription)" | ||
| ) | ||
| } | ||
| } | ||
| let acceptEmpty: Bool | ||
| switch emptyAcceptance { | ||
| case .singleListedSlug: | ||
| acceptEmpty = slugs.count == 1 | ||
| case .singleReadableSlug: | ||
| acceptEmpty = readableCount == 1 | ||
| } | ||
| if let emptyCandidate, !sawTransientFailure, acceptEmpty { | ||
| return .empty(emptyCandidate, enterpriseVerified: false) | ||
| } | ||
| return sawTransientFailure | ||
| ? .temporarilyUnavailable | ||
| : .managed(provenEnterpriseAssociation: false) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.