Ejemplo n.º 1
0
    def test_last_wpt_exported_commit_not_found(self):
        host = MockHost()
        host.executive = MockExecutive2(run_command_fn=lambda _: None)
        host.filesystem = MockFileSystem()
        local_wpt = LocalWPT(host)

        commit = local_wpt.most_recent_chromium_commit()
        self.assertEqual(commit, (None, None))
Ejemplo n.º 2
0
    def test_last_wpt_exported_commit_not_found(self):
        host = MockHost()
        host.executive = MockExecutive(run_command_fn=lambda _: '')
        host.filesystem = MockFileSystem()
        local_wpt = LocalWPT(host, 'token')

        commit = local_wpt.most_recent_chromium_commit()
        self.assertEqual(commit, (None, None))
Ejemplo n.º 3
0
def main():
    configure_logging()
    parser = argparse.ArgumentParser(description='WPT Sync')
    parser.add_argument('--no-fetch', action='store_true')
    options = parser.parse_args()

    host = Host()

    # TODO(jeffcarp): the script does not handle reverted changes right now

    local_wpt = LocalWPT(host, no_fetch=options.no_fetch, use_github=True)
    chromium_wpt = ChromiumWPT(host)
    wpt_commit, chromium_commit = local_wpt.most_recent_chromium_commit()

    if chromium_commit:
        _log.info('Found last exported WPT commit:')
        _log.info('- web-platform-tests@%s', wpt_commit)
        _log.info('- chromium@%s', chromium_commit)
    else:
        _log.info('No Chromium export commits found in WPT, stopping.')
        return

    _log.info('Finding exportable commits in Chromium since %s...', chromium_commit)
    exportable_commits = chromium_wpt.exportable_commits_since(chromium_commit)

    if exportable_commits:
        _log.info('Found %s exportable commits in chromium:', len(exportable_commits))
        for commit in exportable_commits:
            _log.info('- %s %s', commit, chromium_wpt.subject(commit))
    else:
        _log.info('No exportable commits found in Chromium, stopping.')
        return

    for commit in exportable_commits:
        _log.info('Uploading %s', chromium_wpt.subject(commit))
        patch = chromium_wpt.format_patch(commit)
        message = chromium_wpt.message(commit)
        try:
            commit_position = chromium_wpt.commit_position(commit)
        except ScriptError as exp:
            _log.error(exp)
            _log.error('This could mean you have local commits on your chromium branch '
                       '(That lack a Cr-Commit-Position footer).')
            # TODO(jeffcarp): include flag that lets you exclude local commits
            raise

        assert commit_position
        message += '\n\nCr-Commit-Position: {}'.format(commit_position)
        branch_name = 'chromium-try-{}'.format(commit)
        local_wpt.create_branch_with_patch(branch_name, message, patch)

        desc_title = chromium_wpt.subject(commit)
        user = os.environ.get('GH_USER')
        assert user
        pr_branch_name = '{}:{}'.format(user, branch_name)
        github_create_pr(pr_branch_name, desc_title)
Ejemplo n.º 4
0
    def test_last_wpt_exported_commit(self):
        host = MockHost()
        return_vals = [
            '123',
            '9ea4fc353a4b1c11c6e524270b11baa4d1ddfde8',
        ]
        host.executive = MockExecutive2(run_command_fn=lambda _: return_vals.pop())
        host.filesystem = MockFileSystem()
        local_wpt = LocalWPT(host, no_fetch=True)

        commit = local_wpt.most_recent_chromium_commit()
        self.assertEqual(commit, ('9ea4fc353a4b1c11c6e524270b11baa4d1ddfde8', '123'))
Ejemplo n.º 5
0
    def exportable_but_not_exported_commits(self, wpt_path):
        """Checks for commits that might be overwritten by importing.

        Args:
            wpt_path: The path to a local checkout of web-platform-tests.

        Returns:
            A list of commits in the Chromium repo that are exportable
            but not yet exported to the web-platform-tests repo.
        """
        local_wpt = LocalWPT(self.host, path=wpt_path)
        assert self.host.filesystem.exists(wpt_path)
        _, chromium_commit = local_wpt.most_recent_chromium_commit()
        return exportable_commits_since(chromium_commit.sha, self.host, local_wpt)
