damccorm commented on code in PR #30345: URL: https://github.com/apache/beam/pull/30345#discussion_r1498343490
########## .github/flaky_test_detection.py: ########## @@ -0,0 +1,114 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import re +import requests +from github import Github +from github import Auth + + +GIT_ORG = "apache" +GIT_REPO = "beam" +GRAFANA_URL = "http://metrics.beam.apaache.org" +# +ALERT_NAME = "flaky_test" +READ_ONLY = os.environ.get("READ_ONLY", "false") + + +class Alert: + def __init__(self, workflow_id, workflow_url, workflow_name, workflow_filename): + self.workflow_id = workflow_id + self.workflow_url = workflow_url + self.workflow_name = workflow_name + self.workflow_file_name = workflow_filename + + +def extract_workflow_id_from_issue_label(issues): + label_ids = [] + for issue in issues: + for label in issue.get_labels(): + match = re.search(r"workflow_id:\s*(\d+)", str(label.name)) + if match: + label_id = match.group(1) + label_ids.append(label_id) + + return label_ids + + +def create_github_issue(repo, alert): + failing_runs_url = f"https://github.com/{GIT_ORG}/beam/actions/{alert.workflow_file_name}?query=is%3Afailure+branch%3Amaster" + title = f"The {alert.workflow_name} job is flaky" + body = f"The {alert.workflow_name } is constantly failing.\nPlease visit {failing_runs_url} to see the logs." + labels = ["flaky_test", f"workflow_id: {alert.workflow_id}", "bug", "P1"] + print("___") + print(f"Title: {title}") + print(f"Body: {body}") + print(f"Labels: {labels}") + print("___") + + if READ_ONLY == "true": + print("READ_ONLY is true, not creating issue") + else: + repo.create_issue(title=title, body=body, labels=labels) + + +def get_grafana_alerts(): + url = f"{GRAFANA_URL}/api/alertmanager/grafana/api/v2/alerts?active=true&filter=alertname%3D{ALERT_NAME}" + response = requests.get(url) + if response.status_code != 200: + raise RuntimeError( + "Request to %s failed with status %d: %s" + % (url, response.status_code, response.text) + ) + alerts = [] + if response.text: + for alert in response.json(): + alerts.append( + Alert( + alert["labels"]["workflow_id"], + alert["labels"]["workflow_url"], + alert["labels"]["workflow_name"], + alert["labels"]["workflow_filename"], + ) + ) + return alerts + + +def main(): + if "GITHUB_TOKEN" not in os.environ: + print("Please set the GITHUB_TOKEN environment variable.") + return + + token = os.environ["GITHUB_TOKEN"] + auth = Auth.Token(token) + g = Github(auth=auth) + repo = g.get_repo(f"{GIT_ORG}/{GIT_REPO}") + + alerts = get_grafana_alerts() + open_issues = repo.get_issues(state="open", labels=["flaky_test"]) Review Comment: Sounds good to me. > I would prefer to open a new issue every time we see deviations just to observe how the entire solution works for the Beam repo and improve it later if necessary. I think that is fine for v1; I think reopening is also helpful for the case where the assignee didn't actually successfully fix the bug, but I'm fine adding this in later if needed (shouldn't be too hard). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@beam.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org