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