def test_filter_transform_patch_removes_baselines(self): host = Host() finder = PathFinder(host.filesystem) resources_path = finder.path_from_tools_scripts( 'webkitpy', 'w3c', 'resources') sample_patch = host.filesystem.read_text_file( host.filesystem.join(resources_path, 'sample2.patch')) expected_patch = host.filesystem.read_text_file( host.filesystem.join(resources_path, 'expected2.patch')) cl = GerritCL({'change_id': 1}, MockGerritAPI(None, None, None)) actual_patch = cl.filter_transform_patch(sample_patch) self.assertEqual(actual_patch, expected_patch)
def test_does_not_create_pr_if_cl_review_has_not_started(self): host = MockHost() test_exporter = TestExporter(host, 'gh-username', 'gh-token', gerrit_user=None, gerrit_token=None, dry_run=False) test_exporter.wpt_github = MockWPTGitHub(pull_requests=[]) test_exporter.get_exportable_commits = lambda: [] test_exporter.gerrit = MockGerritAPI(host, 'gerrit-username', 'gerrit-token') test_exporter.gerrit.get = lambda path, raw: base64.b64encode('sample diff') # pylint: disable=unused-argument test_exporter.gerrit.query_exportable_open_cls = lambda: [ GerritCL(data={ 'change_id': '1', 'subject': 'subject', '_number': '1', 'current_revision': '2', 'has_review_started': False, 'revisions': { '1': { 'commit_with_footers': 'a commit with footers 1', 'description': 'subject 1', }, '2': { 'commit_with_footers': 'a commit with footers 2', 'description': 'subject 2', }, }, 'owner': {'email': '*****@*****.**'}, }, api=test_exporter.gerrit), ] test_exporter.run() self.assertEqual(test_exporter.wpt_github.calls, []) self.assertEqual(test_exporter.wpt_github.pull_requests_created, [])
def test_gerrit_cl_no_update_if_pr_with_same_revision(self): host = MockHost() test_exporter = TestExporter(host, 'gh-username', 'gh-token', gerrit_user=None, gerrit_token=None, dry_run=False) test_exporter.wpt_github = MockWPTGitHub(pull_requests=[ PullRequest(title='title1', number=1234, body='description\nWPT-Export-Revision: 1', state='open', labels=[]), ]) test_exporter.get_exportable_commits = lambda: [] test_exporter.gerrit = MockGerritAPI(host, 'gerrit-username', 'gerrit-token') test_exporter.gerrit.query_exportable_open_cls = lambda: [ GerritCL(data={ 'change_id': '1', 'subject': 'subject', '_number': '1', 'current_revision': '1', 'has_review_started': True, 'revisions': { '1': {'commit_with_footers': 'a commit with footers'} }, 'owner': {'email': '*****@*****.**'}, }, api=test_exporter.gerrit), ] test_exporter.run() self.assertEqual(test_exporter.wpt_github.calls, [ 'pr_with_change_id', ]) self.assertEqual(test_exporter.wpt_github.pull_requests_created, [])
def test_new_gerrit_cl(self): host = MockHost() test_exporter = TestExporter(host, 'gh-username', 'gh-token', gerrit_user=None, gerrit_token=None, dry_run=False) test_exporter.wpt_github = MockWPTGitHub(pull_requests=[]) test_exporter.get_exportable_commits = lambda: [] test_exporter.gerrit = MockGerritAPI(host, 'gerrit-username', 'gerrit-token') test_exporter.gerrit.get = lambda path, raw: base64.b64encode('sample diff') # pylint: disable=unused-argument test_exporter.gerrit.query_exportable_open_cls = lambda: [ GerritCL(data={ 'change_id': '1', 'subject': 'subject', '_number': '1', 'current_revision': '1', 'has_review_started': True, 'revisions': { '1': {'commit_with_footers': 'a commit with footers'} }, 'owner': {'email': '*****@*****.**'}, }, api=test_exporter.gerrit), ] test_exporter.run() self.assertEqual(test_exporter.wpt_github.calls, [ '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-1', 'subject', 'a commit with footers\nWPT-Export-Revision: 1'), ])
def test_strip_commit_positions(self): commit_with_footers = ( 'Test commit\nChange-Id: foobar\n' 'Cr-Original-Commit-Position: refs/heads/master@{#10}\n' 'Cr-Commit-Position: refs/heads/master@{#10}') self.assertEqual(GerritCL.strip_commit_positions(commit_with_footers), 'Test commit\nChange-Id: foobar')
def test_url(self): data = { 'change_id': 'Ib58c7125d85d2fd71af711ea8bbd2dc927ed02cb', '_number': 638250, } gerrit_cl = GerritCL(data, MockGerritAPI()) self.assertEqual(gerrit_cl.url, 'https://chromium-review.googlesource.com/638250')
def test_fetch_current_revision_commit(self): host = MockHost() host.executive = mock_git_commands( { 'fetch': '', 'rev-parse': '4de71d0ce799af441c1f106c5432c7fa7256be45', 'footers': 'no-commit-position-yet' }, strict=True) data = { 'change_id': 'Ib58c7125d85d2fd71af711ea8bbd2dc927ed02cb', 'subject': 'fake subject', '_number': 638250, 'current_revision': '1', 'revisions': { '1': { 'fetch': { 'http': { 'url': 'https://chromium.googlesource.com/chromium/src', 'ref': 'refs/changes/50/638250/1' } } } }, 'owner': { 'email': '*****@*****.**' }, } gerrit_cl = GerritCL(data, MockGerritAPI()) commit = gerrit_cl.fetch_current_revision_commit(host) self.assertEqual(commit.sha, '4de71d0ce799af441c1f106c5432c7fa7256be45') self.assertEqual(host.executive.calls, [[ 'git', 'fetch', 'https://chromium.googlesource.com/chromium/src', 'refs/changes/50/638250/1' ], ['git', 'rev-parse', 'FETCH_HEAD'], [ 'git', 'footers', '--position', '4de71d0ce799af441c1f106c5432c7fa7256be45' ]])
def merge_pull_request(self, pull_request): _log.info('In-flight PR found: %s', pull_request.title) _log.info('%spull/%d', WPT_GH_URL, pull_request.number) if self.dry_run: _log.info('[dry_run] Would have attempted to merge PR') return if PROVISIONAL_PR_LABEL in pull_request.labels: _log.info('Removing provisional label "%s"...', PROVISIONAL_PR_LABEL) self.wpt_github.remove_label(pull_request.number, PROVISIONAL_PR_LABEL) _log.info('Attempting to merge...') # This is outside of the try block because if there's a problem communicating # with the GitHub API, we should hard fail. branch = self.wpt_github.get_pr_branch(pull_request.number) try: self.wpt_github.merge_pull_request(pull_request.number) # This is in the try block because if a PR can't be merged, we shouldn't # delete its branch. self.wpt_github.delete_remote_branch(branch) change_id = self.wpt_github.extract_metadata( 'Change-Id: ', pull_request.body) if change_id: cl = GerritCL(data={'change_id': change_id}, api=self.gerrit) pr_url = '{}pull/{}'.format(WPT_GH_URL, pull_request.number) cl.post_comment(( 'The WPT PR for this CL has been merged upstream! {pr_url}' ).format(pr_url=pr_url)) except MergeError: _log.info('Could not merge PR.')
def test_gerrit_cl_updates_if_cl_has_new_revision(self): test_exporter = TestExporter(self.host) test_exporter.wpt_github = MockWPTGitHub(pull_requests=[ PullRequest(title='title1', number=1234, body='description\nWPT-Export-Revision: 1', state='open', labels=[]), ]) test_exporter.get_exportable_commits = lambda: ([], []) test_exporter.gerrit = MockGerritAPI(self.host, 'gerrit-username', 'gerrit-token') test_exporter.gerrit.get = lambda path, raw: base64.b64encode( 'sample diff') # pylint: disable=unused-argument test_exporter.gerrit.query_exportable_open_cls = lambda: [ GerritCL(data={ 'change_id': '1', 'subject': 'subject', '_number': '1', 'current_revision': '2', 'has_review_started': True, 'revisions': { '1': { 'commit_with_footers': 'a commit with footers 1', 'description': 'subject 1', }, '2': { 'commit_with_footers': 'a commit with footers 2', 'description': 'subject 2', }, }, 'owner': { 'email': '*****@*****.**' }, }, api=test_exporter.gerrit), ] test_exporter.main(['--credentials-json', '/tmp/credentials.json']) self.assertEqual(test_exporter.wpt_github.calls, [ 'pr_with_change_id', 'update_pr', ]) self.assertEqual(test_exporter.wpt_github.pull_requests_created, []) self.assertEqual(test_exporter.wpt_github.pull_requests_merged, [])
def test_new_gerrit_cl(self): host = MockHost() test_exporter = TestExporter(host, 'gh-username', 'gh-token', gerrit_user=None, gerrit_token=None, dry_run=False) test_exporter.wpt_github = MockWPTGitHub(pull_requests=[]) test_exporter.get_exportable_commits = lambda limit: [] test_exporter.gerrit = MockGerritAPI(host, 'gerrit-username', 'gerrit-token') test_exporter.gerrit.query_exportable_open_cls = lambda: [ GerritCL(data={ 'change_id': '1', 'subject': 'subject', '_number': '1', 'current_revision': '1', 'revisions': { '1': { 'commit_with_footers': 'a commit with footers' } }, 'owner': { 'email': '*****@*****.**' }, }, api=test_exporter.gerrit), ] test_exporter.run() self.assertEqual(test_exporter.wpt_github.calls, [ 'pr_with_change_id', 'create_pr', 'add_label', ]) self.assertEqual(test_exporter.wpt_github.pull_requests_created, [ ('chromium-export-cl-1', 'subject', 'a commit with footers\nWPT-Export-Revision: 1'), ])
def test_gerrit_cl_no_update_if_pr_with_same_revision(self): test_exporter = TestExporter(self.host) test_exporter.wpt_github = MockWPTGitHub(pull_requests=[ PullRequest(title='title1', number=1234, body='description\nWPT-Export-Revision: 1', state='open', labels=[]), ]) test_exporter.get_exportable_commits = lambda: ([], []) test_exporter.gerrit = MockGerritAPI(self.host, 'gerrit-username', 'gerrit-token') test_exporter.gerrit.query_exportable_open_cls = lambda: [ GerritCL(data={ 'change_id': '1', 'subject': 'subject', '_number': '1', 'current_revision': '1', 'has_review_started': True, 'revisions': { '1': { 'commit_with_footers': 'a commit with footers' } }, 'owner': { 'email': '*****@*****.**' }, }, api=test_exporter.gerrit), ] success = test_exporter.main( ['--credentials-json', '/tmp/credentials.json']) self.assertTrue(success) self.assertEqual(test_exporter.wpt_github.calls, [ 'pr_with_change_id', ]) self.assertEqual(test_exporter.wpt_github.pull_requests_created, []) self.assertEqual(test_exporter.wpt_github.pull_requests_merged, [])