mirror of
https://github.com/Jackett/Jackett.git
synced 2025-09-17 17:34:09 +02:00
Update redeliver-failed-deliveries.js
This commit is contained in:
@@ -1,18 +1,28 @@
|
|||||||
// This script uses GitHub's Octokit SDK to make API requests. For more information, see "[AUTOTITLE](/rest/guides/scripting-with-the-rest-api-and-javascript)."
|
// This script uses GitHub's Octokit SDK to make API requests. For more information, see "[AUTOTITLE](/rest/guides/scripting-with-the-rest-api-and-javascript)."
|
||||||
const { Octokit } = require("octokit");
|
const { App, Octokit } = require("octokit");
|
||||||
|
|
||||||
//
|
//
|
||||||
async function checkAndRedeliverWebhooks() {
|
async function checkAndRedeliverWebhooks() {
|
||||||
// Get the values of environment variables that were set by the GitHub Actions workflow.
|
// Get the values of environment variables that were set by the GitHub Actions workflow.
|
||||||
|
const APP_ID = process.env.APP_ID;
|
||||||
|
const PRIVATE_KEY = process.env.PRIVATE_KEY;
|
||||||
const TOKEN = process.env.TOKEN;
|
const TOKEN = process.env.TOKEN;
|
||||||
const ORGANIZATION_NAME = process.env.ORGANIZATION_NAME;
|
const LAST_REDELIVERY_VARIABLE_NAME = process.env.ISSUEBOT_LAST_REDELIVERY_TIME;
|
||||||
const HOOK_ID = process.env.HOOK_ID;
|
|
||||||
const LAST_REDELIVERY_VARIABLE_NAME = process.env.ISSUEBOT_LAST_DELIVERY_TIME;
|
|
||||||
|
|
||||||
const WORKFLOW_REPO_NAME = process.env.WORKFLOW_REPO_NAME;
|
const WORKFLOW_REPO_NAME = process.env.WORKFLOW_REPO;
|
||||||
const WORKFLOW_REPO_OWNER = process.env.WORKFLOW_REPO_OWNER;
|
const WORKFLOW_REPO_OWNER = process.env.WORKFLOW_REPO_OWNER;
|
||||||
|
|
||||||
|
// Create an instance of the octokit `App` using the app ID and private key values that were set in the GitHub Actions workflow.
|
||||||
|
//
|
||||||
|
// This will be used to make API requests to the webhook-related endpoints.
|
||||||
|
const app = new App({
|
||||||
|
appId: APP_ID,
|
||||||
|
privateKey: PRIVATE_KEY,
|
||||||
|
});
|
||||||
|
|
||||||
// Create an instance of `Octokit` using the token values that were set in the GitHub Actions workflow.
|
// Create an instance of `Octokit` using the token values that were set in the GitHub Actions workflow.
|
||||||
|
//
|
||||||
|
// This will be used to update the configuration variable that stores the last time that this script ran.
|
||||||
const octokit = new Octokit({
|
const octokit = new Octokit({
|
||||||
auth: TOKEN,
|
auth: TOKEN,
|
||||||
});
|
});
|
||||||
@@ -31,12 +41,7 @@ async function checkAndRedeliverWebhooks() {
|
|||||||
const newWebhookRedeliveryTime = Date.now().toString();
|
const newWebhookRedeliveryTime = Date.now().toString();
|
||||||
|
|
||||||
// Get the webhook deliveries that were delivered after `lastWebhookRedeliveryTime`.
|
// Get the webhook deliveries that were delivered after `lastWebhookRedeliveryTime`.
|
||||||
const deliveries = await fetchWebhookDeliveriesSince({
|
const deliveries = await fetchWebhookDeliveriesSince({lastWebhookRedeliveryTime, app});
|
||||||
lastWebhookRedeliveryTime,
|
|
||||||
organizationName: ORGANIZATION_NAME,
|
|
||||||
hookId: HOOK_ID,
|
|
||||||
octokit,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Consolidate deliveries that have the same globally unique identifier (GUID). The GUID is constant across redeliveries of the same delivery.
|
// Consolidate deliveries that have the same globally unique identifier (GUID). The GUID is constant across redeliveries of the same delivery.
|
||||||
let deliveriesByGuid = {};
|
let deliveriesByGuid = {};
|
||||||
@@ -63,12 +68,7 @@ async function checkAndRedeliverWebhooks() {
|
|||||||
|
|
||||||
// Redeliver any failed deliveries.
|
// Redeliver any failed deliveries.
|
||||||
for (const deliveryId of failedDeliveryIDs) {
|
for (const deliveryId of failedDeliveryIDs) {
|
||||||
await redeliverWebhook({
|
await redeliverWebhook({deliveryId, app});
|
||||||
deliveryId,
|
|
||||||
organizationName: ORGANIZATION_NAME,
|
|
||||||
hookId: HOOK_ID,
|
|
||||||
octokit,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the configuration variable (or create the variable if it doesn't already exist) to store the time that this script started.
|
// Update the configuration variable (or create the variable if it doesn't already exist) to store the time that this script started.
|
||||||
@@ -108,17 +108,10 @@ async function checkAndRedeliverWebhooks() {
|
|||||||
// If a page of results includes deliveries that occurred before `lastWebhookRedeliveryTime`,
|
// If a page of results includes deliveries that occurred before `lastWebhookRedeliveryTime`,
|
||||||
// it will store only the deliveries that occurred after `lastWebhookRedeliveryTime` and then stop.
|
// it will store only the deliveries that occurred after `lastWebhookRedeliveryTime` and then stop.
|
||||||
// Otherwise, it will store all of the deliveries from the page and request the next page.
|
// Otherwise, it will store all of the deliveries from the page and request the next page.
|
||||||
async function fetchWebhookDeliveriesSince({
|
async function fetchWebhookDeliveriesSince({lastWebhookRedeliveryTime, app}) {
|
||||||
lastWebhookRedeliveryTime,
|
const iterator = app.octokit.paginate.iterator(
|
||||||
organizationName,
|
"GET /app/hook/deliveries",
|
||||||
hookId,
|
|
||||||
octokit,
|
|
||||||
}) {
|
|
||||||
const iterator = octokit.paginate.iterator(
|
|
||||||
"GET /orgs/{org}/hooks/{hook_id}/deliveries",
|
|
||||||
{
|
{
|
||||||
org: organizationName,
|
|
||||||
hook_id: hookId,
|
|
||||||
per_page: 100,
|
per_page: 100,
|
||||||
headers: {
|
headers: {
|
||||||
"x-github-api-version": "2022-11-28",
|
"x-github-api-version": "2022-11-28",
|
||||||
@@ -153,20 +146,10 @@ async function fetchWebhookDeliveriesSince({
|
|||||||
}
|
}
|
||||||
|
|
||||||
// This function will redeliver a failed webhook delivery.
|
// This function will redeliver a failed webhook delivery.
|
||||||
async function redeliverWebhook({
|
async function redeliverWebhook({deliveryId, app}) {
|
||||||
deliveryId,
|
await app.octokit.request("POST /app/hook/deliveries/{delivery_id}/attempts", {
|
||||||
organizationName,
|
|
||||||
hookId,
|
|
||||||
octokit,
|
|
||||||
}) {
|
|
||||||
await octokit.request(
|
|
||||||
"POST /orgs/{org}/hooks/{hook_id}/deliveries/{delivery_id}/attempts",
|
|
||||||
{
|
|
||||||
org: organizationName,
|
|
||||||
hook_id: hookId,
|
|
||||||
delivery_id: deliveryId,
|
delivery_id: deliveryId,
|
||||||
}
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function gets the value of a configuration variable.
|
// This function gets the value of a configuration variable.
|
||||||
|
Reference in New Issue
Block a user