def test_new_gerrit_cl(self):
        test_exporter = TestExporter(self.host)
        test_exporter.wpt_github = MockWPTGitHub(pull_requests=[])
        test_exporter.get_exportable_commits = lambda: ([], [])
        test_exporter.gerrit = MockGerritAPI()
        test_exporter.gerrit.exportable_open_cls = [
            MockGerritCL(data={
                'change_id': 'I001',
                'subject': 'subject',
                '_number': 1234,
                'current_revision': '1',
                'has_review_started': True,
                'revisions': {
                    '1': {
                        'commit_with_footers': 'a commit with footers'
                    }
                },
                'owner': {
                    'email': '*****@*****.**'
                },
            },
                         api=test_exporter.gerrit,
                         chromium_commit=MockChromiumCommit(
                             self.host,
                             subject='subject',
                             body='fake body <html>',
                             change_id='I001')),
            MockGerritCL(data={
                'change_id': 'I002',
                'subject': 'subject',
                '_number': 1235,
                'current_revision': '1',
                'has_review_started': True,
                'revisions': {
                    '1': {
                        'commit_with_footers': 'a commit with footers'
                    }
                },
                'owner': {
                    'email': '*****@*****.**'
                },
            },
                         api=test_exporter.gerrit,
                         chromium_commit=MockChromiumCommit(self.host,
                                                            subject='subject',
                                                            body='body',
                                                            change_id=None)),
        ]
        test_exporter.main(['--credentials-json', '/tmp/credentials.json'])

        self.assertEqual(test_exporter.wpt_github.calls, [
            'pr_with_change_id',
            'create_pr',
            'add_label "chromium-export"',
            'add_label "do not merge yet"',
            'pr_with_change_id',
            'create_pr',
            'add_label "chromium-export"',
            'add_label "do not merge yet"',
        ])
        self.assertEqual(test_exporter.wpt_github.pull_requests_created, [
            ('chromium-export-cl-1234', 'subject',
             'fake body \\<html>\n\nChange-Id: I001\nReviewed-on: '
             'https://chromium-review.googlesource.com/1234\n'
             'WPT-Export-Revision: 1'),
            ('chromium-export-cl-1235', 'subject',
             'body\nChange-Id: I002\nReviewed-on: '
             'https://chromium-review.googlesource.com/1235\n'
             'WPT-Export-Revision: 1'),
        ])
        self.assertEqual(test_exporter.wpt_github.pull_requests_merged, [])
    def test_creates_and_merges_pull_requests(self):
        # This tests 4 exportable commits:
        # 1. #458475 has a provisional in-flight PR associated with it. The PR needs to be updated but not merged.
        # 2. #458476 has no PR associated with it and should have one created.
        # 3. #458477 has a closed PR associated with it and should be skipped.
        # 4. #458478 has an in-flight PR associated with it and should be merged successfully.
        # 5. #458479 has an in-flight PR associated with it but can not be merged.
        test_exporter = TestExporter(self.host)
        test_exporter.wpt_github = MockWPTGitHub(
            pull_requests=[
                PullRequest(
                    title='Open PR',
                    number=1234,
                    body=
                    'rutabaga\nCr-Commit-Position: refs/heads/main@{#458475}\nChange-Id: I0005',
                    state='open',
                    labels=['do not merge yet']),
                PullRequest(
                    title='Merged PR',
                    number=2345,
                    body=
                    'rutabaga\nCr-Commit-Position: refs/heads/main@{#458477}\nChange-Id: Idead',
                    state='closed',
                    labels=[]),
                PullRequest(
                    title='Open PR',
                    number=3456,
                    body=
                    'rutabaga\nCr-Commit-Position: refs/heads/main@{#458478}\nChange-Id: I0118',
                    state='open',
                    labels=[]  # It's important that this is empty.
                ),
                PullRequest(
                    title='Open PR',
                    number=4747,
                    body=
                    'rutabaga\nCr-Commit-Position: refs/heads/main@{#458479}\nChange-Id: I0147',
                    state='open',
                    labels=[]  # It's important that this is empty.
                ),
            ],
            unsuccessful_merge_index=3)  # Mark the last PR as unmergable.
        test_exporter.gerrit = MockGerritAPI()
        test_exporter.get_exportable_commits = lambda: ([
            MockChromiumCommit(self.host,
                               position='refs/heads/main@{#458475}',
                               change_id='I0005'),
            MockChromiumCommit(self.host,
                               position='refs/heads/main@{#458476}',
                               change_id='I0476'),
            MockChromiumCommit(self.host,
                               position='refs/heads/main@{#458477}',
                               change_id='Idead'),
            MockChromiumCommit(self.host,
                               position='refs/heads/main@{#458478}',
                               change_id='I0118'),
            MockChromiumCommit(self.host,
                               position='refs/heads/main@{#458479}',
                               change_id='I0147'),
        ], [])
        success = test_exporter.main(
            ['--credentials-json', '/tmp/credentials.json'])

        self.assertTrue(success)
        self.assertEqual(
            test_exporter.wpt_github.calls,
            [
                # 1. #458475
                'pr_for_chromium_commit',
                'get_pr_branch',
                'update_pr',
                'remove_label "do not merge yet"',
                # 2. #458476
                'pr_for_chromium_commit',
                'create_pr',
                'add_label "chromium-export"',
                # 3. #458477
                'pr_for_chromium_commit',
                # 4. #458478
                'pr_for_chromium_commit',
                # Testing the lack of remove_label here. The exporter should not
                # try to remove the provisional label from PRs it has already
                # removed it from.
                'get_pr_branch',
                'merge_pr',
                # 5. #458479
                'pr_for_chromium_commit',
                'get_pr_branch',
                'merge_pr',
            ])
        self.assertEqual(test_exporter.wpt_github.pull_requests_created, [
            ('chromium-export-981776f989', 'Fake subject',
             'Fake body\n\nChange-Id: I0476\n'),
        ])
        self.assertEqual(test_exporter.wpt_github.pull_requests_merged, [3456])
Beispiel #3
0
 def test_commit_that_starts_with_import_is_not_exportable(self):
     commit = MockChromiumCommit(MockHost(), subject='Import message')
     github = MockWPTGitHub(pull_requests=[])
     self.assertEqual(
         get_commit_export_state(commit, MockLocalWPT(), github),
         (CommitExportState.IGNORED, ''))
    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 = {
            'wpt-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, {}, 'notnum', 3),
                    "_revision_number":
                    2
                },
            ],
            'revisions': {
                'hash': {
                    '_number': 2
                }
            }
        }, api=self.notifier.gerrit)
        expected = self.generate_notifier_comment(1234, checks_results, '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 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,
            ['I4fd5039cd4ec991bb8f840eabe55574b37243ef2', 'decafbad'])
        self.assertEqual(self.notifier.gerrit.request_posted,
                         [('/a/changes/decafbad/revisions/current/review', {
                             'message': expected
                         })])
    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 Node ID: notfoo\n'
                     'Taskcluster Link: bar\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 Node ID: foo\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
                         })])