amberjs 1.16.1

JavaScript/TypeScript runtime built with Rust and V8
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// CI/CD Pipeline module
// Provides GitHub Actions, GitLab CI, and Jenkins pipeline support
/// Pipeline configuration
#[derive(Debug, Clone)]
pub struct PipelineConfig {
    /// Platform (github, gitlab, jenkins)
    pub platform: String,
    /// Trigger event
    pub trigger: String,
    /// Target branches
    pub branches: Vec<String>,
    /// Secret name
    pub secret_name: String,
}
/// Pipeline stage - Build
#[derive(Debug, Clone)]
pub struct BuildStage {
    /// Stage name
    pub name: String,
    /// Runner OS
    pub runs_on: String,
    /// Build steps
    pub steps: Vec<String>,
}
/// Pipeline stage - Test
#[derive(Debug, Clone)]
pub struct TestStage {
    /// Stage name
    pub name: String,
    /// Runner OS
    pub runs_on: String,
    /// Test steps
    pub steps: Vec<String>,
}
/// Pipeline stage - Deploy
#[derive(Debug, Clone)]
pub struct DeployStage {
    /// Stage name
    pub name: String,
    /// Environment
    pub environment: String,
    /// Runner OS
    pub runs_on: String,
    /// Deploy steps
    pub steps: Vec<String>,
}
/// Pipeline stage enum
#[derive(Debug, Clone)]
pub enum PipelineStage {
    /// Build stage
    Build {
        name: String,
        runs_on: String,
        steps: Vec<String>,
    },
    /// Test stage
    Test {
        name: String,
        runs_on: String,
        steps: Vec<String>,
    },
    /// Deploy stage
    Deploy {
        name: String,
        environment: String,
        runs_on: String,
        steps: Vec<String>,
    },
}
/// Pipeline status
#[derive(Debug, Clone, PartialEq)]
pub enum PipelineStatus {
    /// Pending
    Pending,
    /// Running
    Running,
    /// Success
    Success,
    /// Failed
    Failed,
    /// Cancelled
    Cancelled,
}
/// Pipeline event
#[derive(Debug, Clone)]
pub enum PipelineEvent {
    /// Push event
    Push,
    /// Pull request event
    PullRequest,
    /// Release event
    Release,
    /// Schedule event
    Schedule,
    /// Manual event
    Manual,
}
/// Pipeline cache configuration
#[derive(Debug, Clone)]
pub struct PipelineCache {
    /// Cache paths
    pub paths: Vec<String>,
    /// Cache key
    pub key: String,
    /// Restore keys
    pub restore_keys: Vec<String>,
}
/// Pipeline artifact
#[derive(Debug, Clone)]
pub struct PipelineArtifact {
    /// Artifact name
    pub name: String,
    /// Artifact path
    pub path: String,
    /// Retention days
    pub retention_days: u32,
}
/// Pipeline secret
#[derive(Debug, Clone)]
pub struct PipelineSecret {
    /// Secret name
    pub name: String,
    /// Secret description
    pub description: String,
}
/// GitHub Actions workflow
#[derive(Debug, Clone)]
pub struct GitHubActionsWorkflow {
    /// Workflow file name
    pub file_name: String,
    /// Workflow name
    pub name: String,
    /// Workflow description
    pub description: String,
    /// Trigger events
    pub on: Vec<String>,
    /// Event listeners
    pub events: Vec<String>,
    /// Pipeline stages
    pub stages: Vec<PipelineStage>,
    /// Cache configuration
    pub cache: Option<PipelineCache>,
    /// Artifacts
    pub artifacts: Vec<String>,
    /// Secrets
    pub secrets: Vec<String>,
    /// Workflow status
    pub status: PipelineStatus,
}
impl GitHubActionsWorkflow {
    /// Create a new GitHub Actions workflow
    pub fn new(file_name: String, name: String) -> Self {
        Self {
            file_name,
            name,
            description: String::new(),
            on: Vec::new(),
            events: Vec::new(),
            stages: Vec::new(),
            cache: None,
            artifacts: Vec::new(),
            secrets: Vec::new(),
            status: PipelineStatus::Pending,
        }
    }
    /// Set description
    pub fn description(mut self, description: String) -> Self {
        self.description = description;
        self
    }
    /// Add trigger event
    pub fn on(mut self, event: String) -> Self {
        self.on.push(event);
        self
    }
    /// Add event listener
    pub fn add_event_listener(&mut self, event: String) {
        self.events.push(event);
    }
    /// Add stage
    pub fn add_stage(&mut self, stage: PipelineStage) {
        self.stages.push(stage);
    }
    /// Enable cache
    pub fn enable_cache(&mut self, path: String, key: String) {
        let cache: _ = PipelineCache {
            paths: vec![path],
            key,
            restore_keys: Vec::new(),
        };
        self.cache = Some(cache);
    }
    /// Add artifact
    pub fn add_artifact(&mut self, artifact: String) {
        self.artifacts.push(artifact);
    }
    /// Add secret
    pub fn add_secret(&mut self, secret: String) {
        self.secrets.push(secret);
    }
    /// Set status
    pub fn set_status(&mut self, status: PipelineStatus) {
        self.status = status;
    }
    /// Get workflow file name
    pub fn get_file_name(&self) -> &str {
        &self.file_name
    }
}
/// GitLab CI pipeline
#[derive(Debug, Clone)]
pub struct GitLabCIPipeline {
    /// Pipeline name
    pub name: String,
    /// Pipeline description
    pub description: String,
    /// Target environment
    pub environment: String,
    /// Pipeline stages
    pub stages: Vec<String>,
    /// Pipeline jobs
    pub jobs: std::collections::HashMap<String, PipelineJob>,
    /// Variables
    pub variables: std::collections::HashMap<String, String>,
    /// Cache configuration
    pub cache: Option<PipelineCache>,
    /// Artifacts
    pub artifacts: Vec<PipelineArtifact>,
    /// Secrets
    pub secrets: Vec<String>,
    /// Pipeline status
    pub status: PipelineStatus,
}
/// Pipeline job
#[derive(Debug, Clone)]
pub struct PipelineJob {
    /// Job name
    pub name: String,
    /// Stage name
    pub stage: String,
    /// Job steps
    pub steps: Vec<String>,
    /// Runner tags
    pub tags: Vec<String>,
    /// Dependencies
    pub dependencies: Vec<String>,
}
impl GitLabCIPipeline {
    /// Create a new GitLab CI pipeline
    pub fn new(name: String, environment: String) -> Self {
        Self {
            name,
            description: String::new(),
            environment,
            stages: Vec::new(),
            jobs: std::collections::HashMap::new(),
            variables: std::collections::HashMap::new(),
            cache: None,
            artifacts: Vec::new(),
            secrets: Vec::new(),
            status: PipelineStatus::Pending,
        }
    }
    /// Set description
    pub fn description(mut self, description: String) -> Self {
        self.description = description;
        self
    }
    /// Add stage
    pub fn add_stage(&mut self, stage: String) {
        self.stages.push(stage);
    }
    /// Check if pipeline has stage
    pub fn has_stage(&self, stage: &str) -> bool {
        self.stages.contains(&stage.to_string())
    }
    /// Add job
    pub fn add_job(&mut self, name: String, stage: String, steps: Vec<String>) {
        let job: _ = PipelineJob {
            name: name.clone(),
            stage,
            steps,
            tags: Vec::new(),
            dependencies: Vec::new(),
        };
        self.jobs.insert(name, job);
    }
    /// Check if pipeline has job
    pub fn has_job(&self, name: &str) -> bool {
        self.jobs.contains_key(name)
    }
    /// Add variable
    pub fn add_variable(&mut self, key: String, value: String) {
        self.variables.insert(key, value);
    }
    /// Add artifact
    pub fn add_artifact(&mut self, artifact: PipelineArtifact) {
        self.artifacts.push(artifact);
    }
    /// Add secret
    pub fn add_secret(&mut self, secret: String) {
        self.secrets.push(secret);
    }
    /// Set status
    pub fn set_status(&mut self, status: PipelineStatus) {
        self.status = status;
    }
}
/// Jenkins pipeline
#[derive(Debug, Clone)]
pub struct JenkinsPipeline {
    /// Pipeline name
    pub name: String,
    /// Pipeline description
    pub description: String,
    /// Pipeline stages
    pub stages: Vec<JenkinsStage>,
    /// Agent configuration
    pub agent: String,
    /// Post-build actions
    pub post: Vec<String>,
    /// Environment variables
    pub environment: std::collections::HashMap<String, String>,
    /// Tools configuration
    pub tools: std::collections::HashMap<String, String>,
    /// Pipeline status
    pub status: PipelineStatus,
}
/// Jenkins stage
#[derive(Debug, Clone)]
pub struct JenkinsStage {
    /// Stage name
    pub name: String,
    /// Stage steps
    pub steps: Vec<String>,
}
impl JenkinsPipeline {
    /// Create a new Jenkins pipeline
    pub fn new(name: String) -> Self {
        Self {
            name,
            description: String::new(),
            stages: Vec::new(),
            agent: "kubernetes".to_string(),
            post: vec!["always".to_string(), "cleanup".to_string()],
            environment: std::collections::HashMap::new(),
            tools: std::collections::HashMap::new(),
            status: PipelineStatus::Pending,
        }
    }
    /// Set description
    pub fn description(mut self, description: String) -> Self {
        self.description = description;
        self
    }
    /// Add stage
    pub fn add_stage(&mut self, name: String, steps: Vec<String>) {
        let stage: _ = JenkinsStage { name, steps };
        self.stages.push(stage);
    }
    /// Set agent
    pub fn agent(mut self, agent: String) -> Self {
        self.agent = agent;
        self
    }
    /// Add post-build action
    pub fn add_post(&mut self, action: String) {
        self.post.push(action);
    }
    /// Add environment variable
    pub fn add_env_var(&mut self, key: String, value: String) {
        self.environment.insert(key, value);
    }
    /// Add tool
    pub fn add_tool(&mut self, name: String, version: String) {
        self.tools.insert(name, version);
    }
    /// Set status
    pub fn set_status(&mut self, status: PipelineStatus) {
        self.status = status;
    }
}
/// Pipeline manager
pub struct PipelineManager {
    /// Platform
    platform: String,
    /// GitHub Actions workflows
    pub workflows: Vec<GitHubActionsWorkflow>,
    /// GitLab CI pipelines
    pub pipelines: Vec<GitLabCIPipeline>,
    /// Jenkins pipelines
    pub jenkins_pipelines: Vec<JenkinsPipeline>,
}
impl PipelineManager {
    /// Create a new pipeline manager
    pub fn new(platform: String) -> Self {
        Self {
            platform,
            workflows: Vec::new(),
            pipelines: Vec::new(),
            jenkins_pipelines: Vec::new(),
        }
    }
    /// Add GitHub Actions workflow
    pub fn add_workflow(&mut self, workflow: GitHubActionsWorkflow) {
        self.workflows.push(workflow);
    }
    /// Get GitHub Actions workflow by name
    pub fn get_workflow(&self, file_name: &str) -> Option<&GitHubActionsWorkflow> {
        self.workflows.iter().find(|w| w.file_name == file_name)
    }
    /// Add GitLab CI pipeline
    pub fn add_pipeline(&mut self, pipeline: GitLabCIPipeline) {
        self.pipelines.push(pipeline);
    }
    /// Get GitLab CI pipeline by name
    pub fn get_pipeline(&self, name: &str) -> Option<&GitLabCIPipeline> {
        self.pipelines.iter().find(|p| p.name == name)
    }
    /// Add Jenkins pipeline
    pub fn add_jenkins_pipeline(&mut self, pipeline: JenkinsPipeline) {
        self.jenkins_pipelines.push(pipeline);
    }
    /// Get Jenkins pipeline by name
    pub fn get_jenkins_pipeline(&self, name: &str) -> Option<&JenkinsPipeline> {
        self.jenkins_pipelines.iter().find(|p| p.name == name)
    }
    /// Get platform
    pub fn get_platform(&self) -> &str {
        &self.platform
    }
    /// Get all workflows
    pub fn get_workflows(&self) -> &[GitHubActionsWorkflow] {
        &self.workflows
    }
    /// Get all pipelines
    pub fn get_pipelines(&self) -> &[GitLabCIPipeline] {
        &self.pipelines
    }
    /// Get all Jenkins pipelines
    pub fn get_jenkins_pipelines(&self) -> &[JenkinsPipeline] {
        &self.jenkins_pipelines
    }
}
/// Error types
#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("Invalid platform: {0}")]
    InvalidPlatform(String),
    #[error("Workflow not found: {file_name}")]
    WorkflowNotFound { file_name: String },
    #[error("Pipeline not found: {name}")]
    PipelineNotFound { name: String },
    #[error("Stage not found: {name}")]
    StageNotFound { name: String },
    #[error("Pipeline error: {0}")]
    PipelineError(String),
    #[error("Configuration error: {0}")]
    ConfigurationError(String),
}
#[cfg(test)]
mod tests {