Ejemplo n.º 6
0
    def test_last_wpt_exported_commit(self):
        host = MockHost()
        host.executive = mock_git_commands({
            'rev-list': '9ea4fc353a4b1c11c6e524270b11baa4d1ddfde8',
            'footers': 'Cr-Commit-Position: 123',
            'crrev-parse': 'add087a97844f4b9e307d9a216940582d96db306',
        }, strict=True)
        host.filesystem = MockFileSystem()
        local_wpt = LocalWPT(host, 'token')

        wpt_sha, chromium_commit = local_wpt.most_recent_chromium_commit()
        self.assertEqual(wpt_sha, '9ea4fc353a4b1c11c6e524270b11baa4d1ddfde8')
        self.assertEqual(chromium_commit.position, '123')
        self.assertEqual(chromium_commit.sha, 'add087a97844f4b9e307d9a216940582d96db306')
Ejemplo n.º 7
0
    def test_last_wpt_exported_commit(self):
        host = MockHost()
        return_vals = [
            'deadbeefcafe',
            '123',
            '9ea4fc353a4b1c11c6e524270b11baa4d1ddfde8',
        ]
        host.executive = MockExecutive(run_command_fn=lambda _: return_vals.pop())
        host.filesystem = MockFileSystem()
        local_wpt = LocalWPT(host)

        wpt_sha, chromium_commit = local_wpt.most_recent_chromium_commit()
        self.assertEqual(wpt_sha, '9ea4fc353a4b1c11c6e524270b11baa4d1ddfde8')
        self.assertEqual(chromium_commit.position, '123')
        self.assertEqual(chromium_commit.sha, 'deadbeefcafe')
Ejemplo n.º 8
0
def main():
    configure_logging()
    options = parse_args()
    host = Host()
    github = GitHub(host)

    local_wpt = LocalWPT(host, no_fetch=options.no_fetch, use_github=True)
    chromium_wpt = ChromiumWPT(host)

    wpt_commit, chromium_commit = local_wpt.most_recent_chromium_commit()
    assert chromium_commit, 'No Chromium commit found, this is impossible'

    _log.info('web-platform-tests@%s (%d behind origin/master)', wpt_commit,
              local_wpt.commits_behind_master(wpt_commit))
    _log.info('chromium@%s (%d behind origin/master)', chromium_commit.sha,
              chromium_commit.num_behind_master())

    exportable_commits = chromium_wpt.exportable_commits_since(
        chromium_commit.sha)

    if exportable_commits:
        _log.info('Found %s exportable commits in chromium:',
                  len(exportable_commits))
        for commit in exportable_commits:
            _log.info('- %s %s', commit, chromium_wpt.subject(commit))
    else:
        _log.info('No exportable commits found in Chromium, stopping.')
        return

    for commit in exportable_commits:
        _log.info('Uploading %s', chromium_wpt.subject(commit))
        chromium_commit = ChromiumCommit(host, sha=commit)

        patch = chromium_wpt.format_patch(commit)
        message = chromium_wpt.message(commit)

        local_wpt.create_branch_with_patch(branch_name, message, patch)

        github.create_pr(local_branch_name='chromium-try-{}'.format(commit),
                         desc_title=chromium_commit.subject(),
                         body=chromium_commit.body())
