From 40d9331dffbe0461866d291fe1dd28ad729e1432 Mon Sep 17 00:00:00 2001
From: Lenka Segura <lsegura@redhat.com>
Date: Jan 17 2023 14:53:56 +0000
Subject: [PATCH 1/3] Disable monitoring for retiring packages


Signed-off-by: Lenka Segura <lsegura@redhat.com>

---

diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py
index 8b20bc0..e98559f 100644
--- a/pyrpkg/__init__.py
+++ b/pyrpkg/__init__.py
@@ -19,6 +19,7 @@ import io
 import json
 import logging
 import os
+import pathlib
 import posixpath
 import random
 import re
@@ -3493,6 +3494,23 @@ class Commands(object):
         cmd.extend([project, srpm_name])
         self._run_command(cmd)
 
+    def disable_monitoring(self, distgit_url, distgit_token):
+        """ Disable monitoring when package is retired. """
+        pagure_anitya_status_url = '{0}/_dg/anitya/{1}/{2}'.format(
+            distgit_url.rstrip('/'), self.ns, pathlib.Path(self.path).name)
+        token_str = "token {0}".format(distgit_token)
+        headers = {"Authorization": token_str}
+        payload = {
+            'anitya_status': 'no-monitoring',
+            }
+        response = requests.post(
+            pagure_anitya_status_url, data=payload, headers=headers, timeout=90)
+        if response.status_code == 200:
+            self.log.info("Monitoring disabled.")
+        else:
+            self.log.info("Monitoring not disabled. Disable the monitoring manually.")
+        return response
+
     def remove_side_tag(self, tag):
         self.kojisession.removeSideTag(tag)
 
@@ -3517,7 +3535,7 @@ class Commands(object):
         """
         return self.layout.is_retired()
 
-    def retire(self, message):
+    def retire(self, message, distgit_url=None, distgit_token=None):
         """Delete all tracked files and commit a new dead.package file for rpms
         or dead.module file for modules.
 
@@ -3546,6 +3564,9 @@ class Commands(object):
         fd.write(message + '\n')
         fd.close()
 
+        if None not in (distgit_url, distgit_token):
+            self.disable_monitoring(distgit_url, distgit_token)
+
         cmd = ['git', 'add', os.path.join(self.path, marker)]
         self._run_command(cmd, cwd=self.path)
 
diff --git a/pyrpkg/cli.py b/pyrpkg/cli.py
index b0a16fb..7d81802 100644
--- a/pyrpkg/cli.py
+++ b/pyrpkg/cli.py
@@ -2788,7 +2788,7 @@ class cliClient(object):
             % (self.cmd.build_client, tag_info["name"])
         )
 
-    def retire(self):
+    def retire(self, distgit_url=None, distgit_token=None):
         # Skip if package/module is already retired...
         marker = self.cmd.is_retired()
         # marker is file that indicates retirement
@@ -2798,7 +2798,7 @@ class cliClient(object):
                              'existing {0} file.'.format(marker))
             return 1
         else:
-            self.cmd.retire(self.args.reason)
+            self.cmd.retire(self.args.reason, distgit_url, distgit_token)
             self.push()
 
     def scratch_build(self):

From 17af017588dbe0a5d36997fb10fbf120820ae2c7 Mon Sep 17 00:00:00 2001
From: Lenka Segura <lsegura@redhat.com>
Date: Jan 18 2023 10:05:45 +0000
Subject: [PATCH 2/3] Add tests for disable_monitoring


---

diff --git a/requirements/test-pypi.txt b/requirements/test-pypi.txt
index 830d8c3..fc05c19 100644
--- a/requirements/test-pypi.txt
+++ b/requirements/test-pypi.txt
@@ -4,6 +4,7 @@ flake8
 pytest
 pytest-cov
 argcomplete
+requests_mock
 
 # used in MBS tests
 openidc-client
