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

"This is now live" automatic notifications #715

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
53 changes: 53 additions & 0 deletions bin/lib/amazon.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ def release_for(releases, s3_key):
return None


def get_current_release(cfg: Config) -> Optional[Release]:
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
current = get_current_key(cfg)
return release_for(get_releases(), current) if current else None


def get_events_file(cfg: Config) -> str:
try:
o = s3_client.get_object(
Expand Down Expand Up @@ -386,3 +391,51 @@ def has_bouncelock_file(cfg: Config):
return True
except s3_client.exceptions.NoSuchKey:
return False


def notify_file_name():
return 'ce-notify-file'


def has_notify_file():
try:
s3_client.get_object(
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
Bucket='compiler-explorer',
Key=notify_file_name()
)
return True
except s3_client.exceptions.NoSuchKey:
return False


def put_notify_file(body: str):
s3_client.put_object(
Bucket='compiler-explorer',
Key=notify_file_name(),
Body=body,
ACL='public-read',
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
CacheControl='public, max-age=60'
)


def delete_notify_file():
s3_client.delete_object(
Bucket='compiler-explorer',
Key=notify_file_name()
)


def set_current_notify(sha: str):
if not has_notify_file():
put_notify_file(sha)


def get_current_notify():
try:
o = s3_client.get_object(
Bucket='compiler-explorer',
Key=notify_file_name()
)
return o['Body'].read().decode("utf-8")
except s3_client.exceptions.NoSuchKey:
return None
6 changes: 5 additions & 1 deletion bin/lib/cli/builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

from lib.amazon import download_release_file, download_release_fileobj, find_latest_release, find_release, \
log_new_build, set_current_key, get_ssm_param, get_all_current, get_releases, remove_release, get_current_key, \
list_all_build_logs, list_period_build_logs, put_bouncelock_file, delete_bouncelock_file, has_bouncelock_file
list_all_build_logs, list_period_build_logs, put_bouncelock_file, delete_bouncelock_file, has_bouncelock_file, \
set_current_notify
from lib.cdn import DeploymentJob
from lib.ce_utils import describe_current_release, are_you_sure, display_releases, confirm_branch, confirm_action
from lib.cli import cli
Expand Down Expand Up @@ -102,6 +103,9 @@ def builds_set_current(cfg: Config, branch: Optional[str], version: str, raw: bo
old_deploy_staticfiles(branch, to_set)
set_current_key(cfg, to_set)
if release:
if cfg.env.value == cfg.env.PROD:
print("Logging for notifications")
set_current_notify(release.hash.hash)
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
print("Marking as a release in sentry...")
token = get_ssm_param("/compiler-explorer/sentryAuthToken")
result = requests.post(
Expand Down
16 changes: 14 additions & 2 deletions bin/lib/cli/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import click

from lib.amazon import get_autoscaling_groups_for, as_client
from lib.amazon import get_autoscaling_groups_for, as_client, get_current_release, get_current_notify, get_ssm_param, \
delete_notify_file
from lib.ce_utils import are_you_sure, describe_current_release, set_update_message
from lib.cli import cli
from lib.env import Config, Environment

from lib.notify import handle_notify


@cli.group()
def environment():
Expand Down Expand Up @@ -43,14 +46,17 @@ def environment_start(cfg: Config):
help='While updating, ensure at least PERCENT are healthy', default=75, show_default=True)
@click.option('--motd', type=str, default='Site is being updated',
help='Set the message of the day used during refresh', show_default=True)
@click.option('--notify/--no-notify', help='Send GitHub notifications for newly released PRs', default=True)
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
@click.pass_obj
def environment_refresh(cfg: Config, min_healthy_percent: int, motd: str):
def environment_refresh(cfg: Config, min_healthy_percent: int, motd: str, notify: bool):
"""Refreshes an environment.

This replaces all the instances in the ASGs associated with an environment with
new instances (with the latest code), while ensuring there are some left to handle
the traffic while we update."""
set_update_message(cfg, motd)
current_release = get_current_release(cfg)

for asg in get_autoscaling_groups_for(cfg):
group_name = asg['AutoScalingGroupName']
if asg['DesiredCapacity'] == 0:
Expand Down Expand Up @@ -96,6 +102,12 @@ def environment_refresh(cfg: Config, min_healthy_percent: int, motd: str):
last_log = log
if status in ('Successful', 'Failed', 'Cancelled'):
break
if cfg.env.value == cfg.env.PROD and notify:
current_notify = get_current_notify()
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
if current_notify is not None and current_release is not None:
gh_token = get_ssm_param("/compiler-explorer/githubAuthToken")
handle_notify(current_notify, current_release.hash.hash, gh_token)
delete_notify_file()
set_update_message(cfg, '')


Expand Down
134 changes: 134 additions & 0 deletions bin/lib/notify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import urllib.request
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
import urllib.parse
import json
from typing import List

OWNER_REPO = "compiler-explorer/compiler-explorer"
USER_AGENT = "CE Live Now Notification Bot"
mattgodbolt marked this conversation as resolved.
Show resolved Hide resolved


def post(entity: str, token: str, query: dict = None, dry=False) -> dict:
if query is None:
query = {}
path = entity
querystring = json.dumps(query).encode()
print(f"Posting {path}")
req = urllib.request.Request(
f"https://api.github.com/{path}",
data=querystring,
headers={
"User-Agent": USER_AGENT,
"Authorization": f"token {token}",
"Accept": "application/vnd.github.v3+json",
},
)
if dry:
return {}
result = urllib.request.urlopen(req)
# It's ok not to check for error codes here. We'll throw either way
return json.loads(result.read())


def get(entity: str, token: str, query: dict = None) -> dict:
if query is None:
query = {}
path = entity
if query:
querystring = urllib.parse.urlencode(query)
path += f"?{querystring}"
print(f"Getting {path}")
req = urllib.request.Request(
f"https://api.github.com/{path}",
None,
{
"User-Agent": USER_AGENT,
"Authorization": f"token {token}",
"Accept": "application/vnd.github.v3+json",
},
)
result = urllib.request.urlopen(req)
# It's ok not to check for error codes here. We'll throw either way
return json.loads(result.read())


def paginated_get(entity: str, token: str, query: dict = None) -> List[dict]:
if query is None:
query = {}
result: List[dict] = []
results_per_page = 50
query["page"] = 1
query["per_page"] = results_per_page
while True:
current_page_results = get(entity, token, query)
result.extend(current_page_results)
if len(current_page_results) == results_per_page:
query["page"] += 1
else:
break
return result


def list_inbetween_commits(end_commit: str, new_commit: str, token: str) -> List[dict]:
commits = get(f"repos/{OWNER_REPO}/compare/{end_commit}...{new_commit}", token=token)
return commits["commits"]


def get_linked_pr(commit: str, token: str) -> dict:
pr = get(f"repos/{OWNER_REPO}/commits/{commit}/pulls", token=token)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this is should be a paginated get, but if we find a PR with 30 or more linked issues.. Wow

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the GraphQL query only lists the first 10, so there won't be more than that in the response, which fit in a single page

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can graphql query on label status? I think that finding results that haven't been tagged live might be safer and easier on humans to find if it goes wrong, instead of having to look inside all the comments inside each PR

return pr


def get_linked_issues(pr: str, token: str):
query = """
query {
repository(owner: "compiler-explorer", name: "compiler-explorer") {
pullRequest(number: %s) {
closingIssuesReferences(first: 10) {
edges {
node {
number
}
}
}
}
}
}
""" % pr
return post("graphql", token, {"query": query})


def get_issue_comments(issue: str, token: str) -> List[dict]:
return paginated_get(f"repos/{OWNER_REPO}/issues/{issue}/comments", token)


def comment_on_issue(issue: str, msg: str, token: str):
result = post(f"repos/{OWNER_REPO}/issues/{issue}/comments", token, {"body": msg}, dry=True)
return result


def send_live_message(issue: str, token: str):
comment_on_issue(issue, "This is now live", token=token)


def get_edges(issue: dict) -> List[dict]:
return issue["data"]["repository"]["pullRequest"]["closingIssuesReferences"]["edges"]


def handle_notify(base, new, token):
print(f'Notifying from {base} to {new}')

commits = list_inbetween_commits(base, new, token)

prs = [get_linked_pr(commit["sha"], token) for commit in commits]
ids = [pr[0]["number"] for pr in prs]
linked_issues = [get_linked_issues(pr, token) for pr in ids]
issues_ids = [get_edges(issue) for issue in linked_issues if len(get_edges(issue)) > 0]
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved

for edge in issues_ids:
for node in edge:
issue = node["node"]["number"]
comments = get_issue_comments(issue, token)
if not any(["This is now live" in comment["body"] for comment in comments]):
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
send_live_message(issue, token)
else:
print(f"Skipping notifying {issue}, it's already been done")