Ejemplo n.º 9
0
class TestExporter(object):

    def __init__(self, host, gh_user, gh_token, dry_run=False):
        self.host = host
        self.wpt_github = WPTGitHub(host, gh_user, gh_token)
        self.dry_run = dry_run
        self.local_wpt = LocalWPT(self.host, gh_token)
        self.local_wpt.fetch()

    def run(self):
        """Query in-flight pull requests, then merges PR or creates one.

        This script assumes it will be run on a regular interval. On
        each invocation, it will either attempt to merge or attempt to
        create a PR, never both.
        """
        pull_requests = self.wpt_github.in_flight_pull_requests()
        self.merge_all_pull_requests(pull_requests)

        # TODO(jeffcarp): The below line will enforce draining all open PRs before
        # adding any more to the queue, which mirrors current behavior. After this
        # change lands, modify the following to:
        # - for each exportable commit
        #   - check if there's a corresponding PR
        #   - if not, create one
        if not pull_requests:
            _log.info('No in-flight PRs found, looking for exportable commits.')
            self.export_first_exportable_commit()

    def merge_all_pull_requests(self, pull_requests):
        for pr in pull_requests:
            self.merge_pull_request(pr)

    def merge_pull_request(self, pull_request):
        _log.info('In-flight PR found: %s', pull_request.title)
        _log.info('https://github.com/w3c/web-platform-tests/pull/%d', pull_request.number)
        _log.info('Attempting to merge...')

        if self.dry_run:
            _log.info('[dry_run] Would have attempted to merge PR')
            return

        branch = self.wpt_github.get_pr_branch(pull_request.number)
        try:
            self.wpt_github.merge_pull_request(pull_request.number)
            self.wpt_github.delete_remote_branch(branch)
        except MergeError:
            _log.info('Could not merge PR.')

    def export_first_exportable_commit(self):
        """Looks for exportable commits in Chromium, creates PR if found."""

        wpt_commit, chromium_commit = self.local_wpt.most_recent_chromium_commit()
        assert chromium_commit, 'No Chromium commit found, this is impossible'

        wpt_behind_master = self.local_wpt.commits_behind_master(wpt_commit)

        _log.info('\nLast Chromium export commit in web-platform-tests:')
        _log.info('web-platform-tests@%s', wpt_commit)
        _log.info('(%d behind web-platform-tests@origin/master)', wpt_behind_master)

        _log.info('\nThe above WPT commit points to the following Chromium commit:')
        _log.info('chromium@%s', chromium_commit.sha)
        _log.info('(%d behind chromium@origin/master)', chromium_commit.num_behind_master())

        exportable_commits = exportable_commits_since(chromium_commit.sha, self.host, self.local_wpt)

        if not exportable_commits:
            _log.info('No exportable commits found in Chromium, stopping.')
            return

        _log.info('Found %d exportable commits in Chromium:', len(exportable_commits))
        for commit in exportable_commits:
            _log.info('- %s %s', commit, commit.subject())

        outbound_commit = exportable_commits[0]
        _log.info('Picking the earliest commit and creating a PR')
        _log.info('- %s %s', outbound_commit.sha, outbound_commit.subject())

        patch = outbound_commit.format_patch()
        message = outbound_commit.message()
        author = outbound_commit.author()

        if self.dry_run:
            _log.info('[dry_run] Stopping before creating PR')
            _log.info('\n\n[dry_run] message:')
            _log.info(message)
            _log.info('\n\n[dry_run] patch:')
            _log.info(patch)
            return

        remote_branch_name = self.local_wpt.create_branch_with_patch(message, patch, author)

        response_data = self.wpt_github.create_pr(
            remote_branch_name=remote_branch_name,
            desc_title=outbound_commit.subject(),
            body=outbound_commit.body())

        _log.info('Create PR response: %s', response_data)

        if response_data:
            data, status_code = self.wpt_github.add_label(response_data['number'])
            _log.info('Add label response (status %s): %s', status_code, data)
