autoland: add Revision API endpoint (bug 1368516) r?glob draft
authorMāris Fogels <mars@mozilla.com>
Mon, 29 May 2017 12:52:26 -0400
changeset 11118 939a574d2988ac65cf78020f785598e669df4e62
parent 11117 66a288d290d5f6b2147e28e4155121e2d04c721b
push id1688
push usermfogels@mozilla.com
push dateMon, 29 May 2017 20:09:07 +0000
reviewersglob
bugs1368516
autoland: add Revision API endpoint (bug 1368516) r?glob Add a new API endpoint for working with Phabricator Revisions. MozReview-Commit-ID: BbK9rkGx64g
autoland/autoland/autoland_rest.py
autoland/tests/unit/test_revision_api.py
--- a/autoland/autoland/autoland_rest.py
+++ b/autoland/autoland/autoland_rest.py
@@ -195,16 +195,37 @@ def autoland_status(request_id):
         status['result'] = result if landed else ''
         status['error_msg'] = result if not landed else ''
 
         return jsonify(status)
 
     abort(404)
 
 
+@app.route('/revisions', methods=['POST'])
+def revisions():
+    """Resource representing Phabricator Revisions."""
+    dbconn = get_dbconn()
+    cursor = dbconn.cursor()
+
+    query = """
+        insert into Transplant(destination, request)
+        values (%s, %s)
+        returning id
+    """
+
+    cursor.execute(
+        query, (request.json['destination'], json.dumps(request.json)))
+
+    request_id = cursor.fetchone()[0]
+    dbconn.commit()
+
+    return jsonify({'request_id': request_id})
+
+
 @app.route('/')
 def hi_there():
     result = 'Welcome to Autoland'
 
     headers = [
         ("Content-Type", "text/plain"),
         ("Content-Length", str(len(result)))
     ]
new file mode 100644
--- /dev/null
+++ b/autoland/tests/unit/test_revision_api.py
@@ -0,0 +1,21 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+import json
+
+from flask import url_for
+
+
+def test_posting_revision_queues_job(
+    client, accept_json, content_json, dbcursor
+):
+    # The database will received one SQL "INSERT" which should return the ID of
+    # the new row.
+    dbcursor.fetchone.return_value = [123]
+    request = {'revision': 'D1', 'destination': 'autoland'}
+
+    headers = accept_json + content_json
+    response = client.post(
+        url_for('revisions'), headers=headers, data=json.dumps(request)
+    )
+    assert response.json == {'request_id': 123}