Extract bug/fix from branch only on create

depot_tools supports extracting information from branch name. E.g. if
branch contains fix-\d or bug-\d, commit description will contains
appropriate git footers.

However, such behavior should happen only on initial CL upload. Should
user decide to delete such footer, we shouldn't set bug / fix on any
following PS.

R=ehmaldonado@chromium.org

Bug: 1225663
Change-Id: I66adfdca070083ab66727d132919d47feb7ddd43
Fixed: 1225663
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3010709
Auto-Submit: Josip Sokcevic <sokcevic@google.com>
Reviewed-by: Andy Perelson <ajp@chromium.org>
Commit-Queue: Josip Sokcevic <sokcevic@google.com>
changes/09/3010709/4
Josip Sokcevic 4 years ago committed by LUCI CQ
parent 76159d5a25
commit 340edc365b

@ -1192,7 +1192,7 @@ class Changelist(object):
return '%s/%s' % (server, issue) return '%s/%s' % (server, issue)
def GetUsePython3(self): def GetUsePython3(self):
return settings.GetUsePython3() return settings.GetUsePython3()
def FetchDescription(self, pretty=False): def FetchDescription(self, pretty=False):
assert self.GetIssue(), 'issue is required to query Gerrit' assert self.GetIssue(), 'issue is required to query Gerrit'
@ -1414,16 +1414,25 @@ class Changelist(object):
if options.title and options.squash: if options.title and options.squash:
description = options.title + '\n\n' + description description = options.title + '\n\n' + description
# Extract bug number from branch name.
bug = options.bug bug = options.bug
fixed = options.fixed fixed = options.fixed
match = re.match(r'(?P<type>bug|fix(?:e[sd])?)[_-]?(?P<bugnum>\d+)', if not self.GetIssue():
self.GetBranch()) # Extract bug number from branch name, but only if issue is being created.
if not bug and not fixed and match: # It must start with bug or fix, followed by _ or - and number.
if match.group('type') == 'bug': # Optionally, it may contain _ or - after number with arbitrary text.
bug = match.group('bugnum') # Examples:
else: # bug-123
fixed = match.group('bugnum') # bug_123
# fix-123
# fix-123-some-description
match = re.match(
r'^(?P<type>bug|fix(?:e[sd])?)[_-]?(?P<bugnum>\d+)([-_]|$)',
self.GetBranch())
if not bug and not fixed and match:
if match.group('type') == 'bug':
bug = match.group('bugnum')
else:
fixed = match.group('bugnum')
change_description = ChangeDescription(description, bug, fixed) change_description = ChangeDescription(description, bug, fixed)

@ -1585,15 +1585,15 @@ class TestGitCl(unittest.TestCase):
} }
cl = git_cl.Changelist(issue=1234) cl = git_cl.Changelist(issue=1234)
actual = cl._GetDescriptionForUpload( actual = cl._GetDescriptionForUpload(options=mock.Mock(
options=mock.Mock( bug=bug,
bug=bug, fixed=fixed,
fixed=fixed, reviewers=reviewers,
reviewers=reviewers, tbrs=tbrs,
tbrs=tbrs, add_owners_to=add_owners_to,
add_owners_to=add_owners_to), message=initial_description),
git_diff_args=None, git_diff_args=None,
files=list(owners_by_path)) files=list(owners_by_path))
self.assertEqual(expected_description, actual.description) self.assertEqual(expected_description, actual.description)
def testGetDescriptionForUpload(self): def testGetDescriptionForUpload(self):
@ -1617,8 +1617,9 @@ class TestGitCl(unittest.TestCase):
'Fixed: prefix:1234', 'Fixed: prefix:1234',
])) ]))
@mock.patch('git_cl.Changelist.GetIssue')
def testGetDescriptionForUpload_BugFromBranch(self): def testGetDescriptionForUpload_BugFromBranch(self, mockGetIssue):
mockGetIssue.return_value = None
self.getDescriptionForUploadTest( self.getDescriptionForUploadTest(
branch='bug-1234', branch='bug-1234',
expected_description='\n'.join([ expected_description='\n'.join([
@ -1627,7 +1628,9 @@ class TestGitCl(unittest.TestCase):
'Bug: prefix:1234', 'Bug: prefix:1234',
])) ]))
def testGetDescriptionForUpload_FixedFromBranch(self): @mock.patch('git_cl.Changelist.GetIssue')
def testGetDescriptionForUpload_FixedFromBranch(self, mockGetIssue):
mockGetIssue.return_value = None
self.getDescriptionForUploadTest( self.getDescriptionForUploadTest(
branch='fix-1234', branch='fix-1234',
expected_description='\n'.join([ expected_description='\n'.join([
@ -1636,6 +1639,12 @@ class TestGitCl(unittest.TestCase):
'Fixed: prefix:1234', 'Fixed: prefix:1234',
])) ]))
def testGetDescriptionForUpload_SkipBugFromBranchIfAlreadyUploaded(self):
self.getDescriptionForUploadTest(
branch='bug-1234',
expected_description='desc',
)
def testGetDescriptionForUpload_AddOwnersToR(self): def testGetDescriptionForUpload_AddOwnersToR(self):
self.getDescriptionForUploadTest( self.getDescriptionForUploadTest(
reviewers=['a@example.com'], reviewers=['a@example.com'],

Loading…
Cancel
Save