From 5b2e8d796b2ce4d0a1a9214ce7a7caad7cd201c6 Mon Sep 17 00:00:00 2001 From: Mara Date: Mon, 4 Mar 2024 17:43:38 +0100 Subject: [PATCH] fix: save rateLimit for repository --- src/commands/index.ts | 10 +++++++++- src/main.ts | 8 ++++++++ src/settings/interface.ts | 2 ++ src/utils/data_validation_test.ts | 22 +++++++--------------- src/utils/parse_frontmatter.ts | 1 + 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/commands/index.ts b/src/commands/index.ts index 60261a53..31a0ab58 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -174,7 +174,15 @@ export async function shareOneNote( const frontmatter = frontmatterFromFile(file, PublisherManager.plugin); try { const repoFrontmatter = getRepoFrontmatter(settings, repository, frontmatter); - const isValid = await checkRepositoryValidityWithRepoFrontmatter(PublisherManager, repoFrontmatter); + let isValid: boolean; + if (repoFrontmatter instanceof Array) { + const isValidArray = []; + for (const repo of repoFrontmatter) { + isValidArray.push(await checkRepositoryValidityWithRepoFrontmatter(PublisherManager, repo)); + } + isValid = isValidArray.every((v) => v === true); + } else isValid = await checkRepositoryValidityWithRepoFrontmatter(PublisherManager, repoFrontmatter); + const multiRepo: MultiRepoProperties = { frontmatter: repoFrontmatter, repo: repository diff --git a/src/main.ts b/src/main.ts index d75e2649..242830c8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -214,6 +214,14 @@ export default class GithubPublisher extends Plugin { await this.saveSettings(); } + for (const repository of this.settings.github.otherRepo) { + const repoOctokit = await this.reloadOctokit(repository.smartKey); + repository.verifiedRepo = await checkRepositoryValidity(repoOctokit, repository, null, false); + repository.rateLimit = await verifyRateLimitAPI(repoOctokit.octokit, this.settings); + } + await this.saveSettings(); + + this.registerEvent( //@ts-ignore this.app.workspace.on("file-menu", (menu: Menu, folder: TAbstractFile) => { diff --git a/src/settings/interface.ts b/src/settings/interface.ts index 7a75b0ee..002efee2 100644 --- a/src/settings/interface.ts +++ b/src/settings/interface.ts @@ -29,6 +29,7 @@ export interface Repository { branch: string; automaticallyMergePR: boolean; verifiedRepo?: boolean; + rateLimit?: number; token?: string; api: { tiersForApi: GithubTiersVersion; @@ -416,6 +417,7 @@ export interface RepoFrontmatter { automaticallyMergePR: boolean; verifiedRepo?: boolean; path?: Path; + rateLimit?: number; dryRun: { enable: boolean; folderName: string; diff --git a/src/utils/data_validation_test.ts b/src/utils/data_validation_test.ts index ae1c8cda..13c5784a 100644 --- a/src/utils/data_validation_test.ts +++ b/src/utils/data_validation_test.ts @@ -331,34 +331,25 @@ export async function checkRepositoryValidity( /** * Check the validity of the repository settings, from the frontmatter of the file or from the settings of the plugin * @param {GithubBranch} PublisherManager - * @param {RepoFrontmatter | RepoFrontmatter[]} repoFrontmatter + * @param {RepoFrontmatter} repoFrontmatter * @param {number} numberOfFile the number of file to publish * @return {Promise} */ export async function checkRepositoryValidityWithRepoFrontmatter( PublisherManager: GithubBranch, - repoFrontmatter: RepoFrontmatter | RepoFrontmatter[], + repoFrontmatter: RepoFrontmatter, numberOfFile: number=1 ): Promise { const settings = PublisherManager.settings; if (settings.github.dryRun.enable) return true; try { - /** - * verify for each repoFrontmatter if verifiedRepo is true - */ - let verified = false; - if (repoFrontmatter instanceof Array) { - verified = repoFrontmatter.every((repo) => { - return repo.verifiedRepo; - }); - } else if (repoFrontmatter.verifiedRepo) { - verified = true; - } - if (verified && settings.github.rateLimit > 0) return true; + const verified = repoFrontmatter.verifiedRepo; + const rateLimit = repoFrontmatter.rateLimit; + if (verified && rateLimit) return true; const isNotEmpty = await checkEmptyConfiguration(repoFrontmatter, PublisherManager.plugin); if (isNotEmpty) { await PublisherManager.checkRepository(repoFrontmatter, true); - if (settings.github.rateLimit === 0 || numberOfFile > 20) { + if (repoFrontmatter?.rateLimit === 0 || numberOfFile > 20) { return await verifyRateLimitAPI(PublisherManager.octokit, settings, false, numberOfFile) > 0; } return true; @@ -385,6 +376,7 @@ export function defaultRepo(settings: GitHubPublisherSettings): Repository { branch: settings.github.branch, automaticallyMergePR: settings.github.automaticallyMergePR, verifiedRepo: settings.github.verifiedRepo, + rateLimit: settings.github.rateLimit, api: { tiersForApi: settings.github.api.tiersForApi, hostname: settings.github.api.hostname, diff --git a/src/utils/parse_frontmatter.ts b/src/utils/parse_frontmatter.ts index ff16fe14..7af21936 100644 --- a/src/utils/parse_frontmatter.ts +++ b/src/utils/parse_frontmatter.ts @@ -103,6 +103,7 @@ export function getRepoFrontmatter( commitMsg: github.workflow.commitMessage, automaticallyMergePR: github.automaticallyMergePR, verifiedRepo: github.verifiedRepo ?? false, + rateLimit: github.rateLimit, dryRun: { ...settings.github.dryRun, autoclean: settings.upload.autoclean.enable && settings.github.dryRun.enable