Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@aws-sdk/client-secrets-manager": "^3.1004.0",
"@hono/node-server": "^1.19.11",
"@octokit/auth-app": "^8.2.0",
"@octokit/plugin-retry": "^8.1.0",
"@octokit/rest": "^22.0.1",
"hono": "^4.12.5",
"jose": "^6.2.1",
Expand Down
29 changes: 10 additions & 19 deletions server/src/access-token-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Octokit as OctokitCore} from '@octokit/core';
import {paginateRest} from "@octokit/plugin-paginate-rest";
import {restEndpointMethods} from "@octokit/plugin-rest-endpoint-methods";
import {retry as retryPlugin} from '@octokit/plugin-retry';
import {z} from 'zod';
import type {components} from '@octokit/openapi-types';
import {createAppAuth} from '@octokit/auth-app';
Expand All @@ -17,7 +18,6 @@ import {
isRecord,
mapObjectEntries,
resultOf,
retry,
unique,
} from './common/common-utils.js';
import {
Expand All @@ -39,7 +39,7 @@ import {logger} from './logger.js';
import {RestEndpointMethodTypes} from '@octokit/rest';

const Octokit = OctokitCore
.plugin(restEndpointMethods).plugin(paginateRest);
.plugin(restEndpointMethods, paginateRest, retryPlugin);

const ACCESS_POLICY_MAX_SIZE = 100 * 1024; // 100kb
const GITHUB_API_CONCURRENCY_LIMIT = limit(8);
Expand Down Expand Up @@ -604,12 +604,11 @@ async function getAccessPolicy<T extends typeof GitHubAccessPolicySchema>(client
preprocessor: (value: unknown) => unknown,
}): Promise<z.infer<T>> {
const policyValue = await findFirstNotNull(paths, async (path) => {
try {
return await getRepositoryFileContent(client, {owner, repo, path, maxSize: ACCESS_POLICY_MAX_SIZE});
} catch (error) {
logger.error({owner, repo, path, error: String(error)}, 'Failed to get access policy file content');
return null;
}
return getRepositoryFileContent(client, {owner, repo, path, maxSize: ACCESS_POLICY_MAX_SIZE})
.catch((error) => {
logger.error({owner, repo, path, error: String(error)}, 'Failed to get access policy file content');
return null;
});
});
if (!policyValue) {
throw new GithubAccessPolicyError(`Access policy not found`);
Expand Down Expand Up @@ -920,17 +919,9 @@ function formatAccessPolicyError(error: GithubAccessPolicyError) {
async function getAppInstallation(client: Octokit, {owner}: {
owner: string
}): Promise<GitHubAppInstallation | null> {
// WORKAROUND: for some reason sometimes the request connection gets closed unexpectedly (line closed),
// therefore, we retry on any error
return retry(
async () => client.rest.apps.getUserInstallation({username: owner})
.then((res) => res.data)
.catch(async (error) => (error.status === Status.NOT_FOUND ? null : _throw(error))),
{
delay: 1000,
retries: 3,
},
);
return client.rest.apps.getUserInstallation({username: owner})
.then((res) => res.data)
.catch(async (error) => (error.status === Status.NOT_FOUND ? null : _throw(error)));
}

/**
Expand Down
51 changes: 0 additions & 51 deletions server/src/common/common-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,57 +153,6 @@ export function filterObjectEntries<V>(
return Object.fromEntries(Object.entries(object).filter(fn));
}

/**
* This function will return a promise that will resolve after the given time
* @param ms - time in milliseconds
* @return promise
*/
export function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

/**
* This function will return a promise that will resolve after the given time
* @param fn - function to retry
* @param options - retry options
* @param options.retries - number of retries
* @param options.delay - delay between retries
* @param options.onRetry - function to call on retry, return false to stop retrying
* @param options.onError - function to call on error, return false to stop retrying
* @return promise
*/
export async function retry<T>(
fn: () => Promise<T>,
options: {
retries: number,
delay: number,
onRetry?: (result: T) => boolean | Promise<boolean>,
onError?: (error: unknown) => boolean | Promise<boolean>,
} = {
retries: 1,
delay: 1000,
},
): Promise<T> {
const {retries, delay} = options;
for (let attempts = 0; attempts < retries; attempts++) {
try {
const result = await fn();
if (!options.onRetry || !options.onRetry(result)) {
return result;
}
} catch (error: unknown) {
if (options.onError && !options.onError(error)) {
throw error;
}
if (attempts >= retries) {
throw error;
}
await sleep(delay);
}
}
throw Error('Illegal state');
}

/**
* Indent string
* @param string - string to indent
Expand Down
Loading