From 8786d5c6e7757e09c73f0340af8636f3a891771a Mon Sep 17 00:00:00 2001 From: jonathanmetzman <31354670+jonathanmetzman@users.noreply.github.com> Date: Thu, 26 Dec 2024 09:09:31 -0500 Subject: [PATCH] Optimize get_artifacts_for_build() with regex filtering (#4297) (#4544) Introduces regex-based filtering directly in the API to improve performance and reduce number of calls to ab api server. Cherry pick: #4297 Co-authored-by: aditya-wazir <108256495+aditya-wazir@users.noreply.github.com> --- .../platforms/android/fetch_artifact.py | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/clusterfuzz/_internal/platforms/android/fetch_artifact.py b/src/clusterfuzz/_internal/platforms/android/fetch_artifact.py index 60f9f331bf..e01195d714 100644 --- a/src/clusterfuzz/_internal/platforms/android/fetch_artifact.py +++ b/src/clusterfuzz/_internal/platforms/android/fetch_artifact.py @@ -19,6 +19,8 @@ import json import os import re +from typing import List +from typing import Optional import apiclient from oauth2client.service_account import ServiceAccountCredentials @@ -119,10 +121,22 @@ def download_artifact(client, bid, target, attempt_id, name, output_directory, return output_path -def get_artifacts_for_build(client, bid, target, attempt_id='latest'): +def get_artifacts_for_build(client, + bid: str, + target: str, + attempt_id: str = 'latest', + regexp: Optional[str] = None) -> List[str]: """Return list of artifacts for a given build.""" - request = client.buildartifact().list( - buildId=bid, target=target, attemptId=attempt_id) + if not regexp: + request = client.buildartifact().list( + buildId=bid, target=target, attemptId=attempt_id) + else: + request = client.buildartifact().list( + buildId=bid, + target=target, + attemptId=attempt_id, + nameRegexp=regexp, + maxResults=100) request_str = (f'{request.uri}, {request.method}, ' f'{request.body}, {request.methodId}') @@ -239,7 +253,7 @@ def get(bid, target, regex, output_directory, output_filename=None): def run_script(client, bid, target, regex, output_directory, output_filename): """Download artifacts as specified.""" artifacts = get_artifacts_for_build( - client=client, bid=bid, target=target, attempt_id='latest') + client=client, bid=bid, target=target, attempt_id='latest', regexp=regex) if not artifacts: logs.error(f'Artifact could not be fetched for target {target}, ' f'build id {bid}.')