#1907 Add API entrypoint for regenerating repos
Merged 3 years ago by frostyx. Opened 3 years ago by tstellar.
copr/ tstellar/copr regen-repos-api  into  main

@@ -2,15 +2,17 @@ 

  from coprs.views.apiv3_ns import (query_params, get_copr, pagination, Paginator,

                                    GET, POST, PUT, DELETE, set_defaults)

  from coprs.views.apiv3_ns.json2form import get_form_compatible_data, get_input_dict

- from coprs import db, models, forms

+ from coprs import db, models, forms, db_session_scope

  from coprs.views.misc import api_login_required

  from coprs.views.apiv3_ns import apiv3_ns

+ from coprs.logic.actions_logic import ActionsLogic

  from coprs.logic.coprs_logic import CoprsLogic, CoprChrootsLogic, MockChrootsLogic

  from coprs.logic.complex_logic import ComplexLogic

  from coprs.logic.users_logic import UsersLogic

  from coprs.exceptions import (DuplicateException, NonAdminCannotCreatePersistentProject,

                                NonAdminCannotDisableAutoPrunning, ActionInProgressException,

                                InsufficientRightsException, BadRequest, ObjectNotFound)

+ from . import editable_copr

  

  

  def to_dict(copr):
@@ -265,3 +267,15 @@ 

      else:

          raise BadRequest(form.errors)

      return flask.jsonify(copr_dict)

+ 

+ @apiv3_ns.route("/project/regenerate-repos/<ownername>/<projectname>", methods=PUT)

+ @api_login_required

+ @editable_copr

+ def regenerate_repos(copr):

+     """

+     This function will regenerate all repository metadata for a project.

+     """

+     with db_session_scope():

+         ActionsLogic.send_createrepo(copr)

+ 

+     return flask.jsonify(to_dict(copr))

@@ -188,6 +188,17 @@ 

          assert copr.fedora_review

          assert copr.delete_after_days == 5

  

+     @TransactionDecorator("u1")

+     @pytest.mark.usefixtures("f_users", "f_coprs", "f_users_api", "f_mock_chroots", "f_db")

+     def test_regenerate_repos(self):

+         """

+         Make sure that the regenerate-repos api works.

+         """

+         self.db.session.add(self.c1)

+         route = "/api_3/project/regenerate-repos/{}".format(self.c1.full_name)

+         r = self.api3.post(route, {})

+         assert r.status_code == 200

+ 

  

  class TestApiV3Permissions(CoprsTestCase):

  

@@ -350,3 +350,26 @@ 

                  data=permissions)

  

          request.send()

+ 

+     def regenerate_repos(self, ownername, projectname):

+         """

+         Regenerate repositories for a project

+ 

+         :param str ownername: owner of the project to regenerate

+         :param str projectname: name of the project to regenerate

+         """

+         endpoint = "/projects/regenerate-repo/{ownername}/{projectname}"

+         params = {

+             "ownername": ownername,

+             "projectname": projectname

+         }

+         request = Request(

+                 endpoint,

+                 api_base_url=self.api_base_url,

+                 auth=self.auth,

+                 method=PUT,

+                 params=params)

+ 

+         request.send()

+         response = request.send()

+         return munchify(response)

I would like to add support for regenerating repos via the copr api. I'm new to copr development and am just guessing how to do this. Am I on the right track here?

Build failed. More information on how to proceed and troubleshoot errors available at https://fedoraproject.org/wiki/Zuul-based-ci

Thanks for the contribution!

When you use @editable_copr decorator, there's no need to get_copr() again - decorator sets the first copr argument for you automatically.

We should handle the response somehow at least.

Am I on the right track here?

Definitely, thank you again.

Metadata Update from @praiskup:
- Pull-request tagged with: wip

3 years ago

@tstellar, on top of the comments above, the frontend testsuite seems to be broken for some (see the failed "failure" badge above).

Build failed. More information on how to proceed and troubleshoot errors available at https://fedoraproject.org/wiki/Zuul-based-ci

rebased onto 77c546d0117409b86f5347b8351d7c5bd57400b2

3 years ago

Build failed. More information on how to proceed and troubleshoot errors available at https://fedoraproject.org/wiki/Zuul-based-ci

rebased onto b25fe692d9928904ac04bd75b870d2f758cd9672

3 years ago

Build failed. More information on how to proceed and troubleshoot errors available at https://fedoraproject.org/wiki/Zuul-based-ci

rebased onto cfc94583a5fec0c407931a8fbc3b9115f5f24f57

3 years ago

Build succeeded.

I added some response handling and a test case, but I'm not sure if the test case is actually testing anything.

I think that the post() method here can be used, and put is not needed.

The response needs an assert here, so the test works (e.g. see the typo in regenerate-reps vs regenerate-repo).

Use just:

with db_session_scope(): 
    ActionsLogic.send_createrepo(copr)

The pre-existing post method should work as well here.

Looks much better, thank you

Metadata Update from @praiskup:
- Pull-request untagged with: wip
- Pull-request tagged with: needs-work

3 years ago

rebased onto b29938b4af4a734328ad2aea6ecb36b1e24e1771

3 years ago

Build succeeded.

The test case should be fixed now and I've adopted the other code improvements that were suggested.

Metadata Update from @praiskup:
- Pull-request untagged with: needs-work

3 years ago

+1, I like this, thank you. But I would ask @frostyx if it is OK.
The only question that remains here from my side is if we should return full copr object, or, for example, only "True" value or something alike.

Thank you @tstellar, I like the PR.

The only question that remains here from my side is if we should return full copr object, or, for example, only "True" value or something alike.

Personally, I like returning the whole copr object. I usually find it more useful, than a success message because I kinda expect success based on the status code. And by having the whole object, I can construct a message that best fits the client use-case. By having the whole object it is also very easy to check, that some value in fact changed. But true, in this particular case, we don't have any value in the JSON that would represent the last repo regeneration. Maybe we could add something.

I would also like to see a copr-cli command using this API endpoint and some beaker test for it. Let me know @tstellar if you are interested in working on those as well or if I should implement them.

But don't want to block this PR with any of these.

rebased onto d2c13f3

3 years ago

Pull-Request has been merged by frostyx

3 years ago

Build succeeded.

Thank you @tstellar, I like the PR.

The only question that remains here from my side is if we should return full copr object, or, for example, only "True" value or something alike.

Personally, I like returning the whole copr object. I usually find it more useful, than a success message because I kinda expect success based on the status code. And by having the whole object, I can construct a message that best fits the client use-case. By having the whole object it is also very easy to check, that some value in fact changed. But true, in this particular case, we don't have any value in the JSON that would represent the last repo regeneration. Maybe we could add something.

I would also like to see a copr-cli command using this API endpoint and some beaker test for it. Let me know @tstellar if you are interested in working on those as well or if I should implement them.

I don't have any plans to do more work on this feature, because my use case only requires having the REST API for this feature.

I don't have any plans to do more work on this feature, because my use case only requires having the REST API for this feature.

Okay, thank you @tstellar, no problem with me. I created #1915 so we don't forget about it and assigned myself to implement it.