From 0868c7b77c2a2c19356f4db6d9fab92c6cad7945 Mon Sep 17 00:00:00 2001 From: Mara Date: Thu, 2 Feb 2023 22:34:45 +0100 Subject: [PATCH] feat: adding github entreprise API see #98 --- plugin/i18n/locales/en.ts | 10 ++++++++ plugin/i18n/locales/fr.ts | 12 +++++++++- plugin/i18n/locales/ru.ts | 10 ++++++++ plugin/i18n/locales/zh-cn.ts | 10 ++++++++ plugin/main.ts | 13 +++++++++-- plugin/settings.ts | 45 ++++++++++++++++++++++++++---------- plugin/settings/interface.ts | 10 +++++++- 7 files changed, 94 insertions(+), 16 deletions(-) diff --git a/plugin/i18n/locales/en.ts b/plugin/i18n/locales/en.ts index 69b96cc4..46d97489 100644 --- a/plugin/i18n/locales/en.ts +++ b/plugin/i18n/locales/en.ts @@ -37,6 +37,16 @@ export default { exportSettings: "Export settings", importSettings: "Import settings", github: { + apiType: { + title: "API Type", + desc: "Choose between the Github API or the Github Enterprise API (for Github Enterprise users).", + hostname: "Github Enterprise Hostname", + hostnameDesc: "The hostname of your Github Enterprise instance.", + dropdown: { + free: "Free/Pro/Team (default)", + enterprise: "Enterprise", + } + }, githubConfiguration: "Github Configuration", repoName: "Repo Name", repoNameDesc: diff --git a/plugin/i18n/locales/fr.ts b/plugin/i18n/locales/fr.ts index df31d0b3..e7ddbdcb 100644 --- a/plugin/i18n/locales/fr.ts +++ b/plugin/i18n/locales/fr.ts @@ -40,6 +40,16 @@ export default { exportSettings: "Exporter les paramètres", importSettings: "Importer des paramètres", github: { + apiType: { + title: "Type d'API", + desc: "Choisir entre l'API GitHub ou l'API pour GitHub Entreprise (uniquement pour les utilisateur de GitHub entreprise).", + hostname: "Instance GitHub Entreprise", + hostnameDesc: "Le nom de votre instance GitHub Entreprise.", + dropdown: { + free: "Free/Pro/Team (défaut)", + enterprise: "Entreprise", + } + }, githubConfiguration: "Configuration GitHub", repoName: "Nom du dépôt", repoNameDesc: @@ -58,7 +68,7 @@ export default { testConnection: "Tester la connexion", }, uploadConfig: { - uploadConfig: "Configuration d'upload", //désolée du franglais ici mais je trouve pas de traduction propre + title: "Configuration du transfert", //désolée du franglais ici mais je trouve pas de traduction propre pathSetting: "Paramètres du chemin d'accès", folderBehavior: "Comportement du dossier", folderBehaviorDesc: diff --git a/plugin/i18n/locales/ru.ts b/plugin/i18n/locales/ru.ts index 77c92ed9..ceaf0ddb 100644 --- a/plugin/i18n/locales/ru.ts +++ b/plugin/i18n/locales/ru.ts @@ -40,6 +40,16 @@ export default { exportSettings: "Export settings", importSettings: "Import settings", github: { + apiType: { + title: "API Type", + desc: "Choose between the Github API or the Github Enterprise API (for Github Enterprise users).", + hostname: "Github Enterprise Hostname", + hostnameDesc: "The hostname of your Github Enterprise instance.", + dropdown: { + free: "Free/Pro/Team (default)", + enterprise: "Enterprise", + } + }, githubConfiguration: "Настройки интеграции с Github", repoName: "Repo Name", repoNameDesc: diff --git a/plugin/i18n/locales/zh-cn.ts b/plugin/i18n/locales/zh-cn.ts index 58cf7ad2..6e6ff970 100644 --- a/plugin/i18n/locales/zh-cn.ts +++ b/plugin/i18n/locales/zh-cn.ts @@ -36,6 +36,16 @@ export default { exportSettings: "Export settings", importSettings: "Import settings", github: { + apiType: { + title: "API Type", + desc: "Choose between the Github API or the Github Enterprise API (for Github Enterprise users).", + hostname: "Github Enterprise Hostname", + hostnameDesc: "The hostname of your Github Enterprise instance.", + dropdown: { + free: "Free/Pro/Team (default)", + enterprise: "Enterprise", + } + }, githubConfiguration: "Github设置", repoName: "仓库名", repoNameDesc: "你博客所在的github仓库名", diff --git a/plugin/main.ts b/plugin/main.ts index b36e8dbe..136957e2 100644 --- a/plugin/main.ts +++ b/plugin/main.ts @@ -1,6 +1,6 @@ import {FrontMatterCache, Menu, Plugin, TFile} from "obsidian"; import {GithubPublisherSettings} from "./settings"; -import {DEFAULT_SETTINGS, GitHubPublisherSettings, RepoFrontmatter} from "./settings/interface"; +import {DEFAULT_SETTINGS, GitHubPublisherSettings, GithubTiersVersion, RepoFrontmatter} from "./settings/interface"; import {convertOldSettings, disablePublish, getRepoFrontmatter,} from "./src/utils"; import {GithubBranch} from "./publishing/branch"; import {Octokit} from "@octokit/core"; @@ -38,7 +38,16 @@ export default class GithubPublisher extends Plugin { * Create a new instance of Octokit to load a new instance of GithubBranch */ reloadOctokit() { - const octokit = new Octokit({ auth: this.settings.GhToken }); + let octokit: Octokit; + if (this.settings.tiersForApi === GithubTiersVersion.entreprise && this.settings.hostname.length > 0) { + octokit = new Octokit( + { + baseUrl: `${this.settings.hostname}/api/v3`, + auth: this.settings.GhToken + }); + } else { + octokit = new Octokit({auth: this.settings.GhToken}); + } return new GithubBranch( this.settings, octokit, diff --git a/plugin/settings.ts b/plugin/settings.ts index ee20d12d..8a0e2fc6 100644 --- a/plugin/settings.ts +++ b/plugin/settings.ts @@ -10,7 +10,7 @@ import { import { folderSettings, TextCleaner, - PUBLISHER_TABS, + PUBLISHER_TABS, GithubTiersVersion, } from "./settings/interface"; import {settings, StringFunc, subSettings, t} from "./i18n"; import { @@ -129,6 +129,36 @@ export class GithubPublisherSettings extends PluginSettingTab { } renderGithubConfiguration() { + new Setting(this.settingsPage) + .setName(subSettings("github.apiType.title") as string) + .setDesc(subSettings("github.apiType.desc") as string) + .addDropdown((dropdown) => { + dropdown + .addOption(GithubTiersVersion.free, subSettings("github.apiType.dropdown.free") as string) + .addOption(GithubTiersVersion.entreprise, subSettings("github.apiType.dropdown.enterprise") as string) + .setValue(this.plugin.settings.tiersForApi) + .onChange(async (value) => { + this.plugin.settings.tiersForApi = value as GithubTiersVersion; + await this.plugin.saveSettings(); + this.settingsPage.empty(); + this.renderGithubConfiguration(); + }); + }); + if (this.plugin.settings.tiersForApi === GithubTiersVersion.entreprise) { + new Setting(this.settingsPage) + .setName(subSettings("github.apiType.hostname") as string) + .setDesc(subSettings("github.apiType.hostnameDesc") as string) + .addText((text) => + text + .setPlaceholder("https://github.mycompany.com") + .setValue(this.plugin.settings.hostname) + .onChange(async (value) => { + this.plugin.settings.hostname = value.trim(); + await this.plugin.saveSettings(); + }) + ); + } + new Setting(this.settingsPage) .setName(settings("github", "repoName") as string) .setDesc(settings("github", "repoNameDesc") as string) @@ -208,16 +238,7 @@ export class GithubPublisherSettings extends PluginSettingTab { .setButtonText(settings("github", "testConnection") as string) .setClass("github-publisher-connect-button") .onClick(async () => { - const octokit = new Octokit({auth: this.plugin.settings.GhToken}); - console.log(octokit); - const publisher = new GithubBranch( - this.plugin.settings, - octokit, - this.app.vault, - this.app.metadataCache, - this.plugin, - ); - await checkRepositoryValidity(this.branchName, publisher, this.plugin.settings, null, this.app.metadataCache); + await checkRepositoryValidity(this.branchName, this.plugin.reloadOctokit(), this.plugin.settings, null, this.app.metadataCache); }) ); this.settingsPage.createEl("h3", { text: "Github Workflow" }); @@ -422,7 +443,7 @@ export class GithubPublisherSettings extends PluginSettingTab { const folderNoteSettings = new Setting(this.settingsPage) .setName(subSettings("textConversion.links.folderNote") as string) - .setClass("github-publisher") + .setClass("github-publisher-folderNote") .setDesc( subSettings("textConversion.links.folderNoteDesc") as string ) diff --git a/plugin/settings/interface.ts b/plugin/settings/interface.ts index f34ee0cf..6743fb2c 100644 --- a/plugin/settings/interface.ts +++ b/plugin/settings/interface.ts @@ -87,6 +87,8 @@ export interface GitHubPublisherSettings { convertInternalNonShared: boolean; frontmatterTitleRegex: string; frontmatterTitleReplacement: string; + tiersForApi: GithubTiersVersion; + hostname: string; } /** @@ -99,6 +101,10 @@ export enum folderSettings { fixed = "fixed", } +export enum GithubTiersVersion { + free = "Github Free/Pro/Team (default)", + entreprise = "Enterprise", +} /** * Default settings of the plugins * @type {{downloadedFolder: folderSettings.fixed, autoCleanUpExcluded: any[], subFolder: string, embedImage: boolean, dataviewFields: any[], githubName: string, useFrontmatterTitle: boolean, convertDataview: boolean, githubRepo: string, editorMenu: boolean, frontmatterTitleKey: string, metadataFileFields: any[], yamlFolderKey: string, folderDefaultName: string, copyLink: boolean, metadataExtractorPath: string, excludeDataviewValue: any[], GhToken: string, githubBranch: string, fileMenu: boolean, convertWikiLinks: boolean, embedNotes: boolean, folderNote: boolean, rootFolder: string, defaultImageFolder: string, mainLink: string, shareKey: string, workflowName: string, shareExternalModified: boolean, convertForGithub: boolean, autoCleanUp: boolean, inlineTags: boolean, logNotice: boolean, excludedFolder: any[], hardBreak: boolean, automaticallyMergePR: boolean, linkRemover: string, censorText: any[]}} @@ -150,6 +156,8 @@ export const DEFAULT_SETTINGS: GitHubPublisherSettings = { convertInternalNonShared: false, frontmatterTitleRegex: "", frontmatterTitleReplacement: "", + tiersForApi: GithubTiersVersion.free, + hostname: "", }; export interface MetadataExtractor { @@ -190,7 +198,7 @@ export const PUBLISHER_TABS = { icon: "cloud", }, "upload-configuration": { - name: "Upload Configuration", + name: settings("uploadConfig", "title") as string, icon: "upload", }, "text-conversion": {