Skip to content

Commit

Permalink
add default template check
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhaoYangyang0403 committed Sep 25, 2023
1 parent 2958824 commit b5e1612
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
8 changes: 4 additions & 4 deletions lyrebird_bugit/event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def on_upload_files(msg):


def on_notice(msg):
conf_autoissue = application.config.get('plugin.bugit.autoissue', False)
if not conf_autoissue:
return
template_loader.notice_handler(msg)
sender_file = msg.get('sender', {}).get('file', '')
autoissue_checker = application.config.get('event.notice.autoissue.checker', [])
if sender_file in autoissue_checker:
template_loader.notice_handler(msg)
49 changes: 36 additions & 13 deletions lyrebird_bugit/template_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

logger = get_logger()

default_template_ready = None

def get_workspace():
bugit_workspace = application.config.get('bugit.workspace')
Expand All @@ -27,14 +28,19 @@ def get_workspace():
def get_default_template_path():

Check notice

Code scanning / CodeQL

Explicit returns mixed with implicit (fall through) returns Note

Mixing implicit and explicit returns may indicate an error as implicit returns always return None.
bugit_workspace = application.config.get('bugit.workspace', '')
bugit_default_template = application.config.get('bugit.default_template', '')
if default_template_ready is None:
default_template_check(bugit_workspace, bugit_default_template)
template_path = Path(bugit_workspace + bugit_default_template)
if bugit_workspace and bugit_default_template and template_path.exists():
if bugit_default_template:
return template_path


def template_list():
template_list = []
default_template_path = get_default_template_path()
if not default_template_ready:
return template_list

for template_file in get_workspace().iterdir():
if not template_file.name.endswith('.py'):
continue
Expand Down Expand Up @@ -68,26 +74,43 @@ def template_check(template):
template.submit), "BugIt template should have submit function"


def default_template_check(workspace, default_template):
global default_template_ready

template_path = Path(workspace + default_template)
if not template_path.exists():
logger.error('Default template path is not existed.')
default_template_ready = False
return

if application.config.get('event.notice.autoissue.checker'):
if not default_template:
logger.error('Default template is not configured.')
default_template_ready = False
return
template = get_template(template_path)
if not (hasattr(template, 'auto_issue_handler') and callable(template.auto_issue_handler)):
logger.error('BugIt template should have auto_issue_handler function.')
default_template_ready = False
return

default_template_ready = True


def get_template(file_path):
template = imp.load_source(Path(file_path).stem, str(file_path))
return template


def notice_handler(msg):
# Filter out messages with invalid types
if not isinstance(msg, dict):
return

# Filter out messages from unconfigured extensions
checker_switch = application.config.get('plugin.bugit.autoissue.checker_switch', {})
sender_file = msg.get('sender', {}).get('file', '')
if sender_file not in checker_switch:
default_template_path = get_default_template_path()

if not default_template_ready:
return

default_template_path = get_default_template_path()
if default_template_path is None:
logger.error(f'Init Auto Issue Server Failed. Template path is configured incorrectly: {default_template_path}')
# Filter out messages with invalid types
if not isinstance(msg, dict):
return

template = get_template(default_template_path)
template.AutoIssue().auto_issue_handler(msg)
template.auto_issue_handler(msg)

0 comments on commit b5e1612

Please sign in to comment.