Skip to content

Commit 05bfc64

Browse files
authored
Description of PR [202405]manual cherry pick PR 19116 to Fix github api rate limit issue #19116
1 parent 2292ff8 commit 05bfc64

File tree

2 files changed

+58
-21
lines changed

2 files changed

+58
-21
lines changed

setup-container.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ declare -r VERBOSE_MIN="${VERBOSE_ERROR}"
3838
# Arguments -----------------------------------------------------------------------------------------------------------
3939
#
4040

41+
ENV_VARS=""
4142
CONTAINER_NAME=""
4243
IMAGE_ID=""
4344
LINK_DIR=""
@@ -96,6 +97,7 @@ function show_help_and_exit() {
9697
echo " -n <container_name> set the name of the Docker container"
9798
echo
9899
echo "Other options:"
100+
echo " -e <VAR=value> set environment variable inside the container (can be used multiple times)"
99101
echo " -i <image_id> specify Docker image to use. This can be an image ID (hashed value) or an image name."
100102
echo " If no value is provided, defaults to the following images in the specified order:"
101103
echo " 1. The local image named \"docker-sonic-mgmt\""
@@ -298,7 +300,7 @@ EOF
298300
function start_local_container() {
299301
log_info "creating a container: ${CONTAINER_NAME} ..."
300302

301-
eval "docker run -d -t ${PUBLISH_PORTS} -h ${CONTAINER_NAME} \
303+
eval "docker run -d -t ${PUBLISH_PORTS} ${ENV_VARS} -h ${CONTAINER_NAME} \
302304
-v \"$(dirname "${SCRIPT_DIR}"):${LINK_DIR}:rslave\" ${MOUNT_POINTS} \
303305
--name \"${CONTAINER_NAME}\" \"${LOCAL_IMAGE}\" /bin/bash ${SILENT_HOOK}" || \
304306
exit_failure "failed to start a container: ${CONTAINER_NAME}"
@@ -345,11 +347,14 @@ if [[ $# -eq 0 ]]; then
345347
show_help_and_exit "${EXIT_SUCCESS}"
346348
fi
347349

348-
while getopts "n:i:d:m:p:fvxh" opt; do
350+
while getopts "e:n:i:d:m:p:fvxh" opt; do
349351
case "${opt}" in
350352
n )
351353
CONTAINER_NAME="${OPTARG}"
352354
;;
355+
e )
356+
ENV_VARS+=" -e ${OPTARG}"
357+
;;
353358
i )
354359
IMAGE_ID="${OPTARG}"
355360
;;

tests/common/plugins/conditional_mark/issue.py

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
"""
33
import logging
44
import multiprocessing
5+
import os
56
import re
6-
import six
7-
import requests
8-
97
from abc import ABCMeta, abstractmethod
8+
from urllib.parse import urlencode
9+
10+
import requests
11+
import six
1012

1113
logger = logging.getLogger(__name__)
1214

@@ -38,28 +40,58 @@ def __init__(self, url, proxies):
3840
self.proxies = proxies
3941

4042
def is_active(self):
41-
"""Check if the issue is still active.
43+
"""Check if the GitHub issue is still active.
4244
43-
If unable to get issue state, always consider it as active.
45+
Attempt to fetch issue details via proxy if configured. If proxy fails, retry with direct GitHub API URL.
46+
If unable to retrieve issue state, assume the issue is active (safe default).
4447
4548
Returns:
4649
bool: False if the issue is closed else True.
4750
"""
48-
try:
49-
response = requests.get(self.api_url, proxies=self.proxies, timeout=10)
51+
52+
def fetch_issue(url):
53+
response = requests.get(url, proxies=self.proxies, timeout=10)
5054
response.raise_for_status()
51-
issue_data = response.json()
52-
if issue_data.get('state', '') == 'closed':
53-
logger.debug('Issue {} is closed'.format(self.url))
54-
labels = issue_data.get('labels', [])
55-
if any(['name' in label and 'duplicate' in label['name'].lower() for label in labels]):
56-
logger.warning('GitHub issue: {} looks like duplicate and was closed. Please re-check and ignore'
57-
'the test on the parent issue'.format(self.url))
58-
return False
59-
except Exception as e:
60-
logger.error('Get details for {} failed with: {}'.format(self.url, repr(e)))
61-
62-
logger.debug('Issue {} is active. Or getting issue state failed, consider it as active anyway'.format(self.url))
55+
return response.json()
56+
57+
direct_url = self.api_url
58+
proxy_url = os.getenv("SONIC_AUTOMATION_PROXY_GITHUB_ISSUES_URL")
59+
60+
issue_data = None
61+
62+
# Attempt to access via proxy first (if configured)
63+
# The proxy is used to work around GitHub's unauthenticated rate limit (60 requests/hour per IP).
64+
# For details, refer to GitHub API rate limits documentation:
65+
# https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#primary-rate-limit-for-unauthenticated-users
66+
if proxy_url:
67+
try:
68+
proxy_endpoint = f"{proxy_url.rstrip('/')}/?{urlencode({'github_issue_url': direct_url})}"
69+
logger.info("Attempting to access GitHub API via proxy.")
70+
issue_data = fetch_issue(proxy_endpoint)
71+
except Exception as proxy_err:
72+
logger.warning(f"Proxy access failed: {proxy_err}. Falling back to direct API.")
73+
74+
# Fallback to direct URL if proxy is not set or fails
75+
if issue_data is None:
76+
try:
77+
logger.info(f"Accessing GitHub API directly: {direct_url}")
78+
issue_data = fetch_issue(direct_url)
79+
except Exception as direct_err:
80+
logger.error(f"Access GitHub API directly failed for {direct_url}: {direct_err}")
81+
logger.debug(f"Issue {direct_url} is considered active due to API access failure.")
82+
return True
83+
84+
# Check issue state
85+
if issue_data.get('state') == 'closed':
86+
logger.debug(f"Issue {direct_url} is closed.")
87+
labels = issue_data.get('labels', [])
88+
if any('name' in label and 'duplicate' in label['name'].lower() for label in labels):
89+
logger.warning(
90+
f"GitHub issue {direct_url} appears to be a duplicate and was closed. "
91+
f"Consider ignoring related test failures.")
92+
return False
93+
94+
logger.debug(f"Issue {direct_url} is active.")
6395
return True
6496

6597

0 commit comments

Comments
 (0)