forked from fedora-copr/copr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
frontend: migrate monitor, modules, mock_chroots, webhooks enpoints t…
…o restx
- Loading branch information
Showing
7 changed files
with
222 additions
and
117 deletions.
There are no files selected for viewing
This file contains 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
43 changes: 31 additions & 12 deletions
43
frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_mock_chroots.py
This file contains 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 |
---|---|---|
@@ -1,17 +1,36 @@ | ||
import flask | ||
from http import HTTPStatus | ||
|
||
from flask_restx import Namespace, Resource | ||
from flask_restx.fields import String | ||
from html2text import html2text | ||
from coprs.views.apiv3_ns import apiv3_ns | ||
|
||
from coprs.views.apiv3_ns import api | ||
from coprs.logic.coprs_logic import MockChrootsLogic | ||
|
||
|
||
@apiv3_ns.route("/mock-chroots/list") | ||
def list_chroots(): | ||
chroots = MockChrootsLogic.active_names_with_comments() | ||
response = {} | ||
for chroot, comment in chroots: | ||
if comment: | ||
response[chroot] = html2text(comment).strip("\n") | ||
else: | ||
response[chroot] = "" | ||
apiv3_mock_chroots_ns = Namespace("mock-chroots", description="Mock chroots") | ||
api.add_namespace(apiv3_mock_chroots_ns) | ||
|
||
|
||
# this model is so tiny, ez and generic it's not worth it defining in our schema.py | ||
mock_chroot_model = api.model("MockChroot", {String: String}) | ||
|
||
|
||
@apiv3_mock_chroots_ns.route("/list") | ||
class MockChroot(Resource): | ||
@apiv3_mock_chroots_ns.marshal_with(mock_chroot_model) | ||
@apiv3_mock_chroots_ns.response(HTTPStatus.OK.value, "OK, Mock chroot data follows...") | ||
def get(self): | ||
""" | ||
Get list of mock chroots | ||
Get list of all currently active mock chroots with additional comment. | ||
""" | ||
chroots = MockChrootsLogic.active_names_with_comments() | ||
response = {} | ||
for chroot, comment in chroots: | ||
if comment: | ||
response[chroot] = html2text(comment).strip("\n") | ||
else: | ||
response[chroot] = "" | ||
|
||
return flask.jsonify(response) | ||
return response |
77 changes: 49 additions & 28 deletions
77
frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_modules.py
This file contains 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 |
---|---|---|
@@ -1,43 +1,64 @@ | ||
import flask | ||
import sqlalchemy | ||
|
||
from http import HTTPStatus | ||
from flask_restx import Namespace, Resource | ||
from requests.exceptions import RequestException, InvalidSchema | ||
from wtforms import ValidationError | ||
|
||
from coprs import forms, db_session_scope | ||
from coprs.views.apiv3_ns import apiv3_ns, get_copr, file_upload, POST | ||
from coprs.views.misc import api_login_required | ||
from coprs.views.apiv3_ns import api, get_copr, restx_file_upload | ||
from coprs.views.apiv3_ns.schema.schemas import module_build_model, fullname_params | ||
from coprs.views.misc import restx_api_login_required, restx_file_upload | ||
from coprs.exceptions import DuplicateException, BadRequest, InvalidForm | ||
from coprs.logic.modules_logic import ModuleProvider, ModuleBuildFacade | ||
|
||
|
||
apiv3_module_ns = Namespace("module", description="Module") | ||
api.add_namespace(apiv3_module_ns) | ||
|
||
|
||
def to_dict(module): | ||
return { | ||
"nsv": module.nsv, | ||
} | ||
|
||
|
||
@apiv3_ns.route("/module/build/<ownername>/<projectname>", methods=POST) | ||
@api_login_required | ||
@file_upload() | ||
def build_module(ownername, projectname): | ||
copr = get_copr(ownername, projectname) | ||
form = forms.get_module_build_form(meta={'csrf': False}) | ||
if not form.validate_on_submit(): | ||
raise InvalidForm(form) | ||
|
||
facade = None | ||
try: | ||
mod_info = ModuleProvider.from_input(form.modulemd.data or form.scmurl.data) | ||
facade = ModuleBuildFacade(flask.g.user, copr, mod_info.yaml, | ||
mod_info.filename, form.distgit.data) | ||
with db_session_scope(): | ||
module = facade.submit_build() | ||
return flask.jsonify(to_dict(module)) | ||
|
||
except (ValidationError, RequestException, InvalidSchema, RuntimeError) as ex: | ||
raise BadRequest(str(ex)) from ex | ||
|
||
except sqlalchemy.exc.IntegrityError as err: | ||
raise DuplicateException("Module {}-{}-{} already exists" | ||
.format(facade.modulemd.get_module_name(), | ||
facade.modulemd.get_stream_name(), | ||
facade.modulemd.get_version())) from err | ||
@apiv3_module_ns.route("/build/<ownername>/<projectname>") | ||
class Module(Resource): | ||
# TODO: what data needs to be in expect? | ||
@restx_api_login_required | ||
@restx_file_upload | ||
@apiv3_module_ns.doc(params=fullname_params) | ||
@apiv3_module_ns.marshal_with(module_build_model) | ||
@apiv3_module_ns.response(HTTPStatus.OK.value, "Module build successfully submitted") | ||
@apiv3_module_ns.response( | ||
HTTPStatus.BAD_REQUEST.value, HTTPStatus.BAD_REQUEST.description | ||
) | ||
def post(self, ownername, projectname): | ||
""" | ||
Create a module build | ||
Create a module build for ownername/projectname project. | ||
""" | ||
copr = get_copr(ownername, projectname) | ||
form = forms.get_module_build_form(meta={'csrf': False}) | ||
if not form.validate_on_submit(): | ||
raise InvalidForm(form) | ||
|
||
facade = None | ||
try: | ||
mod_info = ModuleProvider.from_input(form.modulemd.data or form.scmurl.data) | ||
facade = ModuleBuildFacade(flask.g.user, copr, mod_info.yaml, | ||
mod_info.filename, form.distgit.data) | ||
with db_session_scope(): | ||
module = facade.submit_build() | ||
return to_dict(module) | ||
|
||
except (ValidationError, RequestException, InvalidSchema, RuntimeError) as ex: | ||
raise BadRequest(str(ex)) from ex | ||
|
||
except sqlalchemy.exc.IntegrityError as err: | ||
raise DuplicateException("Module {}-{}-{} already exists" | ||
.format(facade.modulemd.get_module_name(), | ||
facade.modulemd.get_stream_name(), | ||
facade.modulemd.get_version())) from err |
This file contains 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 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
Oops, something went wrong.