Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions patchwork/steps/CreatePR/CreatePR.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import git
from git.exc import GitCommandError
from typing import Optional

from patchwork.common.client.scm import (
GithubClient,
Expand Down Expand Up @@ -52,6 +53,7 @@ def __init__(self, inputs: dict):
self.pr_body = inputs.get("pr_body", "")
self.title = inputs.get("pr_title", "Patchwork PR")
self.force = bool(inputs.get("force_pr_creation", False))
self.issue_url = inputs.get("issue_url") # Optional GitHub Issue URL to link the PR to
self.base_branch = inputs.get("base_branch")
if self.enabled and self.base_branch is None:
logger.warn("Base branch not provided. Skipping PR creation.")
Expand Down Expand Up @@ -108,6 +110,7 @@ def run(self) -> dict:
target_branch_name=self.target_branch,
scm_client=self.scm_client,
force=self.force,
issue_url=self.issue_url, # Add issue URL to link PR with issue
)

logger.info(f"[green]PR created at [link={url}]{url}[/link][/]", extra={"markup": True})
Expand Down Expand Up @@ -148,20 +151,26 @@ def create_pr(
target_branch_name: str,
scm_client: ScmPlatformClientProtocol,
force: bool = False,
issue_url: Optional[str] = None, # Optional GitHub Issue URL to link the PR to
):
prs = scm_client.find_prs(repo_slug, original_branch=base_branch_name, feature_branch=target_branch_name)
pr = next(iter(prs), None)
if pr is None:
final_body = body
if issue_url is not None:
issue_info = scm_client.get_slug_and_id_from_url(issue_url)
if issue_info is not None:
_, issue_number = issue_info
final_body = f"{body}\n\nResolves #{issue_number}"

Comment on lines +159 to +165
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only applicable for github repos. Perhaps check the scm_client's type first before performing this.

pr = scm_client.create_pr(
repo_slug,
title,
body,
final_body,
base_branch_name,
target_branch_name,
)

pr.set_pr_description(body)

return pr.url()

if force:
Expand Down
3 changes: 2 additions & 1 deletion patchwork/steps/CreatePR/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
- `pr_title`: Title for the pull request
- `force_pr_creation`: Flag to force creation of the pull request
- `base_branch`: Base branch for the pull request
- `issue_url`: GitHub Issue URL to link the PR to (optional). When provided, adds "Resolves #<ISSUE_NUMBER>" to PR description

## Outputs
- `run() -> dict`: Method to run the pull request creation process with the following output:
- `pr_url`: URL of the created pull request

This module provides functionality to create a pull request on a source control management platform (Github or Gitlab) based on the input data provided. It includes methods for checking required data, handling platform-specific API keys, setting up the pull request parameters, and executing the pull request creation process. The `create_pr` method within the module helps in finding or creating a pull request with necessary details and descriptions. The module also logs information throughout the process for tracking and verification purposes.
This module provides functionality to create a pull request on a source control management platform (Github or Gitlab) based on the input data provided. It includes methods for checking required data, handling platform-specific API keys, setting up the pull request parameters, and executing the pull request creation process. The `create_pr` method within the module helps in finding or creating a pull request with necessary details and descriptions, including the ability to link PRs to GitHub issues. The module also logs information throughout the process for tracking and verification purposes. This module is used by both PR and PRPB (Pull Request Patch Builder) steps, where PRPB is an alias for PR defined in `patchwork/steps/__init__.py`.
2 changes: 2 additions & 0 deletions patchwork/steps/CreatePR/typed.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing_extensions import Annotated, TypedDict
from typing import Optional

from patchwork.common.utils.step_typing import StepTypeConfig

Expand All @@ -16,6 +17,7 @@ class CreatePRInputs(__CreatePRRequiredInputs, total=False):
scm_url: Annotated[str, StepTypeConfig(is_config=True)]
gitlab_api_key: Annotated[str, StepTypeConfig(is_config=True)]
github_api_key: Annotated[str, StepTypeConfig(is_config=True)]
issue_url: Optional[str]
azuredevops_api_key: Annotated[str, StepTypeConfig(is_config=True)]


Expand Down
Loading