Skip to content

Commit 7e680b2

Browse files
author
Greyforge Admin
committed
Restrict GitHub workflow uninstall detection
1 parent 7954d02 commit 7e680b2

2 files changed

Lines changed: 86 additions & 1 deletion

File tree

src/cortex-cli/src/github_cmd.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,10 @@ fn get_help_message() -> String {
584584
.to_string()
585585
}
586586

587+
fn is_cortex_generated_workflow(content: &str) -> bool {
588+
content.contains("# Generated by: cortex github install")
589+
}
590+
587591
/// Check GitHub Actions installation status.
588592
async fn run_status(args: StatusArgs) -> Result<()> {
589593
let repo_path = args.path.unwrap_or_else(|| PathBuf::from("."));
@@ -695,7 +699,7 @@ async fn run_uninstall(args: UninstallArgs) -> Result<()> {
695699
if path.exists() {
696700
// Verify it's a Cortex workflow
697701
if let Ok(content) = std::fs::read_to_string(&path)
698-
&& (content.contains("Cortex") || content.contains("cortex"))
702+
&& is_cortex_generated_workflow(&content)
699703
{
700704
found_workflow = Some(path);
701705
break;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use std::process::Command;
2+
3+
use tempfile::tempdir;
4+
5+
#[test]
6+
fn github_uninstall_rejects_unrelated_workflow_mentioning_cortex() {
7+
let repo_dir = tempdir().unwrap();
8+
let workflows_dir = repo_dir.path().join(".github").join("workflows");
9+
std::fs::create_dir_all(&workflows_dir).unwrap();
10+
let workflow_path = workflows_dir.join("ci.yml");
11+
std::fs::write(
12+
&workflow_path,
13+
r#"name: CI
14+
on: [push]
15+
# notify cortex team on failure
16+
jobs:
17+
test:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- run: echo unrelated
21+
"#,
22+
)
23+
.unwrap();
24+
25+
let output = Command::new(env!("CARGO_BIN_EXE_Cortex"))
26+
.args([
27+
"github",
28+
"uninstall",
29+
"--path",
30+
repo_dir.path().to_str().unwrap(),
31+
"--workflow-name",
32+
"ci",
33+
"--force",
34+
])
35+
.output()
36+
.unwrap();
37+
38+
let stderr = String::from_utf8_lossy(&output.stderr);
39+
assert!(!output.status.success());
40+
assert!(
41+
stderr.contains("Cortex workflow 'ci' not found"),
42+
"unexpected stderr: {stderr}"
43+
);
44+
assert!(workflow_path.exists(), "unrelated workflow was deleted");
45+
}
46+
47+
#[test]
48+
fn github_uninstall_removes_generated_workflow() {
49+
let repo_dir = tempdir().unwrap();
50+
let workflows_dir = repo_dir.path().join(".github").join("workflows");
51+
std::fs::create_dir_all(&workflows_dir).unwrap();
52+
let workflow_path = workflows_dir.join("Cortex.yml");
53+
std::fs::write(
54+
&workflow_path,
55+
r#"# Cortex CI/CD Automation
56+
# Generated by: cortex github install
57+
name: Cortex
58+
"#,
59+
)
60+
.unwrap();
61+
62+
let output = Command::new(env!("CARGO_BIN_EXE_Cortex"))
63+
.args([
64+
"github",
65+
"uninstall",
66+
"--path",
67+
repo_dir.path().to_str().unwrap(),
68+
"--workflow-name",
69+
"Cortex",
70+
"--force",
71+
])
72+
.output()
73+
.unwrap();
74+
75+
let stderr = String::from_utf8_lossy(&output.stderr);
76+
assert!(output.status.success(), "unexpected stderr: {stderr}");
77+
assert!(
78+
!workflow_path.exists(),
79+
"generated workflow was not deleted"
80+
);
81+
}

0 commit comments

Comments
 (0)