use crate::protocol::Response;
pub fn is_github_resource_path(path: &str) -> bool {
path.starts_with("issue://") || path.starts_with("pr://")
}
pub const GITHUB_WRITE_ENABLE_HINT: &str = "set \"github\": { \"write\": true } in the user config (~/.config/cortexkit/aft.jsonc) and restart the host";
pub const GITHUB_RESOURCE_NOT_FILE: &str = "github_resource_not_file";
pub fn github_resource_not_file_message(operation: &str, path: &str) -> String {
format!(
"{operation}: '{path}' is a GitHub issue or pull-request address, not a local file, so nothing was changed. \
Only `write` (post a new comment) and `edit` with one edits[] find/replace entry on issue://N/comments/K \
(change a comment) accept GitHub addresses, and only while github.write is enabled: {GITHUB_WRITE_ENABLE_HINT}."
)
}
pub fn reject_github_resource_path(
req_id: &str,
operation: &str,
path: &str,
) -> Result<(), Response> {
if is_github_resource_path(path) {
return Err(Response::error(
req_id,
GITHUB_RESOURCE_NOT_FILE,
github_resource_not_file_message(operation, path),
));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn github_prefixes_keep_the_same_filesystem_refusal() {
for address in ["issue://7", "pr://owner/repo/8/comments/2", "issue://"] {
let refusal = reject_github_resource_path("request", "file operation", address)
.expect_err("GitHub prefixes are not local files");
assert!(!refusal.success);
assert_eq!(refusal.data["code"], "github_resource_not_file");
}
for path in ["issue:/7", "pr/8", "ISSUE://7", "src/pr.rs"] {
assert!(reject_github_resource_path("request", "file operation", path).is_ok());
}
}
}