GitHubFileEditor
Edit, create, delete, or undo changes to files in GitHub repositories through the GitHub API—designed for AI agents that autonomously modify code.
Key Features
- Supports four operations:
edit(replace content),create(new file),delete, andundo(revert the last commit). - Resolves a
GITHUB_TOKENsecret at runtime, keeping credentials out of the pipeline YAML. - Accepts a default repository and branch at initialization; both can be overridden at run time.
- For
edit, verifies that the target string is unique before making any change, preventing accidental multi-site replacements. - For
undo, confirms the last commit was made by the authenticated user before reverting. - Raises an exception or returns an error string on failure, depending on
raise_on_failure.
Configuration
- Store your GitHub personal access token as a secret named
GITHUB_TOKEN. For instructions, see Create Secrets. - Drag the
GitHubFileEditorcomponent onto the canvas from the Component Library. - Click on the component to open the configuration panel.
- Set
repoto the default repository inowner/repoformat. - Set
branchto the default branch (for example,main).
Connections
GitHubFileEditor receives a command string (one of edit, undo, create, or delete) and a payload dict with operation-specific parameters (path, original string, replacement, message, and so on). It outputs a result string describing the outcome. You can connect the output to a prompt builder or another component that evaluates the edit result.
Source Code
To check this component's source code, open file_editor.py in the Haystack Core Integrations repository.
Usage Examples
Basic Configuration
GitHubFileEditor:
type: haystack_integrations.components.connectors.github.file_editor.GitHubFileEditor
init_parameters:
github_token:
type: env_var
env_vars:
- GITHUB_TOKEN
strict: true
repo: owner/my-repo
branch: main
raise_on_failure: true
Using the Component in a Pipeline
This example shows a pipeline where an agent reads a repository, decides on a change, and then applies it.
# haystack-pipeline
components:
repo_viewer:
type: haystack_integrations.components.connectors.github.repo_viewer.GitHubRepoViewer
init_parameters:
github_token:
type: env_var
env_vars:
- GITHUB_TOKEN
strict: true
repo: owner/my-repo
branch: main
file_editor:
type: haystack_integrations.components.connectors.github.file_editor.GitHubFileEditor
init_parameters:
github_token:
type: env_var
env_vars:
- GITHUB_TOKEN
strict: true
repo: owner/my-repo
branch: main
raise_on_failure: true
inputs:
command:
- file_editor.command
payload:
- file_editor.payload
path:
- repo_viewer.path
outputs:
result: file_editor.result
documents: repo_viewer.documents
max_runs_per_component: 100
metadata: {}
Parameters
Inputs
| Parameter | Type | Default | Description |
|---|---|---|---|
command | str or Command | Operation to perform. One of edit, undo, create, or delete. | |
payload | dict | Command-specific parameters. For edit: path, original, replacement, message. For create: path, content, message. For delete: path, message. For undo: no extra keys needed. | |
repo | Optional[str] | None | Repository in owner/repo format. Overrides the default set at initialization. |
branch | Optional[str] | None | Branch to operate on. Overrides the default set at initialization. |
Outputs
| Parameter | Type | Description |
|---|---|---|
result | str | A string describing the outcome of the operation (for example, Edit successful or an error message). |
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
github_token | Secret | Secret.from_env_var("GITHUB_TOKEN") | GitHub personal access token used for API authentication. |
repo | Optional[str] | None | Default repository in owner/repo format. |
branch | str | main | Default branch to operate on. |
raise_on_failure | bool | True | If True, raises an exception on API errors. If False, returns an error string in result. |
Related Information
Was this page helpful?