Ejemplo n.º 10
0
class TestExporter(object):
    def __init__(self, host, wpt_github, dry_run=False):
        self.host = host
        self.wpt_github = wpt_github
        self.dry_run = dry_run
        self.local_wpt = LocalWPT(self.host)
        self.local_wpt.fetch()

    def run(self):
        """Query in-flight pull requests, then merge PR or create one.

        This script assumes it will be run on a regular interval. On
        each invocation, it will either attempt to merge or attempt to
        create a PR, never both.
        """
        pull_requests = self.wpt_github.in_flight_pull_requests()

        if len(pull_requests) == 1:
            self.merge_in_flight_pull_request(pull_requests.pop())
        elif len(pull_requests) > 1:
            _log.error(pull_requests)
            # TODO(jeffcarp): Print links to PRs
            raise Exception('More than two in-flight PRs!')
        else:
            self.export_first_exportable_commit()

    def merge_in_flight_pull_request(self, pull_request):
        """Attempt to merge an in-flight PR.

        Args:
            pull_request: a PR object returned from the GitHub API.
        """

        _log.info('In-flight PR found: #%d', pull_request['number'])
        _log.info(pull_request['title'])

        # TODO(jeffcarp): Check the PR status here (for Travis CI, etc.)

        if self.dry_run:
            _log.info('[dry_run] Would have attempted to merge PR')
            return

        _log.info('Merging...')
        self.wpt_github.merge_pull_request(pull_request['number'])
        _log.info('PR merged! Deleting branch.')
        self.wpt_github.delete_remote_branch('chromium-export-try')
        _log.info('Branch deleted!')

    def export_first_exportable_commit(self):
        """Looks for exportable commits in Chromium, creates PR if found."""

        wpt_commit, chromium_commit = self.local_wpt.most_recent_chromium_commit(
        )
        assert chromium_commit, 'No Chromium commit found, this is impossible'

        wpt_behind_master = self.local_wpt.commits_behind_master(wpt_commit)

        _log.info('\nLast Chromium export commit in web-platform-tests:')
        _log.info('web-platform-tests@%s', wpt_commit)
        _log.info('(%d behind web-platform-tests@origin/master)',
                  wpt_behind_master)

        _log.info(
            '\nThe above WPT commit points to the following Chromium commit:')
        _log.info('chromium@%s', chromium_commit.sha)
        _log.info('(%d behind chromium@origin/master)',
                  chromium_commit.num_behind_master())

        exportable_commits = exportable_commits_since(chromium_commit.sha,
                                                      self.host,
                                                      self.local_wpt)

        if not exportable_commits:
            _log.info('No exportable commits found in Chromium, stopping.')
            return

        _log.info('Found %d exportable commits in Chromium:',
                  len(exportable_commits))
        for commit in exportable_commits:
            _log.info('- %s %s', commit, commit.subject())

        outbound_commit = exportable_commits[0]
        _log.info('Picking the earliest commit and creating a PR')
        _log.info('- %s %s', outbound_commit.sha, outbound_commit.subject())

        patch = outbound_commit.format_patch()
        message = outbound_commit.message()
        author = outbound_commit.author()

        if self.dry_run:
            _log.info('[dry_run] Stopping before creating PR')
            _log.info('\n\n[dry_run] message:')
            _log.info(message)
            _log.info('\n\n[dry_run] patch:')
            _log.info(patch)
            return

        remote_branch_name = self.local_wpt.create_branch_with_patch(
            message, patch, author)

        response_data = self.wpt_github.create_pr(
            remote_branch_name=remote_branch_name,
            desc_title=outbound_commit.subject(),
            body=outbound_commit.body())

        _log.info('Create PR response: %s', response_data)

        if response_data:
            data, status_code = self.wpt_github.add_label(
                response_data['number'])
            _log.info('Add label response (status %s): %s', status_code, data)
