Skip to content

Commit

Permalink
[IMPROVEMENT] Delete and repost bot comment, remove GITHUB_BOT from c…
Browse files Browse the repository at this point in the history
…onfig (#855)

* Delete old bot comments and repost

Signed-off-by: Tarun Arora <[email protected]>

* Remove role of GITHUB_BOT in config

Signed-off-by: Tarun Arora <[email protected]>

---------

Signed-off-by: Tarun Arora <[email protected]>
  • Loading branch information
Tarun-Arora authored Aug 26, 2023
1 parent 694d34d commit 72c1846
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 28 deletions.
1 change: 0 additions & 1 deletion config_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
APPLICATION_ROOT = None
CSRF_ENABLED = True
DATABASE_URI = 'mysql+pymysql://root:@localhost:3306/test?charset=utf8'
GITHUB_BOT = ''
GITHUB_TOKEN = ''
GITHUB_OWNER = 'CCExtractor'
GITHUB_REPOSITORY = 'ccextractor'
Expand Down
15 changes: 5 additions & 10 deletions mod_ci/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1612,18 +1612,13 @@ def comment_pr(test_id, state, pr_nr, platform) -> None:
# Pull requests are just issues with code, so GitHub considers PR comments in issues
pull_request = repository.get_pull(number=pr_nr)
comments = pull_request.get_issue_comments()
bot_name = g.github['bot_name']
comment_id = None
bot_name = gh.get_user().login
for comment in comments:
if comment.user.login == bot_name and platform in comment.body:
comment_id = comment.id
comment.edit(body=message)
break
log.debug(f"GitHub PR Comment ID Fetched for Test_id: {test_id}")
if comment_id is None:
comment = pull_request.create_issue_comment(body=message)
comment_id = comment.id
log.debug(f"GitHub PR Comment ID {comment_id} Uploaded for Test_id: {test_id}")
comment.delete()
log.debug(f"GitHub PR old comment deleted for test_id: {test_id}")
comment = pull_request.create_issue_comment(body=message)
log.debug(f"GitHub PR Comment ID {comment.id} Uploaded for Test_id: {test_id}")
except Exception as e:
log.error(f"GitHub PR Comment Failed for Test_id: {test_id} with Exception {e}")

Expand Down
1 change: 0 additions & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ def get_github_config(config: Dict[str, str]) -> Dict[str, str]:
return {
'ci_key': config.get('GITHUB_CI_KEY', ''),
'bot_token': config.get('GITHUB_TOKEN', ''),
'bot_name': config.get('GITHUB_BOT', ''),
'repository_owner': config.get('GITHUB_OWNER', ''),
'repository': config.get('GITHUB_REPOSITORY', '')
}
Expand Down
1 change: 0 additions & 1 deletion tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ def load_config(file):
'SQLALCHEMY_POOL_SIZE': 1,
'GITHUB_CI_KEY': "test_ci",
'GITHUB_TOKEN': "",
'GITHUB_BOT': "",
'GITHUB_OWNER': "test_owner",
'GITHUB_REPOSITORY': "test_repo",
'HMAC_KEY': "test_key",
Expand Down
23 changes: 8 additions & 15 deletions tests/test_ci/test_controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,15 +337,16 @@ def test_comments_successfully_in_passed_pr_test(self, mock_github):
repository = mock_github(g.github['bot_token']).get_repo(
f"{g.github['repository_owner']}/{g.github['repository']}")
pull_request = repository.get_pull(number=1)
mock_github(g.github['bot_token']).get_user().login = 'test-bot'

comment1 = MagicMock(IssueComment)
comment1.user.login = 'invalid'

comment2 = MagicMock(IssueComment)
comment2.user.login = ''
comment2.user.login = 'test-bot'
comment2.body = 'linux test passed'

# When previous comment is found, and is to be edited
# Delete old bot comments and create a new comment
pull_request.get_issue_comments.return_value = [comment1, comment2]
comment_pr(1, Status.SUCCESS, 1, 'linux')
mock_github.assert_called_with(g.github['bot_token'])
Expand All @@ -354,31 +355,23 @@ def test_comments_successfully_in_passed_pr_test(self, mock_github):

repository.get_pull.assert_called_with(number=1)
pull_request.get_issue_comments.assert_called_once()
args, kwargs = comment2.edit.call_args
message = kwargs['body']
if "passed" not in message:
assert False, "Message not Correct"
comment1.delete.assert_not_called()
comment2.delete.assert_called_once()

# When commit is not found, and is to be created
pull_request.reset_mock()
pull_request.get_issue_comments.return_value = [comment1]
comment_pr(1, Status.SUCCESS, 1, 'linux')
repository.get_pull.assert_called_with(number=1)
pull_request.get_issue_comments.assert_called_once()
args, kwargs = pull_request.create_issue_comment.call_args
message = kwargs['body']
if "passed" not in message:
assert False, "Message not Correct"

@mock.patch('github.Github.get_repo')
def test_comments_successfuly_in_failed_pr_test(self, mock_repo):
@mock.patch('github.Github')
def test_comments_successfuly_in_failed_pr_test(self, mock_github):
"""Check comments in failed PR test."""
import mod_ci.controllers
reload(mod_ci.controllers)
from github.IssueComment import IssueComment

from mod_ci.controllers import Status, comment_pr
pull_request = mock_repo.return_value.get_pull(number=1)
pull_request = mock_github.return_value.get_repo.return_value.get_pull(number=1)
message = ("<b>CCExtractor CI platform</b> finished running the "
"test files on <b>linux</b>. Below is a summary of the test results")
pull_request.get_issue_comments.return_value = [MagicMock(IssueComment)]
Expand Down

0 comments on commit 72c1846

Please sign in to comment.