Skip to content

Commit

Permalink
fix(workflows/issue_checker): update linting and pep8
Browse files Browse the repository at this point in the history
  • Loading branch information
Wabri committed Aug 14, 2024
1 parent af803b7 commit a69606a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ body:
- type: dropdown
id: os-family
attributes:
label: OS Family
label: OS family
description: What operating system are you using to run the role?
options:
- RHEL (7.x, 8.x, 9.x)
Expand Down
84 changes: 35 additions & 49 deletions workflows/issue_checker/issue_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,38 @@
"Authorization": f"token {TOKEN}",
"Accept": "application/vnd.github+json"
}
URL_ISSUE = f"https://github.com/{REPOSITORY}/issues/{ISSUE_NUMBER}"
API_ISSUE = f"https://api.github.com/repos/{REPOSITORY}/issues/{ISSUE_NUMBER}"

ISSUE_STANDARD_TITLE = r'^\[Bug|Feature\](?: [\w\s]+)?(?: on [\w\s]+)?: .+$'
ISSUE_ROLE_BODY = r'### Role\s+([\w_]+)'
ISSUE_OS_BODY = r'### OS Family\s+([\w_]+)'
ISSUE_OS_BODY = r'### OS family\s+([\w_]+)'

COMMENT_FOLLOW_STANDARD = """
Hi,
COMMENT_FOLLOW_STANDARD = ("Hi,\n\n"
"The standard for the issue title should be like this:\n"
"```\n"
"[Bug|Feature]: <short_description>\n"
"```\n\n"
"Please update the issue title to follow this standard.\n\n"
"Example: `[Bug]: pacemaker stop working on sles15`\n\n"
"Thanks")

The standard for the issue title should be like this: `[Bug|Feature]: <short_description>`. Please update the issue title to follow this standard.
Example:
`[Bug]: pacemaker stop working on sles15`
Thanks
"""
COMMENT_TITLE_NOT_ALIGNED = """
Hi,
It seems that the title and the labels are not aligned. Check if the label `bug` is selected if the issue is a Bug and the label `enhancement` is selected if is a Feature.
Thanks
"""
COMMENT_MISS_SHORT_DESCRIPTION = """
Hi,
It seems that the title miss a short description.
Could you please update the tile by adding something after the issue specification?
Example:
`[Bug]: pacemaker stop working on sles15`
Thanks
"""
COMMENT_TITLE_NOT_ALIGNED = ("Hi,\n\n"
"It seems that the title and the labels are not aligned. "
"Check if the label `bug` is selected if the issue is a Bug and the "
"label `enhancement` is selected if is a Feature.\n\n"
"Thanks")


def get_issue_descriptor():
response = requests.get(
f"https://api.github.com/repos/{REPOSITORY}/issues/{ISSUE_NUMBER}",
API_ISSUE,
headers=HEADERS)
if response.status_code == 200:
print(f"INFO: Issue found -> https://github.com/{REPOSITORY}/issues/{ISSUE_NUMBER}")
print(f"INFO: Issue found -> {URL_ISSUE}")
return response.json()
else:
print(f"ERROR: Failed to update the issue. Status code: {response.status_code}.")
print(f"ERROR: Issue not found. Status code: {response.status_code}.")
return {}


Expand Down Expand Up @@ -87,27 +74,27 @@ def post_comment_on_issue(body):
"body": body
}
response = requests.post(
f"https://api.github.com/repos/{REPOSITORY}/issues/{ISSUE_NUMBER}/comments",
f"{API_ISSUE}/comments",
headers=HEADERS,
data=json.dumps(comment_data))
if response.status_code == 201:
print(f"INFO: Comment done in -> https://github.com/{REPOSITORY}/issues/{ISSUE_NUMBER}")
print(f"INFO: Comment done in -> {URL_ISSUE}")
else:
print(f"ERROR: Failed to create comment. Status code: {response.status_code}.")
print(f"ERROR: Failed to create comment with {response.status_code}.")


def update_title_on_issue(title):
title_data = {
"title": title
}
response = requests.post(
f"https://api.github.com/repos/{REPOSITORY}/issues/{ISSUE_NUMBER}",
API_ISSUE,
headers=HEADERS,
data=json.dumps(title_data))
if response.status_code == 200:
print(f"INFO: Title updated -> https://github.com/{REPOSITORY}/issues/{ISSUE_NUMBER}")
print(f"INFO: Title updated -> {URL_ISSUE}")
else:
print(f"ERROR: Failed to update title. Status code: {response.status_code}.")
print(f"ERROR: Failed to update title with {response.status_code}.")


def title_composer(issue_descriptor):
Expand All @@ -132,24 +119,23 @@ def title_composer(issue_descriptor):
return object_title + role_selected + os_selected + ":" + description_title


def is_short_description_filled(title):
return True if str.split(title, ':')[-1].strip(" ") else False

if __name__ == '__main__':
issue_descriptor = get_issue_descriptor()
if issue_descriptor:
if not is_title_standardise(issue_descriptor['title']):
post_comment_on_issue(COMMENT_FOLLOW_STANDARD)
sys.exit(1)

is_bug_not_bug = "Bug" in issue_descriptor['title'] and not is_a_bug(issue_descriptor['labels'])
is_feat_not_feat = "Feature" in issue_descriptor['title'] and not is_a_feature(issue_descriptor['labels'])
is_bug_not_bug = (
"Bug" in issue_descriptor['title'] and
not is_a_bug(issue_descriptor['labels'])
)
is_feat_not_feat = (
"Feature" in issue_descriptor['title'] and
not is_a_feature(issue_descriptor['labels'])
)
if is_bug_not_bug or is_feat_not_feat:
post_comment_on_issue(COMMENT_TITLE_NOT_ALIGNED)
sys.exit(1)

if not is_short_description_filled(issue_descriptor['title']):
post_comment_on_issue(COMMENT_MISS_SHORT_DESCRIPTION)
if not is_title_standardise(issue_descriptor['title']):
post_comment_on_issue(COMMENT_FOLLOW_STANDARD)
sys.exit(1)

title = title_composer(issue_descriptor)
Expand Down

0 comments on commit a69606a

Please sign in to comment.