|
| 1 | +# Copyright 2025 The WPT Dashboard Project. All rights reserved. |
| 2 | +# Use of this source code is governed by a BSD-style license that can be |
| 3 | +# found in the LICENSE file. |
| 4 | + |
| 5 | +""" |
| 6 | +This script requires the following environment variables to be set: |
| 7 | +
|
| 8 | +GIT_CHECK_PR_STATUS_TOKEN: A GitHub personal access token with permissions to |
| 9 | + update pull request statuses. |
| 10 | +REPO_OWNER: The owner of the GitHub repository (e.g., "owner_name"). |
| 11 | +REPO_NAME: The name of the GitHub repository (e.g., "repo_name"). |
| 12 | +PR_NUMBER: The number of the pull request. |
| 13 | +
|
| 14 | +Please ensure these variables are configured before running the script. |
| 15 | +""" |
| 16 | + |
| 17 | +import os |
| 18 | +import requests |
| 19 | +from time import time |
| 20 | +from google.cloud import storage |
| 21 | + |
| 22 | +DEFAULT_TIMEOUT = 600.0 |
| 23 | +BUCKET_NAME = 'wpt-versions' |
| 24 | +NEW_REVISION_FILE = 'pinned_chromium_revision_NEW' |
| 25 | +OLD_REVISION_FILE = 'pinned_chromium_revision' |
| 26 | +PLATFORM_INFO = [ |
| 27 | + ("Win_x64", "chrome-win.zip"), |
| 28 | + ("Win", "chrome-win.zip"), |
| 29 | + ("Linux_x64", "chrome-linux.zip"), |
| 30 | + ("Mac", "chrome-mac.zip") |
| 31 | +] |
| 32 | +SNAPSHOTS_PATH = "https://storage.googleapis.com/chromium-browser-snapshots/" |
| 33 | + |
| 34 | + |
| 35 | +def trigger_ci_tests() -> str | None: |
| 36 | + # Reopen the PR to run the CI tests. |
| 37 | + s = requests.Session() |
| 38 | + s.headers.update({ |
| 39 | + "Authorization": f"token {get_token()}", |
| 40 | + # Specified API version. See https://docs.github.com/en/rest/about-the-rest-api/api-versions |
| 41 | + "X-GitHub-Api-Version": "2022-11-28", |
| 42 | + }) |
| 43 | + repo_owner = os.environ["REPO_OWNER"] |
| 44 | + repo_name = os.environ["REPO_NAME"] |
| 45 | + pr_number = os.environ["PR_NUMBER"] |
| 46 | + url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/pulls/{pr_number}" |
| 47 | + |
| 48 | + response = s.patch(url, data='{"state": "closed"}') |
| 49 | + if response.status_code != 200: |
| 50 | + return f'Failed to close PR {pr_number}' |
| 51 | + |
| 52 | + response = s.patch(url, data='{"state": "open"}') |
| 53 | + if response.status_code != 200: |
| 54 | + return f'Failed to open PR {pr_number}' |
| 55 | + |
| 56 | + |
| 57 | +def get_token() -> str | None: |
| 58 | + """Get token to check on the CI runs.""" |
| 59 | + return os.environ["GIT_CHECK_PR_STATUS_TOKEN"] |
| 60 | + |
| 61 | + |
| 62 | +def get_start_revision() -> int: |
| 63 | + """Get the latest revision for Linux as a starting point to check for a |
| 64 | + valid revision for all platforms.""" |
| 65 | + try: |
| 66 | + url = f"{SNAPSHOTS_PATH}Linux_x64/LAST_CHANGE" |
| 67 | + start_revision = int(requests.get(url).text.strip()) |
| 68 | + except requests.RequestException as e: |
| 69 | + raise requests.RequestException(f"Failed LAST_CHANGE lookup: {e}") |
| 70 | + |
| 71 | + return start_revision |
| 72 | + |
| 73 | + |
| 74 | +def check_new_chromium_revision() -> str: |
| 75 | + """Find a new Chromium revision that is available for all major platforms (Win/Mac/Linux)""" |
| 76 | + timeout = DEFAULT_TIMEOUT |
| 77 | + start = time() |
| 78 | + |
| 79 | + # Load existing pinned revision. |
| 80 | + storage_client = storage.Client() |
| 81 | + bucket = storage_client.bucket(BUCKET_NAME) |
| 82 | + # Read new revision number. |
| 83 | + blob = bucket.blob(OLD_REVISION_FILE) |
| 84 | + existing_revision = int(blob.download_as_string()) |
| 85 | + |
| 86 | + start_revision = get_start_revision() |
| 87 | + |
| 88 | + if start_revision == existing_revision: |
| 89 | + print("No new revision.") |
| 90 | + return "No new revision." |
| 91 | + |
| 92 | + # Step backwards through revision numbers until we find one |
| 93 | + # that is available for all platforms. |
| 94 | + candidate_revision = start_revision |
| 95 | + new_revision = -1 |
| 96 | + timed_out = False |
| 97 | + while new_revision == -1 and candidate_revision > existing_revision: |
| 98 | + available_for_all = True |
| 99 | + # For each platform, check if Chromium is available for download from snapshots. |
| 100 | + for platform, filename in PLATFORM_INFO: |
| 101 | + try: |
| 102 | + url = (f"{SNAPSHOTS_PATH}{platform}/" |
| 103 | + f"{candidate_revision}/{filename}") |
| 104 | + # Check the headers of each possible download URL. |
| 105 | + r = requests.head(url) |
| 106 | + # If the file is not available for download, decrement the revision and try again. |
| 107 | + if r.status_code != 200: |
| 108 | + candidate_revision -= 1 |
| 109 | + available_for_all = False |
| 110 | + break |
| 111 | + except requests.RequestException: |
| 112 | + print(f"Failed to fetch headers for revision {candidate_revision}. Skipping it.") |
| 113 | + candidate_revision -= 1 |
| 114 | + available_for_all = False |
| 115 | + break |
| 116 | + |
| 117 | + if available_for_all: |
| 118 | + new_revision = candidate_revision |
| 119 | + if time() - start > timeout: |
| 120 | + timed_out = True |
| 121 | + break |
| 122 | + |
| 123 | + end = time() |
| 124 | + if timed_out: |
| 125 | + raise Exception(f"Reached timeout {timeout}s while checking revision {candidate_revision}") |
| 126 | + |
| 127 | + if new_revision <= existing_revision: |
| 128 | + message = ("No new mutually available revision found after " |
| 129 | + f"{'{:.2f}'.format(end - start)} seconds. Keeping revision {existing_revision}.") |
| 130 | + print(message) |
| 131 | + return message |
| 132 | + |
| 133 | + |
| 134 | + # Replace old revision number with new number. |
| 135 | + blob = bucket.blob(NEW_REVISION_FILE) |
| 136 | + blob.upload_from_string(str(new_revision)) |
| 137 | + pr_error_msg = trigger_ci_tests() |
| 138 | + message = (f"Found mutually available revision at {new_revision}.\n" |
| 139 | + f"This process started at {start_revision} and checked " |
| 140 | + f"{start_revision - new_revision} revisions.\n" |
| 141 | + f"The whole process took {'{:.2f}'.format(end - start)} seconds.\n") |
| 142 | + if pr_error_msg: |
| 143 | + raise Exception(f"PR interaction error: {pr_error_msg}") |
| 144 | + print(message) |
| 145 | + return message |
| 146 | + |
| 147 | + |
| 148 | +def main(args, _) -> None: |
| 149 | + return check_new_chromium_revision() |
0 commit comments