check github token

This commit is contained in:
tdurieux
2021-04-30 14:21:30 +02:00
parent b1ab7e5659
commit fa79df7efa
3 changed files with 220 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
const ofs = require("fs");
const { OAuthApp } = require("@octokit/oauth-app");
const db = require("./database");
const repoUtils = require("./repository");
@@ -6,6 +7,12 @@ const fileUtils = require("./file");
const config = require("../config");
const app = new OAuthApp({
clientType: "github-app",
clientId: config.CLIENT_ID,
clientSecret: config.CLIENT_SECRET,
});
module.exports.getToken = async (repoConfig) => {
if (repoConfig.owner) {
const user = await db
@@ -20,7 +27,15 @@ module.exports.getToken = async (repoConfig) => {
}
}
if (repoConfig.token) {
return repoConfig.token;
try {
await app.checkToken({
token: repoConfig.token,
});
return repoConfig.token;
} catch (error) {
console.debug("Token is invalid.", error);
delete repoConfig.token;
}
}
return config.GITHUB_TOKEN;
};