mirror of
https://github.com/github/codeql-action.git
synced 2025-12-26 17:20:10 +08:00
76 lines
2.1 KiB
JavaScript
76 lines
2.1 KiB
JavaScript
// pkg/dist-src/auth.js
|
|
async function auth(reason) {
|
|
return {
|
|
type: "unauthenticated",
|
|
reason
|
|
};
|
|
}
|
|
|
|
// pkg/dist-src/is-rate-limit-error.js
|
|
function isRateLimitError(error) {
|
|
if (error.status !== 403) {
|
|
return false;
|
|
}
|
|
if (!error.response) {
|
|
return false;
|
|
}
|
|
return error.response.headers["x-ratelimit-remaining"] === "0";
|
|
}
|
|
|
|
// pkg/dist-src/is-abuse-limit-error.js
|
|
var REGEX_ABUSE_LIMIT_MESSAGE = /\babuse\b/i;
|
|
function isAbuseLimitError(error) {
|
|
if (error.status !== 403) {
|
|
return false;
|
|
}
|
|
return REGEX_ABUSE_LIMIT_MESSAGE.test(error.message);
|
|
}
|
|
|
|
// pkg/dist-src/hook.js
|
|
async function hook(reason, request, route, parameters) {
|
|
const endpoint = request.endpoint.merge(
|
|
route,
|
|
parameters
|
|
);
|
|
return request(endpoint).catch((error) => {
|
|
if (error.status === 404) {
|
|
error.message = `Not found. May be due to lack of authentication. Reason: ${reason}`;
|
|
throw error;
|
|
}
|
|
if (isRateLimitError(error)) {
|
|
error.message = `API rate limit exceeded. This maybe caused by the lack of authentication. Reason: ${reason}`;
|
|
throw error;
|
|
}
|
|
if (isAbuseLimitError(error)) {
|
|
error.message = `You have triggered an abuse detection mechanism. This maybe caused by the lack of authentication. Reason: ${reason}`;
|
|
throw error;
|
|
}
|
|
if (error.status === 401) {
|
|
error.message = `Unauthorized. "${endpoint.method} ${endpoint.url}" failed most likely due to lack of authentication. Reason: ${reason}`;
|
|
throw error;
|
|
}
|
|
if (error.status >= 400 && error.status < 500) {
|
|
error.message = error.message.replace(
|
|
/\.?$/,
|
|
`. May be caused by lack of authentication (${reason}).`
|
|
);
|
|
}
|
|
throw error;
|
|
});
|
|
}
|
|
|
|
// pkg/dist-src/index.js
|
|
var createUnauthenticatedAuth = function createUnauthenticatedAuth2(options) {
|
|
if (!options || !options.reason) {
|
|
throw new Error(
|
|
"[@octokit/auth-unauthenticated] No reason passed to createUnauthenticatedAuth"
|
|
);
|
|
}
|
|
return Object.assign(auth.bind(null, options.reason), {
|
|
hook: hook.bind(null, options.reason)
|
|
});
|
|
};
|
|
export {
|
|
createUnauthenticatedAuth
|
|
};
|