-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor [github] initialization (#1861)
This creates a new convenience class which consolidates all the Github initialization. It supports dependency injection and facilitates refactoring the persistence along the lines of #1205. Also ref #1848
- Loading branch information
1 parent
7a664ca
commit c11d97a
Showing
3 changed files
with
65 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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 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 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,55 @@ | ||
'use strict' | ||
|
||
const path = require('path') | ||
const githubAuth = require('../../lib/github-auth') | ||
const serverSecrets = require('../../lib/server-secrets') | ||
const log = require('../../lib/log') | ||
const GithubApiProvider = require('./github-api-provider') | ||
const { setRoutes: setAdminRoutes } = require('./auth/admin') | ||
|
||
// Convenience class with all the stuff related to the Github API and its | ||
// authorization tokens, to simplify server initialization. | ||
class GithubConstellation { | ||
constructor(config) { | ||
this._debugEnabled = config.service.debug.enabled | ||
this._debugIntervalSeconds = config.service.debug.intervalSeconds | ||
this._userTokensPath = path.resolve( | ||
config.persistence.dir, | ||
'github-user-tokens.json' | ||
) | ||
|
||
const baseUrl = process.env.GITHUB_URL || 'https://api.github.com' | ||
this.apiProvider = new GithubApiProvider({ baseUrl }) | ||
} | ||
|
||
scheduleDebugLogging() { | ||
if (this._debugEnabled) { | ||
this.debugInterval = setInterval(() => { | ||
log(githubAuth.serializeDebugInfo()) | ||
}, 1000 * this._debugIntervalSeconds) | ||
} | ||
} | ||
|
||
async initialize(server) { | ||
this.scheduleDebugLogging() | ||
|
||
githubAuth.scheduleAutosaving(this._userTokensPath) | ||
// TODO Catch errors and send them to Sentry. | ||
|
||
setAdminRoutes(server) | ||
|
||
if (serverSecrets && serverSecrets.gh_client_id) { | ||
githubAuth.setRoutes(server) | ||
} | ||
} | ||
|
||
stop() { | ||
githubAuth.cancelAutosaving() | ||
if (this.debugInterval) { | ||
clearInterval(this.debugInterval) | ||
this.debugInterval = undefined | ||
} | ||
} | ||
} | ||
|
||
module.exports = GithubConstellation |