-
Notifications
You must be signed in to change notification settings - Fork 22
Check the status of landing jobs #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,5 @@ __pycache__ | |
| /*.egg-info/ | ||
| /.cache/ | ||
| /docker-compose.override.yml | ||
| *.pyc | ||
| /.db/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' | ||
| 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): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?