apiforge 0.4.0

Production-grade API release automation CLI. From merged code to healthy pods in production — one command.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use crate::config::Language;
use crate::error::{ApiForgeError, Result};
use crate::integrations::git::GitRepo;
use crate::steps::{Step, StepContext, StepOutput};
use crate::utils::version::read_version;
use crate::utils::{bump_version, BumpType};
use async_trait::async_trait;
use std::fs;
use std::path::PathBuf;
use std::sync::RwLock;

pub struct VersionBumpStep {
    bump_type: BumpType,
    /// Stores original file content before modification for safe rollback.
    /// Using RwLock to allow interior mutability in async context.
    original_content: RwLock<Option<(PathBuf, String)>>,
}

impl VersionBumpStep {
    pub fn new(bump_type: BumpType) -> Self {
        Self {
            bump_type,
            original_content: RwLock::new(None),
        }
    }

    fn write_rust_version(path: &PathBuf, new_version: &str) -> Result<()> {
        let content = fs::read_to_string(path)?;
        let mut doc: toml_edit::DocumentMut = content
            .parse()
            .map_err(|e| ApiForgeError::Config(format!("Failed to parse Cargo.toml: {}", e)))?;

        doc["package"]["version"] = toml_edit::value(new_version);

        fs::write(path, doc.to_string())?;
        Ok(())
    }

    fn write_node_version(path: &PathBuf, new_version: &str) -> Result<()> {
        let content = fs::read_to_string(path)?;
        let mut json: serde_json::Value = serde_json::from_str(&content)
            .map_err(|e| ApiForgeError::Config(format!("Failed to parse package.json: {}", e)))?;

        json["version"] = serde_json::Value::String(new_version.to_string());

        let pretty = serde_json::to_string_pretty(&json).map_err(|e| {
            ApiForgeError::Serialization(format!("Failed to serialize package.json: {}", e))
        })?;
        fs::write(path, format!("{}\n", pretty))?;
        Ok(())
    }

    fn write_python_version(path: &PathBuf, new_version: &str) -> Result<()> {
        let content = fs::read_to_string(path)?;
        let mut doc: toml_edit::DocumentMut = content
            .parse()
            .map_err(|e| ApiForgeError::Config(format!("Failed to parse pyproject.toml: {}", e)))?;

        // Try to set in tool.poetry.version first
        if doc.get("tool").and_then(|t| t.get("poetry")).is_some() {
            doc["tool"]["poetry"]["version"] = toml_edit::value(new_version);
        } else if doc.get("project").is_some() {
            // Otherwise try project.version (PEP 621)
            doc["project"]["version"] = toml_edit::value(new_version);
        } else {
            return Err(ApiForgeError::Config(
                "Could not find version location in pyproject.toml (expected tool.poetry or project section)".to_string()
            ));
        }

        fs::write(path, doc.to_string())?;
        Ok(())
    }

    fn write_go_version(path: &PathBuf, new_version: &str) -> Result<()> {
        // Try to write to version.go if it exists
        let version_file = path
            .parent()
            .map(|p| p.join("version.go"))
            .filter(|p| p.exists());

        if let Some(vf) = version_file {
            let content = fs::read_to_string(&vf)?;
            let mut new_content = String::new();
            let mut found = false;

            for line in content.lines() {
                if line.contains("Version") && line.contains('=') && !found {
                    // Replace the version string
                    if let Some(quote_start) = line.find('"') {
                        if let Some(quote_end) = line[quote_start + 1..].find('"') {
                            let new_line = format!(
                                "{}\"{}\"{}",
                                &line[..quote_start + 1],
                                new_version,
                                &line[quote_start + 1 + quote_end..]
                            );
                            new_content.push_str(&new_line);
                            new_content.push('\n');
                            found = true;
                            continue;
                        }
                    }
                }
                new_content.push_str(line);
                new_content.push('\n');
            }

            if found {
                fs::write(&vf, new_content)?;
                return Ok(());
            }
        }

        // Fallback: add a comment to go.mod
        let content = fs::read_to_string(path)?;
        let new_content = format!("{}\n// {}\n", content.trim_end(), new_version);
        fs::write(path, new_content)?;

        tracing::warn!("Wrote version to go.mod comment. Consider creating a version.go file for better version management.");
        Ok(())
    }

