|
| 1 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | +from landoapi.models.storage import db |
| 5 | +from landoapi.phabricator_client import PhabricatorClient |
| 6 | +from landoapi.transplant_client import TransplantClient |
| 7 | + |
| 8 | +TRANSPLANT_JOB_STARTED = 'started' |
| 9 | +TRANSPLANT_JOB_FINISHED = 'finished' |
| 10 | + |
| 11 | + |
| 12 | +def _get_revision(revision_id, api_key=None): |
| 13 | + """ Gets revision from Phabricator. |
| 14 | +
|
| 15 | + Returns None or revision. |
| 16 | + """ |
| 17 | + phab = PhabricatorClient(api_key) |
| 18 | + revision = phab.get_revision(id=revision_id) |
| 19 | + if not revision: |
| 20 | + return None |
| 21 | + |
| 22 | + raw_repo = phab.get_repo(revision['repositoryPHID']) |
| 23 | + return { |
| 24 | + 'id': int(revision['id']), |
| 25 | + 'phid': revision['phid'], |
| 26 | + 'repo_url': raw_repo['uri'], |
| 27 | + 'title': revision['title'], |
| 28 | + 'url': revision['uri'], |
| 29 | + 'date_created': int(revision['dateCreated']), |
| 30 | + 'date_modified': int(revision['dateModified']), |
| 31 | + 'status': int(revision['status']), |
| 32 | + 'status_name': revision['statusName'], |
| 33 | + 'summary': revision['summary'], |
| 34 | + 'test_plan': revision['testPlan'], |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | +class Landing(db.Model): |
| 39 | + __tablename__ = "landings" |
| 40 | + |
| 41 | + id = db.Column(db.Integer, primary_key=True) |
| 42 | + request_id = db.Column(db.Integer, unique=True) |
| 43 | + revision_id = db.Column(db.String(30)) |
| 44 | + status = db.Column(db.Integer) |
| 45 | + |
| 46 | + def __init__( |
| 47 | + self, request_id=None, revision_id=None, status=TRANSPLANT_JOB_STARTED |
| 48 | + ): |
| 49 | + self.request_id = request_id |
| 50 | + self.revision_id = revision_id |
| 51 | + self.status = status |
| 52 | + |
| 53 | + @classmethod |
| 54 | + def create(cls, revision_id, phabricator_api_key=None, save=True): |
| 55 | + """ Land revision and create a Transplant item in storage. """ |
| 56 | + revision = _get_revision(revision_id, phabricator_api_key) |
| 57 | + if not revision: |
| 58 | + raise RevisionNotFoundException(revision_id) |
| 59 | + |
| 60 | + trans = TransplantClient() |
| 61 | + request_id = trans.land( |
| 62 | + 'ldap_username@example.com', revision['repo_url'] |
| 63 | + ) |
| 64 | + if not request_id: |
| 65 | + raise LandingNotCreatedException |
| 66 | + |
| 67 | + landing = cls( |
| 68 | + revision_id=revision_id, |
| 69 | + request_id=request_id, |
| 70 | + status=TRANSPLANT_JOB_STARTED |
| 71 | + ) |
| 72 | + if save: |
| 73 | + landing.save(create=True) |
| 74 | + |
| 75 | + return landing |
| 76 | + |
| 77 | + @classmethod |
| 78 | + def get(cls, landing_id): |
| 79 | + """ Get Landing object from storage. """ |
| 80 | + landing = cls.query.get(landing_id) |
| 81 | + if not landing: |
| 82 | + raise LandingNotFoundException() |
| 83 | + |
| 84 | + return landing |
| 85 | + |
| 86 | + def save(self, create=False): |
| 87 | + """ Save objects in storage. """ |
| 88 | + if create: |
| 89 | + db.session.add(self) |
| 90 | + |
| 91 | + db.session.commit() |
| 92 | + return self |
| 93 | + |
| 94 | + def __repr__(self): |
| 95 | + return '<Landing: %s>' % self.id |
| 96 | + |
| 97 | + def serialize(self): |
| 98 | + """ Serialize to JSON compatible dictionary. """ |
| 99 | + return { |
| 100 | + 'id': self.id, |
| 101 | + 'revision_id': self.revision_id, |
| 102 | + 'request_id': self.request_id, |
| 103 | + 'status': self.status |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | +class LandingNotCreatedException(Exception): |
| 108 | + """ Transplant service failed to land a revision. """ |
| 109 | + pass |
| 110 | + |
| 111 | + |
| 112 | +class LandingNotFoundException(Exception): |
| 113 | + """ No specific Landing was found in database. """ |
| 114 | + pass |
| 115 | + |
| 116 | + |
| 117 | +class RevisionNotFoundException(Exception): |
| 118 | + """ Phabricator returned 404 for a given revision id. """ |
| 119 | + |
| 120 | + def __init__(self, revision_id): |
| 121 | + super().__init__() |
| 122 | + self.revision_id = revision_id |
0 commit comments