From befbffed44b8abbb88b10e8471d8745a99d379fa Mon Sep 17 00:00:00 2001 From: Alamion Date: Sun, 16 Mar 2025 14:21:54 +0300 Subject: [PATCH] Refactor code to fit Obsidian guidelines. --- manifest.json | 4 ++-- src/api/base.ts | 26 ++++++++++++++------------ src/settings/JiraSettingTab.ts | 22 ++++++++++------------ src/tools/debugLogging.ts | 20 ++++++++++++++++++++ src/tools/sectionTools.ts | 4 +++- 5 files changed, 49 insertions(+), 27 deletions(-) create mode 100644 src/tools/debugLogging.ts diff --git a/manifest.json b/manifest.json index 755b6aa..103104c 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { - "id": "obsidian-jira-sync", + "id": "jira-sync", "name": "Jira Issue Manager", - "version": "1.1.0", + "version": "1.1.2", "minAppVersion": "1.7.7", "description": "Get Jira issues, create and update them. Issue status and worklog management.", "author": "Alamion", diff --git a/src/api/base.ts b/src/api/base.ts index d05bec6..2299aa9 100644 --- a/src/api/base.ts +++ b/src/api/base.ts @@ -1,6 +1,7 @@ import JiraPlugin from "../main"; import {requestUrl} from "obsidian"; import {getAuthHeaders} from "./auth"; +import {debugLog} from "../tools/debugLogging"; export async function baseRequest( plugin: JiraPlugin, @@ -8,24 +9,25 @@ export async function baseRequest( additional_url_path: string, body?: string ): Promise { - return await requestUrl({ + const response = await requestUrl({ url: `${plugin.settings.jiraUrl}/rest/api/2${additional_url_path}`, method: method, headers: getAuthHeaders(plugin), contentType: "application/json", throw: false, body - }).then(response => { - if (response.status < 200 || response.status >= 300) { - const error = new Error(`Error updating issue: + }); + if (response.status < 200 || response.status >= 300) { + const error = new Error(`Error updating issue: ${response.text || "Unknown error"} ${body && '\nbody:' + body || ""}`); - console.error(error); - throw error; - } - console.debug(response); - console.debug({"url": `${plugin.settings.jiraUrl}/rest/api/2${additional_url_path}`, - "method": method, "body": body}); - return response.status === 204 ? null : response.json; - }); + console.error(error); + throw error; + } + debugLog(response); + debugLog({"url": `${plugin.settings.jiraUrl}/rest/api/2${additional_url_path}`, + "method": method, "body": body}); + return response.status === 204 ? null : response.json; + } + diff --git a/src/settings/JiraSettingTab.ts b/src/settings/JiraSettingTab.ts index 7752c5e..6a73fc6 100644 --- a/src/settings/JiraSettingTab.ts +++ b/src/settings/JiraSettingTab.ts @@ -1,4 +1,4 @@ -import {App, PluginSettingTab, setIcon, Setting} from "obsidian"; +import {App, normalizePath, PluginSettingTab, setIcon, Setting} from "obsidian"; import JiraPlugin from "../main"; import {FieldMapping, fieldMappings} from "../tools/mappingObsidianJiraFields"; import {functionToArrowString, safeStringToFunction} from "../tools/convertFunctionString"; @@ -19,10 +19,10 @@ export class JiraSettingTab extends PluginSettingTab { const { containerEl } = this; containerEl.empty(); - containerEl.createEl("h2", { text: "Jira Integration Settings" }); + containerEl.createEl("h2", { text: "Jira integration settings" }); new Setting(containerEl) - .setName("Jira Username") + .setName("Jira username") .setDesc("Your Jira username or email") .addText((text) => text @@ -35,7 +35,7 @@ export class JiraSettingTab extends PluginSettingTab { ); new Setting(containerEl) - .setName("Jira Password or API Token") + .setName("Jira password or API token") .setDesc("Your Jira password or API token") .addText((text) => { text.inputEl.type = "password"; @@ -62,7 +62,7 @@ export class JiraSettingTab extends PluginSettingTab { ); new Setting(containerEl) - .setName("Issues Folder") + .setName("Issues folder") .setDesc("Folder where Jira issues will be stored") .addText((text) => text @@ -75,7 +75,7 @@ export class JiraSettingTab extends PluginSettingTab { ); new Setting(containerEl) - .setName("Session Cookie Name") + .setName("Session cookie name") .setDesc("The name of the session cookie used by your Jira instance") .addText((text) => text @@ -96,16 +96,14 @@ export class JiraSettingTab extends PluginSettingTab { .setValue(this.plugin.settings.templatePath) .onChange(async (value) => { // Normalize path - ensure it has .md extension - if (value && !value.toLowerCase().endsWith('.md')) { - value = value + '.md'; - } + value = normalizePath(value); this.plugin.settings.templatePath = value; await this.plugin.saveSettings(); }) ); // Add Jira-Obsidian Mapping setting - containerEl.createEl("h3", { text: "Jira-Obsidian Field Mapping" }); + containerEl.createEl("h3", { text: "Jira-Obsidian field mapping" }); // Create Field Mappings UI const mappingSection = containerEl.createDiv({ cls: "jira-field-mappings" }); @@ -276,7 +274,7 @@ export class JiraSettingTab extends PluginSettingTab { // Load existing mappings if available const loadExistingMappings = () => { // Clear existing field list - fieldsList.innerHTML = ""; + fieldsList.empty(); // If we have string representations stored, use those if (this.plugin.settings.fieldMappingsStrings && @@ -318,7 +316,7 @@ export class JiraSettingTab extends PluginSettingTab { // Add example mappings section const examplesSection = mappingSection.createDiv({ cls: "mapping-examples" }); - examplesSection.createEl("h4", { text: "Example Field Mappings" }); + examplesSection.createEl("h4", { text: "Example field mappings" }); const examplesList = examplesSection.createEl("ul"); diff --git a/src/tools/debugLogging.ts b/src/tools/debugLogging.ts new file mode 100644 index 0000000..c286e8d --- /dev/null +++ b/src/tools/debugLogging.ts @@ -0,0 +1,20 @@ +// Create a debug utility module (debug.ts) +export const DEBUG_MODE = process.env.NODE_ENV !== 'production'; + +export function debugLog(...args: any[]): void { + if (DEBUG_MODE) { + console.log('[DEBUG]', ...args); + } +} + +export function debugWarn(...args: any[]): void { + if (DEBUG_MODE) { + console.warn('[DEBUG WARNING]', ...args); + } +} + +export function debugError(...args: any[]): void { + if (DEBUG_MODE) { + console.error('[DEBUG ERROR]', ...args); + } +} diff --git a/src/tools/sectionTools.ts b/src/tools/sectionTools.ts index 63c8f61..e51c439 100644 --- a/src/tools/sectionTools.ts +++ b/src/tools/sectionTools.ts @@ -1,3 +1,5 @@ +import {debugLog} from "./debugLogging"; + /** * Updates or adds a Jira sync section in the file content * @param fileContent - The current content of the file @@ -72,7 +74,7 @@ function updateJiraSyncLine(fileContent: string, lineName: string, lineContent: export function extractAllJiraSyncValuesFromContent(fileContent: string): Record { const sections = extractAllJiraSyncValuesFromSections(fileContent); const lines = extractAllJiraSyncValuesFromLines(fileContent); - console.debug(`extracted from file: ${JSON.stringify(sections)}`); + debugLog(`extracted from file: ${JSON.stringify(sections)}`); return { ...sections, ...lines }; }