forked from longhorn/bot
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create gui issue when assign require-ui label (#27)
Signed-off-by: Jack Yu <[email protected]>
- Loading branch information
Showing
6 changed files
with
170 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import abc | ||
|
||
class ActionRequest: | ||
def __init__(self): | ||
pass | ||
def setAction(self, action): | ||
self.action = action | ||
|
||
|
||
class Action(abc.ABC): | ||
# isMatched returns True if the actionRequest is matched with the action from github webook | ||
@abc.abstractmethod | ||
def isMatched(self, actionRequest): | ||
raise NotImplementedError | ||
|
||
@abc.abstractmethod | ||
def action(self, request): | ||
raise NotImplementedError | ||
|
||
class LabelAction(abc.ABC): | ||
# isMatched returns True if it meets the condition to execute the action | ||
@abc.abstractmethod | ||
def isMatched(self, request): | ||
raise NotImplementedError | ||
|
||
@abc.abstractmethod | ||
def action(self, request): | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from harvester_github_bot.label_action.create_gui_issue import CreateGUIIssue | ||
from harvester_github_bot.label_action.create_backport import CreateBackport | ||
from harvester_github_bot.action import Action | ||
|
||
ALL_LABEL_ACTIONS = [ | ||
CreateBackport, | ||
CreateGUIIssue | ||
] | ||
|
||
class ActionLabel(Action): | ||
def __init__(self): | ||
pass | ||
|
||
def isMatched(self, actionRequest): | ||
if actionRequest.action not in ['labeled']: | ||
return False | ||
return True | ||
|
||
def action(self, request): | ||
for label_action in ALL_LABEL_ACTIONS: | ||
__label_action = label_action() | ||
if __label_action.isMatched(request): | ||
__label_action.action(request) | ||
|
||
return "labeled related actions succeed" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
github-bot/harvester_github_bot/label_action/create_gui_issue.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from harvester_github_bot import repo | ||
from harvester_github_bot.action import LabelAction | ||
import re | ||
|
||
CREATE_GUI_ISSUE_LABEL = "require-ui" | ||
AREA_UI_LABEL = "area/ui" | ||
class CreateGUIIssue(LabelAction): | ||
def __init__(self): | ||
pass | ||
|
||
def isMatched(self, request): | ||
matched = False | ||
|
||
# We can't expect the labels order from Github Webhook request. | ||
# It might be ["require-ui/small", "area/ui"] or ["area/ui", "require-ui/small"] | ||
# So we need to consider those two cases. | ||
# "area/ui" is high priority label. | ||
# If "area/ui" is in the labels, we can skip the rest of the labels. | ||
for label in request['issue']['labels']: | ||
if AREA_UI_LABEL in label['name']: | ||
return False | ||
if CREATE_GUI_ISSUE_LABEL in label['name']: | ||
matched = True | ||
|
||
return matched | ||
|
||
def action(self, request): | ||
self.labels = [AREA_UI_LABEL] | ||
|
||
for label in request['issue']['labels']: | ||
if CREATE_GUI_ISSUE_LABEL not in label['name']: | ||
self.labels.append(label['name']) | ||
|
||
self.issue_number = request['issue']['number'] | ||
self.original_issue = repo.get_issue(self.issue_number) | ||
|
||
if self.__is_gui_issue_exist(): | ||
return "GUI issue already exist" | ||
self.__create_gui_issue() | ||
self.__create_comment() | ||
return "create GUI issue success" | ||
|
||
def __create_gui_issue(self): | ||
issue_data = { | ||
'title': f"[GUI] {self.original_issue.title}", | ||
'body': f"GUI Issue from #{self.issue_number}", | ||
'labels': self.labels, | ||
'assignees': self.original_issue.assignees, | ||
} | ||
if self.original_issue.milestone is not None: | ||
issue_data['milestone'] = self.original_issue.milestone | ||
self.gui_issue = repo.create_issue(**issue_data) | ||
|
||
def __create_comment(self): | ||
self.original_issue.create_comment(body=f"GUI issue created #{self.gui_issue.number}.") | ||
|
||
def __is_gui_issue_exist(self): | ||
comment_pattern = r'GUI issue created #[\d].' | ||
comments = self.original_issue.get_comments() | ||
for comment in comments: | ||
if re.match(comment_pattern, comment.body): | ||
return True | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters