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 .hgexportbuilder import build_patch_for_revision
5
7
from landoapi .models .storage import db
6
8
from landoapi .phabricator_client import PhabricatorClient
7
9
from landoapi .transplant_client import TransplantClient
8
10
11
+ TRANSPLANT_JOB_STARTING = 'pending'
9
12
TRANSPLANT_JOB_STARTED = 'started'
10
- TRANSPLANT_JOB_FINISHED = 'finished'
13
+ TRANSPLANT_JOB_LANDED = 'landed'
14
+ TRANSPLANT_JOB_FAILED = 'failed'
11
15
12
16
13
17
class Landing (db .Model ):
@@ -17,16 +21,21 @@ class Landing(db.Model):
17
21
request_id = db .Column (db .Integer , unique = True )
18
22
revision_id = db .Column (db .String (30 ))
19
23
status = db .Column (db .Integer )
24
+ error = db .Column (db .String (128 ), default = '' )
25
+ result = db .Column (db .String (128 ))
20
26
21
27
def __init__ (
22
- self , request_id = None , revision_id = None , status = TRANSPLANT_JOB_STARTED
28
+ self ,
29
+ request_id = None ,
30
+ revision_id = None ,
31
+ status = TRANSPLANT_JOB_STARTING
23
32
):
24
33
self .request_id = request_id
25
34
self .revision_id = revision_id
26
35
self .status = status
27
36
28
37
@classmethod
29
- def create (cls , revision_id , phabricator_api_key = None , save = True ):
38
+ def create (cls , revision_id , phabricator_api_key = None ):
30
39
""" Land revision and create a Transplant item in storage. """
31
40
phab = PhabricatorClient (phabricator_api_key )
32
41
revision = phab .get_revision (id = revision_id )
@@ -40,39 +49,34 @@ def create(cls, revision_id, phabricator_api_key=None, save=True):
40
49
41
50
repo = phab .get_revision_repo (revision )
42
51
52
+ # save landing to make sure we've got the callback
53
+ landing = cls (
54
+ revision_id = revision_id ,
55
+ ).save ()
56
+
43
57
trans = TransplantClient ()
58
+ callback = '%s/landings/%s/update' % (
59
+ os .getenv ('HOST_URL' ), landing .id
60
+ )
44
61
# The LDAP username used here has to be the username of the patch
45
62
# pusher.
46
63
# FIXME: what value do we use here?
47
64
# FIXME: This client, or the person who pushed the 'Land it!' button?
48
65
request_id = trans .land (
49
- 'ldap_username@example.com' , hgpatch , repo ['uri' ]
66
+ 'ldap_username@example.com' , hgpatch , repo ['uri' ], callback
50
67
)
51
68
if not request_id :
52
69
raise LandingNotCreatedException
53
70
54
- landing = cls (
55
- revision_id = revision_id ,
56
- request_id = request_id ,
57
- status = TRANSPLANT_JOB_STARTED
58
- )
59
- if save :
60
- landing .save (create = True )
71
+ landing .request_id = request_id
72
+ landing .status = TRANSPLANT_JOB_STARTED
73
+ landing .save ()
61
74
62
75
return landing
63
76
64
- @classmethod
65
- def get (cls , landing_id ):
66
- """ Get Landing object from storage. """
67
- landing = cls .query .get (landing_id )
68
- if not landing :
69
- raise LandingNotFoundException ()
70
-
71
- return landing
72
-
73
- def save (self , create = False ):
77
+ def save (self ):
74
78
""" Save objects in storage. """
75
- if create :
79
+ if not self . id :
76
80
db .session .add (self )
77
81
78
82
db .session .commit ()
@@ -87,7 +91,9 @@ def serialize(self):
87
91
'id' : self .id ,
88
92
'revision_id' : self .revision_id ,
89
93
'request_id' : self .request_id ,
90
- 'status' : self .status
94
+ 'status' : self .status ,
95
+ 'error_msg' : self .error ,
96
+ 'result' : self .result
91
97
}
92
98
93
99
@@ -96,11 +102,6 @@ class LandingNotCreatedException(Exception):
96
102
pass
97
103
98
104
99
- class LandingNotFoundException (Exception ):
100
- """ No specific Landing was found in database. """
101
- pass
102
-
103
-
104
105
class RevisionNotFoundException (Exception ):
105
106
""" Phabricator returned 404 for a given revision id. """
106
107
0 commit comments