From 2e627dd5a80b493a955e156fdd020011684c1beb Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Sat, 14 Nov 2026 15:27:37 -0800 Subject: [PATCH] Refactor ros-ci-for-pr functions in preparation for PR stack support * Extract `parse_pull_spec()` from `validate_and_fetch_pull_list()` * Extract `prompt_continue_or_abort()` from `validate_pulls_target()` * Add optional `description_pulls` parameter to `create_ci_gist()` to be able to list different PRs in the gist description * Add optional `argv` parameter to `parse_args()` to make it testable Also add tests for these functions. Signed-off-by: Christophe Bedard --- ros_github_scripts/ci_for_pr.py | 73 +++++++++++++++++-------- test/test_ci_for_pr.py | 97 ++++++++++++++++++++++++++++++++- 2 files changed, 144 insertions(+), 26 deletions(-) diff --git a/ros_github_scripts/ci_for_pr.py b/ros_github_scripts/ci_for_pr.py index d314690..5e4a703 100755 --- a/ros_github_scripts/ci_for_pr.py +++ b/ros_github_scripts/ci_for_pr.py @@ -19,6 +19,7 @@ from typing import Dict from typing import List from typing import Optional +from typing import Tuple import github from github import Auth, Github, InputFileContent @@ -70,19 +71,22 @@ def fetch_repos(github_instance: Github, target_release: str) -> dict: def create_ci_gist( github_instance: Github, pulls: List[github.PullRequest.PullRequest], - target_release: str + target_release: str, + description_pulls: Optional[List[github.PullRequest.PullRequest]] = None, ) -> github.Gist.Gist: - """Create gist for the list of pull requests.""" + """ + Create gist for the list of pull requests. + + :param pulls: pull requests whose source branches to use in the ros2.repos file + :param description_pulls: pull requests to list in the gist description, if not `pulls` + """ logger.info('Creating ros2.repos Gist for PRs') master_repos = fetch_repos(github_instance, target_release) - shortnames = [] for github_pr in pulls: pr_ref = github_pr.head.ref pr_repo = github_pr.head.repo.full_name base_repo = github_pr.base.repo.full_name - shortnames.append(f'{base_repo}#{github_pr.number}') - # Remove the existing repository from the list repo_entry = master_repos.pop(base_repo, None) if repo_entry: @@ -102,6 +106,10 @@ def create_ci_gist( yaml_out = yaml.dump({'repositories': master_repos}, default_flow_style=False) + shortnames = [ + f'{github_pr.base.repo.full_name}#{github_pr.number}' + for github_pr in (description_pulls or pulls) + ] input_file = InputFileContent(content=yaml_out) gist = github_instance.get_user().create_gist( public=True, @@ -176,26 +184,38 @@ def prompt_pull_selection( return (texts, [issue.as_pull_request() for issue in choices.values()]) +def parse_pull_spec(pull: str) -> Tuple[str, int]: + """ + Parse a pull request specifier. + + :param pull: pull request, in the ORG/REPO#PULLNUMBER or + https://github.com/ORG/REPO/pull/PULLNUMBER format + :returns: the ORG/REPO repository name and the pull request number + """ + github_url_re = re.compile(r'https://github.com/(.*)/(.*)/pull/([0-9]+)') + url_match = re.match(github_url_re, pull) + if url_match is not None: + repo = '/'.join([url_match[1], url_match[2]]) + pull_number_str = url_match[3] + else: + if pull.count('#') != 1: + panic("Pull request descriptor doesn't match org/repo#number format: {}".format(pull)) + repo, pull_number_str = pull.split('#') + + try: + pull_number = int(pull_number_str) + except ValueError: + panic("Pull request number '{}' isn't a number...".format(pull_number_str)) + return repo, pull_number + + def validate_and_fetch_pull_list( github_instance: Github, pull_list: List[str] ) -> List[github.PullRequest.PullRequest]: """Fetch GitHub pull requests given the "org/repo#N" string input specifier format.""" - github_url_re = re.compile(r'https://github.com/(.*)/(.*)/pull/([0-9]+)') repos_to_prs = {} for pull in pull_list: - url_match = re.match(github_url_re, pull) - if url_match is not None: - repo = '/'.join([url_match[1], url_match[2]]) - pull_number_str = url_match[3] - else: - if pull.count('#') != 1: - panic("Pull request descriptor doesn't match org/repo#number format: {}".format(pull)) - repo, pull_number_str = pull.split('#') - - try: - pull_number = int(pull_number_str) - except ValueError: - panic("Pull request number '{}' isn't a number...".format(pull_number_str)) + repo, pull_number = parse_pull_spec(pull) if repo in repos_to_prs: panic(f"Can't simultaneously test multiple PRs for the same repository ({repo})") @@ -210,6 +230,13 @@ def validate_and_fetch_pull_list( return return_prs +def prompt_continue_or_abort(abort_message: str) -> None: + """Prompt for confirmation to continue anyway, and panic with the given message if not.""" + answer = input('Continue anyway? [y/N] ').strip().lower() + if answer != 'y': + panic(abort_message) + + def validate_pulls_target( pulls: List[github.PullRequest.PullRequest], target_release: str @@ -227,9 +254,7 @@ def validate_pulls_target( for pull, base_ref in mismatches: print(f' {pull.base.repo.full_name}#{pull.number}: {base_ref}') print(f' Selected target release: {target_release}') - answer = input('Continue anyway? [y/N] ').strip().lower() - if answer != 'y': - panic('Target mismatch, aborting') + prompt_continue_or_abort('Target mismatch, aborting') def format_ci_details( @@ -463,7 +488,7 @@ def comment_results( print(contents) -def parse_args(): +def parse_args(argv: Optional[List[str]] = None) -> argparse.Namespace: parser = argparse.ArgumentParser( description='Generate a CI build request for Pull Request(s)', add_help=False) @@ -530,7 +555,7 @@ def printable_package_name(value): '(e.g. https://ci.ros2.org/job/ci_launcher/19468). ' 'Use with --pulls and --comment to post the summary on the PR(s). ' 'Cannot be combined with --build.') - return parser.parse_args() + return parser.parse_args(argv) def main(): diff --git a/test/test_ci_for_pr.py b/test/test_ci_for_pr.py index d1e61e5..3012cdf 100644 --- a/test/test_ci_for_pr.py +++ b/test/test_ci_for_pr.py @@ -12,10 +12,44 @@ # See the License for the specific language governing permissions and # limitations under the License. +from contextlib import redirect_stderr, redirect_stdout +import io import unittest -from unittest.mock import MagicMock +from unittest.mock import MagicMock, patch -from ros_github_scripts.ci_for_pr import validate_and_fetch_pull_list +from ros_github_scripts.ci_for_pr import ( + create_ci_gist, + parse_args, + parse_pull_spec, + validate_and_fetch_pull_list, + validate_pulls_target, +) +import yaml + + +def make_pull(number, base_ref='rolling'): + """Make a mock pull request in the ros2/rviz repository.""" + pull = MagicMock() + pull.number = number + pull.head.ref = f'branch-{number}' + pull.head.repo.full_name = 'ros2/rviz' + pull.base.ref = base_ref + pull.base.repo.full_name = 'ros2/rviz' + return pull + + +class TestParsePullSpec(unittest.TestCase): + + def test_valid_pull_descriptors(self): + self.assertEqual(parse_pull_spec('ros2/rviz#1915'), ('ros2/rviz', 1915)) + self.assertEqual( + parse_pull_spec('https://github.com/ros2/rviz/pull/1915'), ('ros2/rviz', 1915)) + + def test_invalid_pull_descriptors(self): + for pull in ('ros2/rviz', 'ros2/rviz#1#2', 'ros2/rviz#abc'): + with self.subTest(pull=pull): + with self.assertRaises(RuntimeError): + parse_pull_spec(pull) class TestValidateAndFetchPullList(unittest.TestCase): @@ -39,3 +73,62 @@ def test_invalid_pull_number(self): with self.subTest(pull=pull): with self.assertRaisesRegex(RuntimeError, "isn't a number"): validate_and_fetch_pull_list(MagicMock(), [pull]) + + +class TestValidatePullsTarget(unittest.TestCase): + + @patch('builtins.input') + def test_target_match(self, mock_input): + validate_pulls_target([make_pull(1), make_pull(2)], 'rolling') + mock_input.assert_not_called() + + @patch('builtins.input', return_value='n') + def test_target_mismatch_abort(self, mock_input): + with redirect_stdout(io.StringIO()) as stdout: + with self.assertRaisesRegex(RuntimeError, 'Target mismatch'): + validate_pulls_target([make_pull(1), make_pull(2, 'lyrical')], 'rolling') + self.assertIn('ros2/rviz#2: lyrical', stdout.getvalue()) + self.assertNotIn('ros2/rviz#1:', stdout.getvalue()) + + @patch('builtins.input', return_value='y') + def test_target_mismatch_continue(self, mock_input): + with redirect_stdout(io.StringIO()): + validate_pulls_target([make_pull(1, 'lyrical')], 'rolling') + mock_input.assert_called_once() + + +class TestParseArgs(unittest.TestCase): + + # Can't use --pulls and --interactive together + def test_pulls_interactive_exclusive(self): + with redirect_stderr(io.StringIO()), self.assertRaises(SystemExit): + parse_args(['--pulls', 'ros2/rclcpp#1', '--interactive']) + + +class TestCreateCIGist(unittest.TestCase): + + def run_create_ci_gist(self, pulls, description_pulls=None): + """Create a gist for mock PRs, and return its repositories and its description.""" + github_instance = MagicMock() + github_instance.get_repo.return_value.get_contents.return_value.decoded_content = ( + b'repositories:\n' + b' ros2/rviz:\n' + b' type: git\n' + b' url: https://github.com/ros2/rviz.git\n' + b' version: rolling\n') + with patch('ros_github_scripts.ci_for_pr.InputFileContent') as input_file_content: + create_ci_gist(github_instance, pulls, 'rolling', description_pulls=description_pulls) + repos = yaml.safe_load(input_file_content.call_args.kwargs['content'])['repositories'] + create_gist = github_instance.get_user.return_value.create_gist + return repos, create_gist.call_args.kwargs['description'] + + def test_pulls(self): + repos, description = self.run_create_ci_gist([make_pull(1915)]) + self.assertEqual(repos['ros2/rviz']['version'], 'branch-1915') + self.assertEqual(description, 'CI input for PR ros2/rviz#1915') + + def test_description_pulls(self): + pulls = [make_pull(1915), make_pull(1916)] + repos, description = self.run_create_ci_gist([pulls[-1]], description_pulls=pulls) + self.assertEqual(repos['ros2/rviz']['version'], 'branch-1916') + self.assertEqual(description, 'CI input for PR ros2/rviz#1915 ros2/rviz#1916')