Skip to content

Commit

Permalink
refactor: usage of logs & call
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Jun 1, 2024
1 parent dba35fd commit f096f40
Show file tree
Hide file tree
Showing 24 changed files with 412 additions and 462 deletions.
32 changes: 17 additions & 15 deletions src/GitHub/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import i18next from "i18next";
import { Notice } from "obsidian";
import { FilesManagement } from "src/GitHub/files";
import type Enveloppe from "src/main";
import { logs, notif } from "src/utils";
import type { Logs } from "../utils/logs";

export class GithubBranch extends FilesManagement {
console: Logs;
constructor(octokit: Octokit, plugin: Enveloppe) {
super(octokit, plugin);
this.console = plugin.console;
}

/**
Expand Down Expand Up @@ -48,8 +50,8 @@ export class GithubBranch extends FilesManagement {
ref: `refs/heads/${this.branchName}`,
sha: shaMainBranch,
});
notif(
{ settings: this.settings },
this.console.notif(
{},
i18next.t("publish.branch.success", { branchStatus: branch.status, repo: prop })
);
return branch.status === 201;
Expand All @@ -66,16 +68,16 @@ export class GithubBranch extends FilesManagement {
const mainBranch = allBranch.data.find(
(branch: { name: string }) => branch.name === this.branchName
);
notif(
{ settings: this.settings },
this.console.notif(
{},
i18next.t("publish.branch.alreadyExists", {
branchName: this.branchName,
repo: prop,
})
);
return !!mainBranch;
} catch (e) {
notif({ settings: this.settings, e: true }, e);
this.console.notif({ e: true }, e);
return false;
}
}
Expand All @@ -100,7 +102,7 @@ export class GithubBranch extends FilesManagement {
});
return pr.data.number;
} catch (e) {
logs({ settings: this.settings, e: true }, e);
this.console.logs({ e: true }, e);
//trying to get the last open PR number
try {
const pr = await this.octokit.request("GET /repos/{owner}/{repo}/pulls", {
Expand All @@ -111,8 +113,8 @@ export class GithubBranch extends FilesManagement {
return pr.data[0]?.number || 0;
} catch (e) {
// there is no open PR and impossible to create a new one
notif(
{ settings: this.settings, e: true },
this.console.notif(
{ e: true },
i18next.t("publish.branch.error", { error: e, repo: prop })
);
return 0;
Expand All @@ -138,7 +140,7 @@ export class GithubBranch extends FilesManagement {
);
return branch.status === 200;
} catch (e) {
logs({ settings: this.settings, e: true }, e);
this.console.logs({ e: true }, e);
return false;
}
}
Expand Down Expand Up @@ -171,7 +173,7 @@ export class GithubBranch extends FilesManagement {
);
return branch.status === 200;
} catch (e) {
notif({ settings: this.settings, e: true }, e);
this.console.notif({ e: true }, e);
new Notice(i18next.t("error.mergeconflic"));
return false;
}
Expand Down Expand Up @@ -215,7 +217,7 @@ export class GithubBranch extends FilesManagement {
}
return true;
} catch (e) {
logs({ settings: this.settings, e: true }, e);
this.console.logs({ e: true }, e);
new Notice(i18next.t("error.errorConfig", { repo: prop }));
return false;
}
Expand Down Expand Up @@ -252,8 +254,8 @@ export class GithubBranch extends FilesManagement {
});
//@ts-ignore
if (repoExist.status === 200) {
notif(
{ settings: this.settings },
this.console.notif(
{},
i18next.t("commands.checkValidity.repoExistsTestBranch", { repo })
);

Expand Down Expand Up @@ -281,7 +283,7 @@ export class GithubBranch extends FilesManagement {
}
}
} catch (e) {
logs({ settings: this.settings, e: true }, e);
this.console.logs({ e: true }, e);
new Notice(i18next.t("commands.checkValidity.error", { repo }));
break;
}
Expand Down
30 changes: 16 additions & 14 deletions src/GitHub/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import {
Vault,
} from "obsidian";
import type { FilesManagement } from "src/GitHub/files";
import { logs, notif, trimObject } from "src/utils";
import { trimObject } from "src/utils";
import { isAttachment, verifyRateLimitAPI } from "src/utils/data_validation_test";
import { frontmatterSettingsRepository } from "src/utils/parse_frontmatter";
import type Enveloppe from "../main";

/**
* Delete file from github, based on a list of file in the original vault
Expand Down Expand Up @@ -71,21 +72,22 @@ async function deleteFromGithubOneRepo(
repoProperties: MonoRepoProperties
): Promise<Deleted> {
const repo = repoProperties.frontmatter;
const pconsole = filesManagement.console;
if (repo.dryRun.autoclean) return cleanDryRun(silent, filesManagement, repoProperties);
if (!repo.autoclean) return { success: false, deleted: [], undeleted: [] };
const getAllFile = await filesManagement.getAllFileFromRepo(branchName, repo);
const settings = filesManagement.settings;
const octokit = filesManagement.octokit;
const { octokit, plugin } = filesManagement;
const filesInRepo = await filterGithubFile(getAllFile, settings, repo);
if (
(settings.github.rateLimit === 0 || filesInRepo.length > settings.github.rateLimit) &&
(await verifyRateLimitAPI(octokit, settings, false, filesInRepo.length)) === 0
(await verifyRateLimitAPI(octokit, plugin, false, filesInRepo.length)) === 0
) {
console.warn("Rate limited exceeded, please try again later");
return { success: false, deleted: [], undeleted: [] };
}
if (filesInRepo.length === 0) {
logs({ settings }, `No file to delete in ${repo.owner}/${repo.repo}`);
pconsole.logs({}, `No file to delete in ${repo.owner}/${repo.repo}`);
return { success: false, deleted: [], undeleted: [] };
}
const allSharedFiles = filesManagement.getAllFileWithPath(
Expand Down Expand Up @@ -119,12 +121,12 @@ async function deleteFromGithubOneRepo(
const isNeedToBeDeleted = isInObsidian ? isMarkdownForAnotherRepo : true;
if (isNeedToBeDeleted) {
const checkingIndex = file.file.contains(settings.upload.folderNote.rename)
? await checkIndexFiles(octokit, settings, file.file, repo)
? await checkIndexFiles(octokit, plugin, file.file, repo)
: false;
try {
if (!checkingIndex) {
notif(
{ settings },
pconsole.notif(
{},
`trying to delete file : ${file.file} from ${repo.owner}/${repo.repo}`
);
const reponse = await octokit.request(
Expand All @@ -147,7 +149,7 @@ async function deleteFromGithubOneRepo(
}
}
} catch (e) {
if (!(e instanceof DOMException)) logs({ settings, e: true }, e);
if (!(e instanceof DOMException)) pconsole.logs({ e: true }, e);
}
}
}
Expand Down Expand Up @@ -253,7 +255,7 @@ function parseYamlFrontmatter(contents: string): unknown {

async function checkIndexFiles(
octokit: Octokit,
settings: EnveloppeSettings,
plugin: Enveloppe,
path: string,
prop: Properties
): Promise<boolean> {
Expand Down Expand Up @@ -284,7 +286,7 @@ async function checkIndexFiles(
}
} catch (e) {
if (!(e instanceof DOMException)) {
notif({ settings, e: true }, e);
plugin.console.notif({ e: true }, e);
return false;
}
}
Expand All @@ -296,8 +298,8 @@ function cleanDryRun(
filesManagement: FilesManagement,
repoProperties: MonoRepoProperties
): Deleted {
const { vault, settings } = filesManagement;
const app = filesManagement.plugin.app;
const { vault, settings, console, plugin } = filesManagement;
const app = plugin.app;
const repo = repoProperties.frontmatter;
const dryRunFolderPath = normalizePath(
repo.dryRun.folderName
Expand Down Expand Up @@ -358,8 +360,8 @@ function cleanDryRun(
? indexFileDryRun(file as TFile, app.metadataCache)
: false;
if (!indexFile) {
notif(
{ settings },
console.notif(
{},
`[DRYRUN] trying to delete file : ${file.path} from ${dryRunFolderPath}`
);
vault.trash(file, false);
Expand Down
31 changes: 11 additions & 20 deletions src/GitHub/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@ import { getAPI, type Link } from "obsidian-dataview";
import { getImagePath, getReceiptFolder } from "src/conversion/file_path";
import Publisher from "src/GitHub/upload";
import type Enveloppe from "src/main";
import { logs } from "src/utils";
import { isAttachment, isShared } from "src/utils/data_validation_test";
import {
frontmatterFromFile,
getFrontmatterSettings,
getProperties,
} from "src/utils/parse_frontmatter";
import type { Logs } from "../utils/logs";

export class FilesManagement extends Publisher {
/**
* @param {Octokit} octokit The octokit instance
* @param {EnveloppeSettings} plugin The plugin
*/

console: Logs;
constructor(octokit: Octokit, plugin: Enveloppe) {
super(octokit, plugin);
this.console = plugin.console;
}

/**
Expand All @@ -45,7 +46,7 @@ export class FilesManagement extends Publisher {
sharedFile.push(file);
}
} catch (e) {
logs({ settings: this.settings, e: true }, e);
this.console.logs({ e: true }, e);
}
}
return sharedFile;
Expand Down Expand Up @@ -73,7 +74,7 @@ export class FilesManagement extends Publisher {
files.push(file as TFile);
}
} catch (e) {
logs({ settings: this.settings, e: true }, e);
this.console.logs({ e: true }, e);
}
}
}
Expand Down Expand Up @@ -220,7 +221,7 @@ export class FilesManagement extends Publisher {
linkedFiles.push(thisLinkedFile);
}
} catch (e) {
logs({ settings: this.settings }, e);
this.console.logs({}, e);
}
}
}
Expand Down Expand Up @@ -281,11 +282,7 @@ export class FilesManagement extends Publisher {
embedList.push(thisEmbed);
}
} catch (e) {
logs(
{ settings: this.settings, e: true },
`Error with this links : ${embedCache.link}`,
e
);
this.console.logs({ e: true }, `Error with this links : ${embedCache.link}`, e);
}
}
return [...new Set(embedList)];
Expand Down Expand Up @@ -347,11 +344,7 @@ export class FilesManagement extends Publisher {
fromWhat
) as TFile;
} catch (e) {
logs(
{ settings: this.settings, e: true },
`Error with this file : ${embed.displayText}`,
e
);
this.console.logs({ e: true }, `Error with this file : ${embed.displayText}`, e);
}
return undefined;
}
Expand Down Expand Up @@ -415,7 +408,7 @@ export class FilesManagement extends Publisher {
}
}
} catch (e) {
logs({ settings: this.settings, e: true }, e);
this.console.logs({ e: true }, e);
}
return filesInRepo;
}
Expand Down Expand Up @@ -578,7 +571,6 @@ export class FilesManagement extends Publisher {
* Get all edited file since the last sync, compared to the github repo
* @param {ConvertedLink[]} allFileWithPath - all shared file with their path
* @param {GithubRepo[]} githubSharedFiles - all file in the github repo
* @param {Vault} vault - vault
* @param {TFile[]} newFiles - new file to add to the repo
* @return {Promise<TFile[]>} newFiles - File to add in the repo
*/
Expand All @@ -600,12 +592,11 @@ export class FilesManagement extends Publisher {
fileInVault instanceof TFile &&
fileInVault?.extension === "md" &&
!fileInVault?.name.endsWith(".excalidraw.md");
console.log(fileInVault, isMarkdown, repoEditedTime, githubSharedFile);
if (fileInVault && isMarkdown) {
const vaultEditedTime = new Date(fileInVault.stat.mtime);
if (repoEditedTime && vaultEditedTime > repoEditedTime) {
logs(
{ settings: this.settings },
this.console.logs(
{},
`edited file : ${fileInVault.path} / ${vaultEditedTime} vs ${repoEditedTime}`
);
newFiles.push(fileInVault);
Expand Down
Loading

0 comments on commit f096f40

Please sign in to comment.