workflowforge 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
1
+ """
2
+ WorkflowForge - A robust and flexible library for creating CI/CD pipelines.
3
+
4
+ This library allows you to create GitHub Actions workflows, Jenkins pipelines,
5
+ and AWS CodeBuild BuildSpecs programmatically with type validation and autocompletion.
6
+ """
7
+
8
+ # Platform-specific modules
9
+ from . import codebuild as aws_codebuild
10
+ from . import github_actions_module as github_actions
11
+ from . import jenkins as jenkins_platform
12
+
13
+ __version__ = "1.0.0"
14
+ __all__ = [
15
+ "github_actions",
16
+ "jenkins_platform",
17
+ "aws_codebuild",
18
+ ]
@@ -0,0 +1,138 @@
1
+ """AI-powered documentation generation for WorkflowForge."""
2
+
3
+ import requests
4
+ from pydantic import BaseModel, Field
5
+
6
+
7
+ class OllamaClient(BaseModel):
8
+ """Client for interacting with Ollama local LLM."""
9
+
10
+ base_url: str = Field(
11
+ default="http://localhost:11434", description="Ollama server URL"
12
+ )
13
+ model: str = Field(default="llama3.2", description="Model to use")
14
+ timeout: int = Field(default=30, description="Request timeout in seconds")
15
+
16
+ def is_available(self) -> bool:
17
+ """Check if Ollama is running and accessible."""
18
+ try:
19
+ response = requests.get(f"{self.base_url}/api/tags", timeout=5)
20
+ return bool(response.status_code == 200)
21
+ except Exception:
22
+ return False
23
+
24
+ def generate(self, prompt: str) -> str | None:
25
+ """Generate text using Ollama."""
26
+ if not self.is_available():
27
+ return None
28
+
29
+ try:
30
+ response = requests.post(
31
+ f"{self.base_url}/api/generate",
32
+ json={"model": self.model, "prompt": prompt, "stream": False},
33
+ timeout=self.timeout,
34
+ )
35
+
36
+ if response.status_code == 200:
37
+ result = response.json().get("response", "")
38
+ return result.strip() if isinstance(result, str) else None
39
+ return None
40
+ except Exception:
41
+ return None
42
+
43
+
44
+ def generate_workflow_readme(
45
+ workflow_content: str, platform: str = "github", use_ai: bool = True
46
+ ) -> str:
47
+ """Generate README documentation for a workflow.
48
+
49
+ Args:
50
+ workflow_content: The YAML/Jenkinsfile content
51
+ platform: Platform type (github, jenkins, codebuild)
52
+ use_ai: Whether to use AI generation (requires Ollama)
53
+
54
+ Returns:
55
+ Generated README content
56
+ """
57
+ if use_ai:
58
+ return _ai_generate_readme(workflow_content, platform)
59
+ else:
60
+ return _template_generate_readme(workflow_content, platform)
61
+
62
+
63
+ def _ai_generate_readme(workflow_content: str, platform: str) -> str:
64
+ """Generate README using Ollama AI."""
65
+ client = OllamaClient()
66
+
67
+ if not client.is_available():
68
+ return _template_generate_readme(workflow_content, platform)
69
+
70
+ prompt = f"""
71
+ Analyze this {platform.upper()} CI/CD workflow and generate a comprehensive README.md.
72
+
73
+ Workflow content:
74
+ ```
75
+ {workflow_content}
76
+ ```
77
+
78
+ Please provide:
79
+ 1. Brief description of what this workflow does
80
+ 2. Triggers (when it runs)
81
+ 3. Jobs/stages breakdown with explanations
82
+ 4. Required secrets/variables/credentials
83
+ 5. Prerequisites and setup instructions
84
+ 6. Expected outputs/artifacts
85
+
86
+ Format as proper markdown with clear sections and code blocks where appropriate.
87
+ Keep it concise but informative for developers.
88
+ """
89
+
90
+ ai_response = client.generate(prompt)
91
+
92
+ if ai_response:
93
+ return (
94
+ f"# Workflow Documentation\n\n{ai_response}\n\n---\n"
95
+ "*Documentation generated with WorkflowForge AI*"
96
+ )
97
+ else:
98
+ return _template_generate_readme(workflow_content, platform)
99
+
100
+
101
+ def _template_generate_readme(workflow_content: str, platform: str) -> str:
102
+ """Generate basic README using templates."""
103
+ platform_name = {
104
+ "github": "GitHub Actions",
105
+ "jenkins": "Jenkins Pipeline",
106
+ "codebuild": "AWS CodeBuild",
107
+ }.get(platform, platform.title())
108
+
109
+ return f"""# {platform_name} Workflow
110
+
111
+ ## Overview
112
+ This {platform_name} workflow was generated using WorkflowForge.
113
+
114
+ ## Workflow Content
115
+ ```yaml
116
+ {workflow_content}
117
+ ```
118
+
119
+ ## Setup Instructions
120
+ 1. Review the workflow configuration above
121
+ 2. Configure required secrets/variables in your {platform_name} settings
122
+ 3. Ensure all dependencies and permissions are properly set
123
+ 4. Test the workflow in a development environment first
124
+
125
+ ## Maintenance
126
+ - Review and update dependencies regularly
127
+ - Monitor workflow execution and performance
128
+ - Update documentation when making changes
129
+
130
+ ---
131
+ *Generated with WorkflowForge*
132
+ """
133
+
134
+
135
+ # Factory function
136
+ def ai_documentation_client(model: str = "llama3.2") -> OllamaClient:
137
+ """Create an Ollama client for AI documentation generation."""
138
+ return OllamaClient(model=model)
@@ -0,0 +1,348 @@
1
+ """AWS CodeBuild BuildSpec generation for WorkflowForge."""
2
+
3
+ from typing import Any
4
+
5
+ from pydantic import BaseModel, Field
6
+ from ruamel.yaml import YAML
7
+
8
+
9
+ class BuildPhase(BaseModel):
10
+ """Represents a CodeBuild phase."""
11
+
12
+ run_as: str | None = Field(None, description="Linux user to run commands")
13
+ on_failure: str | None = Field(None, description="Action on failure")
14
+ runtime_versions: dict[str, str] | None = Field(
15
+ None, description="Runtime versions"
16
+ )
17
+ commands: list[str] = Field(default_factory=list, description="Commands to execute")
18
+ finally_commands: list[str] | None = Field(None, description="Finally commands")
19
+
20
+ def add_command(self, command: str) -> "BuildPhase":
21
+ """Add a command to the phase."""
22
+ self.commands.append(command)
23
+ return self
24
+
25
+ def add_runtime(self, runtime: str, version: str) -> "BuildPhase":
26
+ """Add runtime version."""
27
+ if self.runtime_versions is None:
28
+ self.runtime_versions = {}
29
+ self.runtime_versions[runtime] = version
30
+ return self
31
+
32
+ def set_on_failure(self, action: str) -> "BuildPhase":
33
+ """Set on-failure action."""
34
+ self.on_failure = action
35
+ return self
36
+
37
+
38
+ class BuildEnvironment(BaseModel):
39
+ """Represents CodeBuild environment configuration."""
40
+
41
+ shell: str | None = Field(None, description="Shell to use")
42
+ variables: dict[str, str] | None = Field(None, description="Environment variables")
43
+ parameter_store: dict[str, str] | None = Field(
44
+ None, description="Parameter Store variables"
45
+ )
46
+ secrets_manager: dict[str, str] | None = Field(
47
+ None, description="Secrets Manager variables"
48
+ )
49
+ exported_variables: list[str] | None = Field(None, description="Exported variables")
50
+ git_credential_helper: bool | None = Field(
51
+ None, description="Git credential helper"
52
+ )
53
+
54
+ def add_variable(self, key: str, value: str) -> "BuildEnvironment":
55
+ """Add environment variable."""
56
+ if self.variables is None:
57
+ self.variables = {}
58
+ self.variables[key] = value
59
+ return self
60
+
61
+ def add_parameter_store(self, key: str, parameter: str) -> "BuildEnvironment":
62
+ """Add Parameter Store variable."""
63
+ if self.parameter_store is None:
64
+ self.parameter_store = {}
65
+ self.parameter_store[key] = parameter
66
+ return self
67
+
68
+ def add_secret(self, key: str, secret: str) -> "BuildEnvironment":
69
+ """Add Secrets Manager variable."""
70
+ if self.secrets_manager is None:
71
+ self.secrets_manager = {}
72
+ self.secrets_manager[key] = secret
73
+ return self
74
+
75
+
76
+ class BuildArtifacts(BaseModel):
77
+ """Represents CodeBuild artifacts configuration."""
78
+
79
+ files: list[str] = Field(..., description="Files to include")
80
+ name: str | None = Field(None, description="Artifact name")
81
+ discard_paths: bool | None = Field(None, description="Discard paths")
82
+ base_directory: str | None = Field(None, description="Base directory")
83
+ exclude_paths: list[str] | None = Field(None, description="Paths to exclude")
84
+ enable_symlinks: bool | None = Field(None, description="Enable symlinks")
85
+ s3_prefix: str | None = Field(None, description="S3 prefix")
86
+
87
+ def add_file(self, file_pattern: str) -> "BuildArtifacts":
88
+ """Add file pattern."""
89
+ self.files.append(file_pattern)
90
+ return self
91
+
92
+
93
+ class BuildCache(BaseModel):
94
+ """Represents CodeBuild cache configuration."""
95
+
96
+ key: str | None = Field(None, description="Cache key")
97
+ fallback_keys: list[str] | None = Field(None, description="Fallback keys")
98
+ action: str | None = Field(None, description="Cache action")
99
+ paths: list[str] = Field(default_factory=list, description="Paths to cache")
100
+
101
+ def add_path(self, path: str) -> "BuildCache":
102
+ """Add cache path."""
103
+ self.paths.append(path)
104
+ return self
105
+
106
+
107
+ class BuildSpec(BaseModel):
108
+ """Represents a complete AWS CodeBuild BuildSpec."""
109
+
110
+ version: str = Field(default="0.2", description="BuildSpec version")
111
+ run_as: str | None = Field(None, description="Global run-as user")
112
+ env: BuildEnvironment | None = Field(None, description="Environment configuration")
113
+ install: BuildPhase | None = Field(None, description="Install phase")
114
+ pre_build: BuildPhase | None = Field(None, description="Pre-build phase")
115
+ build: BuildPhase | None = Field(None, description="Build phase")
116
+ post_build: BuildPhase | None = Field(None, description="Post-build phase")
117
+ artifacts: BuildArtifacts | None = Field(
118
+ None, description="Artifacts configuration"
119
+ )
120
+ cache: BuildCache | None = Field(None, description="Cache configuration")
121
+ reports: dict[str, dict[str, Any]] | None = Field(
122
+ None, description="Reports configuration"
123
+ )
124
+
125
+ def set_env(self, env: BuildEnvironment) -> "BuildSpec":
126
+ """Set environment configuration."""
127
+ self.env = env
128
+ return self
129
+
130
+ def set_install_phase(self, phase: BuildPhase) -> "BuildSpec":
131
+ """Set install phase."""
132
+ self.install = phase
133
+ return self
134
+
135
+ def set_pre_build_phase(self, phase: BuildPhase) -> "BuildSpec":
136
+ """Set pre-build phase."""
137
+ self.pre_build = phase
138
+ return self
139
+
140
+ def set_build_phase(self, phase: BuildPhase) -> "BuildSpec":
141
+ """Set build phase."""
142
+ self.build = phase
143
+ return self
144
+
145
+ def set_post_build_phase(self, phase: BuildPhase) -> "BuildSpec":
146
+ """Set post-build phase."""
147
+ self.post_build = phase
148
+ return self
149
+
150
+ def set_artifacts(self, artifacts: BuildArtifacts) -> "BuildSpec":
151
+ """Set artifacts configuration."""
152
+ self.artifacts = artifacts
153
+ return self
154
+
155
+ def set_cache(self, cache: BuildCache) -> "BuildSpec":
156
+ """Set cache configuration."""
157
+ self.cache = cache
158
+ return self
159
+
160
+ def to_yaml(self) -> str:
161
+ """Convert to BuildSpec YAML format."""
162
+ data: dict[str, Any] = {"version": self.version}
163
+
164
+ if self.run_as:
165
+ data["run-as"] = self.run_as
166
+
167
+ # Environment
168
+ if self.env:
169
+ env_data: dict[str, Any] = {}
170
+ if self.env.shell:
171
+ env_data["shell"] = self.env.shell
172
+ if self.env.variables:
173
+ env_data["variables"] = self.env.variables
174
+ if self.env.parameter_store:
175
+ env_data["parameter-store"] = self.env.parameter_store
176
+ if self.env.secrets_manager:
177
+ env_data["secrets-manager"] = self.env.secrets_manager
178
+ if self.env.exported_variables:
179
+ env_data["exported-variables"] = self.env.exported_variables
180
+ if self.env.git_credential_helper is not None:
181
+ env_data["git-credential-helper"] = (
182
+ "yes" if self.env.git_credential_helper else "no"
183
+ )
184
+ if env_data:
185
+ data["env"] = env_data
186
+
187
+ # Phases
188
+ phases: dict[str, Any] = {}
189
+ for phase_name, phase in [
190
+ ("install", self.install),
191
+ ("pre_build", self.pre_build),
192
+ ("build", self.build),
193
+ ("post_build", self.post_build),
194
+ ]:
195
+ if phase:
196
+ phase_data: dict[str, Any] = {}
197
+ if phase.run_as:
198
+ phase_data["run-as"] = phase.run_as
199
+ if phase.on_failure:
200
+ phase_data["on-failure"] = phase.on_failure
201
+ if phase.runtime_versions:
202
+ phase_data["runtime-versions"] = phase.runtime_versions
203
+ if phase.commands:
204
+ phase_data["commands"] = phase.commands
205
+ if phase.finally_commands:
206
+ phase_data["finally"] = phase.finally_commands
207
+ if phase_data:
208
+ phases[phase_name] = phase_data
209
+
210
+ if phases:
211
+ data["phases"] = phases
212
+
213
+ # Artifacts
214
+ if self.artifacts:
215
+ artifacts_data: dict[str, Any] = {"files": self.artifacts.files}
216
+ if self.artifacts.name:
217
+ artifacts_data["name"] = self.artifacts.name
218
+ if self.artifacts.discard_paths is not None:
219
+ artifacts_data["discard-paths"] = (
220
+ "yes" if self.artifacts.discard_paths else "no"
221
+ )
222
+ if self.artifacts.base_directory:
223
+ artifacts_data["base-directory"] = self.artifacts.base_directory
224
+ if self.artifacts.exclude_paths:
225
+ artifacts_data["exclude-paths"] = self.artifacts.exclude_paths
226
+ if self.artifacts.enable_symlinks is not None:
227
+ artifacts_data["enable-symlinks"] = (
228
+ "yes" if self.artifacts.enable_symlinks else "no"
229
+ )
230
+ if self.artifacts.s3_prefix:
231
+ artifacts_data["s3-prefix"] = self.artifacts.s3_prefix
232
+ data["artifacts"] = artifacts_data
233
+
234
+ # Cache
235
+ if self.cache:
236
+ cache_data: dict[str, Any] = {}
237
+ if self.cache.key:
238
+ cache_data["key"] = self.cache.key
239
+ if self.cache.fallback_keys:
240
+ cache_data["fallback-keys"] = self.cache.fallback_keys
241
+ if self.cache.action:
242
+ cache_data["action"] = self.cache.action
243
+ if self.cache.paths:
244
+ cache_data["paths"] = self.cache.paths
245
+ if cache_data:
246
+ data["cache"] = cache_data
247
+
248
+ # Reports
249
+ if self.reports:
250
+ data["reports"] = self.reports
251
+
252
+ yaml = YAML()
253
+ yaml.default_flow_style = False
254
+ yaml.indent(mapping=2, sequence=4, offset=2)
255
+ yaml.width = 120
256
+ yaml.preserve_quotes = True
257
+
258
+ from io import StringIO
259
+
260
+ stream = StringIO()
261
+ yaml.dump(data, stream)
262
+ yaml_content = stream.getvalue()
263
+
264
+ return f"# Do not modify - Generated with WorkflowForge\n{yaml_content}"
265
+
266
+ def generate_readme(self, use_ai: bool = True, ai_model: str = "llama3.2") -> str:
267
+ """Generate README documentation for this BuildSpec.
268
+
269
+ Args:
270
+ use_ai: Whether to use AI generation (requires Ollama)
271
+ ai_model: AI model to use for generation
272
+
273
+ Returns:
274
+ Generated README content
275
+ """
276
+ from .ai_documentation import generate_workflow_readme
277
+
278
+ return generate_workflow_readme(self.to_yaml(), "codebuild", use_ai)
279
+
280
+ def generate_diagram(self, output_format: str = "png") -> str:
281
+ """Generate visual diagram of this BuildSpec.
282
+
283
+ Args:
284
+ output_format: Output format (png, svg, pdf, dot)
285
+
286
+ Returns:
287
+ Path to generated diagram file
288
+ """
289
+ from .visualization import visualizer
290
+
291
+ viz = visualizer(output_format=output_format)
292
+ return viz.generate_codebuild_diagram(self)
293
+
294
+ def save(
295
+ self,
296
+ filepath: str = "buildspec.yml",
297
+ generate_readme: bool = False,
298
+ use_ai: bool = True,
299
+ generate_diagram: bool = True,
300
+ ) -> None:
301
+ """Save BuildSpec to file.
302
+
303
+ Args:
304
+ filepath: Path to save the buildspec YAML (default: buildspec.yml)
305
+ Can be customized (e.g., buildspec_debug.yml, config/buildspec.yml)
306
+ generate_readme: Whether to also generate a README file
307
+ use_ai: Whether to use AI for README generation
308
+ generate_diagram: Whether to generate visual diagram
309
+ """
310
+ with open(filepath, "w", encoding="utf-8") as f:
311
+ f.write(self.to_yaml())
312
+
313
+ if generate_readme:
314
+ readme_path = filepath.replace(".yml", "_README.md").replace(
315
+ ".yaml", "_README.md"
316
+ )
317
+ with open(readme_path, "w", encoding="utf-8") as f:
318
+ f.write(self.generate_readme(use_ai=use_ai))
319
+
320
+ if generate_diagram:
321
+ diagram_path = self.generate_diagram()
322
+ print(f"📊 BuildSpec diagram saved: {diagram_path}")
323
+
324
+
325
+ # Factory functions
326
+ def buildspec() -> BuildSpec:
327
+ """Create a new BuildSpec."""
328
+ return BuildSpec()
329
+
330
+
331
+ def phase() -> BuildPhase:
332
+ """Create a new BuildPhase."""
333
+ return BuildPhase()
334
+
335
+
336
+ def environment() -> BuildEnvironment:
337
+ """Create a new BuildEnvironment."""
338
+ return BuildEnvironment()
339
+
340
+
341
+ def artifacts(files: list[str]) -> BuildArtifacts:
342
+ """Create BuildArtifacts."""
343
+ return BuildArtifacts(files=files)
344
+
345
+
346
+ def cache(paths: list[str]) -> BuildCache:
347
+ """Create BuildCache."""
348
+ return BuildCache(paths=paths)
@@ -0,0 +1,25 @@
1
+ """Definición de entornos para jobs."""
2
+
3
+ from typing import Any
4
+
5
+ from pydantic import BaseModel, Field
6
+
7
+
8
+ class Environment(BaseModel):
9
+ """Represents deployment environment."""
10
+
11
+ name: str = Field(..., description="Nombre del entorno")
12
+ url: str | None = Field(None, description="URL del entorno")
13
+
14
+ def model_dump(self, **kwargs: Any) -> dict[str, Any]:
15
+ """Serialize environment."""
16
+ result = {"name": self.name}
17
+ if self.url:
18
+ result["url"] = self.url
19
+ return result
20
+
21
+
22
+ # Factory function para crear entornos fácilmente
23
+ def environment(name: str, url: str | None = None) -> Environment:
24
+ """Crea un Environment de manera conveniente."""
25
+ return Environment(name=name, url=url)
@@ -0,0 +1,71 @@
1
+ """
2
+ GitHub Actions module for WorkflowForge.
3
+
4
+ Provides all GitHub Actions specific functionality in a single namespace.
5
+ """
6
+
7
+ from .job import Job
8
+ from .secrets import GitHubContext, Secret, Variable, github_context, secret, variable
9
+ from .step import ActionStep, RunStep, Step, action, run
10
+ from .strategy import Matrix, Strategy, matrix, strategy
11
+ from .triggers import (
12
+ PullRequestTrigger,
13
+ PushTrigger,
14
+ ReleaseTrigger,
15
+ ScheduleTrigger,
16
+ WorkflowDispatchTrigger,
17
+ on_pull_request,
18
+ on_push,
19
+ on_release,
20
+ on_schedule,
21
+ on_workflow_dispatch,
22
+ )
23
+ from .workflow import Workflow
24
+
25
+ # Aliases with snake_case naming
26
+ workflow = Workflow
27
+ job = Job
28
+ push_trigger = PushTrigger
29
+ pull_request_trigger = PullRequestTrigger
30
+ schedule_trigger = ScheduleTrigger
31
+ workflow_dispatch_trigger = WorkflowDispatchTrigger
32
+ release_trigger = ReleaseTrigger
33
+
34
+ __all__ = [
35
+ "Workflow",
36
+ "Job",
37
+ "Step",
38
+ "ActionStep",
39
+ "RunStep",
40
+ "PushTrigger",
41
+ "PullRequestTrigger",
42
+ "ScheduleTrigger",
43
+ "WorkflowDispatchTrigger",
44
+ "ReleaseTrigger",
45
+ "Strategy",
46
+ "Matrix",
47
+ "Secret",
48
+ "Variable",
49
+ "GitHubContext",
50
+ # Functions (snake_case)
51
+ "workflow",
52
+ "job",
53
+ "action",
54
+ "run",
55
+ "on_push",
56
+ "on_pull_request",
57
+ "on_schedule",
58
+ "on_workflow_dispatch",
59
+ "on_release",
60
+ "matrix",
61
+ "strategy",
62
+ "secret",
63
+ "variable",
64
+ "github_context",
65
+ # Aliases (snake_case)
66
+ "push_trigger",
67
+ "pull_request_trigger",
68
+ "schedule_trigger",
69
+ "workflow_dispatch_trigger",
70
+ "release_trigger",
71
+ ]