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



##########
File path: release/src/main/scripts/download_github_actions_artifacts.py
##########
@@ -0,0 +1,234 @@
+#
+# 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/{repo_url}/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 request_url(url, return_raw_request=False, *args, **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 get_yes_or_no_answer(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 get_yes_or_no_answer("Uhhhh... please enter ")
+
+
+def get_build_wheels_workflow_id():
+  url = GH_API_URL_WORKLOW_FMT.format(repo_url=REPO_URL)
+  data = request_url(url)
+  return data["id"]
+
+
+def get_last_run(workflow_id):

Review comment:
       The script would be easier to follow if we explicitly state the 
parameters here instead of passing them as global vars:
   ```
   def get_last_run(workflow_id, release_branch_name, commit_hash)
   ```
   Also instead of retrieving all data associated with the run and passing the 
jsons of undefied structure between functions,  consider using:
   
   ```
   def get_last_run_id(workflow_id, branch_name, commit_hash)
     """ Retrieves the latest github actions run id for the specified commit on 
the specified branch."""
   
   def get_artifacts_url(run_id):
      """Returns artifacts url associated with GitHub action run."""
     
   def wait_for_run_completion(run_id)
        """Waits for run to complete if it is in progress, and verifies it 
completed successfully."""
   ```
   If you want to save api calls, you can still nest helpers inside functions 
that use them and access api response from the outter function context.




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