Skip to content

Commit

Permalink
Showing 4 changed files with 73 additions and 21 deletions.
2 changes: 1 addition & 1 deletion biosimulators_utils/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.18'
__version__ = '0.1.19'
44 changes: 30 additions & 14 deletions biosimulators_utils/gh_action/core.py
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
:License: MIT
"""

from .data_model import Comment, GitHubActionCaughtError
import abc
import functools
import io
@@ -17,15 +18,9 @@
__all__ = [
'GitHubAction',
'GitHubActionErrorHandling',
'GitHubActionCaughtError',
]


class GitHubActionCaughtError(Exception):
""" An error caught during the execution of a GitHub action """
pass # pragma: no cover


class GitHubActionErrorHandling(object):
""" Methods for handing errors in the execution of GitHu actions """
@classmethod
@@ -83,8 +78,10 @@ def get_uncaught_exception_msg(exception):
Returns:
:obj:`str`: error message to display to users
"""
return ('Sorry. We encountered an unexpected error. Our team will review the error.' +
'\n\n ' + str(exception).replace('\n', '\n '))
return [
Comment(text='Sorry. We encountered an unexpected error. Our team will review the error.'),
Comment(str(exception), error=True),
]


class GitHubAction(abc.ABC):
@@ -234,24 +231,43 @@ def add_comment_to_issue(cls, issue_number, comment):
response.raise_for_status()

@classmethod
def add_error_comment_to_issue(cls, issue_number, comment, raise_error=True):
def add_error_comment_to_issue(cls, issue_number, comments, raise_error=True):
""" Post an error to the GitHub issue
Args:
issue_number (:obj:`str`): issue number
comment (:obj:`str`): comment
comments (:obj:`list` of :obj:`Comment`): comment
raise_error (:obj:`bool`, optional): if :obj:`True`, raise error
Raises:
:obj:`ValueError`
"""
cls.add_comment_to_issue(issue_number, ''.join([
formatted_text = []
for comment in comments:
if comment.error:
formatted_text.append(cls.format_error_comment(comment.text))
else:
formatted_text.append(comment.text)

cls.add_comment_to_issue(issue_number, '\n\n'.join(formatted_text))
if raise_error:
raise GitHubActionCaughtError('\n\n'.join(comment.text for comment in comments))

@classmethod
def format_error_comment(cls, comment):
""" Format comment to display as error
Args:
comment (:obj:`str`): comment to format as error
Returns:
:obj:`str`: formatted comment
"""
return ''.join([
'```diff\n',
'- ' + comment.rstrip().replace('\n', '\n- ') + '\n',
'```\n',
]))
if raise_error:
raise GitHubActionCaughtError(comment)
])

@classmethod
def close_issue(cls, issue_number):
35 changes: 35 additions & 0 deletions biosimulators_utils/gh_action/data_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
""" Data model for GitHub action workflows
:Author: Jonathan Karr <karr@mssm.edu>
:Date: 2020-12-08
:Copyright: 2020, Center for Reproducible Biomedical Modeling
:License: MIT
"""

__all__ = [
'GitHubActionCaughtError',
'Comment',
]


class GitHubActionCaughtError(Exception):
""" An error caught during the execution of a GitHub action """
pass # pragma: no cover


class Comment(object):
""" A comment on an issue
Attributes:
text (:obj:`str`): text
error (:obj:`bool`): if :obj:`True`, format as error
"""

def __init__(self, text=None, error=None):
"""
Args:
text (:obj:`str`): text
error (:obj:`bool`): if :obj:`True`, format as error
"""
self.text = text
self.error = error
13 changes: 7 additions & 6 deletions tests/gh_action/test_gh_action_core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from biosimulators_utils.gh_action.core import GitHubAction, GitHubActionErrorHandling, GitHubActionCaughtError
from biosimulators_utils.gh_action.core import GitHubAction, GitHubActionErrorHandling
from biosimulators_utils.gh_action.data_model import Comment, GitHubActionCaughtError
from unittest import mock
import os
import unittest
@@ -86,7 +87,7 @@ def requests_method(url, auth, headers, json):
return mock.Mock(raise_for_status=lambda: None)
with mock.patch('requests.post', side_effect=requests_method):
with self.assertRaises(GitHubActionCaughtError):
action.add_error_comment_to_issue('4', 'xxxx')
action.add_error_comment_to_issue('4', [Comment(text='xxxx', error=True)])

def requests_method(url, auth, json):
self.assertEqual(url, 'https://api.github.com/repos/biosimulators/Biosimulators/issues/4')
@@ -155,11 +156,11 @@ def requests_method(self, url, headers, auth, json):
else:
self.unittest_self.assertEqual(url, 'https://api.github.com/repos/biosimulators/Biosimulators/issues/4/comments')
print(json['body'])
self.unittest_self.assertEqual(json['body'], (
self.unittest_self.assertEqual(json['body'], (
'Sorry. We encountered an unexpected error. Our team will review the error.\n'
'\n'
'```diff\n'
'- Sorry. We encountered an unexpected error. Our team will review the error.\n'
'- \n'
'- Details about error\n'
'- Details about error\n'
'```\n'))
self.unittest_self.assertEqual(auth, (env['GH_ISSUES_USER'], env['GH_ISSUES_ACCESS_TOKEN']))
return mock.Mock(raise_for_status=lambda: None)

0 comments on commit 9b820ef

Please sign in to comment.