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 e621074
Showing 1 changed file with 41 additions and 40 deletions.
81 changes: 41 additions & 40 deletions workflows/issue_checker/issue_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,45 @@
"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_]+)'

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

COMMENT_MISS_SHORT_DESCRIPTION = "Hi,\n\n" \
"It seems that the title miss a short description. " \
"Could you please update the tile by adding something after the issue "\
"specification?\n\n" \
"Example: `[Bug]: pacemaker stop working on sles15`\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 +81,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 Down Expand Up @@ -135,15 +129,22 @@ def title_composer(issue_descriptor):
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)
Expand Down

0 comments on commit e621074

Please sign in to comment.