Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove support for post_pr_comment #49

Merged
merged 2 commits into from
Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

* New API to post checks on GitHub. [#45]

* Remove support for ``post_pr_comment`` - instead we now support exclusively
status checks. [#49]

0.2 (2018-11-22)
----------------

Expand Down
115 changes: 24 additions & 91 deletions baldrick/plugins/github_pull_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ def process_pull_request(repository, number, installation):
if not pr_config.get("enabled", False):
return "Skipping PR checks, disabled in config."

post_comment = pr_config.get("post_pr_comment", False)
pull_request_substring = pr_config.get('pull_request_substring', '')

# Disable if the config is not present
if pr_config is None:
return
Expand All @@ -89,21 +86,6 @@ def process_pull_request(repository, number, installation):
repo_handler = RepoHandler(pr_handler.head_repo_name,
pr_handler.head_branch, installation)

def is_previous_comment(message):
if len(pull_request_substring) > 0:
return pull_request_substring in message
else:
return True

# Find previous comments by this app
comment_ids = pr_handler.find_comments(f'{current_app.bot_username}[bot]',
filter_keep=is_previous_comment)

if len(comment_ids) == 0:
comment_id = None
else:
comment_id = comment_ids[-1]

# First check whether there are labels that indicate the checks should be
# skipped

Expand All @@ -112,12 +94,6 @@ def is_previous_comment(message):

for label in pr_handler.labels:
if label in skip_labels:
if post_comment:
skip_message = pr_config.get("skip_message", "Pull request checks have "
"been skipped as this pull request has been "
f"labelled as **{label}**")
skip_message = skip_message.format(pr_handler=pr_handler, repo_handler=repo_handler)
pr_handler.submit_comment(skip_message, comment_id=comment_id)
if skip_fails:
pr_handler.set_status('failure', "Skipping checks due to {0} label".format(label), current_app.bot_username)
return
Expand All @@ -129,81 +105,38 @@ def is_previous_comment(message):
if result is not None:
results.update(result)

failures = [details['description'] for details in results.values() if details['state'] in ('error', 'failure')]

if post_comment:

# Post all failures in a comment, and have a single status check

if failures:

pull_request_prologue = pr_config.get('fail_prologue', '')
pull_request_epilogue = pr_config.get('fail_epilogue', '')

fail_status = pr_config.get('fail_status', 'Failed some checks')

message = pull_request_prologue.format(pr_handler=pr_handler, repo_handler=repo_handler)
for failure in failures:
message += f'* {failure}\n'
message += pull_request_epilogue.format(pr_handler=pr_handler, repo_handler=repo_handler)
comment_url = pr_handler.submit_comment(message, comment_id=comment_id, return_url=True)
# Post each failure as a status

pr_handler.set_status('failure', fail_status, current_app.bot_username, target_url=comment_url)
existing_statuses = pr_handler.list_statuses()

else:
for context, details in sorted(results.items()):

pass_status = pr_config.get('pass_status', 'Passed all checks')
full_context = current_app.bot_username + ':' + context

all_passed_message = pr_config.get('all_passed_message', '')
all_passed_message = all_passed_message.format(pr_handler=pr_handler, repo_handler=repo_handler)
# NOTE: we could in principle check if the status has been posted
# before, and if so not post it again, but we had this in the past
# and there were some strange caching issues where GitHub would
# return old status messages, so we avoid doing that.

if all_passed_message:
pr_handler.submit_comment(all_passed_message, comment_id=comment_id)
pr_handler.set_status(details['state'], details['description'],
full_context,
target_url=details.get('target_url'))

pr_handler.set_status('success', pass_status, current_app.bot_username)
# For statuses that have been skipped this time but existed before, set
# status to pass and set message to say skipped

else:

# Post each failure as a status

existing_statuses = pr_handler.list_statuses()

for context, details in sorted(results.items()):

full_context = current_app.bot_username + ':' + context

# NOTE: we could in principle check if the status has been posted
# before, and if so not post it again, but we had this in the past
# and there were some strange caching issues where GitHub would
# return old status messages, so we avoid doing that.
for full_context in existing_statuses:

pr_handler.set_status(details['state'], details['description'],
full_context,
target_url=details.get('target_url'))

# For statuses that have been skipped this time but existed before, set
# status to pass and set message to say skipped

for full_context in existing_statuses:

if full_context.startswith(current_app.bot_username + ':'):
context = full_context[len(current_app.bot_username) + 1:]
if context not in results:
pr_handler.set_status('success', 'This check has been skipped',
current_app.bot_username + ':' + context)

# Also set the general 'single' status check as a skipped check if it
# is present
if full_context == current_app.bot_username:
if full_context.startswith(current_app.bot_username + ':'):
context = full_context[len(current_app.bot_username) + 1:]
if context not in results:
pr_handler.set_status('success', 'This check has been skipped',
current_app.bot_username)

# If a comment has been posted before, and to be careful only if it is
# a comment that matches the specified substring, we edit the comment.
if len(pull_request_substring) > 0 and len(comment_ids) > 0:
for comment_id in comment_ids:
pr_handler.submit_comment(f'Check results are now reported in the '
'status checks at the bottom of this page.',
comment_id=comment_id)
current_app.bot_username + ':' + context)

# Also set the general 'single' status check as a skipped check if it
# is present
if full_context == current_app.bot_username:
pr_handler.set_status('success', 'This check has been skipped',
current_app.bot_username)

return 'Finished pull requests checks'
Loading