githubprcreatortool.mdx
  1  ---
  2  title: "GitHubPRCreatorTool"
  3  id: githubprcreatortool
  4  slug: "/githubprcreatortool"
  5  description: "A Tool that allows Agents and ToolInvokers to create pull requests from a fork back to the original repository."
  6  ---
  7  
  8  # GitHubPRCreatorTool
  9  
 10  A Tool that allows Agents and ToolInvokers to create pull requests from a fork back to the original repository.
 11  
 12  |                              |                                                                                          |
 13  | ---------------------------- | ---------------------------------------------------------------------------------------- |
 14  | **Mandatory init variables** | "github_token": GitHub personal access token. Can be set with `GITHUB_TOKEN` env var.    |
 15  | **API reference**            | [Tools](/reference/tools-api)                                                            |
 16  | **GitHub link**              | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/github |
 17  
 18  ## Overview
 19  
 20  `GitHubPRCreatorTool` wraps the [`GitHubPRCreator`](../../pipeline-components/connectors/githubprcreator.mdx) component, providing a tool interface for use in agent workflows and tool-based pipelines.
 21  
 22  The tool takes a GitHub issue URL and creates a pull request from your fork to the original repository, automatically linking it to the specified issue. It's designed to work with existing forks and assumes you have already made changes in a branch.
 23  
 24  ### Parameters
 25  
 26  - `name` is _optional_ and defaults to "pr_creator". Specifies the name of the tool.
 27  - `description` is _optional_ and provides context to the LLM about what the tool does.
 28  - `github_token` is _mandatory_ and must be a GitHub personal access token from the fork owner. The default setting uses the environment variable `GITHUB_TOKEN`.
 29  - `raise_on_failure` is _optional_ and defaults to `True`. If False, errors are returned instead of raising exceptions.
 30  
 31  ## Usage
 32  
 33  Install the GitHub integration to use the `GitHubPRCreatorTool`:
 34  
 35  ```shell
 36  pip install github-haystack
 37  ```
 38  
 39  :::note
 40  Repository Placeholder
 41  
 42  To run the following code snippets, you need to replace the `owner/repo` with your own GitHub repository name.
 43  :::
 44  
 45  ### On its own
 46  
 47  Basic usage to create a pull request:
 48  
 49  ```python
 50  from haystack_integrations.tools.github import GitHubPRCreatorTool
 51  
 52  tool = GitHubPRCreatorTool()
 53  result = tool.invoke(
 54      issue_url="https://github.com/owner/repo/issues/123",
 55      title="Fix issue #123",
 56      body="This PR addresses issue #123 by implementing the requested changes.",
 57      branch="fix-123",  # Branch in your fork with the changes
 58      base="main",  # Branch in original repo to merge into
 59  )
 60  
 61  print(result)
 62  ```
 63  
 64  ```bash
 65  {'result': 'Pull request #16 created successfully and linked to issue #4'}
 66  ```
 67  
 68  ### With an Agent
 69  
 70  You can use `GitHubPRCreatorTool` with the [Agent](../../pipeline-components/agents-1/agent.mdx) component. The Agent will automatically invoke the tool when needed to create pull requests.
 71  
 72  ```python
 73  from haystack.components.generators.chat import OpenAIChatGenerator
 74  from haystack.dataclasses import ChatMessage
 75  from haystack.components.agents import Agent
 76  from haystack_integrations.tools.github import GitHubPRCreatorTool
 77  
 78  pr_tool = GitHubPRCreatorTool(name="github_pr_creator")
 79  
 80  agent = Agent(
 81      chat_generator=OpenAIChatGenerator(),
 82      tools=[pr_tool],
 83      exit_conditions=["text"],
 84  )
 85  
 86  agent.warm_up()
 87  response = agent.run(
 88      messages=[
 89          ChatMessage.from_user(
 90              "Create a pull request for issue https://github.com/owner/repo/issues/4 with title 'Fix authentication bug' and empty body using my fix-4 branch and main as target branch",
 91          ),
 92      ],
 93  )
 94  
 95  print(response["last_message"].text)
 96  ```
 97  
 98  ```bash
 99  The pull request titled "Fix authentication bug" has been created successfully and linked to issue [#123](https://github.com/owner/repo/issues/4).
100  ```