Skip to content

Commit

Permalink
MRG: Merge pull request #49 from octue/enhancement/enhance-error-hand…
Browse files Browse the repository at this point in the history
…ling-if-pull-request-not-found

Improve release notes compiler error handling if pull request not found
  • Loading branch information
cortadocodes authored Nov 15, 2021
2 parents d8daaeb + 8778dbe commit eba3411
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
12 changes: 12 additions & 0 deletions conventional_commits/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import logging


formatter = logging.Formatter("[" + " | ".join(("%(asctime)s", "%(levelname)s", "%(name)s")) + "]" + " %(message)s")

handler = logging.StreamHandler()
handler.setFormatter(formatter)
handler.setLevel(logging.INFO)

logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.INFO)
26 changes: 21 additions & 5 deletions conventional_commits/compile_release_notes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import logging
import re
import subprocess

Expand All @@ -9,6 +10,9 @@
)


logger = logging.getLogger(__name__)


LAST_RELEASE = "LAST_RELEASE"
LAST_PULL_REQUEST = "LAST_PULL_REQUEST"
PULL_REQUEST_START = "PULL_REQUEST_START"
Expand Down Expand Up @@ -75,12 +79,14 @@ def __init__(

self.stop_point = stop_point.upper()

self.current_pull_request = None
self.previous_notes = None

if pull_request_url:
self.current_pull_request = self._get_current_pull_request(pull_request_url, api_token)
self.previous_notes = self.current_pull_request["body"]
else:
self.current_pull_request = None
self.previous_notes = None

if self.current_pull_request:
self.previous_notes = self.current_pull_request["body"]

self.header = header
self.list_item_symbol = list_item_symbol
Expand All @@ -93,6 +99,8 @@ def __init__(
else:
self.stop_point = LAST_RELEASE

logger.info(f"Using {self.stop_point!r} stop point.")

def compile_release_notes(self):
"""Compile the commit messages since the given stop point into a new set of release notes, sorting them into
headed sections according to their commit codes via the commit-codes-to-headings mapping. If the previous set
Expand Down Expand Up @@ -142,7 +150,15 @@ def _get_current_pull_request(self, pull_request_url, api_token):
else:
headers = {"Authorization": f"token {api_token}"}

return requests.get(pull_request_url, headers=headers).json()
response = requests.get(pull_request_url, headers=headers)

if response.status_code == 200:
return response.json()

logger.warning(
f"Pull request could not be accessed; resorting to using {LAST_RELEASE} stop point.\n"
f"{response.status_code}: {response.text}."
)

def _get_git_log(self):
"""Get the one-line decorated git log formatted in the pattern of "hash|§header|§body|§decoration@@@".
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = conventional_commits
version = 0.4.0
version = 0.4.1
description = A pre-commit hook, semantic version checker, and release note compiler for facilitating continuous deployment via Conventional Commits.
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
18 changes: 17 additions & 1 deletion tests/test_compile_release_notes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from unittest.mock import patch
from unittest.mock import Mock, patch

from conventional_commits.compile_release_notes import ReleaseNotesCompiler, main

Expand Down Expand Up @@ -673,6 +673,22 @@ def test_include_link_to_pull_request(self):

self.assertEqual(release_notes, expected)

def test_warning_raised_if_pull_request_is_not_accessible(self):
"""Test that a warning is logged and the LAST_PULL_REQUEST stop point is used if the given pull request isn't
accessible.
"""
with patch(self.GIT_LOG_METHOD_PATH, return_value=MOCK_GIT_LOG):
with patch("requests.get", return_value=Mock(status_code=404)):
with self.assertLogs() as logging_context:
ReleaseNotesCompiler(
stop_point="LAST_PULL_REQUEST",
pull_request_url="https://api.github.com/repos/octue/conventional-commits/pulls/40",
include_link_to_pull_request=True,
).compile_release_notes()

self.assertEqual(logging_context.records[0].levelname, "WARNING")
self.assertEqual(logging_context.records[1].message, "Using 'LAST_PULL_REQUEST' stop point.")


class TestMain(unittest.TestCase):
def test_cli_with_no_link_to_pull_request(self):
Expand Down

0 comments on commit eba3411

Please sign in to comment.