diff --git a/tests/test_retire.py b/tests/test_retire.py
index 1fdeafd..630e9d0 100644
--- a/tests/test_retire.py
+++ b/tests/test_retire.py
@@ -9,6 +9,9 @@ import pyrpkg.cli
 import six
 from pyrpkg.errors import rpkgError
 from six.moves import configparser
+import requests_mock
+from pyrpkg import Commands
+
 
 try:
     from unittest import mock
@@ -144,6 +147,42 @@ class TestPackageRetirement(RetireTestCase):
         self.assertIn('dead.package found, package or module is already retired',
                       args[0])
 
+    @requests_mock.Mocker()
+    def test_package_disable_monitoring_200(self, mock_requests):
+        distgit_token = "test_token"
+        headers = {"Authorization": "token {0}".format(distgit_token)}
+        distgit_url = "http://distgit.example.com"
+        payload = {'anitya_status': 'no-monitoring'}
+        pagure_anitya_status_url = "{0}/_dg/anitya/rpms/test_project".format(distgit_url)
+        mock_requests.post(pagure_anitya_status_url, headers=headers, json=payload, status_code=200)
+
+        cmd = Commands('test_project', None, None, None, None, None, None, None, None)
+        cmd.ns = 'rpms'
+        cmd.log = mock.Mock()
+        response = cmd.disable_monitoring(distgit_url, distgit_token)
+        self.assertEqual(response.status_code, 200)
+
+        args, kwargs = cmd.log.info.call_args
+        self.assertIn('Monitoring disabled.', args[0])
+
+    @requests_mock.Mocker()
+    def test_package_disable_monitoring_400(self, mock_requests):
+        distgit_token = "test_token"
+        headers = {"Authorization": "token {0}".format(distgit_token)}
+        distgit_url = "http://distgit.example.com"
+        payload = {'anitya_status': 'no-monitoring'}
+        pagure_anitya_status_url = "{0}/_dg/anitya/rpms/test_project".format(distgit_url)
+        mock_requests.post(pagure_anitya_status_url, headers=headers, json=payload, status_code=400)
+
+        cmd = Commands('test_project', None, None, None, None, None, None, None, None)
+        cmd.ns = 'rpms'
+        cmd.log = mock.Mock()
+        response = cmd.disable_monitoring(distgit_url, distgit_token)
+        self.assertEqual(response.status_code, 400)
+
+        args, kwargs = cmd.log.info.call_args
+        self.assertIn('Monitoring not disabled. Disable the monitoring manually.', args[0])
+
 
 class TestModuleRetirement(RetireTestCase):
 

From 68ade3589cb150f934db7b4d68861a5a36d8eff3 Mon Sep 17 00:00:00 2001
From: Lenka Segura <lsegura@redhat.com>
Date: Jan 19 2023 11:13:58 +0000
Subject: [PATCH 3/3] Strip quotes from api token


---

diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py
index e98559f..929dfa3 100644
--- a/pyrpkg/__init__.py
+++ b/pyrpkg/__init__.py
@@ -3498,7 +3498,7 @@ class Commands(object):
         """ Disable monitoring when package is retired. """
         pagure_anitya_status_url = '{0}/_dg/anitya/{1}/{2}'.format(
             distgit_url.rstrip('/'), self.ns, pathlib.Path(self.path).name)
-        token_str = "token {0}".format(distgit_token)
+        token_str = "token {0}".format(distgit_token.strip('"').strip("'"))
         headers = {"Authorization": token_str}
         payload = {
             'anitya_status': 'no-monitoring',
@@ -3566,6 +3566,8 @@ class Commands(object):
 
         if None not in (distgit_url, distgit_token):
             self.disable_monitoring(distgit_url, distgit_token)
+        else:
+            self.log.info("Monitoring not disabled. Disable the monitoring manually.")
 
         cmd = ['git', 'add', os.path.join(self.path, marker)]
         self._run_command(cmd, cwd=self.path)

