Files
shields/services/github/github-constellation.js
DCjanusandcopilot-swe-agent[bot] bd23e5fce6 [GitHub] Store accepted token scopes (#11915)
* feat(github): store token scopes

Assisted-by: Codex <gpt-5.5>

* perf(github): index token scopes

Assisted-by: Codex <gpt-5.5>

* feat(github): persist accepted token scopes

Assisted-by: Codex <gpt-5.5>

* docs(github): clarify OAuth scope capture

Assisted-by: Codex <gpt-5.5>

* docs(github): shorten OAuth scope note

Assisted-by: Codex <gpt-5.5>

* docs(github): clarify default OAuth scopes

Assisted-by: Codex <gpt-5.5>

* docs(github): link OAuth scope discussion

Assisted-by: Codex <gpt-5.5>

* docs(github): mark OAuth scopes as TODO

Assisted-by: Codex <gpt-5.5>

* fix(token-pool): refresh existing token data

Assisted-by: Codex <gpt-5.5>

* fix(token-pool): avoid duplicate revalidated tokens

Assisted-by: Codex:gpt-5.5

* fix(token-pool): remove revalidated tokens from priority queue

Assisted-by: Codex:gpt-5.5

* fix(token-pool): preserve token metadata on re-add

Assisted-by: Codex:gpt-5.5

* chore(github): defer token scopes index

Assisted-by: Codex:gpt-5.5

* fix(github): trim accepted token scopes

Assisted-by: Codex:gpt-5.5

* chore(github): keep token scopes index

Assisted-by: Codex:gpt-5.5

* docs(token-pool): document metadata update semantics

Assisted-by: Codex:gpt-5.5

* chore(github): store token scopes without index

Assisted-by: Codex:gpt-5.5

* chore: rerun ci

* test(token-pool): use mixed scope persistence fixture

Assisted-by: Codex:gpt-5.5

* fix(token-pool): replace invalid tokens on re-add

Keep stale queue references invalid so they can be removed lazily without rebuilding the priority queue.

Assisted-by: Codex:gpt-5.6-sol

* chore: apply Prettier 3.9 formatting

Assisted-by: Codex:gpt-5.6-sol

* chore: retry CI

Assisted-by: Codex:gpt-5.6-sol

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
2026-07-20 15:31:01 +02:00

158 lines
4.1 KiB
JavaScript

import { AuthHelper } from '../../core/base-service/auth-helper.js'
import SqlTokenPersistence from '../../core/token-pooling/sql-token-persistence.js'
import log from '../../core/server/log.js'
import GithubApiProvider from './github-api-provider.js'
import { setRoutes as setAcceptorRoutes } from './auth/acceptor.js'
// Convenience class with all the stuff related to the Github API and its
// authorization tokens, to simplify server initialization.
class GithubConstellation {
static _createOauthHelper(config) {
return new AuthHelper(
{
userKey: 'gh_client_id',
passKey: 'gh_client_secret',
authorizedOrigins: ['https://api.github.com'],
isRequired: true,
},
config,
)
}
constructor(config) {
this._debugEnabled = config.service.debug.enabled
this._debugIntervalSeconds = config.service.debug.intervalSeconds
this._metricsIntervalSeconds = config.metricsIntervalSeconds
let authType = GithubApiProvider.AUTH_TYPES.NO_AUTH
const { postgres_url: pgUrl, gh_token: globalToken } = config.private
if (pgUrl) {
log.log('Github Token persistence configured with pgUrl')
this.persistence = new SqlTokenPersistence({
url: pgUrl,
table: 'github_user_tokens',
})
authType = GithubApiProvider.AUTH_TYPES.TOKEN_POOL
}
if (globalToken) {
authType = GithubApiProvider.AUTH_TYPES.GLOBAL_TOKEN
}
log.log(`Github using auth type: ${authType}`)
this.apiProvider = new GithubApiProvider({
baseUrl: config.service.baseUri,
globalToken,
authType,
onTokenInvalidated: tokenString => this.onTokenInvalidated(tokenString),
restApiVersion: config.service.restApiVersion,
})
this.oauthHelper = this.constructor._createOauthHelper(config)
}
scheduleDebugLogging() {
if (this._debugEnabled) {
this.debugInterval = setInterval(() => {
const debugInfo = this.apiProvider.getTokenDebugInfo()
log.log(debugInfo)
}, 1000 * this._debugIntervalSeconds)
}
}
scheduleMetricsCollection() {
if (this.metricInstance) {
this.metricsInterval = setInterval(() => {
const debugInfo = this.apiProvider.getTokenDebugInfo()
this.metricInstance.noteGithubTokenPoolMetrics(debugInfo)
}, 1000 * this._metricsIntervalSeconds)
}
}
async initialize(server, metricInstance) {
if (this.apiProvider.authType !== GithubApiProvider.AUTH_TYPES.TOKEN_POOL) {
return
}
this.metricInstance = metricInstance
this.apiProvider.metricInstance = metricInstance
this.scheduleDebugLogging()
this.scheduleMetricsCollection()
if (!this.persistence) {
return
}
let tokens = []
try {
tokens = await this.persistence.initialize()
} catch (e) {
log.error(e)
}
tokens.forEach(({ token, scopes }) => {
this.apiProvider.addToken(token, { scopes })
})
if (this.oauthHelper.isConfigured) {
setAcceptorRoutes({
server,
authHelper: this.oauthHelper,
onTokenAccepted: (tokenString, data) =>
this.onTokenAdded(tokenString, data),
})
}
}
onTokenAdded(tokenString, data) {
if (!this.persistence) {
throw Error('Token persistence is not configured')
}
this.apiProvider.addToken(tokenString, data)
process.nextTick(async () => {
try {
await this.persistence.noteTokenAdded(tokenString, data)
} catch (e) {
log.error(e)
}
})
}
onTokenInvalidated(tokenString) {
if (this.persistence) {
process.nextTick(async () => {
try {
await this.persistence.noteTokenRemoved(tokenString)
} catch (e) {
log.error(e)
}
})
}
}
async stop() {
if (this.debugInterval) {
clearInterval(this.debugInterval)
this.debugInterval = undefined
}
if (this.metricsInterval) {
clearInterval(this.metricsInterval)
this.metricsInterval = undefined
}
if (this.persistence) {
try {
await this.persistence.stop()
} catch (e) {
log.error(e)
}
}
}
}
export default GithubConstellation