From 51f58702d809afca99006f23267801bf762d1a86 Mon Sep 17 00:00:00 2001 From: amio Date: Tue, 14 May 2019 19:23:56 +0800 Subject: [PATCH] Support multi github tokens --- libs/live-fns/github.js | 10 +++++++--- now.json | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/libs/live-fns/github.js b/libs/live-fns/github.js index df5060e..89c4d93 100644 --- a/libs/live-fns/github.js +++ b/libs/live-fns/github.js @@ -5,7 +5,6 @@ const got = require('../got.js') const v = require('../utils/version-formatter.js') const { GH_TOKEN } = process.env -const authHeader = GH_TOKEN && `token ${GH_TOKEN}` module.exports = async (topic, ...args) => { if (args.length < 2) { @@ -50,10 +49,15 @@ module.exports = async (topic, ...args) => { } } +const pickGithubToken = () => { + const tokens = GH_TOKEN.split(',') + return tokens[Math.floor(Math.random() * tokens.length)] +} + // request github api v3 (rest) const restGithub = path => got.get(`https://api.github.com/${path}`, { headers: { - Authorization: authHeader, + Authorization: `token ${pickGithubToken()}`, Accept: 'application/vnd.github.hellcat-preview+json' } }).then(res => res.body) @@ -63,7 +67,7 @@ const queryGithub = query => { return got.post('https://api.github.com/graphql', { body: { query }, headers: { - Authorization: authHeader, + Authorization: `token ${pickGithubToken()}`, Accept: 'application/vnd.github.hawkgirl-preview+json' } }).then(res => res.body) diff --git a/now.json b/now.json index 1d3ab02..daf894c 100644 --- a/now.json +++ b/now.json @@ -16,7 +16,7 @@ "components" ], "env": { - "GH_TOKEN": "@badgen-gh-token", + "GH_TOKEN": "@badgen-gh-tokens", "SENTRY_URI": "@badgen-sentry-uri", "TRACKING_GA": "@badgen-tracking-ga" },