    fn write_java_version(path: &PathBuf, new_version: &str) -> Result<()> {
        let content = fs::read_to_string(path)?;
        let mut new_content = String::new();
        let mut found = false;
        let mut in_project = false;

        for line in content.lines() {
            let trimmed = line.trim();

            if trimmed.contains("<project") && !trimmed.contains("</project>") {
                in_project = true;
            }

            if trimmed == "</project>" {
                in_project = false;
            }

            if in_project
                && !found
                && trimmed.starts_with("<version>")
                && trimmed.ends_with("</version>")
            {
                // Check this isn't a property reference
                let start = trimmed.find("<version>").unwrap() + "<version>".len();
                let end = trimmed.find("</version>").unwrap();
                let current = &trimmed[start..end];

                if !current.starts_with("${") {
                    // Replace this version
                    let indent = line.len() - line.trim_start().len();
                    let spaces = " ".repeat(indent);
                    new_content
                        .push_str(&format!("{}<version>{}</version>\n", spaces, new_version));
                    found = true;
                    continue;
                }
            }

            new_content.push_str(line);
            new_content.push('\n');
        }

        if !found {
            return Err(ApiForgeError::Config(
                "Could not find project version to update in pom.xml".to_string(),
            ));
        }

        fs::write(path, new_content)?;
        Ok(())
    }

    fn get_version_file_path(&self, ctx: &StepContext) -> Result<PathBuf> {
        let repo = GitRepo::open()?;
        let root = repo.root_path();
        let version_file = ctx.config.project.language.version_file();
        Ok(root.join(version_file))
    }

    fn write_version(&self, ctx: &StepContext, path: &PathBuf, version: &str) -> Result<()> {
        match ctx.config.project.language {
            Language::Rust => Self::write_rust_version(path, version),
            Language::Node => Self::write_node_version(path, version),
            Language::Python => Self::write_python_version(path, version),
            Language::Go => Self::write_go_version(path, version),
            Language::Java => Self::write_java_version(path, version),
        }
    }
}

#[async_trait]
impl Step for VersionBumpStep {
    fn name(&self) -> &str {
        "version-bump"
    }

    fn description(&self) -> &str {
        "Bump project version"
    }

    async fn validate(&self, ctx: &StepContext) -> Result<()> {
        let path = self.get_version_file_path(ctx)?;
        if !path.exists() {
            return Err(ApiForgeError::Config(format!(
                "Version file not found: {}",
                path.display()
            )));
        }
        read_version(ctx.config.project.language, &path)?;
        Ok(())
    }

    async fn execute(&self, ctx: &StepContext) -> Result<StepOutput> {
        let path = self.get_version_file_path(ctx)?;

        // Store original content before modification for safe rollback
        // This preserves any uncommitted changes that the user might have
        let original = fs::read_to_string(&path)?;
        {
            let mut guard = self
                .original_content
                .write()
                .map_err(|_| ApiForgeError::StepFailed("Failed to acquire lock".to_string()))?;
            *guard = Some((path.clone(), original));
        }

        let current = read_version(ctx.config.project.language, &path)?;
        let new_version = bump_version(&current, self.bump_type)?;

        self.write_version(ctx, &path, &new_version.to_string())?;

        Ok(StepOutput::ok(format!(
            "Bumped version from {} to {}",
            current, new_version
        )))
    }

    async fn dry_run(&self, ctx: &StepContext) -> Result<StepOutput> {
        let path = self.get_version_file_path(ctx)?;
        let current = read_version(ctx.config.project.language, &path)?;
        let new_version = bump_version(&current, self.bump_type)?;
        let new_version_str = new_version.to_string();

        let file_name = path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("version file");

        // Generate preview of the change
        let change_preview = match ctx.config.project.language {
            Language::Rust => {
                format!(
                    "  - {}: \"{}\"\"{}\"",
                    file_name, current, new_version_str
                )
            }
            Language::Node => {
                format!(
                    "  - {}: \"{}\"\"{}\"",
                    file_name, current, new_version_str
                )
            }
            Language::Python => {
                format!(
                    "  - {}: \"{}\"\"{}\"",
                    file_name, current, new_version_str
                )
            }
            _ => format!("  - {}: {}{}", file_name, current, new_version_str),
        };

        let file_change = crate::steps::FileChange {
            path: path.display().to_string(),
            operation: crate::steps::FileOperation::Modify,
            diff: Some(change_preview),
        };

        let details = crate::steps::DryRunDetails {
            file_changes: vec![file_change],
            docker_preview: None,
            notes: vec![format!(
                "Version bump from {} to {} ({} bump)",
                current, new_version_str, self.bump_type
            )],
        };

        Ok(StepOutput::ok(format!(
            "Would bump version from {} to {}",
            current, new_version
        ))
        .with_dry_run_details(details))
    }

