Skip to content
This repository was archived by the owner on Feb 19, 2020. It is now read-only.

Commit 24c1af3

Browse files
committed
Add a couple of retries when posting the results
Without this, we might run into transient network failures, which could be solved by retrying the operation. (FT-1235)
1 parent d4fff31 commit 24c1af3

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/codacy/reporter.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
11
"""Codacy coverage reporter for Python"""
22

33
import argparse
4+
import contextlib
45
import json
56
import logging
67
import os
78
from xml.dom import minidom
8-
import requests
99
from math import floor
1010

11+
import requests
12+
from requests.packages.urllib3 import util as urllib3_util
13+
1114
logging.basicConfig(level=logging.INFO,
1215
format='%(asctime)s - %(levelname)s - %(message)s')
1316

1417
CODACY_PROJECT_TOKEN = os.getenv('CODACY_PROJECT_TOKEN')
1518
CODACY_BASE_API_URL = os.getenv('CODACY_API_BASE_URL', 'https://api.codacy.com')
1619
URL = CODACY_BASE_API_URL + '/2.0/coverage/{commit}/python'
1720
DEFAULT_REPORT_FILE = 'coverage.xml'
21+
MAX_RETRIES = 3
22+
BAD_REQUEST = 400
23+
24+
25+
class _Retry(urllib3_util.Retry):
26+
27+
def is_forced_retry(self, method, status_code):
28+
return status_code >= BAD_REQUEST
29+
30+
31+
@contextlib.contextmanager
32+
def _request_session():
33+
retry = _Retry(total=MAX_RETRIES, raise_on_redirect=False)
34+
session = requests.Session()
35+
session.mount("https://", requests.adapters.HTTPAdapter(max_retries=retry))
36+
with session:
37+
yield session
1838

1939

2040
def get_git_revision_hash():
@@ -103,12 +123,14 @@ def upload_report(report, token, commit):
103123

104124
logging.debug(data)
105125

106-
r = requests.post(url, data=data, headers=headers, allow_redirects=True)
126+
with _request_session() as session:
127+
r = session.post(url, data=data, headers=headers, allow_redirects=True)
107128

108129
logging.debug(r.content)
109130
r.raise_for_status()
110131

111132
response = json.loads(r.text)
133+
112134
try:
113135
logging.info(response['success'])
114136
except KeyError:

0 commit comments

Comments
 (0)