Ejemplo n.º 11
0
class TestExporter(object):
    def __init__(self,
                 host,
                 gh_user,
                 gh_token,
                 gerrit_user,
                 gerrit_token,
                 dry_run=False):
        self.host = host
        self.wpt_github = WPTGitHub(host, gh_user, gh_token)

        self.gerrit = GerritAPI(self.host, gerrit_user, gerrit_token)

        self.dry_run = dry_run
        self.local_wpt = LocalWPT(self.host, gh_token)
        self.local_wpt.fetch()

    def run(self):
        """For last n commits on Chromium master, create or try to merge a PR.

        The exporter will look in chronological order at every commit in Chromium.
        """
        open_gerrit_cls = self.gerrit.query_exportable_open_cls()
        self.process_gerrit_cls(open_gerrit_cls)

        exportable_commits = self.get_exportable_commits()
        for exportable_commit in exportable_commits:
            pull_request = self.wpt_github.pr_for_chromium_commit(
                exportable_commit)

            if pull_request:
                if pull_request.state == 'open':
                    self.merge_pull_request(pull_request)
                else:
                    _log.info('Pull request is not open: #%d %s',
                              pull_request.number, pull_request.title)
            else:
                self.create_pull_request(exportable_commit)

    def process_gerrit_cls(self, gerrit_cls):
        """Creates or updates PRs for Gerrit CLs."""
        for cl in gerrit_cls:
            _log.info('Found Gerrit in-flight CL: "%s" %s', cl.subject, cl.url)

            if not cl.has_review_started:
                _log.info('CL review has not started, skipping.')
                continue

            # Check if CL already has a corresponding PR
            pull_request = self.wpt_github.pr_with_change_id(cl.change_id)

            if pull_request:
                pr_url = '{}pull/{}'.format(WPT_GH_URL, pull_request.number)
                _log.info('In-flight PR found: %s', pr_url)

                pr_cl_revision = self.wpt_github.extract_metadata(
                    WPT_REVISION_FOOTER + ' ', pull_request.body)
                if cl.current_revision_sha == pr_cl_revision:
                    _log.info(
                        'PR revision matches CL revision. Nothing to do here.')
                    continue

                _log.info('New revision found, updating PR...')
                self.create_or_update_pull_request_from_cl(cl, pull_request)
            else:
                _log.info('No in-flight PR found for CL. Creating...')
                self.create_or_update_pull_request_from_cl(cl)

    def get_exportable_commits(self):
        return exportable_commits_over_last_n_commits(self.host,
                                                      self.local_wpt,
                                                      self.wpt_github)

    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 export_first_exportable_commit(self):
        """Looks for exportable commits in Chromium, creates PR if found."""

        wpt_commit, chromium_commit = self.local_wpt.most_recent_chromium_commit(
        )
        assert chromium_commit, 'No Chromium commit found, this is impossible'

        wpt_behind_master = self.local_wpt.commits_behind_master(wpt_commit)

        _log.info('\nLast Chromium export commit in web-platform-tests:')
        _log.info('web-platform-tests@%s', wpt_commit)
        _log.info('(%d behind web-platform-tests@origin/master)',
                  wpt_behind_master)

        _log.info(
            '\nThe above WPT commit points to the following Chromium commit:')
        _log.info('chromium@%s', chromium_commit.sha)
        _log.info('(%d behind chromium@origin/master)',
                  chromium_commit.num_behind_master())

        exportable_commits = exportable_commits_over_last_n_commits(
            chromium_commit.sha, self.host, self.local_wpt, self.wpt_github)

        if not exportable_commits:
            _log.info('No exportable commits found in Chromium, stopping.')
            return

        _log.info('Found %d exportable commits in Chromium:',
                  len(exportable_commits))
        for commit in exportable_commits:
            _log.info('- %s %s', commit, commit.subject())

        outbound_commit = exportable_commits[0]
        _log.info('Picking the earliest commit and creating a PR')
        _log.info('- %s %s', outbound_commit.sha, outbound_commit.subject())

        self.create_pull_request(outbound_commit)

    def create_pull_request(self, outbound_commit):
        patch = outbound_commit.format_patch()
        message = outbound_commit.message()
        author = outbound_commit.author()

        if self.dry_run:
            _log.info('[dry_run] Stopping before creating PR')
            _log.info('\n\n[dry_run] message:')
            _log.info(message)
            _log.info('\n\n[dry_run] patch:')
            _log.info(patch)
            return

        branch_name = 'chromium-export-{sha}'.format(
            sha=outbound_commit.short_sha)
        self.local_wpt.create_branch_with_patch(branch_name, message, patch,
                                                author)

        response_data = self.wpt_github.create_pr(
            remote_branch_name=branch_name,
            desc_title=outbound_commit.subject(),
            body=outbound_commit.body())

        _log.info('Create PR response: %s', response_data)

        if response_data:
            data, status_code = self.wpt_github.add_label(
                response_data['number'], EXPORT_PR_LABEL)
            _log.info('Add label response (status %s): %s', status_code, data)

        return response_data

    def create_or_update_pull_request_from_cl(self, cl, pull_request=None):
        patch = cl.get_patch()
        updating = bool(pull_request)
        action_str = 'updating' if updating else 'creating'

        if self.local_wpt.test_patch(patch) == '':
            _log.error('Gerrit CL patch did not apply cleanly.')
            _log.error('First 500 characters of patch: %s', patch[0:500])
            return

        if self.dry_run:
            _log.info('[dry_run] Stopping before %s PR from CL', action_str)
            _log.info('\n\n[dry_run] subject:')
            _log.info(cl.subject)
            _log.debug('\n\n[dry_run] patch[0:500]:')
            _log.debug(patch[0:500])
            return

        message = cl.latest_commit_message_with_footers()

        # Annotate revision footer for Exporter's later use.
        message = '\n'.join([
            line for line in message.split('\n')
            if WPT_REVISION_FOOTER not in line
        ])
        message += '\n{} {}'.format(WPT_REVISION_FOOTER,
                                    cl.current_revision_sha)

        branch_name = 'chromium-export-cl-{id}'.format(id=cl.change_id)
        self.local_wpt.create_branch_with_patch(branch_name,
                                                message,
                                                patch,
                                                cl.owner_email,
                                                force_push=True)

        if updating:
            response_data = self.wpt_github.update_pr(pull_request.number,
                                                      cl.subject, message)
            _log.debug('Update PR response: %s', response_data)

            # TODO(jeffcarp): Turn PullRequest into a class with a .url method

            cl.post_comment(
                ('Successfully updated WPT GitHub pull request with '
                 'new revision "{subject}": {pr_url}').format(
                     subject=cl.current_revision_description,
                     pr_url='%spull/%d' % (WPT_GH_URL, pull_request.number),
                 ))
        else:
            response_data = self.wpt_github.create_pr(branch_name, cl.subject,
                                                      message)
            _log.debug('Create PR response: %s', response_data)

            self.wpt_github.add_label(response_data['number'], EXPORT_PR_LABEL)
            self.wpt_github.add_label(response_data['number'],
                                      PROVISIONAL_PR_LABEL)

            cl.post_comment((
                'Exportable changes to web-platform-tests were detected in this CL '
                'and a pull request in the upstream repo has been made: {pr_url}.\n\n'
                'If this CL lands and Travis CI upstream is green, we will auto-merge the PR.\n\n'
                'Note: Please check the Travis CI status (at the bottom of the PR) '
                'before landing this CL and only land this CL if the status is green. '
                'Otherwise a human needs to step in and resolve it manually, during which time '
                'the WPT Importer is blocked from operating.\n\n'
                '(There is ongoing work to 1. prevent CLs with red upstream PRs from landing '
                '(https://crbug.com/711447) and 2. prevent the importer from being blocked on '
                'stuck exportable changes (https://crbug.com/734121))\n\n'
                'WPT Export docs:\n'
                'https://chromium.googlesource.com/chromium/src/+/master'
                '/docs/testing/web_platform_tests.md#Automatic-export-process'
            ).format(pr_url='%spull/%d' %
                     (WPT_GH_URL, response_data['number'])))

        return response_data
