Skip to content

Commit

Permalink
feat(upload): prevent forced update (default: false)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Oct 4, 2024
2 parents b7053a2 + 37ca281 commit 7f92528
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 24 deletions.
18 changes: 10 additions & 8 deletions src/GitHub/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ export class GithubBranch extends FilesManagement {
await this.newBranchOnRepo(repo);
}
}

private async findMainBranch(prop: Properties) {
const allBranch = await this.octokit.request("GET /repos/{owner}/{repo}/branches", {
owner: prop.owner,
repo: prop.repo,
});
return allBranch.data.find(
(branch: { name: string }) => branch.name === prop.branch
);
return allBranch.data.find((branch: { name: string }) => branch.name === prop.branch);
}

/**
Expand Down Expand Up @@ -258,17 +256,21 @@ export class GithubBranch extends FilesManagement {
this.console.info(
i18next.t("commands.checkValidity.repoExistsTestBranch", { repo })
);

const branchExist = await this.findMainBranch(repo);
if (!branchExist) {
const errorMsg = i18next.t("commands.checkValidity.inBranch.error404", {repo});
const errorMsg = i18next.t("commands.checkValidity.inBranch.error404", {
repo,
});
this.console.fatal(errorMsg);

break;
}
this.console.info(i18next.t("commands.checkValidity.success", {repo}));
if (!silent) {
this.console.noticeSuccess(i18next.t("commands.checkValidity.success", {repo}));
this.console.noticeSuccess(
i18next.t("commands.checkValidity.success", {repo})
);
}
}
} catch (e) {
Expand Down
28 changes: 28 additions & 0 deletions src/GitHub/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,4 +606,32 @@ export class FilesManagement extends Publisher {
}
return newFiles;
}

async wasEditedSinceLastSync(
file: TFile,
repo: Repository | null,
repoPath: string
): Promise<boolean> {
//first check if the file exist in github
if (this.settings.embed.forcePush || this.settings.embed.forcePush == null)
return true;
const githubFile = await this.octokit.request("GET /repos/{owner}/{repo}/commits", {
owner: repo?.user ?? this.settings.github.user,
repo: repo?.repo ?? this.settings.github.repo,
path: repoPath,
});
if (githubFile.status !== 200) return true; //doesn't exists
const vaultEditedTime = new Date(file.stat.mtime);
const lastCommittedFile = githubFile.data[0];
if (
!lastCommittedFile ||
!lastCommittedFile.commit ||
!lastCommittedFile.commit.committer ||
!lastCommittedFile.commit.committer.date
) {
return true;
}
const githubDate = new Date(lastCommittedFile.commit.committer.date);
return vaultEditedTime > githubDate;
}
}
37 changes: 27 additions & 10 deletions src/GitHub/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,23 @@ export default class Publisher {
) {
const shareFiles = new FilesManagement(this.octokit, this.plugin);
let frontmatter = frontmatterFromFile(file, this.plugin, null);
const filePath = getReceiptFolder(
file,
repo.repository,
this.plugin,
repo.frontmatter
);
if (!isShared(frontmatter, this.settings, file, repo.repository)) return false;
if (!(await shareFiles.wasEditedSinceLastSync(file, repo.repository, filePath))) {
const msg = i18next.t("publish.upToDate", {
file: file.name,
repo: `${repo.repository?.user ?? this.settings.github.user}/${
repo.repository?.repo ?? this.settings.github.repo
}:${repo.repository?.branch ?? this.branchName}`,
});
new Notice(msg);
return false;
}
frontmatter = mergeFrontmatter(
frontmatter,
sourceFrontmatter,
Expand All @@ -182,6 +198,7 @@ export default class Publisher {
const prop = getProperties(this.plugin, repo.repository, frontmatter);
const isNotEmpty = await checkEmptyConfiguration(prop, this.plugin);
repo.frontmatter = prop;

if (
fileHistory.includes(file) ||
!checkIfRepoIsInAnother(prop, repo.frontmatter) ||
Expand All @@ -207,24 +224,24 @@ export default class Publisher {
frontmatterRepository,
frontmatterSettingsFromFile
);
let embedFiles = shareFiles.getSharedEmbed(file, frontmatterSettings);
embedFiles = await shareFiles.getMetadataLinks(
file,
embedFiles,
frontmatterSettings
);
const linkedFiles = shareFiles.getLinkedByEmbedding(file);

let text = await this.vault.cachedRead(file);
const multiProperties: MultiProperties = {
plugin: this.plugin,
frontmatter: {
general: frontmatterSettings,
prop: repo.frontmatter,
},
repository: repo.repository,
filepath: getReceiptFolder(file, repo.repository, this.plugin, repo.frontmatter),
filepath: filePath,
};

let embedFiles = shareFiles.getSharedEmbed(file, frontmatterSettings);
embedFiles = await shareFiles.getMetadataLinks(
file,
embedFiles,
frontmatterSettings
);
const linkedFiles = shareFiles.getLinkedByEmbedding(file);
let text = await this.vault.cachedRead(file);
text = await mainConverting(text, file, frontmatter, linkedFiles, multiProperties);
const path = multiProperties.filepath;
const prop = Array.isArray(repo.frontmatter)
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/locales
1 change: 0 additions & 1 deletion src/interfaces/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export const DEFAULT_SETTINGS: Partial<EnveloppeSettings> = {
unHandledObsidianExt: [],
sendSimpleLinks: true,
forcePush: true,

},
plugin: {
shareKey: "share",
Expand Down
1 change: 0 additions & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,6 @@ export class EnveloppeSettingsTab extends PluginSettingTab {
});
});


if (embedSettings.notes) {
new Setting(this.settingsPage)
.setName(i18next.t("settings.embed.links.title"))
Expand Down
6 changes: 3 additions & 3 deletions src/utils/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ export class Logs {
this.logger.debug(...messages);
this.notif(messages);
}

trace(...messages: unknown[]) {
this.logger.trace(...messages);
this.notif(messages);
}

silly(...messages: unknown[]) {
this.logger.silly(...messages);
this.notif(messages);
Expand Down Expand Up @@ -132,7 +132,7 @@ export class Logs {
cls: ["error", "enveloppe", "icons", "notification"],
});
const html = sanitizeHTMLToDom(message);
setIcon(notifSpan, "mail-question");
setIcon(notifSpan, "alert-triangle");
notif
.createSpan({
cls: ["error", "enveloppe", "notification"],
Expand Down

0 comments on commit 7f92528

Please sign in to comment.