githubissuecommenter.mdx
1 --- 2 title: "GitHubIssueCommenter" 3 id: githubissuecommenter 4 slug: "/githubissuecommenter" 5 description: "This component posts comments to GitHub issues using the GitHub API." 6 --- 7 8 # GitHubIssueCommenter 9 10 This component posts comments to GitHub issues using the GitHub API. 11 12 <div className="key-value-table"> 13 14 | | | 15 | --- | --- | 16 | **Most common position in a pipeline** | After a Chat Generator that provides the comment text to post 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** | `url`: A GitHub issue URL <br /> <br />`comment`: Comment text to post | 19 | **Output variables** | `success`: Boolean indicating whether the comment was posted successfully | 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 `GitHubIssueCommenter` takes a GitHub issue URL and comment text, then posts the comment to the specified issue. 28 29 The component requires authentication with a GitHub personal access token since posting comments is an authenticated operation. 30 31 ### Authorization 32 33 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. 34 35 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 issue management. 36 37 ### Installation 38 39 Install the GitHub integration with pip: 40 41 ```shell 42 pip install github-haystack 43 ``` 44 45 ## Usage 46 47 :::info[Repository Placeholder] 48 49 To run the following code snippets, you need to replace the `owner/repo` with your own GitHub repository name. 50 ::: 51 52 ### On its own 53 54 Basic usage with environment variable authentication: 55 56 ```python 57 from haystack_integrations.components.connectors.github import GitHubIssueCommenter 58 59 commenter = GitHubIssueCommenter() 60 result = commenter.run( 61 url="https://github.com/owner/repo/issues/123", 62 comment="Thanks for reporting this issue! We'll look into it.", 63 ) 64 65 print(result) 66 ``` 67 68 ```bash 69 {'success': True} 70 ``` 71 72 ### In a pipeline 73 74 The following pipeline analyzes a GitHub issue and automatically posts a response: 75 76 ```python 77 from haystack import Pipeline 78 from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder 79 from haystack.components.generators.chat import OpenAIChatGenerator 80 from haystack.dataclasses import ChatMessage 81 from haystack_integrations.components.connectors.github import ( 82 GitHubIssueViewer, 83 GitHubIssueCommenter, 84 ) 85 86 issue_viewer = GitHubIssueViewer() 87 issue_commenter = GitHubIssueCommenter() 88 89 prompt_template = [ 90 ChatMessage.from_system( 91 "You are a helpful assistant that analyzes GitHub issues and creates appropriate responses.", 92 ), 93 ChatMessage.from_user( 94 "Based on the following GitHub issue:\n" 95 "{% for document in documents %}" 96 "{% if document.meta.type == 'issue' %}" 97 "**Issue Title:** {{ document.meta.title }}\n" 98 "**Issue Description:** {{ document.content }}\n" 99 "{% endif %}" 100 "{% endfor %}\n" 101 "Generate a helpful response comment for this issue. Keep it professional and concise.", 102 ), 103 ] 104 105 prompt_builder = ChatPromptBuilder(template=prompt_template, required_variables="*") 106 llm = OpenAIChatGenerator(model="gpt-4o-mini") 107 108 pipeline = Pipeline() 109 pipeline.add_component("issue_viewer", issue_viewer) 110 pipeline.add_component("prompt_builder", prompt_builder) 111 pipeline.add_component("llm", llm) 112 pipeline.add_component("issue_commenter", issue_commenter) 113 114 pipeline.connect("issue_viewer.documents", "prompt_builder.documents") 115 pipeline.connect("prompt_builder.prompt", "llm.messages") 116 pipeline.connect("llm.replies", "issue_commenter.comment") 117 118 issue_url = "https://github.com/owner/repo/issues/123" 119 result = pipeline.run( 120 data={"issue_viewer": {"url": issue_url}, "issue_commenter": {"url": issue_url}}, 121 ) 122 123 print(f"Comment posted successfully: {result['issue_commenter']['success']}") 124 ``` 125 126 ``` 127 Comment posted successfully: True 128 ```