fix: read pr tempalte from disk

This commit is contained in:
ndom91 2024-09-10 17:14:35 +02:00
parent aaa3dbae5d
commit eecea80884
5 changed files with 29 additions and 18 deletions

View File

@ -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) {

View File

@ -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'
});
}

View File

@ -112,3 +112,17 @@ pub fn read_toml_file_or_default<T: DeserializeOwned + 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<String> {
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)
}

View File

@ -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,

View File

@ -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<String, Error> {
let template_contents = read_file_from_workspace(&path)?;
Ok(template_contents)
}
}
#[derive(serde::Deserialize, serde::Serialize)]