#1861 frontend, python, cli: generate webhook secrets using APIv3
Merged 2 years ago by msuchy. Opened 2 years ago by frostyx.
copr/ frostyx/copr apiv3-webhook-secret  into  main

file modified
+2 -24
@@ -376,30 +376,8 @@ 

  

          if answer == 'y':

              ownername, projectname = self.parse_name(args.name)

- 

-             copr_url = self.client.config["copr_url"]

-             api_url = "{0}/api".format(copr_url)

-             url = "{0}/coprs/{1}/{2}/{3}/".format(

-                 api_url, ownername, projectname, "new_webhook_secret")

- 

-             auth = (self.client.config["login"],

-                     self.client.config["token"])

- 

-             # @TODO Rewrite this call to the APIv3.

-             # @TODO I am not doing it right away because it is a release-blocker

-             try:

-                 response = requests.post(url=url, auth=auth)

-                 result = response.json()

-             except requests.ConnectionError as ex:

-                 sys.stderr.write(str(ex) + "\n")

-                 return

- 

-             if result["output"] != "ok":

-                 sys.stderr.write(result["error"] + "\n")

-                 sys.stderr.write("Un-expected data returned, please report this issue\n")

-                 return

- 

-             print(result["message"])

+             token = self.client.webhook_proxy.generate(ownername, projectname)

+             print("Generated new token: {0}".format(token.webhook_secret))

  

      @requires_api_auth

      def action_build(self, args):

@@ -108,7 +108,7 @@ 

  from coprs.views import apiv3_ns

  from coprs.views.apiv3_ns import (apiv3_general, apiv3_builds, apiv3_packages, apiv3_projects, apiv3_project_chroots,

                                    apiv3_modules, apiv3_build_chroots, apiv3_mock_chroots,

-                                   apiv3_permissions)

+                                   apiv3_permissions, apiv3_webhooks)

  

  from coprs.views import batches_ns

  from coprs.views.batches_ns import coprs_batches

@@ -0,0 +1,37 @@ 

+ """

+ APIv3 endpoints related to webhooks

+ """

+ 

+ import flask

+ from coprs import db

+ from coprs.views.misc import api_login_required

+ from coprs.views.apiv3_ns import editable_copr, POST

+ from coprs.views.apiv3_ns import apiv3_ns

+ 

+ 

+ def to_dict(copr):

+     """

+     Convert `models.Copr` object to an APIv3 representation of

+     webhook-related data

+     """

+     return {

+         "id": copr.id,

+         "name": copr.name,

+         "ownername": copr.owner_name,

+         "full_name": copr.full_name,

+         "webhook_secret": copr.webhook_secret

+     }

+ 

+ 

+ @apiv3_ns.route("/webhook/generate/<ownername>/<projectname>", methods=POST)

+ @api_login_required

+ @editable_copr

+ def new_webhook_secret(copr):

+     """

+     Generate a new webhook secret for a given project.

+     Not an additional secret, though. The previous secret gets lost.

+     """

+     copr.new_webhook_secret()

+     db.session.add(copr)

+     db.session.commit()

+     return flask.jsonify(to_dict(copr))

@@ -7,6 +7,7 @@ 

  from .proxies.mock_chroot import MockChrootProxy

  from .proxies.project_chroot import ProjectChrootProxy

  from .proxies.build_chroot import BuildChrootProxy

+ from .proxies.webhook import WebhookProxy

  

  

  class Client(object):
@@ -20,6 +21,7 @@ 

          self.mock_chroot_proxy = MockChrootProxy(config)

          self.project_chroot_proxy = ProjectChrootProxy(config)

          self.build_chroot_proxy = BuildChrootProxy(config)

+         self.webhook_proxy = WebhookProxy(config)

  

      @classmethod

      def create_from_config_file(cls, path=None):

@@ -0,0 +1,35 @@ 

+ """

+ Webhook related actions in APIv3

+ """

+ 

+ from __future__ import absolute_import

+ 

+ from . import BaseProxy

+ from ..requests import Request, munchify, POST

+ from ..helpers import for_all_methods, bind_proxy

+ 

+ 

+ @for_all_methods(bind_proxy)

+ class WebhookProxy(BaseProxy):

+     """

+     This class provides access to all webhook related actions in APIv3

+     Methods call endpoints that starts with /api_3/webhook/

+     """

+ 

+     def generate(self, ownername, projectname):

+         """

+         Generate a new webhook secret

+ 

+         :param str ownername:

+         :param str projectname:

+         :return: Munch

+         """

+         endpoint = "/webhook/generate/{ownername}/{projectname}"

+         params = {

+             "ownername": ownername,

+             "projectname": projectname,

+         }

+         request = Request(endpoint, api_base_url=self.api_base_url, method=POST,

+                           params=params, auth=self.auth)

+         response = request.send()

+         return munchify(response)

Build succeeded.

The failed CI build of fronted in rawhide seems to be unrelated to this change.

The failed CI build of fronted in rawhide seems to be unrelated to this change.

I created a new card on our TODO list; we'll have to decide what to do with non-working
python-flask-whooshee.

rebased onto 2f3ba98

2 years ago

Pull-Request has been merged by msuchy

2 years ago

Build succeeded.