From eecea80884e459e1e3576abe8c95e430182fadf8 Mon Sep 17 00:00:00 2001 From: ndom91 Date: Tue, 10 Sep 2024 17:14:35 +0200 Subject: [PATCH] fix: read pr tempalte from disk --- .../src/lib/branch/BranchHeader.svelte | 3 +-- apps/desktop/src/lib/gitHost/github/github.ts | 21 +++++-------------- crates/gitbutler-fs/src/lib.rs | 14 +++++++++++++ crates/gitbutler-tauri/src/main.rs | 1 + crates/gitbutler-tauri/src/projects.rs | 8 +++++++ 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/apps/desktop/src/lib/branch/BranchHeader.svelte b/apps/desktop/src/lib/branch/BranchHeader.svelte index e2c84b1df..3438e0522 100644 --- a/apps/desktop/src/lib/branch/BranchHeader.svelte +++ b/apps/desktop/src/lib/branch/BranchHeader.svelte @@ -88,8 +88,7 @@ const prTemplatePath = project.git_host.pullRequestTemplatePath; if (usePrTemplate && prTemplatePath) { - const relativeTemplatePath = prTemplatePath.replace(project.path, ''); - pullRequestTemplateBody = await $gitHost.getPrTemplateContent(relativeTemplatePath); + pullRequestTemplateBody = await $gitHost.getPrTemplateContent(prTemplatePath); } if (pullRequestTemplateBody) { diff --git a/apps/desktop/src/lib/gitHost/github/github.ts b/apps/desktop/src/lib/gitHost/github/github.ts index ef42e8233..f577e3def 100644 --- a/apps/desktop/src/lib/gitHost/github/github.ts +++ b/apps/desktop/src/lib/gitHost/github/github.ts @@ -71,26 +71,15 @@ export class GitHub implements GitHost { } async getPrTemplateContent(path: string) { - if (!this.octokit) { - return; - } - try { - const response = await this.octokit.rest.repos.getContent({ - owner: this.repo.owner, - repo: this.repo.name, - path - }); - const b64Content = (response.data as any)?.content; - if (b64Content) { - return decodeURIComponent(escape(atob(b64Content))); - } + const fileContents: string | undefined = await invoke('get_pr_template_contents', { path }); + return fileContents; } catch (err) { - console.error(`Error fetching pull request template at path: ${path}`, err); + console.error(`Error reading pull request template at path: ${path}`, err); showToast({ - title: 'Failed to fetch pull request template', - message: `Template not found at path: \`${path}\`.`, + title: 'Failed to read pull request template', + message: `Could not read: \`${path}\`.`, style: 'neutral' }); } diff --git a/crates/gitbutler-fs/src/lib.rs b/crates/gitbutler-fs/src/lib.rs index de8e7d31d..52354cdac 100644 --- a/crates/gitbutler-fs/src/lib.rs +++ b/crates/gitbutler-fs/src/lib.rs @@ -112,3 +112,17 @@ pub fn read_toml_file_or_default(path: &Path) -> toml::from_str(&contents).with_context(|| format!("Failed to parse {}", path.display()))?; Ok(value) } + +/// Reads file from disk at workspace +pub fn read_file_from_workspace(path: &Path) -> Result { + let mut file = match File::open(path) { + Ok(f) => f, + Err(err) if err.kind() == std::io::ErrorKind::NotFound => { + return Err(anyhow::anyhow!("Unable to read file: {}", path.display())) + } + Err(err) => return Err(err.into()), + }; + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + Ok(contents) +} diff --git a/crates/gitbutler-tauri/src/main.rs b/crates/gitbutler-tauri/src/main.rs index 6496e13a7..0ea01a12c 100644 --- a/crates/gitbutler-tauri/src/main.rs +++ b/crates/gitbutler-tauri/src/main.rs @@ -147,6 +147,7 @@ fn main() { projects::commands::list_projects, projects::commands::set_project_active, projects::commands::open_project_in_window, + projects::commands::get_pr_template_contents, repo::commands::git_get_local_config, repo::commands::git_set_local_config, repo::commands::check_signing_settings, diff --git a/crates/gitbutler-tauri/src/projects.rs b/crates/gitbutler-tauri/src/projects.rs index c19ca3da3..56b4cbb1e 100644 --- a/crates/gitbutler-tauri/src/projects.rs +++ b/crates/gitbutler-tauri/src/projects.rs @@ -4,6 +4,7 @@ pub mod commands { use std::path; use anyhow::Context; + use gitbutler_fs::read_file_from_workspace; use gitbutler_project::{self as projects, Controller, ProjectId}; use tauri::{State, Window}; use tracing::instrument; @@ -96,6 +97,13 @@ pub mod commands { pub fn delete_project(projects: State<'_, Controller>, id: ProjectId) -> Result<(), Error> { projects.delete(id).map_err(Into::into) } + + #[tauri::command(async)] + #[instrument] + pub fn get_pr_template_contents(path: &path::Path) -> Result { + let template_contents = read_file_from_workspace(&path)?; + Ok(template_contents) + } } #[derive(serde::Deserialize, serde::Serialize)]