Files
shields/services/github/github-constellation.js
dependabot[bot] b9d96755ec chore(deps-dev): bump prettier from 2.8.8 to 3.0.0 (#9357)
* chore(deps-dev): bump prettier from 2.8.8 to 3.0.0

Bumps [prettier](https://github.com/prettier/prettier) from 2.8.8 to 3.0.0.
- [Release notes](https://github.com/prettier/prettier/releases)
- [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md)
- [Commits](https://github.com/prettier/prettier/compare/2.8.8...3.0.0)

---
updated-dependencies:
- dependency-name: prettier
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* reformat all the things (prettier 3)

* update tests to await calls to prettier.format()

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: chris48s <git@chris-shaw.dev>
2023-07-10 09:27:51 +00:00

128 lines
3.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
const { postgres_url: pgUrl, gh_token: globalToken } = config.private
if (pgUrl) {
log.log('Token persistence configured with dbUrl')
this.persistence = new SqlTokenPersistence({
url: pgUrl,
table: 'github_user_tokens',
})
}
this.apiProvider = new GithubApiProvider({
baseUrl: config.service.baseUri,
globalToken,
withPooling: !globalToken,
onTokenInvalidated: tokenString => this.onTokenInvalidated(tokenString),
restApiVersion: config.service.restApiVersion,
})
this.oauthHelper = this.constructor._createOauthHelper(config)
}
scheduleDebugLogging() {
if (this._debugEnabled) {
this.debugInterval = setInterval(() => {
log.log(this.apiProvider.getTokenDebugInfo())
}, 1000 * this._debugIntervalSeconds)
}
}
async initialize(server) {
if (!this.apiProvider.withPooling) {
return
}
this.scheduleDebugLogging()
if (!this.persistence) {
return
}
let tokens = []
try {
tokens = await this.persistence.initialize()
} catch (e) {
log.error(e)
}
tokens.forEach(tokenString => {
this.apiProvider.addToken(tokenString)
})
if (this.oauthHelper.isConfigured) {
setAcceptorRoutes({
server,
authHelper: this.oauthHelper,
onTokenAccepted: tokenString => this.onTokenAdded(tokenString),
})
}
}
onTokenAdded(tokenString) {
if (!this.persistence) {
throw Error('Token persistence is not configured')
}
this.apiProvider.addToken(tokenString)
process.nextTick(async () => {
try {
await this.persistence.noteTokenAdded(tokenString)
} 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.persistence) {
try {
await this.persistence.stop()
} catch (e) {
log.error(e)
}
}
}
}
export default GithubConstellation