Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ __pycache__
/*.egg-info/
/.cache/
/docker-compose.override.yml
*.pyc
/.db/
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ our code management microservice ecosystem.

##### Running the development server

To create a database schema:

```bash
$ invoke create_db
```

To build and start the development services' containers:

```bash
Expand Down Expand Up @@ -53,3 +59,12 @@ Server: Werkzeug/0.12.1 Python/3.5.3

Start a development server and expose its ports as documented above, and visit
`http://localhost:8000/ui/` in your browser to view the API documentation.

## Testing

We're using `pytest` with `pytest-flask`. All tests are placed in `./tests/`
To run the tests please call

```bash
$ invoke test
```
1 change: 0 additions & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ py==1.4.33 \
--hash=sha256:1f9a981438f2acc20470b301a07a496375641f902320f70e31916fe3377385a9
requests-mock==1.3.0 \
--hash=sha256:23edd6f7926aa13b88bf79cb467632ba2dd5a253034e9f41563f60ed305620c7

5 changes: 4 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ services:
- PHABRICATOR_URL=https://mozphab.dev.mozaws.net
- PHABRICATOR_UNPRIVILEGED_API_KEY=api-123456789
- TRANSPLANT_URL=https://stub.transplant.example.com
- DATABASE_URL=sqlite:////db/sqlite.db
volumes:
- ./.db/:/db/
py3-linter:
build:
context: ./
dockerfile: ./docker/py3-linter-dockerfile
volumes:
- ./:/code/
- ./:/code/
4 changes: 4 additions & 0 deletions docker/Dockerfile-dev
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

FROM python:3.5-alpine

RUN apk --update --no-cache add \
sqlite
RUN mkdir /db

ADD requirements.txt /requirements.txt
RUN pip install --no-cache -r /requirements.txt
ADD dev-requirements.txt /dev-requirements.txt
Expand Down
5 changes: 5 additions & 0 deletions docker/Dockerfile-prod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ FROM python:3.5-alpine
RUN addgroup -g 1001 app && \
adduser -D -u 1001 -G app -s /usr/sbin/nologin app

RUN apk --update --no-cache add \
sqlite
RUN mkdir /db
RUN chown app:app /db

COPY requirements.txt /requirements.txt
RUN pip install --no-cache -r /requirements.txt

Expand Down
Binary file added docker/db/schema.db
Binary file not shown.
73 changes: 73 additions & 0 deletions landoapi/api/landings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 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/.
"""
Transplant API
See the OpenAPI Specification for this API in the spec/swagger.yml file.
"""
from connexion import problem
from flask import request
from landoapi.models.landing import (
Landing,
LandingNotCreatedException,
LandingNotFoundException,
RevisionNotFoundException,
)


def land(data, api_key=None):
""" API endpoint at /revisions/{id}/transplants to land revision. """
# get revision_id from body
revision_id = data['revision_id']
try:
landing = Landing.create(revision_id, api_key)
except RevisionNotFoundException:
# We could not find a matching revision.
return problem(
404,
'Revision not found',
'The requested revision does not exist',
type='https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404'
)
except LandingNotCreatedException:
# We could not find a matching revision.
return problem(
502,
'Landing not created',
'The requested revision does exist, but landing failed.'
'Please retry your request at a later time.',
type='https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/502'
)

return {'id': landing.id}, 202


def get_list(revision_id=None, status=None):
""" API endpoint at /landings to return all Landing objects related to a
Revision or of specific status.
"""
kwargs = {}
if revision_id:
kwargs['revision_id'] = revision_id

if status:
kwargs['status'] = status

landings = Landing.query.filter_by(**kwargs).all()
return list(map(lambda l: l.serialize(), landings)), 200


def get(landing_id):
""" API endpoint at /landings/{landing_id} to return stored Landing.
"""
try:
landing = Landing.get(landing_id)
except LandingNotFoundException:
return problem(
404,
'Landing not found',
'The requested Landing does not exist',
type='https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404'
)

return landing.serialize(), 200
27 changes: 4 additions & 23 deletions landoapi/api/revisions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,13 @@
"""
from connexion import problem
from landoapi.phabricator_client import PhabricatorClient
from landoapi.transplant_client import TransplantClient


def get(revision_id, api_key=None):
""" API endpoint at /revisions/{id} to get revision data. """
phab = PhabricatorClient(api_key)
revision = phab.get_revision(id=revision_id)
""" Gets revision from Phabricator.

if not revision:
# We could not find a matching revision.
return problem(
404,
'Revision not found',
'The requested revision does not exist',
type='https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404'
)

return _format_revision(phab, revision, include_parents=True), 200


def land(revision_id, api_key=None):
""" API endpoint at /revisions/{id}/transplants to land revision. """
Returns None or revision.
"""
phab = PhabricatorClient(api_key)
revision = phab.get_revision(id=revision_id)

Expand All @@ -41,11 +26,7 @@ def land(revision_id, api_key=None):
type='https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404'
)

revision = _format_revision(phab, revision, include_parents=True)

trans = TransplantClient()
id = trans.land('ldap_username@example.com', revision)
return {}, 202
return _format_revision(phab, revision, include_parents=True), 200


def _format_revision(
Expand Down
8 changes: 7 additions & 1 deletion landoapi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import connexion
from connexion.resolver import RestyResolver
from landoapi.dockerflow import dockerflow
from landoapi.models.storage import db


def create_app(version_path):
Expand All @@ -17,8 +18,13 @@ def create_app(version_path):
# Get the Flask app being wrapped by the Connexion app.
flask_app = app.app
flask_app.config['VERSION_PATH'] = version_path
flask_app.register_blueprint(dockerflow)
flask_app.config.setdefault(
'SQLALCHEMY_DATABASE_URI', os.environ.get('DATABASE_URL', 'sqlite://')
)
flask_app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

flask_app.register_blueprint(dockerflow)
db.init_app(flask_app)
return app


Expand Down
21 changes: 21 additions & 0 deletions landoapi/manage.py
Original file line number Diff line number Diff line change
@@ -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/.

from landoapi.app import create_app
from landoapi.models.storage import db

from flask_script import Manager

app = create_app('/version.json')
manager = Manager(app.app)


@manager.command
def create_db():
"""Creates SQLAlchemy database schema."""
return db.create_all()


if __name__ == "__main__":
manager.run()
Empty file added landoapi/models/__init__.py
Empty file.
122 changes: 122 additions & 0 deletions landoapi/models/landing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# 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 landoapi.models.storage import db
from landoapi.phabricator_client import PhabricatorClient
from landoapi.transplant_client import TransplantClient

TRANSPLANT_JOB_STARTED = 'started'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment explaining that these strings are known values copied from the Transplant service API?

TRANSPLANT_JOB_FINISHED = 'finished'


def _get_revision(revision_id, api_key=None):
""" Gets revision from Phabricator.

Returns None or revision.
"""
phab = PhabricatorClient(api_key)
revision = phab.get_revision(id=revision_id)
if not revision:
return None

raw_repo = phab.get_repo(revision['repositoryPHID'])
return {
'id': int(revision['id']),
'phid': revision['phid'],
'repo_url': raw_repo['uri'],
'title': revision['title'],
'url': revision['uri'],
'date_created': int(revision['dateCreated']),
'date_modified': int(revision['dateModified']),
'status': int(revision['status']),
'status_name': revision['statusName'],
'summary': revision['summary'],
'test_plan': revision['testPlan'],
}


class Landing(db.Model):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should store the ldap_username, date_started, and date_landed. We can make another card for it so that we can land this one quicker.

__tablename__ = "landings"

id = db.Column(db.Integer, primary_key=True)
request_id = db.Column(db.Integer, unique=True)
revision_id = db.Column(db.String(30))
status = db.Column(db.Integer)

def __init__(
self, request_id=None, revision_id=None, status=TRANSPLANT_JOB_STARTED
):
self.request_id = request_id
self.revision_id = revision_id
self.status = status

@classmethod
def create(cls, revision_id, phabricator_api_key=None, save=True):
""" Land revision and create a Transplant item in storage. """
revision = _get_revision(revision_id, phabricator_api_key)
if not revision:
raise RevisionNotFoundException(revision_id)

trans = TransplantClient()
request_id = trans.land(
'ldap_username@example.com', revision['repo_url']
)
if not request_id:
raise LandingNotCreatedException

landing = cls(
revision_id=revision_id,
request_id=request_id,
status=TRANSPLANT_JOB_STARTED
)
if save:
landing.save(create=True)

return landing

@classmethod
def get(cls, landing_id):
""" Get Landing object from storage. """
landing = cls.query.get(landing_id)
if not landing:
raise LandingNotFoundException()

return landing

def save(self, create=False):
""" Save objects in storage. """
if create:
db.session.add(self)

db.session.commit()
return self

def __repr__(self):
return '<Landing: %s>' % self.id

def serialize(self):
""" Serialize to JSON compatible dictionary. """
return {
'id': self.id,
'revision_id': self.revision_id,
'request_id': self.request_id,
'status': self.status
}


class LandingNotCreatedException(Exception):
""" Transplant service failed to land a revision. """
pass


class LandingNotFoundException(Exception):
""" No specific Landing was found in database. """
pass


class RevisionNotFoundException(Exception):
""" Phabricator returned 404 for a given revision id. """

def __init__(self, revision_id):
super().__init__()
self.revision_id = revision_id
6 changes: 6 additions & 0 deletions landoapi/models/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 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 flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
Loading