diff --git a/patchwork/steps/CreatePR/CreatePR.py b/patchwork/steps/CreatePR/CreatePR.py index ba2be895e..98acc3651 100644 --- a/patchwork/steps/CreatePR/CreatePR.py +++ b/patchwork/steps/CreatePR/CreatePR.py @@ -2,6 +2,7 @@ import git from git.exc import GitCommandError +from typing import Optional from patchwork.common.client.scm import ( GithubClient, @@ -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.") @@ -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}) @@ -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}" + 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: diff --git a/patchwork/steps/CreatePR/README.md b/patchwork/steps/CreatePR/README.md index 93e5096e3..415d7f4ae 100644 --- a/patchwork/steps/CreatePR/README.md +++ b/patchwork/steps/CreatePR/README.md @@ -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 #" 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. \ No newline at end of file +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`. diff --git a/patchwork/steps/CreatePR/typed.py b/patchwork/steps/CreatePR/typed.py index 9bd7e4401..379646937 100644 --- a/patchwork/steps/CreatePR/typed.py +++ b/patchwork/steps/CreatePR/typed.py @@ -1,4 +1,5 @@ from typing_extensions import Annotated, TypedDict +from typing import Optional from patchwork.common.utils.step_typing import StepTypeConfig @@ -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)]