def setUp(self): super(ExportNotifierTest, self).setUp() self.host = MockHost() self.git = self.host.git() self.gerrit = MockGerritAPI() self.notifier = ExportNotifier(self.host, self.git, self.gerrit)
class ExportNotifierTest(LoggingTestCase): def setUp(self): super(ExportNotifierTest, self).setUp() self.host = MockHost() self.git = self.host.git() self.gerrit = MockGerritAPI() self.notifier = ExportNotifier(self.host, self.git, self.gerrit) def test_get_gerrit_sha_from_comment_success(self): gerrit_comment = self.generate_notifier_comment( 123, 'checks', 'SHA', None) actual = PRStatusInfo.get_gerrit_sha_from_comment(gerrit_comment) self.assertEqual(actual, 'SHA') def test_get_gerrit_sha_from_comment_fail(self): gerrit_comment = 'ABC' actual = PRStatusInfo.get_gerrit_sha_from_comment(gerrit_comment) self.assertIsNone(actual) def test_to_gerrit_comment(self): checks_results = {'key1': 'val1', 'key2': 'val2'} result_comment = '\nkey2 (val2)\nkey1 (val1)' pr_status_info = PRStatusInfo(checks_results, 123, 'SHA') expected = self.generate_notifier_comment(123, result_comment, 'SHA', None) actual = pr_status_info.to_gerrit_comment() self.assertEqual(expected, actual) def test_to_gerrit_comment_latest(self): checks_results = {'key1': 'val1', 'key2': 'val2'} result_comment = '\nkey2 (val2)\nkey1 (val1)' pr_status_info = PRStatusInfo(checks_results, 123, None) expected = self.generate_notifier_comment(123, result_comment, 'Latest', None) actual = pr_status_info.to_gerrit_comment() self.assertEqual(expected, actual) def test_to_gerrit_comment_with_patchset(self): checks_results = {'key1': 'val1', 'key2': 'val2'} result_comment = '\nkey2 (val2)\nkey1 (val1)' pr_status_info = PRStatusInfo(checks_results, 123, 'SHA') expected = self.generate_notifier_comment(123, result_comment, 'SHA', 3) actual = pr_status_info.to_gerrit_comment(3) self.assertEqual(expected, actual) def test_get_relevant_failed_taskcluster_checks_success(self): check_runs = [ { "id": "1", "conclusion": "failure", "name": "wpt-chrome-dev-stability" }, { "id": "2", "conclusion": "failure", "name": "wpt-firefox-nightly-stability" }, { "id": "3", "conclusion": "failure", "name": "lint" }, { "id": "4", "conclusion": "failure", "name": "infrastructure/ tests" }, ] expected = { 'wpt-chrome-dev-stability': 'https://github.com/web-platform-tests/wpt/pull/123/checks?check_run_id=1', 'wpt-firefox-nightly-stability': 'https://github.com/web-platform-tests/wpt/pull/123/checks?check_run_id=2', 'lint': 'https://github.com/web-platform-tests/wpt/pull/123/checks?check_run_id=3', 'infrastructure/ tests': 'https://github.com/web-platform-tests/wpt/pull/123/checks?check_run_id=4', } self.assertEqual( self.notifier.get_relevant_failed_taskcluster_checks( check_runs, 123), expected) def test_get_relevant_failed_taskcluster_checks_empty(self): check_runs = [ { "id": "1", "conclusion": "success", "name": "wpt-chrome-dev-stability" }, { "id": "2", "conclusion": "failure", "name": "infra" }, ] self.assertEqual( self.notifier.get_relevant_failed_taskcluster_checks( check_runs, 123), {}) def test_has_latest_taskcluster_status_commented_false(self): pr_status_info = PRStatusInfo('bar', 123, 'SHA') messages = [{ "date": "2019-08-20 17:42:05.000000000", "message": "Uploaded patch set 1.\nInitial upload", "_revision_number": 1 }] actual = self.notifier.has_latest_taskcluster_status_commented( messages, pr_status_info) self.assertFalse(actual) def test_has_latest_taskcluster_status_commented_true(self): pr_status_info = PRStatusInfo({'a': 'b'}, 123, 'SHA') messages = [ { "date": "2019-08-20 17:42:05.000000000", "message": "Uploaded patch set 1.\nInitial upload", "_revision_number": 1 }, { "date": "2019-08-21 17:41:05.000000000", "message": self.generate_notifier_comment(123, 'result', 'SHA', 3), "_revision_number": 2 }, ] actual = self.notifier.has_latest_taskcluster_status_commented( messages, pr_status_info) self.assertTrue(actual) def test_get_check_runs_success(self): self.notifier.wpt_github = MockWPTGitHub(pull_requests=[]) self.notifier.wpt_github.check_runs = [ { "id": "123", "conclusion": "failure", "name": "wpt-chrome-dev-stability" }, { "id": "456", "conclusion": "success", "name": "firefox" }, ] actual = self.notifier.get_check_runs(123) self.assertEqual(len(actual), 2) self.assertEqual(self.notifier.wpt_github.calls, [ 'get_pr_branch', 'get_branch_check_runs', ]) def test_process_failing_prs_success(self): checks_results = {'key1': 'val1', 'key2': 'val2'} result_comment = '\nkey2 (val2)\nkey1 (val1)' self.notifier.dry_run = False self.notifier.gerrit = MockGerritAPI() self.notifier.gerrit.cl = MockGerritCL(data={ 'change_id': 'abc', 'messages': [ { "date": "2019-08-20 17:42:05.000000000", "message": "Uploaded patch set 1.\nInitial upload", "_revision_number": 1 }, { "date": "2019-08-21 17:41:05.000000000", "message": self.generate_notifier_comment(123, 'notbar', 'notnum', 3), "_revision_number": 2 }, ], 'revisions': { 'SHA': { '_number': 1 } } }, api=self.notifier.gerrit) gerrit_dict = {'abc': PRStatusInfo(checks_results, 123, 'SHA')} expected = self.generate_notifier_comment(123, result_comment, 'SHA', 1) self.notifier.process_failing_prs(gerrit_dict) self.assertEqual(self.notifier.gerrit.cls_queried, ['abc']) self.assertEqual(self.notifier.gerrit.request_posted, [('/a/changes/abc/revisions/current/review', { 'message': expected })]) def test_process_failing_prs_has_commented(self): self.notifier.dry_run = False self.notifier.gerrit = MockGerritAPI() self.notifier.gerrit.cl = MockGerritCL(data={ 'change_id': 'abc', 'messages': [ { "date": "2019-08-20 17:42:05.000000000", "message": "Uploaded patch set 1.\nInitial upload", "_revision_number": 1 }, { "date": "2019-08-21 17:41:05.000000000", "message": self.generate_notifier_comment(123, 'bar', 'SHA', 3), "_revision_number": 2 }, ], 'revisions': { 'SHA': { '_number': 1 } } }, api=self.notifier.gerrit) gerrit_dict = {'abc': PRStatusInfo('bar', 123, 'SHA')} self.notifier.process_failing_prs(gerrit_dict) self.assertEqual(self.notifier.gerrit.cls_queried, ['abc']) self.assertEqual(self.notifier.gerrit.request_posted, []) def test_process_failing_prs_with_latest_sha(self): self.notifier.dry_run = False self.notifier.gerrit = MockGerritAPI() self.notifier.gerrit.cl = MockGerritCL(data={ 'change_id': 'abc', 'messages': [ { "date": "2019-08-20 17:42:05.000000000", "message": "Uploaded patch set 1.\nInitial upload", "_revision_number": 1 }, { "date": "2019-08-21 17:41:05.000000000", "message": self.generate_notifier_comment(123, 'notbar', 'notnum', 3), "_revision_number": 2 }, ], 'revisions': { 'SHA': { '_number': 1 } } }, api=self.notifier.gerrit) checks_results = {'key1': 'val1', 'key2': 'val2'} result_comment = '\nkey2 (val2)\nkey1 (val1)' expected = self.generate_notifier_comment(123, result_comment, 'Latest') gerrit_dict = {'abc': PRStatusInfo(checks_results, 123, None)} self.notifier.process_failing_prs(gerrit_dict) self.assertEqual(self.notifier.gerrit.cls_queried, ['abc']) self.assertEqual(self.notifier.gerrit.request_posted, [('/a/changes/abc/revisions/current/review', { 'message': expected })]) def test_process_failing_prs_raise_gerrit_error(self): self.notifier.dry_run = False self.notifier.gerrit = MockGerritAPI(raise_error=True) gerrit_dict = {'abc': PRStatusInfo('bar', 'SHA')} self.notifier.process_failing_prs(gerrit_dict) self.assertEqual(self.notifier.gerrit.cls_queried, ['abc']) self.assertEqual(self.notifier.gerrit.request_posted, []) self.assertLog([ 'INFO: Processing 1 CLs with failed Taskcluster checks.\n', 'INFO: Change-Id: abc\n', 'ERROR: Could not process Gerrit CL abc: Error from query_cl\n', ]) def test_export_notifier_success(self): self.notifier.wpt_github = MockWPTGitHub(pull_requests=[]) self.notifier.wpt_github.recent_failing_pull_requests = [ PullRequest( title='title1', number=1234, body= 'description\nWPT-Export-Revision: hash\nChange-Id: decafbad', state='open', labels=['']) ] self.notifier.wpt_github.check_runs = [ { "id": "123", "conclusion": "failure", "name": "wpt-chrome-dev-stability" }, { "id": "456", "conclusion": "success", "name": "firefox" }, ] checks_results_comment = ('\nwpt-chrome-dev-stability (' 'https://github.com/web-platform-tests/wpt' '/pull/1234/checks?check_run_id=123)') self.notifier.dry_run = False self.notifier.gerrit = MockGerritAPI() self.notifier.gerrit.cl = MockGerritCL(data={ 'change_id': 'decafbad', 'messages': [ { "date": "2019-08-20 17:42:05.000000000", "message": "Uploaded patch set 1.\nInitial upload", "_revision_number": 1 }, { "date": "2019-08-21 17:41:05.000000000", "message": self.generate_notifier_comment(1234, 'notbar', 'notnum', 3), "_revision_number": 2 }, ], 'revisions': { 'hash': { '_number': 2 } } }, api=self.notifier.gerrit) expected = self.generate_notifier_comment(1234, checks_results_comment, 'hash', 2) exit_code = self.notifier.main() self.assertFalse(exit_code) self.assertEqual(self.notifier.wpt_github.calls, [ 'recent_failing_chromium_exports', 'get_pr_branch', 'get_branch_check_runs', ]) self.assertEqual(self.notifier.gerrit.cls_queried, ['decafbad']) self.assertEqual(self.notifier.gerrit.request_posted, [('/a/changes/decafbad/revisions/current/review', { 'message': expected })]) def generate_notifier_comment(self, pr_number, checks_results_comment, sha, patchset=None): if patchset: comment = ( 'The exported PR, https://github.com/web-platform-tests/wpt/pull/{}, ' 'has failed the following check(s) on GitHub:\n{}' '\n\nThese failures will block the export. ' 'They may represent new or existing problems; please take ' 'a look at the output and see if it can be fixed. ' 'Unresolved failures will be looked at by the Ecosystem-Infra ' 'sheriff after this CL has been landed in Chromium; if you ' 'need earlier help please contact [email protected].\n\n' 'Any suggestions to improve this service are welcome; ' 'crbug.com/1027618.\n\n' 'Gerrit CL SHA: {}\n' 'Patchset Number: {}').format(pr_number, checks_results_comment, sha, patchset) else: comment = ( 'The exported PR, https://github.com/web-platform-tests/wpt/pull/{}, ' 'has failed the following check(s) on GitHub:\n{}' '\n\nThese failures will block the export. ' 'They may represent new or existing problems; please take ' 'a look at the output and see if it can be fixed. ' 'Unresolved failures will be looked at by the Ecosystem-Infra ' 'sheriff after this CL has been landed in Chromium; if you ' 'need earlier help please contact [email protected].\n\n' 'Any suggestions to improve this service are welcome; ' 'crbug.com/1027618.\n\n' 'Gerrit CL SHA: {}').format(pr_number, checks_results_comment, sha) return comment
class ExportNotifierTest(LoggingTestCase): def setUp(self): super(ExportNotifierTest, self).setUp() self.host = MockHost() self.git = self.host.git() self.gerrit = MockGerritAPI() self.notifier = ExportNotifier(self.host, self.git, self.gerrit) def test_get_gerrit_sha_from_comment_success(self): gerrit_comment = self.generate_notifier_comment( 123, 'bar', 'num', None) actual = PRStatusInfo.get_gerrit_sha_from_comment(gerrit_comment) self.assertEqual(actual, 'num') def test_get_gerrit_sha_from_comment_fail(self): gerrit_comment = 'ABC' actual = PRStatusInfo.get_gerrit_sha_from_comment(gerrit_comment) self.assertIsNone(actual) def test_to_gerrit_comment(self): pr_status_info = PRStatusInfo('bar', 123, 'num') expected = self.generate_notifier_comment(123, 'bar', 'num', None) actual = pr_status_info.to_gerrit_comment() self.assertEqual(expected, actual) def test_to_gerrit_comment_latest(self): pr_status_info = PRStatusInfo('bar', 123, None) expected = self.generate_notifier_comment(123, 'bar', 'Latest', None) actual = pr_status_info.to_gerrit_comment() self.assertEqual(expected, actual) def test_to_gerrit_comment_with_patchset(self): pr_status_info = PRStatusInfo('bar', 123, 'num') expected = self.generate_notifier_comment(123, 'bar', 'num', 3) actual = pr_status_info.to_gerrit_comment(3) self.assertEqual(expected, actual) def test_get_failure_taskcluster_status_success(self): taskcluster_status = [{ "state": "failure", "context": "Community-TC (pull_request)", }, { "state": "success", "context": "random", }] self.assertEqual( self.notifier.get_failure_taskcluster_status( taskcluster_status, 123), { "state": "failure", "context": "Community-TC (pull_request)", }) def test_get_failure_taskcluster_status_fail(self): taskcluster_status = [ { "state": "success", "context": "Community-TC (pull_request)", }, ] self.assertEqual( self.notifier.get_failure_taskcluster_status( taskcluster_status, 123), None) def test_get_failure_taskcluster_pending_status(self): taskcluster_status = [ { "state": "pending", "context": "Community-TC (pull_request)", }, ] self.assertEqual( self.notifier.get_failure_taskcluster_status( taskcluster_status, 123), None) def test_has_latest_taskcluster_status_commented_false(self): pr_status_info = PRStatusInfo('bar', 123, 'num') messages = [{ "date": "2019-08-20 17:42:05.000000000", "message": "Uploaded patch set 1.\nInitial upload", "_revision_number": 1 }] actual = self.notifier.has_latest_taskcluster_status_commented( messages, pr_status_info) self.assertFalse(actual) def test_has_latest_taskcluster_status_commented_true(self): pr_status_info = PRStatusInfo('bar', 123, 'num') messages = [ { "date": "2019-08-20 17:42:05.000000000", "message": "Uploaded patch set 1.\nInitial upload", "_revision_number": 1 }, { "date": "2019-08-21 17:41:05.000000000", "message": self.generate_notifier_comment(123, 'bar', 'num', 3), "_revision_number": 2 }, ] actual = self.notifier.has_latest_taskcluster_status_commented( messages, pr_status_info) self.assertTrue(actual) self.assertTrue(actual) def test_get_taskcluster_statuses_success(self): self.notifier.wpt_github = MockWPTGitHub(pull_requests=[ PullRequest(title='title1', number=1234, body='description\nWPT-Export-Revision: 1', state='open', labels=[]), ]) status = [{"description": "foo"}] self.notifier.wpt_github.status = status actual = self.notifier.get_taskcluster_statuses(123) self.assertEqual(actual, status) self.assertEqual(self.notifier.wpt_github.calls, [ 'get_pr_branch', 'get_branch_statuses', ]) def test_process_failing_prs_success(self): self.notifier.dry_run = False self.notifier.gerrit = MockGerritAPI() self.notifier.gerrit.cl = MockGerritCL(data={ 'change_id': 'abc', 'messages': [ { "date": "2019-08-20 17:42:05.000000000", "message": "Uploaded patch set 1.\nInitial upload", "_revision_number": 1 }, { "date": "2019-08-21 17:41:05.000000000", "message": self.generate_notifier_comment(123, 'notbar', 'notnum', 3), "_revision_number": 2 }, ], 'revisions': { 'num': { '_number': 1 } } }, api=self.notifier.gerrit) gerrit_dict = {'abc': PRStatusInfo('bar', 123, 'num')} expected = self.generate_notifier_comment(123, 'bar', 'num', 1) self.notifier.process_failing_prs(gerrit_dict) self.assertEqual(self.notifier.gerrit.cls_queried, ['abc']) self.assertEqual(self.notifier.gerrit.request_posted, [('/a/changes/abc/revisions/current/review', { 'message': expected })]) def test_process_failing_prs_has_commented(self): self.notifier.dry_run = False self.notifier.gerrit = MockGerritAPI() self.notifier.gerrit.cl = MockGerritCL(data={ 'change_id': 'abc', 'messages': [ { "date": "2019-08-20 17:42:05.000000000", "message": "Uploaded patch set 1.\nInitial upload", "_revision_number": 1 }, { "date": "2019-08-21 17:41:05.000000000", "message": self.generate_notifier_comment(123, 'bar', 'num', 3), "_revision_number": 2 }, ], 'revisions': { 'num': { '_number': 1 } } }, api=self.notifier.gerrit) gerrit_dict = {'abc': PRStatusInfo('bar', 123, 'num')} self.notifier.process_failing_prs(gerrit_dict) self.assertEqual(self.notifier.gerrit.cls_queried, ['abc']) self.assertEqual(self.notifier.gerrit.request_posted, []) def test_process_failing_prs_with_latest_sha(self): self.notifier.dry_run = False self.notifier.gerrit = MockGerritAPI() self.notifier.gerrit.cl = MockGerritCL(data={ 'change_id': 'abc', 'messages': [ { "date": "2019-08-20 17:42:05.000000000", "message": "Uploaded patch set 1.\nInitial upload", "_revision_number": 1 }, { "date": "2019-08-21 17:41:05.000000000", "message": self.generate_notifier_comment(123, 'notbar', 'notnum', 3), "_revision_number": 2 }, ], 'revisions': { 'num': { '_number': 1 } } }, api=self.notifier.gerrit) expected = self.generate_notifier_comment(123, 'bar', 'Latest') gerrit_dict = {'abc': PRStatusInfo('bar', 123, None)} self.notifier.process_failing_prs(gerrit_dict) self.assertEqual(self.notifier.gerrit.cls_queried, ['abc']) self.assertEqual(self.notifier.gerrit.request_posted, [('/a/changes/abc/revisions/current/review', { 'message': expected })]) def test_process_failing_prs_raise_gerrit_error(self): self.notifier.dry_run = False self.notifier.gerrit = MockGerritAPI(raise_error=True) gerrit_dict = {'abc': PRStatusInfo('bar', 'num')} self.notifier.process_failing_prs(gerrit_dict) self.assertEqual(self.notifier.gerrit.cls_queried, ['abc']) self.assertEqual(self.notifier.gerrit.request_posted, []) self.assertLog([ 'INFO: Processing 1 CLs with failed Taskcluster status.\n', 'ERROR: Could not process Gerrit CL abc: Error from query_cl\n' ]) def test_export_notifier_success(self): self.notifier.wpt_github = MockWPTGitHub(pull_requests=[]) self.notifier.wpt_github.recent_failing_pull_requests = [ PullRequest( title='title1', number=1234, body= 'description\nWPT-Export-Revision: hash\nChange-Id: decafbad', state='open', labels=['']) ] status = [{ "state": "failure", "context": "Community-TC (pull_request)", "node_id": "foo", "target_url": "bar" }] self.notifier.wpt_github.status = status self.notifier.dry_run = False self.notifier.gerrit = MockGerritAPI() self.notifier.gerrit.cl = MockGerritCL(data={ 'change_id': 'decafbad', 'messages': [ { "date": "2019-08-20 17:42:05.000000000", "message": "Uploaded patch set 1.\nInitial upload", "_revision_number": 1 }, { "date": "2019-08-21 17:41:05.000000000", "message": self.generate_notifier_comment(1234, 'notbar', 'notnum', 3), "_revision_number": 2 }, ], 'revisions': { 'hash': { '_number': 2 } } }, api=self.notifier.gerrit) expected = self.generate_notifier_comment(1234, 'bar', 'hash', 2) exit_code = self.notifier.main() self.assertFalse(exit_code) self.assertEqual(self.notifier.wpt_github.calls, [ 'recent_failing_chromium_exports', 'get_pr_branch', 'get_branch_statuses', ]) self.assertEqual(self.notifier.gerrit.cls_queried, ['decafbad']) self.assertEqual(self.notifier.gerrit.request_posted, [('/a/changes/decafbad/revisions/current/review', { 'message': expected })]) def generate_notifier_comment(self, pr_number, link, sha, patchset=None): if patchset: comment = ( 'The exported PR, https://github.com/web-platform-tests/wpt/pull/{}, ' 'has failed Taskcluster check(s) on GitHub, which could indicate ' 'cross-browser failures on the exported changes. Please contact ' '[email protected] for more information.\n\n' 'Taskcluster Link: {}\n' 'Gerrit CL SHA: {}\n' 'Patchset Number: {}' '\n\nAny suggestions to improve this service are welcome; ' 'crbug.com/1027618.').format(pr_number, link, sha, patchset) else: comment = ( 'The exported PR, https://github.com/web-platform-tests/wpt/pull/{}, ' 'has failed Taskcluster check(s) on GitHub, which could indicate ' 'cross-browser failures on the exported changes. Please contact ' '[email protected] for more information.\n\n' 'Taskcluster Link: {}\n' 'Gerrit CL SHA: {}' '\n\nAny suggestions to improve this service are welcome; ' 'crbug.com/1027618.').format(pr_number, link, sha) return comment
def main(self, argv=None): """Creates PRs for in-flight CLs and merges changes that land on master. Returns: A boolean: True if success, False if there were any patch failures. """ options = self.parse_args(argv) self.dry_run = options.dry_run log_level = logging.DEBUG if options.verbose else logging.INFO configure_logging(logging_level=log_level, include_time=True) if options.verbose: # Print out the full output when executive.run_command fails. self.host.executive.error_output_limit = None credentials = read_credentials(self.host, options.credentials_json) if not (credentials.get('GH_USER') and credentials.get('GH_TOKEN')): _log.error('You must provide your GitHub credentials for this ' 'script to work.') _log.error('See https://chromium.googlesource.com/chromium/src' '/+/master/docs/testing/web_platform_tests.md' '#GitHub-credentials for instructions on how to set ' 'your credentials up.') return False self.wpt_github = self.wpt_github or WPTGitHub( self.host, credentials['GH_USER'], credentials['GH_TOKEN']) self.gerrit = self.gerrit or GerritAPI( self.host, credentials['GERRIT_USER'], credentials['GERRIT_TOKEN']) self.local_wpt = self.local_wpt or LocalWPT(self.host, credentials['GH_TOKEN']) self.local_wpt.fetch() _log.info('Searching for exportable in-flight CLs.') # The Gerrit search API is slow and easy to fail, so we wrap it in a try # statement to continue exporting landed commits when it fails. try: open_gerrit_cls = self.gerrit.query_exportable_open_cls() except GerritError as e: _log.info( 'In-flight CLs cannot be exported due to the following error:') _log.error(str(e)) gerrit_error = True else: self.process_gerrit_cls(open_gerrit_cls) gerrit_error = False _log.info('Searching for exportable Chromium commits.') exportable_commits, git_errors = self.get_exportable_commits() self.process_chromium_commits(exportable_commits) if git_errors: _log.info( 'Attention: The following errors have prevented some commits from being ' 'exported:') for error in git_errors: _log.error(error) export_error = gerrit_error or git_errors if export_error: return not export_error _log.info('Automatic export process has finished successfully.') export_notifier_failure = False if options.surface_failures_to_gerrit: _log.info('Starting surfacing cross-browser failures to Gerrit.') export_notifier_failure = ExportNotifier(self.host, self.wpt_github, self.gerrit, self.dry_run).main() return not export_notifier_failure
class ExportNotifierTest(LoggingTestCase): def setUp(self): super(ExportNotifierTest, self).setUp() self.host = MockHost() self.git = self.host.git() self.gerrit = MockGerritAPI() self.notifier = ExportNotifier(self.host, self.git, self.gerrit) def test_from_gerrit_comment_success(self): gerrit_comment = ( 'The exported PR for the current patch failed Taskcluster check(s) ' 'on GitHub, which could indict cross-broswer failures on the ' 'exportable changes. Please contact ecosystem-infra@ team for ' 'more information.\n\n' 'Taskcluster Link: bar\n' 'Gerrit CL SHA: num') actual = PRStatusInfo.from_gerrit_comment(gerrit_comment) self.assertEqual(actual.link, 'bar') self.assertEqual(actual.gerrit_sha, 'num') def test_from_gerrit_comment_missing_info(self): gerrit_comment = ( 'The exported PR for the current patch failed Taskcluster check(s) ' 'on GitHub, which could indict cross-broswer failures on the ' 'exportable changes. Please contact ecosystem-infra@ team for ' 'more information.\n\n' 'Taskcluster Link: \n' 'Gerrit CL SHA: num') actual = PRStatusInfo.from_gerrit_comment(gerrit_comment) self.assertIsNone(actual) def test_from_gerrit_comment_fail(self): gerrit_comment = 'ABC' actual = PRStatusInfo.from_gerrit_comment(gerrit_comment) self.assertIsNone(actual) def test_to_gerrit_comment(self): pr_status_info = PRStatusInfo('bar', 'num') expected = ( 'The exported PR for the current patch failed Taskcluster check(s) ' 'on GitHub, which could indict cross-broswer failures on the ' 'exportable changes. Please contact ecosystem-infra@ team for ' 'more information.\n\n' 'Taskcluster Link: bar\n' 'Gerrit CL SHA: num') actual = pr_status_info.to_gerrit_comment() self.assertEqual(expected, actual) def test_to_gerrit_comment_latest(self): pr_status_info = PRStatusInfo('bar', None) expected = ( 'The exported PR for the current patch failed Taskcluster check(s) ' 'on GitHub, which could indict cross-broswer failures on the ' 'exportable changes. Please contact ecosystem-infra@ team for ' 'more information.\n\n' 'Taskcluster Link: bar\n' 'Gerrit CL SHA: Latest') actual = pr_status_info.to_gerrit_comment() self.assertEqual(expected, actual) def test_to_gerrit_comment_with_patchset(self): pr_status_info = PRStatusInfo('bar', 'num') expected = ( 'The exported PR for the current patch failed Taskcluster check(s) ' 'on GitHub, which could indict cross-broswer failures on the ' 'exportable changes. Please contact ecosystem-infra@ team for ' 'more information.\n\n' 'Taskcluster Link: bar\n' 'Gerrit CL SHA: num\n' 'Patchset Number: 3') actual = pr_status_info.to_gerrit_comment(3) self.assertEqual(expected, actual) def test_get_failure_taskcluster_status_success(self): taskcluster_status = [{ "state": "failure", "context": "Community-TC (pull_request)", }, { "state": "success", "context": "random", }] self.assertEqual( self.notifier.get_failure_taskcluster_status( taskcluster_status, 123), { "state": "failure", "context": "Community-TC (pull_request)", }) def test_get_failure_taskcluster_status_fail(self): taskcluster_status = [ { "state": "success", "context": "Community-TC (pull_request)", }, ] self.assertEqual( self.notifier.get_failure_taskcluster_status( taskcluster_status, 123), None) def test_has_latest_taskcluster_status_commented_false(self): pr_status_info = PRStatusInfo('bar', 'num') messages = [{ "date": "2019-08-20 17:42:05.000000000", "message": "Uploaded patch set 1.\nInitial upload", "_revision_number": 1 }] actual = self.notifier.has_latest_taskcluster_status_commented( messages, pr_status_info) self.assertFalse(actual) def test_has_latest_taskcluster_status_commented_true(self): pr_status_info = PRStatusInfo('bar', 'num') messages = [ { "date": "2019-08-20 17:42:05.000000000", "message": "Uploaded patch set 1.\nInitial upload", "_revision_number": 1 }, { "date": "2019-08-21 17:41:05.000000000", "message": ('The exported PR for the current patch failed Taskcluster check(s) ' 'on GitHub, which could indict cross-broswer failures on the ' 'exportable changes. Please contact ecosystem-infra@ team for ' 'more information.\n\n' 'Taskcluster Link: bar\n' 'Gerrit CL SHA: num\n' 'Patchset Number: 3'), "_revision_number": 2 }, ] actual = self.notifier.has_latest_taskcluster_status_commented( messages, pr_status_info) self.assertTrue(actual) self.assertTrue(actual) def test_get_taskcluster_statuses_success(self): self.notifier.wpt_github = MockWPTGitHub(pull_requests=[ PullRequest(title='title1', number=1234, body='description\nWPT-Export-Revision: 1', state='open', labels=[]), ]) status = [{"description": "foo"}] self.notifier.wpt_github.status = status actual = self.notifier.get_taskcluster_statuses(123) self.assertEqual(actual, status) self.assertEqual(self.notifier.wpt_github.calls, [ 'get_pr_branch', 'get_branch_statuses', ]) def test_process_failing_prs_success(self): self.notifier.dry_run = False self.notifier.gerrit = MockGerritAPI() self.notifier.gerrit.cl = MockGerritCL(data={ 'change_id': 'abc', 'messages': [ { "date": "2019-08-20 17:42:05.000000000", "message": "Uploaded patch set 1.\nInitial upload", "_revision_number": 1 }, { "date": "2019-08-21 17:41:05.000000000", "message": ('The exported PR for the current patch failed Taskcluster check(s) ' 'on GitHub, which could indict cross-broswer failures on the ' 'exportable changes. Please contact ecosystem-infra@ team for ' 'more information.\n\n' 'Taskcluster Link: notbar\n' 'Gerrit CL SHA: notnum\n' 'Patchset Number: 3'), "_revision_number": 2 }, ], 'revisions': { 'num': { '_number': 1 } } }, api=self.notifier.gerrit) gerrit_dict = {'abc': PRStatusInfo('bar', 'num')} expected = ( 'The exported PR for the current patch failed Taskcluster check(s) ' 'on GitHub, which could indict cross-broswer failures on the ' 'exportable changes. Please contact ecosystem-infra@ team for ' 'more information.\n\n' 'Taskcluster Link: bar\n' 'Gerrit CL SHA: num\n' 'Patchset Number: 1') self.notifier.process_failing_prs(gerrit_dict) self.assertEqual(self.notifier.gerrit.cls_queried, ['abc']) self.assertEqual(self.notifier.gerrit.request_posted, [('/a/changes/abc/revisions/current/review', { 'message': expected })]) def test_process_failing_prs_has_commented(self): self.notifier.dry_run = False self.notifier.gerrit = MockGerritAPI() self.notifier.gerrit.cl = MockGerritCL(data={ 'change_id': 'abc', 'messages': [ { "date": "2019-08-20 17:42:05.000000000", "message": "Uploaded patch set 1.\nInitial upload", "_revision_number": 1 }, { "date": "2019-08-21 17:41:05.000000000", "message": ('The exported PR for the current patch failed Taskcluster check(s) ' 'on GitHub, which could indict cross-broswer failures on the ' 'exportable changes. Please contact ecosystem-infra@ team for ' 'more information.\n\n' 'Taskcluster Link: bar\n' 'Gerrit CL SHA: num\n' 'Patchset Number: 3'), "_revision_number": 2 }, ], 'revisions': { 'num': { '_number': 1 } } }, api=self.notifier.gerrit) gerrit_dict = {'abc': PRStatusInfo('bar', 'num')} self.notifier.process_failing_prs(gerrit_dict) self.assertEqual(self.notifier.gerrit.cls_queried, ['abc']) self.assertEqual(self.notifier.gerrit.request_posted, []) def test_process_failing_prs_with_latest_sha(self): self.notifier.dry_run = False self.notifier.gerrit = MockGerritAPI() self.notifier.gerrit.cl = MockGerritCL(data={ 'change_id': 'abc', 'messages': [ { "date": "2019-08-20 17:42:05.000000000", "message": "Uploaded patch set 1.\nInitial upload", "_revision_number": 1 }, { "date": "2019-08-21 17:41:05.000000000", "message": ('The exported PR for the current patch failed Taskcluster check(s) ' 'on GitHub, which could indict cross-broswer failures on the ' 'exportable changes. Please contact ecosystem-infra@ team for ' 'more information.\n\n' 'Taskcluster Link: notbar\n' 'Gerrit CL SHA: notnum\n' 'Patchset Number: 3'), "_revision_number": 2 }, ], 'revisions': { 'num': { '_number': 1 } } }, api=self.notifier.gerrit) expected = ( 'The exported PR for the current patch failed Taskcluster check(s) ' 'on GitHub, which could indict cross-broswer failures on the ' 'exportable changes. Please contact ecosystem-infra@ team for ' 'more information.\n\n' 'Taskcluster Link: bar\n' 'Gerrit CL SHA: Latest') gerrit_dict = {'abc': PRStatusInfo('bar', None)} self.notifier.process_failing_prs(gerrit_dict) self.assertEqual(self.notifier.gerrit.cls_queried, ['abc']) self.assertEqual(self.notifier.gerrit.request_posted, [('/a/changes/abc/revisions/current/review', { 'message': expected })]) def test_process_failing_prs_raise_gerrit_error(self): self.notifier.dry_run = False self.notifier.gerrit = MockGerritAPI(raise_error=True) gerrit_dict = {'abc': PRStatusInfo('bar', 'num')} self.notifier.process_failing_prs(gerrit_dict) self.assertEqual(self.notifier.gerrit.cls_queried, ['abc']) self.assertEqual(self.notifier.gerrit.request_posted, []) self.assertLog([ 'INFO: Processing 1 CLs with failed Taskcluster status.\n', 'ERROR: Could not process Gerrit CL abc: Error from query_cl\n' ]) def test_export_notifier_success(self): self.notifier.wpt_github = MockWPTGitHub(pull_requests=[]) self.notifier.wpt_github.recent_failing_pull_requests = [ PullRequest( title='title1', number=1234, body= 'description\nWPT-Export-Revision: hash\nChange-Id: decafbad', state='open', labels=['']) ] status = [{ "state": "failure", "context": "Community-TC (pull_request)", "node_id": "foo", "target_url": "bar" }] self.notifier.wpt_github.status = status self.notifier.dry_run = False self.notifier.gerrit = MockGerritAPI() self.notifier.gerrit.cl = MockGerritCL(data={ 'change_id': 'decafbad', 'messages': [ { "date": "2019-08-20 17:42:05.000000000", "message": "Uploaded patch set 1.\nInitial upload", "_revision_number": 1 }, { "date": "2019-08-21 17:41:05.000000000", "message": ('The exported PR for the current patch failed Taskcluster check(s) ' 'on GitHub, which could indict cross-broswer failures on the ' 'exportable changes. Please contact ecosystem-infra@ team for ' 'more information.\n\n' 'Taskcluster Link: notbar\n' 'Gerrit CL SHA: notnum\n' 'Patchset Number: 3'), "_revision_number": 2 }, ], 'revisions': { 'hash': { '_number': 2 } } }, api=self.notifier.gerrit) expected = ( 'The exported PR for the current patch failed Taskcluster check(s) ' 'on GitHub, which could indict cross-broswer failures on the ' 'exportable changes. Please contact ecosystem-infra@ team for ' 'more information.\n\n' 'Taskcluster Link: bar\n' 'Gerrit CL SHA: hash\n' 'Patchset Number: 2') exit_code = self.notifier.main() self.assertFalse(exit_code) self.assertEqual(self.notifier.wpt_github.calls, [ 'recent_failing_chromium_exports', 'get_pr_branch', 'get_branch_statuses', ]) self.assertEqual(self.notifier.gerrit.cls_queried, ['decafbad']) self.assertEqual(self.notifier.gerrit.request_posted, [('/a/changes/decafbad/revisions/current/review', { 'message': expected })])