2020-03-10 16:11:41 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright 2020 Google LLC
|
|
|
|
#
|
|
|
|
# Licensed under the the Apache License v2.0 with LLVM Exceptions (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# https://llvm.org/LICENSE.txt
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
from phabricator import Phabricator
|
|
|
|
from typing import List, Optional, Dict
|
|
|
|
import datetime
|
|
|
|
|
2020-03-13 13:50:15 +01:00
|
|
|
|
|
|
|
_BASE_URL = 'https://reviews.llvm.org'
|
|
|
|
|
2020-03-10 16:11:41 +01:00
|
|
|
_LOGGER = logging.getLogger()
|
|
|
|
|
|
|
|
|
|
|
|
class Revision:
|
|
|
|
"""A Revision on Phabricator"""
|
|
|
|
|
|
|
|
def __init__(self, revision_dict: Dict):
|
|
|
|
self.revision_dict = revision_dict
|
|
|
|
self.diffs = [] # type : "Diff"
|
2020-03-13 13:50:15 +01:00
|
|
|
self.phab_url = '{}/D{}'.format(_BASE_URL, self.id) # type: str
|
2020-03-10 16:11:41 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self) -> str:
|
|
|
|
return self.revision_dict['id']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def phid(self) -> str:
|
|
|
|
return self.revision_dict['phid']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def status(self) -> str:
|
|
|
|
return self.revision_dict['fields']['status']['value']
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'Revision {}: {} - ({})'.format(self.id, self.status,
|
2020-03-13 13:50:15 +01:00
|
|
|
','.join([str(d.id) for d in self.diffs]))
|
2020-03-10 16:11:41 +01:00
|
|
|
|
2020-03-10 17:57:05 +01:00
|
|
|
@property
|
|
|
|
def branch_name(self) -> str:
|
|
|
|
return 'phab-D{}'.format(self.id)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def title(self) -> str:
|
|
|
|
return self.revision_dict['fields']['title']
|
|
|
|
|
2020-03-13 13:50:15 +01:00
|
|
|
@property
|
|
|
|
def pr_title(self) -> str:
|
|
|
|
return 'D{}: {}'.format(self.id, self.title)
|
|
|
|
|
2020-03-10 17:57:05 +01:00
|
|
|
@property
|
|
|
|
def summary(self) -> str:
|
|
|
|
return self.revision_dict['fields']['summary']
|
2020-03-10 16:11:41 +01:00
|
|
|
|
2020-03-13 13:50:15 +01:00
|
|
|
@property
|
|
|
|
def pr_summary(self) -> str:
|
|
|
|
return '{}\n\n{}'.format(self.summary, self.phab_url)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def sorted_diffs(self) -> List["Diff"]:
|
|
|
|
return sorted(self.diffs, key=lambda d: d.id)
|
|
|
|
|
|
|
|
|
2020-03-10 16:11:41 +01:00
|
|
|
class Diff:
|
|
|
|
"""A Phabricator diff."""
|
|
|
|
|
|
|
|
def __init__(self, diff_dict: Dict, revision: Revision):
|
|
|
|
self.diff_dict = diff_dict
|
|
|
|
self.revision = revision
|
2020-03-13 13:50:15 +01:00
|
|
|
# TODO: check in git repo instead of using a flag
|
2020-03-10 16:11:41 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self) -> str:
|
|
|
|
return self.diff_dict['id']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def phid(self) -> str:
|
|
|
|
return self.diff_dict['phid']
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'Diff {}'.format(self.id)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def base_hash(self) -> Optional[str]:
|
|
|
|
for ref in self.diff_dict['fields']['refs']:
|
|
|
|
if ref['type'] == 'base':
|
|
|
|
return ref['identifier']
|
|
|
|
return None
|
|
|
|
|
2020-03-13 13:50:15 +01:00
|
|
|
@property
|
|
|
|
def branch_name(self) -> str:
|
|
|
|
return 'phab-diff-{}'.format(self.id)
|
|
|
|
|
2020-03-10 16:11:41 +01:00
|
|
|
|
|
|
|
class PhabWrapper:
|
|
|
|
"""
|
|
|
|
Wrapper around the interactions with Phabricator.
|
|
|
|
|
|
|
|
Conduit API documentation: https://reviews.llvm.org/conduit/
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.conduit_token = None # type: Optional[str]
|
|
|
|
self.host = None # type: Optional[str]
|
|
|
|
self._load_arcrc()
|
|
|
|
self.phab = self._create_phab() # type: Phabricator
|
|
|
|
|
|
|
|
def _load_arcrc(self):
|
|
|
|
"""Load arc configuration from file if not set."""
|
|
|
|
_LOGGER.info('Loading configuration from ~/.arcrc file')
|
|
|
|
with open(os.path.expanduser('~/.arcrc'), 'r') as arcrc_file:
|
|
|
|
arcrc = json.load(arcrc_file)
|
|
|
|
# use the first host configured in the file
|
|
|
|
self.host = next(iter(arcrc['hosts']))
|
|
|
|
self.conduit_token = arcrc['hosts'][self.host]['token']
|
|
|
|
|
|
|
|
def _create_phab(self) -> Phabricator:
|
|
|
|
"""Create Phabricator API instance and update it."""
|
|
|
|
phab = Phabricator(token=self.conduit_token, host=self.host)
|
|
|
|
# TODO: retry on communication error
|
|
|
|
phab.update_interfaces()
|
|
|
|
return phab
|
|
|
|
|
|
|
|
def get_revisions(self) -> List[Revision]:
|
|
|
|
"""Get relevant revisions."""
|
|
|
|
# TODO: figure out which revisions we really need to pull in
|
|
|
|
_LOGGER.info('Getting revisions from Phabricator')
|
|
|
|
start_date = datetime.datetime.now() - datetime.timedelta(days=3)
|
|
|
|
constraints = {
|
|
|
|
'createdStart': int(start_date.timestamp())
|
2020-03-13 13:50:15 +01:00
|
|
|
#'ids': [76120]
|
2020-03-10 16:11:41 +01:00
|
|
|
}
|
|
|
|
# TODO: handle > 100 responses
|
|
|
|
revision_response = self.phab.differential.revision.search(
|
|
|
|
constraints=constraints)
|
2020-03-13 16:58:12 +01:00
|
|
|
revisions = [Revision(r) for r in revision_response.response['data']]
|
2020-03-10 17:57:05 +01:00
|
|
|
# TODO: only taking the first 10 to speed things up
|
2020-03-13 13:50:15 +01:00
|
|
|
revisions = revisions[0:10]
|
2020-03-10 16:11:41 +01:00
|
|
|
_LOGGER.info('Got {} revisions from the server'.format(len(revisions)))
|
|
|
|
for revision in revisions:
|
|
|
|
# TODO: batch-query diffs for all revisions, reduce number of
|
|
|
|
# API calls this would be much faster. But then we need to locally
|
|
|
|
# map the diffs to the right revisions
|
|
|
|
self._get_diffs(revision)
|
|
|
|
return revisions
|
|
|
|
|
|
|
|
def _get_diffs(self, revision: Revision):
|
|
|
|
"""Get diffs for a revision from Phabricator."""
|
2020-03-13 13:50:15 +01:00
|
|
|
_LOGGER.info('Downloading diffs for Revision D{}...'.format(revision.id))
|
2020-03-10 16:11:41 +01:00
|
|
|
constraints = {
|
|
|
|
'revisionPHIDs': [revision.phid]
|
|
|
|
}
|
|
|
|
diff_response = self.phab.differential.diff.search(
|
|
|
|
constraints=constraints)
|
|
|
|
revision.diffs = [Diff(d, revision) for d in diff_response.response['data']]
|
2020-03-13 13:50:15 +01:00
|
|
|
_LOGGER.info(', '.join([str(d.id) for d in revision.diffs]))
|
2020-03-10 16:11:41 +01:00
|
|
|
|
|
|
|
def get_raw_patch(self, diff: Diff) -> str:
|
|
|
|
"""Get raw patch for diff from Phabricator."""
|
2020-03-13 13:50:15 +01:00
|
|
|
_LOGGER.info('Downloading patch for Diff {}...'.format(diff.id))
|
2020-03-10 16:11:41 +01:00
|
|
|
return self.phab.differential.getrawdiff(diffID=str(diff.id)).response
|