Skip to content

Commit

Permalink
refactor: remove duplicated codes
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Yu <[email protected]>
  • Loading branch information
Yu-Jack committed Aug 7, 2024
1 parent 6dbb19f commit f508147
Showing 1 changed file with 4 additions and 65 deletions.
69 changes: 4 additions & 65 deletions github-bot/harvester_github_bot/action_project.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import re

from flask import render_template

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.issue_transfer import IssueTransfer
from harvester_github_bot.action import Action
from harvester_github_bot import app, gtihub_project_manager, repo, repo_test, \
ZENHUB_PIPELINE, GITHUB_OWNER, GITHUB_REPOSITORY, ZENHUB_PIPELINE, GITHUB_REPOSITORY_TEST

template_re = re.compile('---\n.*?---\n', re.DOTALL)

from harvester_github_bot import app, gtihub_project_manager, \
ZENHUB_PIPELINE, ZENHUB_PIPELINE

class ActionProject(Action):
def __init__(self):
Expand Down Expand Up @@ -45,57 +37,4 @@ def action(self, request):

it = IssueTransfer(issue["number"])
it.create_comment_if_not_exist()
it.create_e2e_issue()


class IssueTransfer:
def __init__(
self,
issue_number
):
self.__comments = None
self.__issue = repo.get_issue(issue_number)

def create_comment_if_not_exist(self):
self.__comments = self.__issue.get_comments()
found = False
for comment in self.__comments:
if comment.body.strip().startswith('## Pre Ready-For-Testing Checklist'):
app.logger.debug('pre-merged checklist already exists, not creating a new one')
found = True
break
if not found:
app.logger.debug('pre-merge checklist does not exist, creating a new one')
self.__issue.create_comment(render_template('pre-merge.md'))

def create_e2e_issue(self):
require_e2e = True
labels = self.__issue.get_labels()
for label in labels:
if label.name == 'not-require/test-plan':
require_e2e = False
break
if require_e2e:
found = False
for comment in self.__comments:
if comment.body.startswith('Automation e2e test issue:'):
app.logger.debug('Automation e2e test issue already exists, not creating a new one')
found = True
break
if not found:
app.logger.debug('Automation e2e test issue does not exist, creating a new one')

issue_link = '{}/{}#{}'.format(GITHUB_OWNER, GITHUB_REPOSITORY, self.__issue.number)
issue_test_title = '[e2e] {}'.format(self.__issue.title)
issue_test_template_content = repo_test.get_contents(
".github/ISSUE_TEMPLATE/test.md").decoded_content.decode()
issue_test_body = template_re.sub("\n", issue_test_template_content, count=1)
issue_test_body += '\nrelated issue: {}'.format(issue_link)
issue_test = repo_test.create_issue(title=issue_test_title, body=issue_test_body)

issue_test_link = '{}/{}#{}'.format(GITHUB_OWNER, GITHUB_REPOSITORY_TEST, issue_test.number)

# link test issue in Harvester issue
self.__issue.create_comment('Automation e2e test issue: {}'.format(issue_test_link))
else:
app.logger.debug('label require/automation-e2e does not exists, not creating test issue')
it.create_e2e_issue()

0 comments on commit f508147

Please sign in to comment.