def process_github_pull_request_review_requested(pullrequest: dict) -> None: """Will handle with care.""" for requested_reviewer in pullrequest["requested_reviewers"]: notify_channel( "new_pull_request_review", f"🔎 a review by " f"{google_chat_username_by_github_user(requested_reviewer['login'])}" f" has been requested for " f"Pull Request '{pullrequest['title']}'", pullrequest["html_url"], )
def process_github_pull_request_review(pullrequest: dict, review: dict) -> None: """Will handle with care.""" if review["state"] == "commented": notify_channel( "pull_request_review", f"{google_chat_username_by_github_user(review['user']['login'])} submitted a review:comment" f" for Pull Request '{pullrequest['title']}'", pullrequest["html_url"], ) elif review["state"] == "approved": add_labels(pullrequest["_links"]["issue"]["href"], ["approved"])
def handle_github_open_issue(issue: dict, repository: dict) -> None: # pragma: no cover """Will handle with care.""" _LOGGER.info(f"An Issue has been opened: {issue['url']}") if issue["title"].startswith("Automatic update of dependency"): _LOGGER.info( f"{issue['url']} is an 'automatic update of dependencies', not sending notification" ) return if issue["title"].startswith("Initial dependency lock"): _LOGGER.info( f"{issue['url']} is an 'automatic dependency lock', not sending notification" ) return if issue["title"].startswith( "Failed to update dependencies to their latest version"): _LOGGER.info( f"{issue['url']} is an 'failed to update dependencies', not sending notification" ) return notify_channel( "new_issue", f"{issue['user']['login']} just opened an issue: {issue['title']}... 🚨", issue["html_url"]) analysis = analyse_github_issue(issue) if "flake" in analysis["status"].keys(): _LOGGER.debug( f"{issue['number']} seems to be a flake: {analysis['status']['reason']}" ) repo = GitHubRepository(GitHubToken(_SESHETA_GITHUB_ACCESS_TOKEN), repository["full_name"]) try: repo.create_label("flake", "#f3ccff") repo.create_label("human_intervention_required", "#f3ccff") repo.create_label("potential_flake", "#f3ccff") except IGitt.ElementAlreadyExistsError as excptn: _LOGGER.error(excptn) igitt_issue = GitHubIssue(GitHubToken(_SESHETA_GITHUB_ACCESS_TOKEN), repository["full_name"], issue["number"]) labels = igitt_issue.labels labels.add("human_intervention_required") labels.add("potential_flake") igitt_issue.labels = labels
def process_github_pull_request_review_requested(pullrequest: dict) -> None: """Will handle with care.""" if pullrequest["title"].startswith("Automatic update of dependency" ) or pullrequest["title"].startswith( "Release of"): return for requested_reviewer in pullrequest["requested_reviewers"]: notify_channel( "new_pull_request_review", f"🔎 a review by " f"{google_chat_username_by_github_user(requested_reviewer['login'])}" f" has been requested for " f"Pull Request '{pullrequest['title']}'", pullrequest["html_url"], )
def process_github_pull_request_labeled(pullrequest: dict) -> None: """Do what needs to be done based on PR labels.""" _LOGGER.debug(f"Acting upon labels of Pull Request '{pullrequest['url']}'") if pullrequest["title"].startswith("Automatic update of dependency"): return if pullrequest["title"].startswith("Release of"): return for label in pullrequest["labels"]: if label["name"] == "needs-rebase": notify_channel( "rebase_pull_request", f"{google_chat_username_by_github_user(pullrequest['user']['login'])} please have a look " f"at pull request: '{pullrequest['title']}' it needs to be rebased.", pullrequest["html_url"], )
def process_github_open_pullrequest(pullrequest: dict) -> None: """Will handle with care.""" _LOGGER.info(f"A Pull Request has been opened: {pullrequest['url']}") if pullrequest["title"].startswith("Automatic update of dependency" ) or pullrequest["title"].startswith( "Routine Docs Update"): return if pullrequest["title"].startswith("Release of"): return notify_channel( "new_pull_request", f"{google_chat_username_by_github_user(pullrequest['user']['login'])} just " f"opened a pull request: '{pullrequest['title']}'", pullrequest["html_url"], )
def handle_prometheus_alert_webhook(): # pragma: no cover """Entry point for prometheus alert webhook.""" payload = request.json url = payload["externalURL"] if payload["status"] == "firing": alert_color = "#ff0000" else: alert_color = "#008000" notify_channel( "prometheus_alert", f"🔎 <font color='{alert_color}'>Prometheus Alert 🚨</font>: \n" f"<b>{payload['commonLabels']['alertname']}</b>" f" in instance <b>{payload['commonLabels']['instance']}</b>.\n" f"Job: <b>{payload['commonLabels']['job']}</b> \n" f"Severity: <font color='{alert_color}'>{payload['commonAnnotations']['severity']}</font>\n" f"<b>Status</b>: <font color='{alert_color}'>{payload['status']}</font>", url, ) return jsonify({"message": "thanks!"}), 200
def handle_github_open_pullrequest_merged_successfully( pullrequest: dict) -> None: # pragma: no cover """Will handle with care.""" _LOGGER.info( f"A Pull Request has been successfully merged: '{pullrequest['title']}'" ) # we simply not notify the DevOps crew about atomated dependency updates if pullrequest["title"].startswith("Automatic update of dependency"): return if pullrequest["title"].startswith("Initial dependency lock"): return # and we check if we should create a release... if pullrequest["title"].startswith("Release of"): if not eligible_release_pullrequest(pullrequest): _LOGGER.warning( f"Merged Release Pull Request: '{pullrequest['title']}', not eligible for release!" ) return commit_hash = pullrequest["merge_commit_sha"] release_issue = get_release_issue(pullrequest) # TODO this could use a try-except release = pullrequest["head"]["ref"][1:] # tag _LOGGER.info(f"Tagging release {release}: hash {commit_hash}.") tag = { "tag": str(release), "message": f"v{release}\n", "object": str(commit_hash), "type": "commit" } r = requests.post(f"{pullrequest['base']['repo']['url']}/git/tags", headers=_GIT_API_REQUEST_HEADERS, data=json.dumps(tag)) if r.status_code == 201: tag_sha = r.json()["sha"] tag_ref = {"ref": f"refs/tags/{release}", "sha": f"{tag_sha}"} requests.post( f"{pullrequest['base']['repo']['url']}/git/refs", headers=_GIT_API_REQUEST_HEADERS, data=json.dumps(tag_ref), ) # comment on issue _LOGGER.info( f"Commenting on {release_issue} that we tagged {release} on hash {commit_hash}." ) comment = { "body": f"I have tagged commit " f"[{commit_hash}](https://github.com/thoth-station/srcops-testing/commit/{commit_hash}) " f"as release {release} :+1:" } requests.post( f"{pullrequest['base']['repo']['url']}/issues/{release_issue}/comments", headers=_GIT_API_REQUEST_HEADERS, data=json.dumps(comment), ) # close issue _LOGGER.info(f"Closing {release_issue}.") requests.patch( f"{pullrequest['base']['repo']['url']}/issues/{release_issue}", headers=_GIT_API_REQUEST_HEADERS, data=json.dumps({"state": "closed"}), ) if not _DRY_RUN: notify_channel( "new_tag", f" I have tagged {commit_hash} to be release {release} of" f" {pullrequest['base']['repo']['full_name']} " + random_positive_emoji2(), pullrequest["url"], ) # happy! 💕 else: # otherwise we notify of merge if not _DRY_RUN: notify_channel( "merged_pull_request", random_positive_emoji2() + f" Pull Request '{pullrequest['title']}' was successfully " f"merged into '{pullrequest['base']['repo']['full_name']}' ", pullrequest["url"], ) return