mirror of
https://github.com/badges/shields.git
synced 2026-11-21 16:06:35 +03:00
* 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>
66 lines
1.4 KiB
JavaScript
66 lines
1.4 KiB
JavaScript
import pg from 'pg'
|
|
import log from '../server/log.js'
|
|
|
|
export default class SqlTokenPersistence {
|
|
constructor({ url, table }) {
|
|
this.url = url
|
|
this.table = table
|
|
this.noteTokenAdded = this.noteTokenAdded.bind(this)
|
|
this.noteTokenRemoved = this.noteTokenRemoved.bind(this)
|
|
}
|
|
|
|
async initialize(pool) {
|
|
if (pool) {
|
|
this.pool = pool
|
|
} else {
|
|
this.pool = new pg.Pool({ connectionString: this.url })
|
|
}
|
|
const result = await this.pool.query(
|
|
`SELECT token, scopes FROM ${this.table} ORDER BY RANDOM();`,
|
|
)
|
|
return result.rows.map(({ token, scopes }) => ({ token, scopes }))
|
|
}
|
|
|
|
async stop() {
|
|
if (this.pool) {
|
|
await this.pool.end()
|
|
}
|
|
}
|
|
|
|
async onTokenAdded(token, { scopes } = {}) {
|
|
return await this.pool.query(
|
|
`
|
|
INSERT INTO ${this.table} (token, scopes)
|
|
VALUES ($1::text, $2::text[])
|
|
ON CONFLICT (token) DO UPDATE
|
|
SET scopes = EXCLUDED.scopes
|
|
WHERE EXCLUDED.scopes IS NOT NULL;
|
|
`,
|
|
[token, scopes],
|
|
)
|
|
}
|
|
|
|
async onTokenRemoved(token) {
|
|
return await this.pool.query(
|
|
`DELETE FROM ${this.table} WHERE token=$1::text;`,
|
|
[token],
|
|
)
|
|
}
|
|
|
|
async noteTokenAdded(token, data) {
|
|
try {
|
|
await this.onTokenAdded(token, data)
|
|
} catch (e) {
|
|
log.error(e)
|
|
}
|
|
}
|
|
|
|
async noteTokenRemoved(token) {
|
|
try {
|
|
await this.onTokenRemoved(token)
|
|
} catch (e) {
|
|
log.error(e)
|
|
}
|
|
}
|
|
}
|