Add RTD project details update support 26/62326/2 v0.28.0
authorDW Talton <dtalton@contractor.linuxfoundation.org>
Mon, 18 Nov 2019 22:39:03 +0000 (15:39 -0700)
committerDW Talton <dtalton@contractor.linuxfoundation.org>
Tue, 19 Nov 2019 00:27:38 +0000 (17:27 -0700)
Add the ability to update RTD project details from the CLI.

Issue: RELENG-2543
Signed-off-by: DW Talton <dtalton@contractor.linuxfoundation.org>
Change-Id: Ic165376cfcd7a81acb4ac602882d9d7925827c6a

lftools/api/endpoints/readthedocs.py
lftools/cli/rtd.py
lftools/deploy.py
releasenotes/notes/readthedocs-ec3b30d399730b9d.yaml [new file with mode: 0644]
tests/test_rtd.py

index f9c47a3..fed07ae 100644 (file)
@@ -46,26 +46,13 @@ class ReadTheDocs(client.RestApi):
         :param kwargs:
         :return: [projects]
         """
-        result = self.get('projects/')[1]  # NOQA
-        more_results = None
+        result = self.get('projects/?limit=999')[1]  # NOQA
         data = result['results']
         project_list = []
 
-        if result['next']:
-            more_results = result['next'].rsplit('/', 1)[-1]
-
-        if more_results:
-            while more_results is not None:
-                get_more_results = self.get('projects/' + more_results)[1]
-                data.append(get_more_results['results'])
-                more_results = get_more_results['next']
-
-                if more_results is not None:
-                    more_results = more_results.rsplit('/', 1)[-1]
-
         for project in data:
-            project_list.append(project['slug'])
-
+            if 'slug' in project:
+                project_list.append(project['slug'])
         return project_list
 
     def project_details(self, project):
@@ -144,6 +131,22 @@ class ReadTheDocs(client.RestApi):
                             data=json_data)
         return result
 
+    def project_update(self, project, *args):
+        """Update any project details.
+
+        :param project: Project's name (slug).
+        :param args: Any of the JSON keys allows by RTD API.
+        :return: Bool
+        """
+        data = args[0]
+        json_data = json.dumps(data)
+        result = self.patch('projects/{}/'.format(project), data=json_data)
+
+        if result.status_code == 204:
+            return True, result.status_code
+        else:
+            return False, result.status_code
+
     def project_create(self, name, repository_url, repository_type, homepage,
                        programming_language, language, **kwargs):
         """Create a new Read the Docs project.
@@ -227,24 +230,10 @@ class ReadTheDocs(client.RestApi):
         :param kwargs:
         :return: [subprojects]
         """
-        result = self.get('projects/{}/subprojects/'.format(project))[1]  # NOQA
-        more_results = None
+        result = self.get('projects/{}/subprojects/?limit=999'.format(project))[1]  # NOQA
         data = result['results']
         subproject_list = []
 
-        if result['next']:
-            more_results = result['next'].rsplit('/', 1)[-1]
-
-        if more_results:
-            while more_results is not None:
-                get_more_results = self.get('projects/{}/subprojects/'
-                                            .format(project) + more_results)[1]
-                data.append(get_more_results['results'])
-                more_results = get_more_results['next']
-
-                if more_results is not None:
-                    more_results = more_results.rsplit('/', 1)[-1]
-
         for subproject in data:
             subproject_list.append(subproject['child']['slug'])
 
index c5d595e..35ab11b 100644 (file)
@@ -92,6 +92,21 @@ def project_create(ctx, project_name, repository_url, repository_type,
     log.info(pformat(data))
 
 
+@click.command(name='project-update',
+               context_settings=dict(ignore_unknown_options=True,
+                                     allow_extra_args=True,))
+@click.argument('project-name')
+@click.pass_context
+def project_update(ctx, project_name):
+    """Create a new project."""
+    r = readthedocs.ReadTheDocs()
+    d = dict()
+    for item in ctx.args:
+        d.update([item.split('=')])
+    data = r.project_update(project_name, d)
+    log.info(pformat(data))
+
+
 @click.command(name='project-build-list')
 @click.argument('project-slug')
 @click.pass_context
@@ -183,6 +198,7 @@ rtd.add_command(project_create)
 rtd.add_command(project_build_list)
 rtd.add_command(project_build_details)
 rtd.add_command(project_build_trigger)
+rtd.add_command(project_update)
 rtd.add_command(subproject_list)
 rtd.add_command(subproject_details)
 rtd.add_command(subproject_create)
index ea56bed..6537e13 100755 (executable)
@@ -12,6 +12,7 @@
 
 import concurrent.futures
 import errno
+import glob
 import gzip
 import io
 import logging
@@ -26,7 +27,6 @@ import tempfile
 import zipfile
 
 from defusedxml.minidom import parseString
-import glob
 import requests
 import six
 
diff --git a/releasenotes/notes/readthedocs-ec3b30d399730b9d.yaml b/releasenotes/notes/readthedocs-ec3b30d399730b9d.yaml
new file mode 100644 (file)
index 0000000..a353d9a
--- /dev/null
@@ -0,0 +1,9 @@
+---
+features:
+  - |
+    Add the ability to update existing project's properties. This is done by invoking
+    lftools rtd project-update PROJECT_NAME key='value' where key is the name of a json
+    API key for the RTD API and value is the new value you require.
+fixes:
+  - |
+    Fixed issues with project and subproject listing.
index 5cb65b7..22d7edc 100644 (file)
@@ -151,7 +151,7 @@ def test_subproject_list(datafiles):
     json_file = open('subproject_list.json', 'r')
     json_data = json.loads(json_file.read())
     responses.add(responses.GET,
-                  url='https://readthedocs.org/api/v3/projects/TestProject1/subprojects/',  # noqa
+                  url='https://readthedocs.org/api/v3/projects/TestProject1/subprojects/?limit=999',  # noqa
                   json=json_data, status=200, match_querystring=True)
     assert 'testproject2' in rtd.subproject_list('TestProject1')