blob: e8fa7bc2e9ff21a9dee7e3dfe2e7ed40f95e3f4c [file] [log] [blame]
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import logging
from webkitpy.w3c.local_wpt import LocalWPT
from webkitpy.w3c.common import exportable_commits_since
from webkitpy.w3c.wpt_github import WPTGitHub
_log = logging.getLogger(__name__)
class TestExporter(object):
def __init__(self, host, gh_user, gh_token, dry_run=False): = host
self.wpt_github = WPTGitHub(host, gh_user, gh_token)
self.dry_run = dry_run
self.local_wpt = LocalWPT(, gh_token)
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:
elif len(pull_requests) > 1:
# TODO(jeffcarp): Print links to PRs
raise Exception('More than two in-flight PRs!')
def merge_in_flight_pull_request(self, pull_request):
"""Attempt to merge an in-flight PR.
pull_request: a PR object returned from the GitHub API.
"""'In-flight PR found: #%d', pull_request['number'])['title'])
# TODO(jeffcarp): Check the PR status here (for Travis CI, etc.)
if self.dry_run:'[dry_run] Would have attempted to merge PR')
self.wpt_github.merge_pull_request(pull_request['number'])'PR merged! Deleting branch.')
self.wpt_github.delete_remote_branch('chromium-export-try')'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)'\nLast Chromium export commit in web-platform-tests:')'web-platform-tests@%s', wpt_commit)'(%d behind web-platform-tests@origin/master)', wpt_behind_master)'\nThe above WPT commit points to the following Chromium commit:')'chromium@%s', chromium_commit.sha)'(%d behind chromium@origin/master)', chromium_commit.num_behind_master())
exportable_commits = exportable_commits_since(chromium_commit.sha,, self.local_wpt)
if not exportable_commits:'No exportable commits found in Chromium, stopping.')
return'Found %d exportable commits in Chromium:', len(exportable_commits))
for commit in exportable_commits:'- %s %s', commit, commit.subject())
outbound_commit = exportable_commits[0]'Picking the earliest commit and creating a PR')'- %s %s', outbound_commit.sha, outbound_commit.subject())
patch = outbound_commit.format_patch()
message = outbound_commit.message()
author =
if self.dry_run:'[dry_run] Stopping before creating PR')'\n\n[dry_run] message:')'\n\n[dry_run] patch:')
remote_branch_name = self.local_wpt.create_branch_with_patch(message, patch, author)
response_data = self.wpt_github.create_pr(
body=outbound_commit.body())'Create PR response: %s', response_data)
if response_data:
data, status_code = self.wpt_github.add_label(response_data['number'])'Add label response (status %s): %s', status_code, data)