|
1 | 1 | # This Source Code Form is subject to the terms of the Mozilla Public
|
2 | 2 | # License, v. 2.0. If a copy of the MPL was not distributed with this
|
3 | 3 | # file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
| 4 | +import os |
| 5 | + |
4 | 6 | from landoapi.models.storage import db
|
5 | 7 | from landoapi.phabricator_client import PhabricatorClient
|
6 | 8 | from landoapi.transplant_client import TransplantClient
|
7 | 9 |
|
| 10 | +TRANSPLANT_JOB_STARTING = 'starting' |
8 | 11 | TRANSPLANT_JOB_STARTED = 'started'
|
9 |
| -TRANSPLANT_JOB_FINISHED = 'finished' |
| 12 | +TRANSPLANT_JOB_LANDED = 'landed' |
| 13 | +TRANSPLANT_JOB_FAILED = 'failed' |
10 | 14 |
|
11 | 15 |
|
12 | 16 | def _get_revision(revision_id, api_key=None):
|
@@ -42,35 +46,45 @@ class Landing(db.Model):
|
42 | 46 | request_id = db.Column(db.Integer, unique=True)
|
43 | 47 | revision_id = db.Column(db.String(30))
|
44 | 48 | status = db.Column(db.Integer)
|
| 49 | + error = db.Column(db.String(128), default='') |
| 50 | + result = db.Column(db.String(128)) |
45 | 51 |
|
46 | 52 | def __init__(
|
47 |
| - self, request_id=None, revision_id=None, status=TRANSPLANT_JOB_STARTED |
| 53 | + self, |
| 54 | + request_id=None, |
| 55 | + revision_id=None, |
| 56 | + status=TRANSPLANT_JOB_STARTING |
48 | 57 | ):
|
49 | 58 | self.request_id = request_id
|
50 | 59 | self.revision_id = revision_id
|
51 | 60 | self.status = status
|
52 | 61 |
|
53 | 62 | @classmethod
|
54 |
| - def create(cls, revision_id, phabricator_api_key=None, save=True): |
| 63 | + def create(cls, revision_id, phabricator_api_key=None): |
55 | 64 | """ Land revision and create a Transplant item in storage. """
|
56 | 65 | revision = _get_revision(revision_id, phabricator_api_key)
|
57 | 66 | if not revision:
|
58 | 67 | raise RevisionNotFoundException(revision_id)
|
59 | 68 |
|
| 69 | + # save landing to make sure we've got the callback |
| 70 | + landing = cls( |
| 71 | + revision_id=revision_id, |
| 72 | + ) |
| 73 | + landing.save() |
| 74 | + |
60 | 75 | trans = TransplantClient()
|
| 76 | + callback = '%s/landings/%s/update' % ( |
| 77 | + os.environ.get('HOST_URL'), landing.id |
| 78 | + ) |
61 | 79 | request_id = trans.land(
|
62 |
| - 'ldap_username@example.com', revision['repo_url'] |
| 80 | + 'ldap_username@example.com', revision['repo_url'], callback |
63 | 81 | )
|
64 | 82 | if not request_id:
|
65 | 83 | raise LandingNotCreatedException
|
66 | 84 |
|
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) |
| 85 | + landing.request_id = request_id |
| 86 | + landing.status = TRANSPLANT_JOB_STARTED |
| 87 | + landing.save(create=True) |
74 | 88 |
|
75 | 89 | return landing
|
76 | 90 |
|
@@ -100,7 +114,9 @@ def serialize(self):
|
100 | 114 | 'id': self.id,
|
101 | 115 | 'revision_id': self.revision_id,
|
102 | 116 | 'request_id': self.request_id,
|
103 |
| - 'status': self.status |
| 117 | + 'status': self.status, |
| 118 | + 'error_msg': self.error, |
| 119 | + 'result': self.result |
104 | 120 | }
|
105 | 121 |
|
106 | 122 |
|
|
0 commit comments