build: add support for fetching github token from sudowoodo (#43886)
* build: add support for fetching github token from sudowoodo Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com> * chore: update release notes cache for tests Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com> * build: support nightlies repo correctly Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com> * build: post token Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com> --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com>
This commit is contained in:
parent
dddcc09185
commit
ff65b58e2c
27 changed files with 123 additions and 35 deletions
57
script/release/github-token.js
Normal file
57
script/release/github-token.js
Normal file
|
@ -0,0 +1,57 @@
|
|||
const { createTokenAuth } = require('@octokit/auth-token');
|
||||
const got = require('got').default;
|
||||
|
||||
const cachedTokens = Object.create(null);
|
||||
|
||||
async function ensureToken (repo) {
|
||||
if (!cachedTokens[repo]) {
|
||||
cachedTokens[repo] = await (async () => {
|
||||
const { ELECTRON_GITHUB_TOKEN, SUDOWOODO_EXCHANGE_URL, SUDOWOODO_EXCHANGE_TOKEN } = process.env;
|
||||
if (ELECTRON_GITHUB_TOKEN) {
|
||||
return ELECTRON_GITHUB_TOKEN;
|
||||
}
|
||||
|
||||
if (SUDOWOODO_EXCHANGE_URL && SUDOWOODO_EXCHANGE_TOKEN) {
|
||||
const resp = await got.post(SUDOWOODO_EXCHANGE_URL + '?repo=' + repo, {
|
||||
headers: {
|
||||
Authorization: SUDOWOODO_EXCHANGE_TOKEN
|
||||
},
|
||||
throwHttpErrors: false
|
||||
});
|
||||
if (resp.statusCode !== 200) {
|
||||
console.error('bad sudowoodo exchange response code:', resp.statusCode);
|
||||
throw new Error('non-200 status code received from sudowoodo exchange function');
|
||||
}
|
||||
try {
|
||||
return JSON.parse(resp.body).token;
|
||||
} catch {
|
||||
// Swallow as the error could include the token
|
||||
throw new Error('Unexpected error parsing sudowoodo exchange response');
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error('Could not find or fetch a valid GitHub Auth Token');
|
||||
})();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.createGitHubTokenStrategy = (repo) => () => {
|
||||
let tokenAuth = null;
|
||||
|
||||
async function ensureTokenAuth () {
|
||||
if (!tokenAuth) {
|
||||
await ensureToken(repo);
|
||||
tokenAuth = createTokenAuth(cachedTokens[repo]);
|
||||
}
|
||||
}
|
||||
|
||||
async function auth () {
|
||||
await ensureTokenAuth();
|
||||
return await tokenAuth();
|
||||
}
|
||||
auth.hook = async (...args) => {
|
||||
await ensureTokenAuth();
|
||||
return await tokenAuth.hook(...args);
|
||||
};
|
||||
return auth;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue