|
| 1 | +/** |
| 2 | + * @file Contains a simple cloud function that can be used to check which PATs are no |
| 3 | + * longer working. It returns a list of valid PATs, expired PATs and PATs with errors. |
| 4 | + * |
| 5 | + * @description This function is currently rate limited to 1 request per 5 minutes. |
| 6 | + */ |
| 7 | + |
| 8 | +import { logger, request, dateDiff } from "../../src/common/utils.js"; |
| 9 | +export const RATE_LIMIT_SECONDS = 60 * 5; // 1 request per 5 minutes |
| 10 | + |
| 11 | +/** |
| 12 | + * Simple uptime check fetcher for the PATs. |
| 13 | + * |
| 14 | + * @param {import('axios').AxiosRequestHeaders} variables |
| 15 | + * @param {string} token |
| 16 | + */ |
| 17 | +const uptimeFetcher = (variables, token) => { |
| 18 | + return request( |
| 19 | + { |
| 20 | + query: ` |
| 21 | + query { |
| 22 | + rateLimit { |
| 23 | + remaining |
| 24 | + resetAt |
| 25 | + }, |
| 26 | + }`, |
| 27 | + variables, |
| 28 | + }, |
| 29 | + { |
| 30 | + Authorization: `bearer ${token}`, |
| 31 | + }, |
| 32 | + ); |
| 33 | +}; |
| 34 | + |
| 35 | +const getAllPATs = () => { |
| 36 | + return Object.keys(process.env).filter((key) => /PAT_\d*$/.exec(key)); |
| 37 | +}; |
| 38 | + |
| 39 | +/** |
| 40 | + * Check whether any of the PATs is expired. |
| 41 | + */ |
| 42 | +const getPATInfo = async (fetcher, variables) => { |
| 43 | + const details = {}; |
| 44 | + const PATs = getAllPATs(); |
| 45 | + |
| 46 | + for (const pat of PATs) { |
| 47 | + try { |
| 48 | + const response = await fetcher(variables, process.env[pat]); |
| 49 | + const errors = response.data.errors; |
| 50 | + const hasErrors = Boolean(errors); |
| 51 | + const errorType = errors?.[0]?.type; |
| 52 | + const isRateLimited = |
| 53 | + (hasErrors && errorType === "RATE_LIMITED") || |
| 54 | + response.data.data?.rateLimit?.remaining === 0; |
| 55 | + |
| 56 | + // Store PATs with errors. |
| 57 | + if (hasErrors && errorType !== "RATE_LIMITED") { |
| 58 | + details[pat] = { |
| 59 | + status: "error", |
| 60 | + error: { |
| 61 | + type: errors[0].type, |
| 62 | + message: errors[0].message, |
| 63 | + }, |
| 64 | + }; |
| 65 | + continue; |
| 66 | + } else if (isRateLimited) { |
| 67 | + const date1 = new Date(); |
| 68 | + const date2 = new Date(response.data?.data?.rateLimit?.resetAt); |
| 69 | + details[pat] = { |
| 70 | + status: "exhausted", |
| 71 | + remaining: 0, |
| 72 | + resetIn: dateDiff(date2, date1) + " minutes", |
| 73 | + }; |
| 74 | + } else { |
| 75 | + details[pat] = { |
| 76 | + status: "valid", |
| 77 | + remaining: response.data.data.rateLimit.remaining, |
| 78 | + }; |
| 79 | + } |
| 80 | + } catch (err) { |
| 81 | + // Store the PAT if it is expired. |
| 82 | + const errorMessage = err.response?.data?.message?.toLowerCase(); |
| 83 | + if (errorMessage === "bad credentials") { |
| 84 | + details[pat] = { |
| 85 | + status: "expired", |
| 86 | + }; |
| 87 | + } else if (errorMessage === "sorry. your account was suspended.") { |
| 88 | + details[pat] = { |
| 89 | + status: "suspended", |
| 90 | + }; |
| 91 | + } else { |
| 92 | + throw err; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + const filterPATsByStatus = (status) => { |
| 98 | + return Object.keys(details).filter((pat) => details[pat].status === status); |
| 99 | + }; |
| 100 | + |
| 101 | + const sortedDetails = Object.keys(details) |
| 102 | + .sort() |
| 103 | + .reduce((obj, key) => { |
| 104 | + obj[key] = details[key]; |
| 105 | + return obj; |
| 106 | + }, {}); |
| 107 | + |
| 108 | + return { |
| 109 | + validPATs: filterPATsByStatus("valid"), |
| 110 | + expiredPATs: filterPATsByStatus("expired"), |
| 111 | + exhaustedPATs: filterPATsByStatus("exhausted"), |
| 112 | + suspendedPATs: filterPATsByStatus("suspended"), |
| 113 | + errorPATs: filterPATsByStatus("error"), |
| 114 | + details: sortedDetails, |
| 115 | + }; |
| 116 | +}; |
| 117 | + |
| 118 | +/** |
| 119 | + * Cloud function that returns information about the used PATs. |
| 120 | + */ |
| 121 | +export default async (_, res) => { |
| 122 | + res.setHeader("Content-Type", "application/json"); |
| 123 | + try { |
| 124 | + // Add header to prevent abuse. |
| 125 | + const PATsInfo = await getPATInfo(uptimeFetcher, {}); |
| 126 | + if (PATsInfo) { |
| 127 | + res.setHeader( |
| 128 | + "Cache-Control", |
| 129 | + `max-age=0, s-maxage=${RATE_LIMIT_SECONDS}`, |
| 130 | + ); |
| 131 | + } |
| 132 | + res.send(JSON.stringify(PATsInfo, null, 2)); |
| 133 | + } catch (err) { |
| 134 | + // Throw error if something went wrong. |
| 135 | + logger.error(err); |
| 136 | + res.setHeader("Cache-Control", "no-store"); |
| 137 | + res.send("Something went wrong: " + err.message); |
| 138 | + } |
| 139 | +}; |
0 commit comments