aft_base/
github_address.rs1use crate::protocol::Response;
4
5pub fn is_github_resource_path(path: &str) -> bool {
6 path.starts_with("issue://") || path.starts_with("pr://")
7}
8
9pub const GITHUB_WRITE_ENABLE_HINT: &str = "set \"github\": { \"write\": true } in the user config (~/.config/cortexkit/aft.jsonc) and restart the host";
12
13pub const GITHUB_RESOURCE_NOT_FILE: &str = "github_resource_not_file";
15
16pub fn github_resource_not_file_message(operation: &str, path: &str) -> String {
21 format!(
22 "{operation}: '{path}' is a GitHub issue or pull-request address, not a local file, so nothing was changed. \
23 Only `write` (post a new comment) and `edit` with one edits[] find/replace entry on issue://N/comments/K \
24 (change a comment) accept GitHub addresses, and only while github.write is enabled: {GITHUB_WRITE_ENABLE_HINT}."
25 )
26}
27
28pub fn reject_github_resource_path(
32 req_id: &str,
33 operation: &str,
34 path: &str,
35) -> Result<(), Response> {
36 if is_github_resource_path(path) {
37 return Err(Response::error(
38 req_id,
39 GITHUB_RESOURCE_NOT_FILE,
40 github_resource_not_file_message(operation, path),
41 ));
42 }
43 Ok(())
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn github_prefixes_keep_the_same_filesystem_refusal() {
52 for address in ["issue://7", "pr://owner/repo/8/comments/2", "issue://"] {
53 let refusal = reject_github_resource_path("request", "file operation", address)
54 .expect_err("GitHub prefixes are not local files");
55 assert!(!refusal.success);
56 assert_eq!(refusal.data["code"], "github_resource_not_file");
57 }
58 for path in ["issue:/7", "pr/8", "ISSUE://7", "src/pr.rs"] {
59 assert!(reject_github_resource_path("request", "file operation", path).is_ok());
60 }
61 }
62}