Ejemplo n.º 12
0
class TestExporter(object):

    def __init__(self, host, gh_user, gh_token, gerrit_user, gerrit_token, dry_run=False):
        self.host = host
        self.wpt_github = WPTGitHub(host, gh_user, gh_token, pr_history_window=PR_HISTORY_WINDOW)

        self.gerrit = GerritAPI(self.host, gerrit_user, gerrit_token)

        self.dry_run = dry_run
        self.local_wpt = LocalWPT(self.host, gh_token)
        self.local_wpt.fetch()

    def run(self):
        """For last n commits on Chromium master, create or try to merge a PR.

        The exporter will look in chronological order at every commit in Chromium.
        """
        open_gerrit_cls = self.gerrit.query_exportable_open_cls()
        self.process_gerrit_cls(open_gerrit_cls)

        exportable_commits = self.get_exportable_commits(limit=COMMIT_HISTORY_WINDOW)
        for exportable_commit in exportable_commits:
            pull_request = self.corresponding_pull_request_for_commit(exportable_commit)

            if pull_request:
                if pull_request.state == 'open':
                    self.merge_pull_request(pull_request)
                else:
                    _log.info('Pull request is not open: #%d %s', pull_request.number, pull_request.title)
            else:
                self.create_pull_request(exportable_commit)

    def process_gerrit_cls(self, gerrit_cls):
        """Creates or updates PRs for Gerrit CLs."""
        for cl in gerrit_cls:
            _log.info('Found Gerrit in-flight CL: "%s" %s', cl.subject, cl.url)

            # Check if CL already has a corresponding PR
            pull_request = self.wpt_github.pr_with_change_id(cl.change_id)

            if pull_request:
                pr_url = '{}pull/{}'.format(WPT_GH_URL, pull_request.number)
                _log.info('In-flight PR found: %s', pr_url)

                pr_cl_revision = self.wpt_github.extract_metadata(WPT_REVISION_FOOTER + ' ', pull_request.body)
                if cl.current_revision_sha == pr_cl_revision:
                    _log.info('PR revision matches CL revision. Nothing to do here.')
                    continue

                _log.info('New revision found, updating PR...')
                self.create_or_update_pull_request_from_cl(cl, pull_request)
            else:
                _log.info('No in-flight PR found for CL. Creating...')
                self.create_or_update_pull_request_from_cl(cl)

    def get_exportable_commits(self, limit):
        return exportable_commits_over_last_n_commits(limit, self.host, self.local_wpt)

    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

        _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 export_first_exportable_commit(self):
        """Looks for exportable commits in Chromium, creates PR if found."""

        wpt_commit, chromium_commit = self.local_wpt.most_recent_chromium_commit()
        assert chromium_commit, 'No Chromium commit found, this is impossible'

        wpt_behind_master = self.local_wpt.commits_behind_master(wpt_commit)

        _log.info('\nLast Chromium export commit in web-platform-tests:')
        _log.info('web-platform-tests@%s', wpt_commit)
        _log.info('(%d behind web-platform-tests@origin/master)', wpt_behind_master)

        _log.info('\nThe above WPT commit points to the following Chromium commit:')
        _log.info('chromium@%s', chromium_commit.sha)
        _log.info('(%d behind chromium@origin/master)', chromium_commit.num_behind_master())

        exportable_commits = exportable_commits_since(chromium_commit.sha, self.host, self.local_wpt)

        if not exportable_commits:
            _log.info('No exportable commits found in Chromium, stopping.')
            return

        _log.info('Found %d exportable commits in Chromium:', len(exportable_commits))
        for commit in exportable_commits:
            _log.info('- %s %s', commit, commit.subject())

        outbound_commit = exportable_commits[0]
        _log.info('Picking the earliest commit and creating a PR')
        _log.info('- %s %s', outbound_commit.sha, outbound_commit.subject())

        self.create_pull_request(outbound_commit)

    def create_pull_request(self, outbound_commit):
        patch = outbound_commit.format_patch()
        message = outbound_commit.message()
        author = outbound_commit.author()

        if self.dry_run:
            _log.info('[dry_run] Stopping before creating PR')
            _log.info('\n\n[dry_run] message:')
            _log.info(message)
            _log.info('\n\n[dry_run] patch:')
            _log.info(patch)
            return

        branch_name = 'chromium-export-{sha}'.format(sha=outbound_commit.short_sha)
        self.local_wpt.create_branch_with_patch(branch_name, message, patch, author)

        response_data = self.wpt_github.create_pr(
            remote_branch_name=branch_name,
            desc_title=outbound_commit.subject(),
            body=outbound_commit.body())

        _log.info('Create PR response: %s', response_data)

        if response_data:
            data, status_code = self.wpt_github.add_label(response_data['number'])
            _log.info('Add label response (status %s): %s', status_code, data)

        return response_data

    def create_or_update_pull_request_from_cl(self, cl, pull_request=None):
        patch = cl.get_patch()
        updating = bool(pull_request)
        action_str = 'updating' if updating else 'creating'

        if self.local_wpt.test_patch(patch) == '':
            _log.error('Gerrit CL patch did not apply cleanly.')
            _log.error('First 500 characters of patch: %s', patch[0:500])
            return

        if self.dry_run:
            _log.info('[dry_run] Stopping before %s PR from CL', action_str)
            _log.info('\n\n[dry_run] subject:')
            _log.info(cl.subject)
            _log.debug('\n\n[dry_run] patch[0:500]:')
            _log.debug(patch[0:500])
            return

        message = cl.latest_commit_message_with_footers()

        # Annotate revision footer for Exporter's later use.
        message = '\n'.join([line for line in message.split('\n') if WPT_REVISION_FOOTER not in line])
        message += '\n{} {}'.format(WPT_REVISION_FOOTER, cl.current_revision_sha)

        branch_name = 'chromium-export-cl-{id}'.format(id=cl.change_id)
        self.local_wpt.create_branch_with_patch(branch_name, message, patch, cl.owner_email, force_push=True)

        if updating:
            response_data = self.wpt_github.update_pr(pull_request.number, cl.subject, message)
            _log.debug('Update PR response: %s', response_data)

            # TODO(jeffcarp): Turn PullRequest into a class with a .url method

            cl.post_comment((
                'Successfully updated WPT GitHub pull request with '
                'new revision "{subject}": {pr_url}'
            ).format(
                subject=cl.current_revision_description,
                pr_url='%spull/%d' % (WPT_GH_URL, pull_request.number),
            ))
        else:
            response_data = self.wpt_github.create_pr(branch_name, cl.subject, message)
            _log.debug('Create PR response: %s', response_data)

            data, status_code = self.wpt_github.add_label(response_data['number'])
            _log.info('Add label response (status %s): %s', status_code, data)

            cl.post_comment((
                'Exportable changes to web-platform-tests were detected in this CL '
                'and a pull request in the upstream repo has been made: {pr_url}.\n\n'
                'Travis CI has been kicked off and if it fails, we will let you know here. '
                'If this CL lands and Travis CI is green, we will auto-merge the PR.'
            ).format(
                pr_url='%spull/%d' % (WPT_GH_URL, response_data['number'])
            ))

        return response_data

    def corresponding_pull_request_for_commit(self, exportable_commit):
        """Search pull requests for one that corresponds to exportable_commit.

        Returns the pull_request if found, else returns None.
        """
        # Check for PRs created by commits on master.
        pull_request = self.wpt_github.pr_with_position(exportable_commit.position)
        if pull_request:
            return pull_request

        # Check for PRs created by open Gerrit CLs.
        change_id = exportable_commit.change_id()
        if change_id:
            return self.wpt_github.pr_with_change_id(change_id)

        return None
