commitindex: trigger_review creates diffs on bugzilla when called (Bug 1350407). r?mars,dkl draft
authorDavid Walsh <dwalsh@mozilla.com>
Fri, 24 Mar 2017 13:48:59 -0500
changeset 5571 aa841a13fb35938f7df879004ac57880cd514f5c
parent 5559 4b25a1db2445af72b64722204317926a85beedce
push id191
push userbmo:dwalsh@mozilla.com
push dateTue, 28 Mar 2017 20:27:38 +0000
reviewersmars, dkl
bugs1350407
commitindex: trigger_review creates diffs on bugzilla when called (Bug 1350407). r?mars,dkl MozReview-Commit-ID: 7NTbVdTqu8x
commitindex/commitindex/commitindex.py
commitindex/commitindex/reviews/triggers.py
commitindex/docker-compose.yml
commitindex/tests/test_bmo_attachments.py
--- a/commitindex/commitindex/commitindex.py
+++ b/commitindex/commitindex/commitindex.py
@@ -1,22 +1,28 @@
 # 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 os
+
 import click
 import connexion
 from connexion.resolver import RestyResolver
 
 app = connexion.App(__name__, specification_dir='spec/')
 app.add_api('swagger.yml', resolver=RestyResolver('commitindex.api'))
 
 
 @click.command()
+@click.option(
+    '--bugzilla-url', envvar='BUGZILLA_URL', default='http://bugzilla'
+)
 @click.option('--debug', envvar='DEBUG', is_flag=True)
 @click.option('--port', envvar='PORT', default=8888)
 def development_server(debug, port):
     """Run the commitindex development server.
 
     This server should not be used for production deployments. Instead
     the commitindex should be served by an external webserver as a wsgi
     app.
     """
+    app.config['BUGZILLA_URL'] = os.environ['BUGZILLA_URL']
     app.run(debug=debug, port=port, host='0.0.0.0')
--- a/commitindex/commitindex/reviews/triggers.py
+++ b/commitindex/commitindex/reviews/triggers.py
@@ -1,8 +1,25 @@
 # 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/.
 
+from commitindex.reviews.bugzilla import Bugzilla
+from flask import current_app
 
-def trigger_review(*args, **kwargs):
+
+def get_bugzilla_client():
+    return Bugzilla(rest_url=current_app.config['BUGZILLA_URL'])
+
+
+def trigger_review(commits):
     """Trigger review creation for an Iteration."""
-    pass
+
+    for commit in commits:
+        # TODO: Create real diff
+        commit['data'] = """diff --git a/dirs/source.py b/dirs/source.py
+--- a/dirs/source.py
++++ b/dirs/source.py
+@@ -1,8 +1,17 @@
+
++from commitindex.reviews.bugzilla import Bugzilla"""
+
+        diff_id = get_bugzilla_client().create_attachment(1, commit)
--- a/commitindex/docker-compose.yml
+++ b/commitindex/docker-compose.yml
@@ -12,13 +12,14 @@ services:
       - ./:/app
     depends_on:
       - mountebank
     ports:
       - "8888:8888"
     environment:
       - PORT=8888
       - DEBUG=1
+      - BUGZILLA_URL=http://mountebank:4000
 
   mountebank:
     build:
       context: ../docker/mountebank/
     command: start --logfile /mb.log --debug --mock
--- a/commitindex/tests/test_bmo_attachments.py
+++ b/commitindex/tests/test_bmo_attachments.py
@@ -1,19 +1,27 @@
 # 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/.
 """
 Mountebank test cases for commit-index
 """
 
+import requests
+
+from commitindex.commitindex import app
+from commitindex.reviews import triggers
 from commitindex.reviews.bugzilla import Bugzilla
+from commitindex.reviews.triggers import get_bugzilla_client, trigger_review
 from testing import MountebankClient
 
 import pytest
+from unittest.mock import MagicMock, call
+
+from flask import current_app
 
 
 class FakeBugzilla:
     """Setups up the imposter test double emulating Bugzilla"""
 
     def __init__(self, mountebank_client):
 
         self.mountebank = mountebank_client
@@ -135,8 +143,42 @@ def test_create_valid_attachment(bugzill
             }
         ]
     }
 
     bugzilla.create_attachment(1234)
     bug_test = Bugzilla(rest_url=bugzilla.url)
     result = bug_test.create_attachment(1234, attach_data, '12345')
     assert result == 12345
+
+
+def test_trigger_review_creates_attachment_for_each_commit(monkeypatch):
+    """Tests that a new bugzilla attachment is created for each commit"""
+    commits = [{"id": "1"}, {"id": "1"}, {"id": "1"}]
+
+    bugzilla = MagicMock()
+
+    def get_bugzilla_stub():
+        return bugzilla
+
+    monkeypatch.setattr(
+        "commitindex.reviews.triggers.get_bugzilla_client",
+        get_bugzilla_stub
+    )
+
+    trigger_review(commits)
+
+    expected_calls = [
+        call.create_attachment(1, commits[0]),
+        call.create_attachment(1, commits[1]),
+        call.create_attachment(1, commits[2]),
+    ]
+
+    assert bugzilla.mock_calls == expected_calls
+
+
+def test_bugzilla_client_properly_created():
+    """Tests that a bugzilla client is properly created with URL"""
+    bugzilla_url = 'http://blah/'
+    with app.app.app_context():
+        current_app.config['BUGZILLA_URL'] = bugzilla_url
+        client = triggers.get_bugzilla_client()
+        assert client.rest_url == bugzilla_url