Skip to content

Commit

Permalink
fix: save rateLimit for repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Mar 4, 2024
1 parent 1e554c2 commit 5b2e8d7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
10 changes: 9 additions & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 2 additions & 0 deletions src/settings/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface Repository {
branch: string;
automaticallyMergePR: boolean;
verifiedRepo?: boolean;
rateLimit?: number;
token?: string;
api: {
tiersForApi: GithubTiersVersion;
Expand Down Expand Up @@ -416,6 +417,7 @@ export interface RepoFrontmatter {
automaticallyMergePR: boolean;
verifiedRepo?: boolean;
path?: Path;
rateLimit?: number;
dryRun: {
enable: boolean;
folderName: string;
Expand Down
22 changes: 7 additions & 15 deletions src/utils/data_validation_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>}
*/
export async function checkRepositoryValidityWithRepoFrontmatter(
PublisherManager: GithubBranch,
repoFrontmatter: RepoFrontmatter | RepoFrontmatter[],
repoFrontmatter: RepoFrontmatter,
numberOfFile: number=1
): Promise<boolean> {
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;
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/utils/parse_frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5b2e8d7

Please sign in to comment.