githubfileeditor.mdx
  1  ---
  2  title: "GitHubFileEditor"
  3  id: githubfileeditor
  4  slug: "/githubfileeditor"
  5  description: "This is a component for editing files in GitHub repositories through the GitHub API."
  6  ---
  7  
  8  # GitHubFileEditor
  9  
 10  This is a component for editing files in GitHub repositories through the GitHub API.
 11  
 12  |  |  |
 13  | --- | --- |
 14  | **Most common position in a pipeline** | After a Chat Generator, or right at the beginning of a pipeline |
 15  | **Mandatory init variables** | "github_token": GitHub personal access token. Can be set with `GITHUB_TOKEN` env var. |
 16  | **Mandatory run variables** | "command": Operation type (edit, create, delete, undo)  <br /> <br />"payload": Command-specific parameters |
 17  | **Output variables** | "result": String that indicates the operation result |
 18  | **API reference** | [GitHub](/reference/integrations-github) |
 19  | **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/github |
 20  
 21  ## Overview
 22  
 23  `GitHubFileEditor` supports multiple file operations, including editing existing files, creating new files, deleting files, and undoing recent changes.
 24  
 25  There are four main commands:
 26  
 27  - **EDIT**: Edit an existing file by replacing specific content
 28  - **CREATE**: Create a new file with specified content
 29  - **DELETE**: Delete an existing file
 30  - **UNDO**: Revert the last commit if made by the same user
 31  
 32  ### Authorization
 33  
 34  This component requires GitHub authentication with a personal access token. You can set the token using the `GITHUB_TOKEN` environment variable, or pass it directly during initialization via the `github_token` parameter.
 35  
 36  To create a personal access token, visit [GitHub's token settings page](https://github.com/settings/tokens). Make sure to grant the appropriate permissions for repository access and content management.
 37  
 38  ### Installation
 39  
 40  Install the GitHub integration with pip:
 41  
 42  ```shell
 43  pip install github-haystack
 44  ```
 45  
 46  ## Usage
 47  
 48  :::note
 49  Repository Placeholder
 50  
 51  To run the following code snippets, you need to replace the `owner/repo` with your own GitHub repository name.
 52  :::
 53  
 54  ### On its own
 55  
 56  Editing an existing file:
 57  
 58  ```python
 59  from haystack_integrations.components.connectors.github import GitHubFileEditor, Command
 60  
 61  editor = GitHubFileEditor(repo="owner/repo", branch="main")
 62  
 63  result = editor.run(
 64      command=Command.EDIT,
 65      payload={
 66          "path": "src/example.py",
 67          "original": "def old_function():",
 68          "replacement": "def new_function():",
 69          "message": "Renamed function for clarity",
 70      },
 71  )
 72  
 73  print(result)
 74  ```
 75  
 76  ```bash
 77  {'result': 'Edit successful'}
 78  ```
 79  
 80  Creating a new file:
 81  
 82  ```python
 83  from haystack_integrations.components.connectors.github import GitHubFileEditor, Command
 84  
 85  editor = GitHubFileEditor(repo="owner/repo")
 86  
 87  result = editor.run(
 88      command=Command.CREATE,
 89      payload={
 90          "path": "docs/new_file.md",
 91          "content": "# New Documentation\n\nThis is a new file.",
 92          "message": "Add new documentation file",
 93      },
 94  )
 95  
 96  print(result)
 97  ```
 98  
 99  ```bash
100  {'result': 'File created successfully'}
101  ```