    async fn rollback(&self, _ctx: &StepContext) -> Result<()> {
        // Try to restore from our saved original content first
        // This is safer than checkout_file because it preserves any uncommitted changes
        // that existed before the release was started
        let restored = {
            let guard = self
                .original_content
                .read()
                .map_err(|_| ApiForgeError::StepFailed("Failed to acquire lock".to_string()))?;

            if let Some((ref path, ref content)) = *guard {
                fs::write(path, content)?;
                tracing::info!("Restored {} from saved original content", path.display());
                true
            } else {
                false
            }
        };

        if !restored {
            // Fallback: if we don't have the original content (shouldn't happen),
            // log a warning. We can't safely restore without knowing the original state.
            tracing::warn!(
                "No original content saved for version file rollback. \
                 The file may need to be manually restored if there were uncommitted changes."
            );
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::utils::version::{read_go_version, read_java_version, read_python_version};
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_read_python_version_poetry() {
        let content = r#"
[tool.poetry]
name = "my-app"
version = "1.2.3"
description = "Test app"
"#;
        let mut file = NamedTempFile::new().unwrap();
        file.write_all(content.as_bytes()).unwrap();
        let result = read_python_version(file.path());
        assert_eq!(result.unwrap(), "1.2.3");
    }

    #[test]
    fn test_read_python_version_pep621() {
        let content = r#"
[project]
name = "my-app"
version = "2.0.0"
description = "Test app"
"#;
        let mut file = NamedTempFile::new().unwrap();
        file.write_all(content.as_bytes()).unwrap();
        let result = read_python_version(file.path());
        assert_eq!(result.unwrap(), "2.0.0");
    }

    #[test]
    fn test_write_python_version_poetry() {
        let content = r#"[tool.poetry]
name = "my-app"
version = "1.0.0"
"#;
        let mut file = NamedTempFile::new().unwrap();
        file.write_all(content.as_bytes()).unwrap();
        VersionBumpStep::write_python_version(&file.path().to_path_buf(), "1.1.0").unwrap();
        let new_content = fs::read_to_string(file.path()).unwrap();
        assert!(new_content.contains("version = \"1.1.0\""));
    }

    #[test]
    fn test_read_java_version() {
        let content = r#"<?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>3.4.5</version>
</project>
"#;
        let mut file = NamedTempFile::new().unwrap();
        file.write_all(content.as_bytes()).unwrap();
        let result = read_java_version(file.path());
        assert_eq!(result.unwrap(), "3.4.5");
    }

    #[test]
    fn test_write_java_version() {
        let content = r#"<?xml version="1.0" encoding="UTF-8"?>
<project>
    <version>1.0.0</version>
</project>
"#;
        let mut file = NamedTempFile::new().unwrap();
        file.write_all(content.as_bytes()).unwrap();
        VersionBumpStep::write_java_version(&file.path().to_path_buf(), "2.0.0").unwrap();
        let new_content = fs::read_to_string(file.path()).unwrap();
        assert!(new_content.contains("<version>2.0.0</version>"));
    }

    #[test]
    fn test_read_go_version_from_mod() {
        let content = r#"
module github.com/example/app

go 1.21
// v1.2.3
"#;
        let mut file = NamedTempFile::new().unwrap();
        file.write_all(content.as_bytes()).unwrap();
        let result = read_go_version(file.path());
        assert_eq!(result.unwrap(), "v1.2.3");
    }

    #[test]
    fn test_write_go_version_to_mod() {
        let content = r#"module github.com/example/app

go 1.21
"#;
        let mut file = NamedTempFile::new().unwrap();
        file.write_all(content.as_bytes()).unwrap();
        VersionBumpStep::write_go_version(&file.path().to_path_buf(), "1.2.4").unwrap();
        let new_content = fs::read_to_string(file.path()).unwrap();
        assert!(new_content.contains("// 1.2.4"));
    }
}