@@ -66,6 +66,7 @@ class Gitlab:
66
66
user_agent: A custom user agent to use for making HTTP requests.
67
67
retry_transient_errors: Whether to retry after 500, 502, 503, 504
68
68
or 52x responses. Defaults to False.
69
+ persist_base_url: reconstruct next url if found not same as user provided url
69
70
"""
70
71
71
72
def __init__ (
@@ -85,6 +86,7 @@ def __init__(
85
86
order_by : Optional [str ] = None ,
86
87
user_agent : str = gitlab .const .USER_AGENT ,
87
88
retry_transient_errors : bool = False ,
89
+ persist_base_url : bool = False ,
88
90
) -> None :
89
91
90
92
self ._api_version = str (api_version )
@@ -95,6 +97,7 @@ def __init__(
95
97
#: Timeout to use for requests to gitlab server
96
98
self .timeout = timeout
97
99
self .retry_transient_errors = retry_transient_errors
100
+ self .persist_base_url = persist_base_url
98
101
#: Headers that will be used in request to GitLab
99
102
self .headers = {"User-Agent" : user_agent }
100
103
@@ -1131,8 +1134,29 @@ def _query(
1131
1134
next_url = requests .utils .parse_header_links (result .headers ["links" ])[
1132
1135
0
1133
1136
]["url" ]
1134
- if not next_url .startswith (self ._gl ._base_url ):
1135
- next_url = re .sub (r"^.*?/api" , f"{ self ._gl ._base_url } /api" , next_url )
1137
+ # if the next url is different with user provided server URL
1138
+ # then give a warning it may because of misconfiguration
1139
+ # but if the option to fix provided then just reconstruct it
1140
+ if not next_url .startswith (self ._gl .url ):
1141
+ search_api_url = re .search (r"(^.*?/api)" , next_url )
1142
+ if search_api_url :
1143
+ next_api_url = search_api_url .group (1 )
1144
+ if self ._gl .persist_base_url :
1145
+ next_url = next_url .replace (
1146
+ next_api_url , f"{ self ._gl ._base_url } /api"
1147
+ )
1148
+ else :
1149
+ utils .warn (
1150
+ message = (
1151
+ f"The base url of the returned next page got "
1152
+ f"different with the user provided "
1153
+ f"{ self ._gl .url } /api* ~> { next_api_url } *, "
1154
+ f"since this may can lead to unexpected behaviour. "
1155
+ f"set argument persist_base_url to True for "
1156
+ f"resonctruct it with the origin one."
1157
+ ),
1158
+ category = UserWarning ,
1159
+ )
1136
1160
self ._next_url = next_url
1137
1161
except KeyError :
1138
1162
self ._next_url = None
0 commit comments