TobKed commented on a change in pull request #12150:
URL: https://github.com/apache/beam/pull/12150#discussion_r450804136



##########
File path: release/src/main/scripts/download_github_actions_artifacts.py
##########
@@ -0,0 +1,231 @@
+#
+# 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.
+#
+
+"""Script for downloading GitHub Actions artifacts from 'Build python wheels' 
workflow."""
+import argparse
+import itertools
+import os
+import shutil
+import tempfile
+import time
+import zipfile
+
+import dateutil.parser
+import requests
+
+GH_API_URL_WORKLOW_FMT = (
+    
"https://api.github.com/repos/{repo_url}/actions/workflows/build_wheels.yml";
+)
+GH_API_URL_WORKFLOW_RUNS_FMT = (
+    
"https://api.github.com/repos/{repo_url}/actions/workflows/{workflow_id}/runs";
+)
+GH_WEB_URL_WORKLOW_RUN_FMT = 
"https://github.com/TobKed/beam/actions/runs/{workflow_id}";
+
+
+def parse_arguments():
+  parser = argparse.ArgumentParser(
+      description=
+      "Script for downloading GitHub Actions artifacts from 'Build python 
wheels' workflow."
+  )
+  parser.add_argument("--github-token", required=True)
+  parser.add_argument("--github-user", required=True)
+  parser.add_argument("--repo-url", required=True)
+  parser.add_argument("--release-branch", required=True)
+  parser.add_argument("--release-commit", required=True)
+  parser.add_argument("--artifacts_dir", required=True)
+
+  args = parser.parse_args()
+
+  global GITHUB_TOKEN, USER_GITHUB_ID, REPO_URL, RELEASE_BRANCH, 
RELEASE_COMMIT, ARTIFACTS_DIR
+  GITHUB_TOKEN = args.github_token
+  USER_GITHUB_ID = args.github_user
+  REPO_URL = args.repo_url
+  RELEASE_BRANCH = args.release_branch
+  RELEASE_COMMIT = args.release_commit
+  ARTIFACTS_DIR = args.artifacts_dir
+
+
+def requester(url, *args, return_raw_request=False, **kwargs):
+  """Helper function form making requests authorized by GitHub token"""
+  r = requests.get(url, *args, auth=("token", GITHUB_TOKEN), **kwargs)
+  r.raise_for_status()
+  if return_raw_request:
+    return r
+  return r.json()
+
+
+def yes_or_no(question):
+  """Helper function to ask yes or no question"""
+  reply = str(input(question + " (y/n): ")).lower().strip()
+  if reply == "y":
+    return True
+  if reply == "n":
+    return False
+  else:
+    return yes_or_no("Uhhhh... please enter ")
+
+
+def get_build_wheels_workflow_id():
+  url = GH_API_URL_WORKLOW_FMT.format(repo_url=REPO_URL)
+  data = requester(url)
+  return data["id"]
+
+
+def get_last_run(workflow_id):
+  url = GH_API_URL_WORKFLOW_RUNS_FMT.format(
+      repo_url=REPO_URL, workflow_id=workflow_id)
+  event_types = ["push", "pull_request"]
+  runs = []
+  for event in event_types:
+    data = requester(
+        url,
+        params={
+            "event": event, "branch": RELEASE_BRANCH
+        },
+    )
+    runs.extend(data["workflow_runs"])
+
+  filtered_commit_runs = list(
+      filter(lambda w: w.get("head_sha", "") == RELEASE_COMMIT, runs))
+  if not filtered_commit_runs:
+    workflow_web_url = GH_WEB_URL_WORKLOW_RUN_FMT.format(
+        workflow_id=workflow_id)
+    raise Exception(f"No runs for workflow. Verify at {workflow_web_url}")
+
+  sorted_runs = sorted(
+      filtered_commit_runs,
+      key=lambda w: dateutil.parser.parse(w["created_at"]),
+      reverse=True,
+  )
+  last_run = sorted_runs[0]
+  print(
+      f"Found last run. SHA: {RELEASE_COMMIT}, created_at: 
'{last_run['created_at']}', id: {last_run['id']}"
+  )
+  workflow_web_url = GH_WEB_URL_WORKLOW_RUN_FMT.format(
+      workflow_id=last_run["id"])
+  print(f"Verify at {workflow_web_url}")
+  print(
+      f"Optional upload to GCS will be available at:\n"
+      
f"\tgs://beam-wheels-staging/{RELEASE_BRANCH}/{RELEASE_COMMIT}-{workflow_id}/"
+  )
+  return last_run
+
+
+def validate_run(run_data):
+  status = run_data["status"]
+  conclusion = run_data["conclusion"]
+  if status == "completed" and conclusion == "success":
+    return run_data
+
+  url = run_data["url"]
+  workflow_web_url = GH_WEB_URL_WORKLOW_RUN_FMT.format(
+      workflow_id=run_data["id"])
+  print(
+      f"Waiting for Workflow run {run_data['id']} to finish. Check on 
{workflow_web_url}"

Review comment:
       When I looked at the terminal it seems to be logical:
   1. waiting for the workflow run to finish.
   2. continue script or stop with descriptive message.
   
   WDYT?




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to