团队, 在 linux 上,在 python 中使用 f-string 或 format 或 concatenate 从服务器获取 404 响应,并且在我静态使用值时工作。一个更奇怪的部分是当我使用 fstring 在 MAC 上本地运行它时。本地mac有python 3.6,linux有3.8
我试过的代码有评论
GERRIT_CN = os.environ.get('GERRIT_CHANGE_NUMBER')
GERRIT_PS = os.environ.get('GERRIT_PATCHSET_NUMBER')
sq_url = 'https://sonar.company.com/'
projectKey="team-pba"
gerrit_cn_ps = f"{GERRIT_CN}-{GERRIT_PS}"
print(gerrit_cn_ps)
def sonar_api():
debug_requests_on()
sonar = SonarQubeClient(sonarqube_url=sq_url, username=myUsr, password=myPass)
# project_pull_requests = sonar.qualitygates.get_project_qualitygates_status(projectKey="team-pba", pullRequest="124434-120") < WORKS
# project_pull_requests = sonar.qualitygates.get_project_qualitygates_status(projectKey=f"{projectKey}", pullRequest=f"{GERRIT_CN}-{GERRIT_PS}") <FAILS
# project_pull_requests = sonar.qualitygates.get_project_qualitygates_status(projectKey=f"{projectKey}", pullRequest=GERRIT_CN+"-"+GERRIT_PS) < FAILS
# project_pull_requests = sonar.qualitygates.get_project_qualitygates_status(projectKey=projectKey, pullRequest=gerrit_cn_ps) < FAILS
使用静态值有效
project_pull_requests = sonar.qualitygates.get_project_qualitygates_status(projectKey="team-pba", pullRequest="124434-139")
但是上面与 fstring 一起使用的相同代码让我 404 不确定 api 不喜欢什么。任何提示或格式如何替换字符串?
仅当我使用 FSTRING 或 FORMAT 时,linux jenkins 容器才会失败。当我输入静态值时有效。
+ python3.8 /home/jenkins/agent/workspace/sonar-api.py
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): sonar.company.com:443
DEBUG:urllib3.connectionpool:https://sonar.company.com:443 "GET /api/qualitygates/project_status?projectKey=team-pba&pullRequest=124434-139 HTTP/1.1" 404 103
Traceback (most recent call last):
File "/home/jenkins/agent/workspace///sonar-api.py", line 61, in <module>
print(sonar_api())
File "/home/jenkins/agent/workspace///sonar-api.py", line 51, in sonar_api
project_pull_requests = sonar.qualitygates.get_project_qualitygates_status(projectKey=projectKey, pullRequest=gerrit_cn_ps)
File "/usr/local/lib/python3.8/dist-packages/sonarqube/utils/common.py", line 132, in inner_func
response = self._get(url_pattern, params=params)
File "/usr/local/lib/python3.8/dist-packages/sonarqube/utils/rest_client.py", line 141, in _get
return self.request("GET", path=path, params=params, data=data, headers=headers)
File "/usr/local/lib/python3.8/dist-packages/sonarqube/utils/rest_client.py", line 99, in request
raise NotFoundError(msg)
sonarqube.utils.exceptions.NotFoundError: Error in request. Possibly Not Found error [404]: Pull request '124434-139' in project 'team-pba' not found
124434-139
send: b'GET /api/qualitygates/project_status?projectKey=team-pba&pullRequest=124434-139 HTTP/1.1\r\nHost: sonar.company.com\r\nUser-Agent: python-requests/2.22.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nAuthorization: Basic xxxxx\r\n\r\n’
reply: 'HTTP/1.1 404 \r\n'
header: Server: nginx/1.12.2
header: Date: Tue, 28 Mar 2023 21:16:09 GMT
header: Content-Type: application/json
header: Content-Length: 103
header: Connection: keep-alive
成功的日志是使用静态值时。
+ python3 /home/jenkins/agent/workspace/sonar-api.py
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): sonar.company.com:443
DEBUG:urllib3.connectionpool:https://sonar.company.com:443 "GET /api/qualitygates/project_status?projectKey=team-pba&pullRequest=124434-139 HTTP/1.1" 200 881
[Pipeline] echo
124434-139
send: b'GET /api/qualitygates/project_status?projectKey=team-pba&pullRequest=124434-139 HTTP/1.1\r\nHost
[Pipeline] }
使用 fstring 在 Linux 上刷新日志失败。
all values
124434
142
124434-142
send: b'GET /api/qualitygates/project_status?projectKey=team-pba&pullRequest=124434-142 HTTP/1.1\r\nHost: sonar.company.com\r\nUser-Agent: python-requests/2.28.2\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nAuthorization: Basic xxxxx\r\n\r\n’
reply: 'HTTP/1.1 404 \r\n'
header: Server: nginx/1.12.2
header: Date: Tue, 28 Mar 2023 21:45:21 GMT
header: Content-Type: application/json
header: Content-Length: 103
header: Connection: keep-alive
[Pipeline] }
fresh 在 mac 上使用 fstring 记录成功
Command> python3 sonar-api.py
all values
124434
142
124434-142
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): sonar.company.com:443
send: b'GET /api/qualitygates/project_status?projectKey=team-pba&pullRequest=124434-142 HTTP/1.1\r\nHost: sonar.company.com\r\nUser-Agent: python-requests/2.27.1\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nAuthorization: Basic xxxx\r\n\r\n’
reply: 'HTTP/1.1 200 \r\n'
header: Server: nginx/1.12.2
header: Date: Tue, 28 Mar 2023 21:53:55 GMT
header: Content-Type: application/json
header: Content-Length: 739
header: Connection: keep-alive
header: X-Frame-Options: SAMEORIGIN
header: X-XSS-Protection: 1; mode=block
header: X-Content-Type-Options: nosniff
header: Cache-Control: no-cache, no-store, must-revalidate
DEBUG:urllib3.connectionpool:https://sonar.company.com:443 "GET /api/qualitygates/project_status?projectKey=team-pba&pullRequest=124434-142 HTTP/1.1" 200 739
52.53:0.0