Ejemplo n.º 13
0
    def run(self):
        # First, poll for an in-flight pull request and merge if exists
        pull_requests = self.wpt_github.in_flight_pull_requests()

        if len(pull_requests) == 1:
            pull_request = pull_requests.pop()

            _log.info('In-flight PR found: #%d', pull_request['number'])
            _log.info(pull_request['title'])

            # TODO(jeffcarp): Check the PR status here

            if self.dry_run:
                _log.info('[dry_run] Would have attempted to merge PR')
            else:
                _log.info('Merging...')
                self.wpt_github.merge_pull_request(pull_request['number'])
                _log.info('PR merged!')
        elif len(pull_requests) > 1:
            _log.error(pull_requests)
            # TODO(jeffcarp): Print links to PRs
            raise Exception('More than two in-flight PRs!')

        # Second, look for exportable commits in Chromium
        # At this point, no in-flight PRs should exist
        # If there was an issue merging, it should have errored out
        local_wpt = LocalWPT(self.host, use_github=False)
        chromium_wpt = ChromiumWPT(self.host)

        # TODO(jeffcarp): have the script running this fetch Chromium origin/master
        # TODO(jeffcarp): move WPT fetch out of its constructor to match planned ChromiumWPT pattern

        wpt_commit, chromium_commit = local_wpt.most_recent_chromium_commit()
        assert chromium_commit, 'No Chromium commit found, this is impossible'

        wpt_behind_master = local_wpt.commits_behind_master(wpt_commit)

        _log.info('\nLast Chromium export commit in web-platform-tests:')
        _log.info('web-platform-tests@%s', wpt_commit)
        _log.info('(%d behind web-platform-tests@origin/master)', wpt_behind_master)

        _log.info('\nThe above WPT commit points to the following Chromium commit:')
        _log.info('chromium@%s', chromium_commit.sha)
        _log.info('(%d behind chromium@origin/master)', chromium_commit.num_behind_master())

        # TODO(jeffcarp): Have this function return ChromiumCommits
        exportable_commits = chromium_wpt.exportable_commits_since(chromium_commit.sha)

        if not exportable_commits:
            _log.info('No exportable commits found in Chromium, stopping.')
            return

        _log.info('Found %d exportable commits in Chromium:', len(exportable_commits))
        for commit in exportable_commits:
            _log.info('- %s %s', commit, chromium_wpt.subject(commit))

        outbound_commit = ChromiumCommit(self.host, sha=exportable_commits[0])
        _log.info('Picking the earliest commit and creating a PR')
        _log.info('- %s %s', outbound_commit.sha, outbound_commit.subject())

        patch = outbound_commit.format_patch()
        message = outbound_commit.message()

        # TODO: now do a test comparison of patch against local WPT

        if self.dry_run:
            _log.info('[dry_run] Stopping before creating PR')
            _log.info('\n\n[dry_run] message:')
            _log.info(message)
            _log.info('\n\n[dry_run] patch:')
            _log.info(patch)
            return

        local_branch_name = local_wpt.create_branch_with_patch(message, patch)

        response_data = self.wpt_github.create_pr(
            local_branch_name=local_branch_name,
            desc_title=outbound_commit.subject(),
            body=outbound_commit.body())

        _log.info('Create PR response: %s', response_data)