    use super::*;
    #[test]
    fn test_github_actions_workflow() {
        let mut workflow =
            GitHubActionsWorkflow::new("ci.yml".to_string(), "Build and Test".to_string());
        workflow.add_event_listener("push".to_string());
        workflow.add_stage(PipelineStage::Build {
            name: "build".to_string(),
            runs_on: "ubuntu-latest".to_string(),
            steps: vec!["npm run build".to_string()],
        });
        assert_eq!(workflow.stages.len(), 1);
        assert!(workflow.events.contains(&"push".to_string()));
    }
    #[test]
    fn test_gitlab_ci_pipeline() {
        let mut pipeline =
            GitLabCIPipeline::new("amberjs-pipeline".to_string(), "production".to_string());
        pipeline.add_stage("build".to_string());
        pipeline.add_job(
            "build-job".to_string(),
            "build".to_string(),
            vec!["docker build -t amberjs .".to_string()],
        );
        assert_eq!(pipeline.stages.len(), 1);
        assert!(pipeline.has_job("build-job"));
    }
    #[test]
    fn test_jenkins_pipeline() {
        let mut pipeline = JenkinsPipeline::new("amberjs-pipeline".to_string());
        pipeline.add_stage("Build".to_string(), vec!["sh 'npm install'".to_string()]);
        assert_eq!(pipeline.stages.len(), 1);
        assert_eq!(pipeline.agent, "kubernetes");
    }
    #[test]
    fn test_pipeline_manager() {
        let mut manager = PipelineManager::new("github".to_string());
        let workflow: _ =
            GitHubActionsWorkflow::new("ci.yml".to_string(), "Build and Test".to_string());
        manager.add_workflow(workflow);
        assert_eq!(manager.workflows.len(), 1);
        assert!(manager.get_workflow("ci.yml").is_some());
    }
    #[test]
    fn test_pipeline_status() {
        let workflow: _ =
            GitHubActionsWorkflow::new("ci.yml".to_string(), "Build and Test".to_string());
        assert_eq!(workflow.status, PipelineStatus::Pending);
    }
}
use std::collections::{BTreeMap, HashMap};