mirror of
https://chromium.googlesource.com/chromium/tools/depot_tools.git
synced 2026-01-11 10:41:31 +00:00
my_activity: Add some tests and fix some bugs
Change-Id: Ib5625ae9d052eca9d6c6b64f12f64e6697aef7a8 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2072874 Reviewed-by: Anthony Polito <apolito@google.com> Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
This commit is contained in:
@@ -129,7 +129,7 @@ def datetime_to_midnight(date):
|
||||
|
||||
def get_quarter_of(date):
|
||||
begin = (datetime_to_midnight(date) -
|
||||
relativedelta(months=(date.month % 3) - 1, days=(date.day - 1)))
|
||||
relativedelta(months=(date.month - 1) % 3, days=(date.day - 1)))
|
||||
return begin, begin + relativedelta(months=3)
|
||||
|
||||
|
||||
@@ -160,6 +160,24 @@ def datetime_from_gerrit(date_string):
|
||||
def datetime_from_monorail(date_string):
|
||||
return datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%S')
|
||||
|
||||
def extract_bug_numbers_from_description(issue):
|
||||
# Getting the description for REST Gerrit
|
||||
revision = issue['revisions'][issue['current_revision']]
|
||||
description = revision['commit']['message']
|
||||
|
||||
bugs = []
|
||||
# Handle both "Bug: 99999" and "BUG=99999" bug notations
|
||||
# Multiple bugs can be noted on a single line or in multiple ones.
|
||||
matches = re.findall(
|
||||
r'^(BUG=|(Bug|Fixed):\s*)((((?:[a-zA-Z0-9-]+:)?\d+)(,\s?)?)+)',
|
||||
description, flags=re.IGNORECASE | re.MULTILINE)
|
||||
if matches:
|
||||
for match in matches:
|
||||
bugs.extend(match[2].replace(' ', '').split(','))
|
||||
# Add default chromium: prefix if none specified.
|
||||
bugs = [bug if ':' in bug else 'chromium:%s' % bug for bug in bugs]
|
||||
|
||||
return sorted(set(bugs))
|
||||
|
||||
class MyActivity(object):
|
||||
def __init__(self, options):
|
||||
@@ -180,32 +198,6 @@ class MyActivity(object):
|
||||
sys.stdout.write(how)
|
||||
sys.stdout.flush()
|
||||
|
||||
def extract_bug_numbers_from_description(self, issue):
|
||||
description = None
|
||||
|
||||
if 'description' in issue:
|
||||
# Getting the description for Rietveld
|
||||
description = issue['description']
|
||||
elif 'revisions' in issue:
|
||||
# Getting the description for REST Gerrit
|
||||
revision = issue['revisions'][issue['current_revision']]
|
||||
description = revision['commit']['message']
|
||||
|
||||
bugs = []
|
||||
if description:
|
||||
# Handle both "Bug: 99999" and "BUG=99999" bug notations
|
||||
# Multiple bugs can be noted on a single line or in multiple ones.
|
||||
matches = re.findall(
|
||||
r'BUG[=:]\s?((((?:[a-zA-Z0-9-]+:)?\d+)(,\s?)?)+)', description,
|
||||
flags=re.IGNORECASE)
|
||||
if matches:
|
||||
for match in matches:
|
||||
bugs.extend(match[0].replace(' ', '').split(','))
|
||||
# Add default chromium: prefix if none specified.
|
||||
bugs = [bug if ':' in bug else 'chromium:%s' % bug for bug in bugs]
|
||||
|
||||
return sorted(set(bugs))
|
||||
|
||||
def gerrit_changes_over_rest(self, instance, filters):
|
||||
# Convert the "key:value" filter to a list of (key, value) pairs.
|
||||
req = list(f.split(':', 1) for f in filters)
|
||||
@@ -272,7 +264,7 @@ class MyActivity(object):
|
||||
ret['replies'] = []
|
||||
ret['reviewers'] = set(r['author'] for r in ret['replies'])
|
||||
ret['reviewers'].discard(ret['author'])
|
||||
ret['bugs'] = self.extract_bug_numbers_from_description(issue)
|
||||
ret['bugs'] = extract_bug_numbers_from_description(issue)
|
||||
return ret
|
||||
|
||||
@staticmethod
|
||||
|
||||
94
tests/my_activity_test.py
Normal file
94
tests/my_activity_test.py
Normal file
@@ -0,0 +1,94 @@
|
||||
#!/usr/bin/env vpython3
|
||||
# Copyright 2020 The Chromium Authors. All rights reserved.
|
||||
# Use of this source code is governed by a BSD-style license that can be
|
||||
# found in the LICENSE file.
|
||||
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, DEPOT_TOOLS_ROOT)
|
||||
|
||||
import my_activity
|
||||
|
||||
class MyActivityTest(unittest.TestCase):
|
||||
def test_datetime_to_midnight(self):
|
||||
self.assertEqual(
|
||||
datetime(2020, 9, 12),
|
||||
my_activity.datetime_to_midnight(datetime(2020, 9, 12, 13, 0, 0)))
|
||||
self.assertEqual(
|
||||
datetime(2020, 12, 31),
|
||||
my_activity.datetime_to_midnight(datetime(2020, 12, 31, 23, 59, 59)))
|
||||
self.assertEqual(
|
||||
datetime(2020, 12, 31),
|
||||
my_activity.datetime_to_midnight(datetime(2020, 12, 31)))
|
||||
|
||||
def test_get_quarter_of(self):
|
||||
self.assertEqual(
|
||||
(datetime(2020, 7, 1), datetime(2020, 10, 1)),
|
||||
my_activity.get_quarter_of(datetime(2020, 9, 12)))
|
||||
# Quarter range includes beggining
|
||||
self.assertEqual(
|
||||
(datetime(2020, 10, 1), datetime(2021, 1, 1)),
|
||||
my_activity.get_quarter_of(datetime(2020, 10, 1)))
|
||||
# Quarter range excludes end
|
||||
self.assertEqual(
|
||||
(datetime(2021, 1, 1), datetime(2021, 4, 1)),
|
||||
my_activity.get_quarter_of(datetime(2021, 1, 1)))
|
||||
self.assertEqual(
|
||||
(datetime(2020, 10, 1), datetime(2021, 1, 1)),
|
||||
my_activity.get_quarter_of(datetime(2020, 12, 31, 23, 59, 59)))
|
||||
|
||||
def test_get_year_of(self):
|
||||
self.assertEqual(
|
||||
(datetime(2020, 1, 1), datetime(2021, 1, 1)),
|
||||
my_activity.get_year_of(datetime(2020, 9, 12)))
|
||||
# Year range includes beggining
|
||||
self.assertEqual(
|
||||
(datetime(2020, 1, 1), datetime(2021, 1, 1)),
|
||||
my_activity.get_year_of(datetime(2020, 1, 1)))
|
||||
# Year range excludes end
|
||||
self.assertEqual(
|
||||
(datetime(2021, 1, 1), datetime(2022, 1, 1)),
|
||||
my_activity.get_year_of(datetime(2021, 1, 1)))
|
||||
|
||||
def test_get_week_of(self):
|
||||
self.assertEqual(
|
||||
(datetime(2020, 9, 7), datetime(2020, 9, 14)),
|
||||
my_activity.get_week_of(datetime(2020, 9, 12)))
|
||||
# Week range includes beginning
|
||||
self.assertEqual(
|
||||
(datetime(2020, 9, 7), datetime(2020, 9, 14)),
|
||||
my_activity.get_week_of(datetime(2020, 9, 7)))
|
||||
# Week range excludes beginning
|
||||
self.assertEqual(
|
||||
(datetime(2020, 9, 14), datetime(2020, 9, 21)),
|
||||
my_activity.get_week_of(datetime(2020, 9, 14)))
|
||||
|
||||
def _get_issue_with_description(self, description):
|
||||
return {
|
||||
'current_revision': 'rev',
|
||||
'revisions': {'rev': {'commit': {'message': description}}},
|
||||
}
|
||||
|
||||
def test_extract_bug_numbers_from_description(self):
|
||||
issue = self._get_issue_with_description(
|
||||
'Title\n'
|
||||
'\n'
|
||||
'Description\n'
|
||||
'A comment:\n'
|
||||
'> Bug: 1234, another:5678\n'
|
||||
'\n'
|
||||
'Bug: another:1234, 5678\n'
|
||||
'BUG=project:13141516\n'
|
||||
'Fixed: fixed:9101112\n'
|
||||
'Change-Id: Iabcdef1234567890\n')
|
||||
self.assertEqual(
|
||||
['another:1234', 'chromium:5678', 'fixed:9101112', 'project:13141516'],
|
||||
my_activity.extract_bug_numbers_from_description(issue))
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user