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

ART-7802 Monitor PR status; notify on merge/close; timeout after 1 week #153

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions artbotlib/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"Failure": "failure"
}

ONE_WEEK = 7 * 24 * 60 * 60

TWELVE_HOURS = 60 * 60 * 12

FIVE_MINUTES = 60 * 5
Expand Down
64 changes: 64 additions & 0 deletions artbotlib/pr_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import datetime
import logging
import time

import requests

from artbotlib import constants

logger = logging.getLogger(__name__)


def pr_status(so, user_id, repo, pr_id):
so.say(f'Ok <@{user_id}>, I\'ll respond here when the PR merges')

pr_url = f'https://github.com/openshift/{repo}/pull/{pr_id}'
api_endpoint = f'{constants.GITHUB_API_OPENSHIFT}/{repo}/pulls/{pr_id}'

start = time.time()
while True:
# Timeout after 12 hrs
if time.time() - start > constants.ONE_WEEK:
so.say(f'PR {pr_url} did not merge after a week.'
f'Giving up...')
return

try:
# Fetch API for PR
logger.info('Fetching PR info from %s', api_endpoint)

data = requests.get(api_endpoint).json()
pr_state = data['state']

# Check PR state
if pr_state == 'open':
logger.info('PR %s is still open. Sleeping for 5 minutes...', pr_url)
time.sleep(constants.FIVE_MINUTES)

else:
logger.info('PR %s has status %s', pr_url, pr_state)

if data['merged_at']:
# PR was merged
dto = datetime.datetime.strptime(data['merged_at'], '%Y-%m-%dT%H:%M:%SZ')
so.say(f'PR {pr_url} was merged at '
f'{datetime.datetime.strftime(dto, "%b %d, %Y at %H:%M:%S")}')
else:
# PR was closed without merging
dto = datetime.datetime.strptime(data['closed_at'], '%Y-%m-%dT%H:%M:%SZ')
so.say(f'PR {pr_url} was closed unmerged at '
f'{datetime.datetime.strftime(dto, "%b %d, %Y at %H:%M:%S")}')

# All done
return

except requests.exceptions.ConnectionError as e:
logger.error('Error fetching data from %s:\n%s', api_endpoint, e)
so.say('Sorry, something went wrong when fetching data for %s', pr_url)
return

except KeyError:
msg = f'Error retrieving PR status from {api_endpoint}'
logger.error(msg)
so.say(msg)
return
7 changes: 7 additions & 0 deletions artbotlib/regex_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from artbotlib import brew_list, elliott
from artbotlib.buildinfo import buildinfo_for_release, alert_on_build_complete
from artbotlib.pr_status import pr_status
from artbotlib.taskinfo import alert_on_task_complete
from artbotlib.constants import PROW_BASE_URL
from artbotlib.help import greet_user, show_help
Expand Down Expand Up @@ -196,6 +197,12 @@ def map_command_to_regex(so, plain_text, user_id):
"flag": re.I,
"function": first_prow_job_succeeds,
"user_id": True
},
{
"regex": r"^Watch \s*(https://)*(github.com/)*(openshift/)*(?P<repo>[a-zA-Z0-9-]+)(/pull/)(?P<pr_id>\d+)",
locriandev marked this conversation as resolved.
Show resolved Hide resolved
"flag": re.I,
"function": pr_status,
"user_id": True
}
]

Expand Down