Skip to content

Commit

Permalink
Cherry-pick 276124@main (846605f). rdar://124627200
Browse files Browse the repository at this point in the history
    [git-webkit] Detect merge-back clone
    https://bugs.webkit.org/show_bug.cgi?id=270986
    rdar://124627200

    Reviewed by Aakash Jain.

    If the user doesn't specify a merge-back behavior when cloning,
    we can infer that behavior based on bug comments.

    * Tools/Scripts/libraries/webkitscmpy/webkitscmpy/program/clone.py:
    (Clone.parser): Add --no-merge-back option.
    (Clone.main): Push reason resolution below issue resolution. Attempt
    to determine the branch issue was committed to, and assume merge-back
    if the issue was not committed to the default branch.
    * Tools/Scripts/libraries/webkitscmpy/webkitscmpy/test/clone_unittest.py:
    (TestClone.test_infer_merge_back):

    Canonical link: https://commits.webkit.org/276124@main

Canonical link: https://commits.webkit.org/272448.739@safari-7618-branch
  • Loading branch information
JonWBedard committed Mar 15, 2024
1 parent 2892e42 commit f84cad4
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 14 deletions.
56 changes: 43 additions & 13 deletions Tools/Scripts/libraries/webkitscmpy/webkitscmpy/program/clone.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2023 Apple Inc. All rights reserved.
# Copyright (C) 2023-2024 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
Expand All @@ -20,6 +20,7 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import re
import sys

from .command import Command
Expand All @@ -33,6 +34,7 @@ class Clone(Command):
name = 'clone'
help = 'Clone the radar a bugzilla or commit refers to'
UMBRELLA = b'\xe2\x98\x82\xef\xb8\x8f'.decode('utf-8')
COMMITTED_RE = re.compile(r'Committed \d+\.?\d+@\S+( \([a-f0-9A-F]+\))? to (?P<branch>\S*) referencing this bug')

@classmethod
def parser(cls, parser, loggers=None):
Expand Down Expand Up @@ -66,10 +68,10 @@ def parser(cls, parser, loggers=None):
action='store_true',
)
parser.add_argument(
'--merge-back',
dest='merge_back', default=False,
'--merge-back', '--no-merge-back',
dest='merge_back', default=None,
help='Annotate the cloned issue for merge-back',
action='store_true',
action=arguments.NoAction,
)

@classmethod
Expand Down Expand Up @@ -100,10 +102,10 @@ def parent(cls, rdar, milestone):
), None)

@classmethod
def main(cls, args, repository, merge_back=False, **kwargs):
def main(cls, args, repository, merge_back=None, **kwargs):
rdar = None
merge_back = merge_back or args.merge_back
prefix = '[merge-back]' if merge_back else ''
if args.merge_back is not None:
merge_back = args.merge_back

for tracker in Tracker._trackers:
if isinstance(tracker, radar.Tracker):
Expand All @@ -117,12 +119,6 @@ def main(cls, args, repository, merge_back=False, **kwargs):
sys.stderr.write('Radar is not available on this machine\n')
return 255

if not args.reason and args.milestone:
args.reason = "Cloning for inclusion in '{}'".format(args.milestone)
if not args.reason:
sys.stderr.write('No reason for cloning issue has been provided\n')
return 255

# First, check if we were provided a bug URL
issue = Tracker.from_string(args.argument[0])
if not issue:
Expand Down Expand Up @@ -160,6 +156,40 @@ def main(cls, args, repository, merge_back=False, **kwargs):
sys.stderr.write("'{}' is not a radar, therefore cannot be cloned\n".format(issue))
return 255

# Attempt to detect if this invocation is likely for merge-back
if merge_back is None:
on_default_branch = False
on_release_branch = False

branch = None
for comment in issue.comments:
mtch = cls.COMMITTED_RE.match(comment.content)
if mtch:
branch = mtch.group('branch')
if branch == repository.default_branch:
on_default_branch = True
break
else:
on_release_branch = True

if on_default_branch:
merge_back = False
elif on_release_branch and (args.prompt or Terminal.choose(
prompt='Change is on {} but not {}, would you like to create a merge-back clone?'.format(branch, repository.default_branch),
default='Yes',
) == 'Yes'):
merge_back = True

prefix = '[merge-back]' if merge_back else ''

if not args.reason and merge_back:
args.reason = "Cloning for merge-back to {}".format(repository.default_branch)
if not args.reason and args.milestone:
args.reason = "Cloning for inclusion in '{}'".format(args.milestone)
if not args.reason:
sys.stderr.write('No reason for cloning issue has been provided\n')
return 255

milestone = None
raw_issue = rdar.client.radar_for_id(issue.id)
milestones = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2023 Apple Inc. All rights reserved.
# Copyright (C) 2023-2024 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -239,3 +239,51 @@ def test_merge_back(self):
"Created '[merge-back] rdar://5 Example issue 1'\n"
'Moved clone to Internal Tools - October and into Analyze: Prepare\n',
)

def test_infer_merge_back(self):
issues = [issue.copy() for issue in bmocks.ISSUES]
issues[0]['category'] = 'Important'

issues.append(dict(
title=u'{} merge-back October release'.format(program.Clone.UMBRELLA),
timestamp=int(time.time()),
opened=True,
creator=bmocks.USERS['Tim Contributor'],
assignee=bmocks.USERS['Tim Contributor'],
description='Umbrella bug tracking October merge-back',
project='WebKit',
component='Text',
version='Other',
milestone='October',
))

with mocks.local.Git(self.path), mocks.local.Svn(), Environment(RADAR_USERNAME='tcontributor'), bmocks.Radar(
issues=issues,
projects=bmocks.PROJECTS,
milestones=bmocks.MILESTONES,
), MockTerminal.input('y', '1'), OutputCapture() as captured, patch('webkitbugspy.Tracker._trackers', [radar.Tracker()]):
tracker = radar.Tracker()
tracker.issue(1).add_comment('Committed 1234.10@some-branch (12345678) to some-branch referencing this bug')

self.assertEqual(0, program.main(
args=('clone', 'rdar://1', '--milestone', 'October'),
path=self.path,
))
raw_issue = tracker.client.radar_for_id(5)

self.assertEqual(raw_issue.milestone.name, 'Internal Tools - October')
self.assertEqual(raw_issue.category.name, 'Important')
self.assertIsNone(raw_issue.event)
self.assertIsNone(raw_issue.tentpole)

parents = tracker.issue(5).related.get('subtask-of')
self.assertEqual(1, len(parents))
self.assertEqual(parents[0].title, u'{} merge-back October release'.format(program.Clone.UMBRELLA))

self.assertEqual(captured.stderr.getvalue(), '')
self.assertEqual(
captured.stdout.getvalue(),
"Change is on some-branch but not main, would you like to create a merge-back clone? ([Yes]/No): \n"
"Created '[merge-back] rdar://5 Example issue 1'\n"
'Moved clone to Internal Tools - October and into Analyze: Prepare\n',
)

0 comments on commit f84cad4

Please sign in to comment.