Refactor and test [github] token persistence (#1863)

Ref #1848 #1205
This commit is contained in:
Paul Melnikow
2018-08-11 20:13:40 -04:00
committed by GitHub
parent 39d393028d
commit 9007658fd0
10 changed files with 212 additions and 59 deletions

View File

@@ -4,6 +4,7 @@ const path = require('path')
const githubAuth = require('../../lib/github-auth')
const serverSecrets = require('../../lib/server-secrets')
const log = require('../../lib/log')
const TokenPersistence = require('../../lib/token-persistence')
const GithubApiProvider = require('./github-api-provider')
const { setRoutes: setAdminRoutes } = require('./auth/admin')
@@ -13,10 +14,12 @@ class GithubConstellation {
constructor(config) {
this._debugEnabled = config.service.debug.enabled
this._debugIntervalSeconds = config.service.debug.intervalSeconds
this._userTokensPath = path.resolve(
const userTokensPath = path.resolve(
config.persistence.dir,
'github-user-tokens.json'
)
this.persistence = new TokenPersistence({ path: userTokensPath })
const baseUrl = process.env.GITHUB_URL || 'https://api.github.com'
this.apiProvider = new GithubApiProvider({ baseUrl })
@@ -33,8 +36,12 @@ class GithubConstellation {
async initialize(server) {
this.scheduleDebugLogging()
githubAuth.scheduleAutosaving(this._userTokensPath)
// TODO Catch errors and send them to Sentry.
try {
await this.persistence.initialize()
} catch (e) {
// TODO Send to sentry.
console.error(e)
}
setAdminRoutes(server)
@@ -43,12 +50,14 @@ class GithubConstellation {
}
}
stop() {
githubAuth.cancelAutosaving()
async stop() {
if (this.debugInterval) {
clearInterval(this.debugInterval)
this.debugInterval = undefined
}
await this.persistence.stop()
this.persistence = undefined
}
}