[geany/www.geany.org] efbdab: Try to read release notes from Github releases

Enrico Tröger git-noreply at xxxxx
Wed May 1 12:33:25 UTC 2019


Branch:      refs/heads/master
Author:      Enrico Tröger <enrico.troeger at uvena.de>
Committer:   Enrico Tröger <enrico.troeger at uvena.de>
Date:        Wed, 01 May 2019 12:33:25 UTC
Commit:      efbdab7096b5fc0f6c91860d4e17f0efb913f999
             https://github.com/geany/www.geany.org/commit/efbdab7096b5fc0f6c91860d4e17f0efb913f999

Log Message:
-----------
Try to read release notes from Github releases

This gets us already pretty Markdown with issue/PR links.
If no Github release is found, fallback to our already
parsed NEWS file content.


Modified Paths:
--------------
    static_docs/github_client.py
    static_docs/views.py

Modified: static_docs/github_client.py
27 lines changed, 26 insertions(+), 1 deletions(-)
===================================================================
@@ -12,7 +12,6 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-# try to get any json implementation
 from base64 import standard_b64decode
 import logging
 
@@ -58,3 +57,29 @@ def _parse_fetch_file_response(self, response_json):
             content_utf8 = standard_b64decode(content)
             return content_utf8.decode('utf-8')
         return content
+
+    # ----------------------------------------------------------------------
+    def get_release_by_tag(self, tag_name):
+        url = 'https://api.github.com/repos/{user}/{repository}/releases/tags/{tag_name}'.format(
+            user=GITHUB_USER,
+            repository=GITHUB_REPOSITORY,
+            tag_name=tag_name)
+        with requests.get(url, timeout=HTTP_REQUEST_TIMEOUT, stream=False) as response:
+            response_json = response.json()
+            self._log_rate_limit(response)
+
+        if response.status_code == 404:
+            return None
+
+        return response_json
+
+    # ----------------------------------------------------------------------
+    def get_latest_release(self):
+        url = 'https://api.github.com/repos/{user}/{repository}/releases/latest'.format(
+            user=GITHUB_USER,
+            repository=GITHUB_REPOSITORY)
+        with requests.get(url, timeout=HTTP_REQUEST_TIMEOUT, stream=False) as response:
+            response_json = response.json()
+            self._log_rate_limit(response)
+
+        return response_json


Modified: static_docs/views.py
75 lines changed, 60 insertions(+), 15 deletions(-)
===================================================================
@@ -74,21 +74,7 @@ def get_context_data(self, **kwargs):
         release = None
 
         version = kwargs.get('version', None)
-        if version is not None:
-            # search for the requested release in the list (we could index the list into a
-            # dictionary but we need the index only at this point)
-            for rel in releases:
-                if rel.version == version:
-                    release = rel
-                    break
-            else:
-                raise Http404()
-        else:
-            # use the first element in the list which is the latest
-            release = releases[0]
-
-        # convert the selected release (the one we want to display) to Markdown
-        release.release_notes = markdown_plain(release.release_notes)
+        release = self._get_release_notes_for_version(releases, version=version)
 
         context = super(ReleaseNotesView, self).get_context_data(**kwargs)
         context['selected_release'] = release
@@ -147,6 +133,65 @@ def _parse_release_line(self, line):
         logger.warning('Failed parsing NEWS file: release line "%s" invalid', line)
         return None, None
 
+    # ----------------------------------------------------------------------
+    def _get_release_notes_for_version(self, releases, version=None):
+        """
+        If version is None: fetch the latest release from Github
+        If version is not None:
+          - check if the requested version has a release on Github and if it has release notes,
+            use it
+          - otherwise fetch the release notes from the already parsed NEWS file
+
+        Finally convert the release notes from Markdown.
+        """
+        if version is None:
+            release = self._get_release_from_github(version=None)
+        else:
+            # first search the release on Github
+            release = self._get_release_from_github(version=version)
+            if release is None:
+                # fallback to the NEWS file release notes
+                for rel in releases:
+                    if rel.version == version:
+                        release = rel
+                        break
+                else:
+                    raise Http404()
+
+        # convert the selected release (the one we want to display) to Markdown
+        release.release_notes = markdown_plain(release.release_notes)
+        return release
+
+    # ----------------------------------------------------------------------
+    @cache_function(CACHE_TIMEOUT_24HOURS)
+    def _get_release_from_github(self, version=None):
+        client = GitHubApiClient()
+        if version is None:
+            github_release = client.get_latest_release()
+        else:
+            tag_name = self._convert_version_to_tag_name(version)
+            github_release = client.get_release_by_tag(tag_name)
+
+        if github_release is None:
+            return None
+
+        # adapt date
+        release_datetime = datetime.strptime(github_release['published_at'], '%Y-%m-%dT%H:%M:%SZ')
+        release_date = release_datetime.strftime('%B %d, %Y')
+
+        release = ReleaseDto()
+        release.version = github_release['tag_name']
+        release.release_date = release_date
+        release.release_notes = github_release['body']
+        return release
+
+    # ----------------------------------------------------------------------
+    def _convert_version_to_tag_name(self, version):
+        if version.count('.') == 1:
+            return '{}.0'.format(version)
+
+        return version
+
 
 class ToDoView(StaticDocsView):
     """



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list