tvalentyn commented on a change in pull request #12150: URL: https://github.com/apache/beam/pull/12150#discussion_r457775629
########## File path: release/src/main/scripts/download_github_actions_artifacts.py ########## @@ -0,0 +1,314 @@ +# +# 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 pprint +import shutil +import sys +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_API_URL_WORKFLOW_RUN_FMT = "https://api.github.com/repos/{repo_url}/actions/runs/{run_id}" +GH_WEB_URL_WORKLOW_RUN_FMT = "https://github.com/{repo_url}/actions/runs/{run_id}" + + +def parse_arguments(): + """ + Gets all neccessary data from the user by parsing arguments or asking for input. + Return: github_token, user_github_id, repo_url, release_branch, release_commit, artifacts_dir + """ + parser = argparse.ArgumentParser( + description= + "Script for downloading GitHub Actions artifacts from 'Build python wheels' workflow." + ) + 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() + github_token = ask_for_github_token() + + print("You passed following arguments:") + pprint.pprint({**vars(args), **{"github_token": github_token}}) + + if not get_yes_or_no_answer("Do you want to continue?"): + print("You said NO. Quitting ...") + sys.exit(1) + + 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 + + return github_token, user_github_id, repo_url, release_branch, release_commit, artifacts_dir + + +def ask_for_github_token(): + """Ask for github token and print basic information about it.""" + url = "https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token" + message = ( + f"You need to have a github access token with public_repo scope. " + f"More info about creating access tokens can be found here {url}") + print(message) + github_token = input("Enter github token: ") + if not github_token: + return ask_for_github_token() + return github_token + + +def request_url(url, github_token, return_json=True, *args, **kwargs): + """Helper function form making requests authorized by GitHub token.""" + r = requests.get(url, *args, auth=("token", github_token), **kwargs) + if return_json: + r.raise_for_status() + return r.json() + return r + + +def safe_get(data, key, url=None): + """Gets data by the key with informative Exception in case of non existent key.""" + if key not in data: + message = f'There is missing key: "{key}" in response data: {data}.' + if url: + message += f" Requested url: {url}" + raise ValueError(message) + return data.get(key) + + +def get_yes_or_no_answer(question): + """Asks yes or no question.""" + reply = str(input(question + " 'y' or 'n'): ")).lower().strip() + if reply == "y": + return True + elif reply == "n": + return False + else: + return get_yes_or_no_answer("Uhhhh... please enter") + + +def get_build_wheels_workflow_id(repo_url, github_token): + """Gets workflow id.""" + url = GH_API_URL_WORKLOW_FMT.format(repo_url=repo_url) + data = request_url(url, github_token) + return safe_get(data, "id", url) + + +def get_single_workflow_run_data(run_id, repo_url, github_token): + """Gets single workflow run data (github api payload).""" + url = GH_API_URL_WORKFLOW_RUN_FMT.format(repo_url=repo_url, run_id=run_id) + return request_url(url, github_token) + + +def get_last_run_id( + workflow_id, repo_url, release_branch, release_commit, github_token): + """ + Gets id of last run for given workflow, repo, branch and commit. + Raises exception when no run found. + """ + url = GH_API_URL_WORKFLOW_RUNS_FMT.format( + repo_url=repo_url, workflow_id=workflow_id) + data = request_url( + url, + github_token, + params={ + "event": "push", "branch": release_branch + }, + ) + runs = safe_get(data, "workflow_runs", url) + + filtered_commit_runs = [ + r for r in runs if r.get("head_sha", "") == release_commit + ] + + if not filtered_commit_runs: + workflow_run_web_url = GH_API_URL_WORKFLOW_RUNS_FMT.format( + repo_url=repo_url, workflow_id=workflow_id) + raise Exception( + f"No runs for workflow (branch {release_branch}, commit {release_commit}). Verify at {workflow_run_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_run_web_url = GH_WEB_URL_WORKLOW_RUN_FMT.format( + repo_url=repo_url, run_id=last_run["id"]) + print(f"Verify at {workflow_run_web_url}") + print( + f"Upload to GCS available at: gs://beam-wheels-staging/{release_branch}/{release_commit}-{workflow_id}/" Review comment: How about: ``` GCS location corresponding to artifacts built in this run: ``` Also at the end of GCS path do we have workflow_id or last_run["id"] ? ########## File path: release/src/main/scripts/build_release_candidate.sh ########## @@ -205,7 +205,7 @@ if [[ $confirmation = "y" ]]; then sha512sum -c apache-beam-${RELEASE}.tar.gz.sha512 echo "-----Signing Source Release apache-beam-${RELEASE}.tar.gz-----" - gpg --local-user ${SIGNING_KEY} --armor --detach-sig apache-beam-${RELEASE}.zip + gpg --local-user ${SIGNING_KEY} --armor --detach-sig apache-beam-${RELEASE}.tar.gz Review comment: Was there a change from .zip to .tar .gz for the source archive? See: https://dist.apache.org/repos/dist/dev/beam/2.23.0/python/ ---------------------------------------------------------------- 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