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