Skip to content

Commit

Permalink
feat(workflows/issue_checker): create issue automation
Browse files Browse the repository at this point in the history
  • Loading branch information
Wabri committed Aug 14, 2024
1 parent 6c8a264 commit af803b7
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 17 deletions.
23 changes: 8 additions & 15 deletions .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
label: OS Family
description: What operating system are you using to run the role?
options:
- RHEL (7.x, 8.x, 9.x)
Expand All @@ -48,7 +48,7 @@ body:
- type: textarea
id: python-version
attributes:
label: Python
label: Python version
description: Please provide the python version you are using
placeholder: 3.9.7
validations:
Expand All @@ -57,7 +57,7 @@ body:
- type: textarea
id: ansible-version
attributes:
label: Ansible-core
label: Ansible-core version
description: Please provide the ansible-core version you are using
placeholder: 2.12.0
validations:
Expand All @@ -66,7 +66,7 @@ body:
- type: textarea
id: reproduction
attributes:
label: Reproduction
label: How to reproduce the bug
description: Please provide a way for us to be able to reproduce the problem you ran into.
placeholder: Reproduction
validations:
Expand All @@ -88,20 +88,13 @@ body:
id: additional
attributes:
label: Additional information
description: Do you intend to submit a pr to solve this bug?
description: Do you intend to submit a **Pull Request** to solve this bug?
options:
- "Yes"
- "No"
default: 1
- I can solve this bug
- I can't solve this bug
validations:
required: true

- type: textarea
id: additonal
attributes:
label: Additional context
description: If applicable, add any other context about the problem here

- type: checkboxes
id: required-info
attributes:
Expand All @@ -110,5 +103,5 @@ body:
options:
- label: I've read [the documentation](https://github.com/sap-linuxlab/community.sap_install/tree/main/docs#readme).
required: true
- label: I've already check for existing duplicated [issues](https://github.com/sap-linuxlab/community.sap_install/issues?q=is%3Aissue+label%3Abug).
- label: I've already checked for existing duplicated [issues](https://github.com/sap-linuxlab/community.sap_install/issues?q=is%3Aissue+label%3Abug).
required: true
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/feature_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ body:
- type: dropdown
id: os-family
attributes:
label: OS
label: OS family
description: For what operating systems family is the feature you want to propose meant to be?
options:
- RHEL (7.x, 8.x, 9.x)
Expand All @@ -62,7 +62,7 @@ body:
description: Do you want to implement this feature by yourself?
options:
- I can implement this feature
- I can't implement by myself this feature
- I can't/won't implement this feature
validations:
required: true

Expand Down
22 changes: 22 additions & 0 deletions .github/workflows/issue_checker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---

name: Issue automation title filler

on:
issues:
types: [opened, edited, reopened]

jobs:
check_outdate_dependencies:
runs-on: ubuntu-latest

steps:
- name: Check out the code
uses: actions/checkout@v4

- name: Run issue check
uses: ./workflows/issue_checker
env:
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_ISSUE_NUMBER: ${{ github.event.issue.number }}
10 changes: 10 additions & 0 deletions workflows/issue_checker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM python:slim

COPY issue_checker.py /run.py
RUN chmod +x /run.py

RUN pip3 install requests; \
apt-get update; \
apt-get install -y --no-install-recommends git

ENTRYPOINT [ "/run.py" ]
7 changes: 7 additions & 0 deletions workflows/issue_checker/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---

name: 'Issue automation title filler'
description: 'This action is trigger whenever a new issue is open and it update the title with the standard'
runs:
using: 'docker'
image: 'Dockerfile'
157 changes: 157 additions & 0 deletions workflows/issue_checker/issue_checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#!/usr/bin/env python

import os
import sys
import requests
import re
import json

TOKEN = str(os.environ.get("GITHUB_TOKEN"))
REPOSITORY = str(os.environ.get("GITHUB_REPOSITORY"))
ISSUE_NUMBER = str(os.environ.get("GITHUB_ISSUE_NUMBER"))
HEADERS = {
"Authorization": f"token {TOKEN}",
"Accept": "application/vnd.github+json"
}

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
"""


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


def is_a_feature(labels):
for label in labels:
if label['name'] == 'enhancement':
return True
return False


def is_a_bug(labels):
for label in labels:
if label['name'] == 'bug':
return True
return False


def is_title_standardise(title):
regex_pattern = re.compile(ISSUE_STANDARD_TITLE)
matches = regex_pattern.findall(title)

return False if not matches else True


def post_comment_on_issue(body):
comment_data = {
"body": body
}
response = requests.post(
f"https://api.github.com/repos/{REPOSITORY}/issues/{ISSUE_NUMBER}/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}")
else:
print(f"ERROR: Failed to create comment. Status code: {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}",
headers=HEADERS,
data=json.dumps(title_data))
if response.status_code == 200:
print(f"INFO: Title updated -> https://github.com/{REPOSITORY}/issues/{ISSUE_NUMBER}")
else:
print(f"ERROR: Failed to update title. Status code: {response.status_code}.")


def title_composer(issue_descriptor):
role_body = re.search(ISSUE_ROLE_BODY, issue_descriptor['body'])
os_body = re.search(ISSUE_OS_BODY, issue_descriptor['body'])
title_body = str.split(issue_descriptor['title'], ':')

object_title = ""
description_title = ""
if title_body:
object_title = "" + str.split(title_body[0], ' ')[0]
description_title = "" + title_body[1]

role_selected = ""
if role_body:
role_selected = " " + role_body.group(1)

os_selected = ""
if os_body:
os_selected = " on " + os_body.group(1)

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'])
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)
sys.exit(1)

title = title_composer(issue_descriptor)
if title != issue_descriptor['title']:
update_title_on_issue(title)

0 comments on commit af803b7

Please sign in to comment.