Skip to content
This repository was archived by the owner on Jan 14, 2021. It is now read-only.

Commit bfc3ecf

Browse files
authored
Merge pull request #66 from fedora-infra/feature/retirement-tweaks
Some changes to the retirement handler...
2 parents 91722ca + e1b0542 commit bfc3ecf

File tree

2 files changed

+48
-46
lines changed

2 files changed

+48
-46
lines changed

pdcupdater/handlers/retirement.py

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import requests
44

55
import pdcupdater.services
6+
import pdcupdater.utils
67

78
log = logging.getLogger(__name__)
89

@@ -54,17 +55,7 @@ def handle(self, pdc, msg):
5455
if branch['active'] is False:
5556
return
5657

57-
self._retire_branch(pdc, branch)
58-
59-
@staticmethod
60-
def _retire_branch(pdc, branch):
61-
""" Internal method for retiring a branch in PDC. """
62-
today = datetime.utcnow().date()
63-
for sla in branch['slas']:
64-
sla_eol = datetime.strptime(sla['eol'], '%Y-%m-%d').date()
65-
if sla_eol > today:
66-
pdc['component-branch-slas'][sla['id']]._ \
67-
+= {'eol': str(today)}
58+
_retire_branch(pdc, branch)
6859

6960
@staticmethod
7061
def _namespace_to_pdc(namespace):
@@ -96,31 +87,6 @@ def _pdc_to_namespace(pdc_type):
9687
else:
9788
return pdc_to_namespace[pdc_type]
9889

99-
@staticmethod
100-
def _is_retired_in_cgit(namespace, repo, branch, requests_session=None):
101-
""" Internal method to determine if a branch is retired in cgit. """
102-
if requests_session is None:
103-
requests_session = requests.Session()
104-
105-
cgit_url = 'https://src.fedoraproject.org/cgit'
106-
# Check to see if they have a dead.package file in dist-git
107-
url = '{base}/{namespace}/{repo}.git/plain/dead.package?h={branch}'
108-
response = requests_session.head(url.format(
109-
base=cgit_url,
110-
namespace=namespace,
111-
repo=repo,
112-
branch=branch,
113-
))
114-
115-
# If there is a dead.package, then the branch is retired in cgit
116-
if response.status_code in [200, 404]:
117-
return response.status_code == 200
118-
else:
119-
raise ValueError(
120-
'The connection to cgit failed. Retirement status could not '
121-
'be determined. The status code was: {0}. The content was: '
122-
'{1}'.format(response.status_code, response.content))
123-
12490
def audit(self, pdc):
12591
""" Returns the difference in retirement status in PDC and dist-git.
12692
@@ -135,14 +101,14 @@ def audit(self, pdc):
135101
for branch in pdc.get_paged(pdc['component-branches']._):
136102
branch_str = '{type}/{global_component}#{name}'.format(**branch)
137103
log.debug('Considering {0}'.format(branch_str))
138-
retired_in_cgit = self._is_retired_in_cgit(
104+
retired_in_dist_git = _is_retired_in_dist_git(
139105
namespace=self._pdc_to_namespace(branch['type']),
140106
repo=branch['global_component'],
141107
branch=branch['name'],
142108
requests_session=session
143109
)
144110

145-
if retired_in_cgit:
111+
if retired_in_dist_git:
146112
branches_retired_in_distgit.add(branch_str)
147113
if not branch['active']:
148114
branches_retired_in_pdc.add(branch_str)
@@ -166,12 +132,47 @@ def initialize(self, pdc):
166132
for branch in pdc.get_paged(pdc['component-branches']._, active=True):
167133
log.debug('Considering {type}/{global_component}#{name}'
168134
.format(**branch))
169-
retired_in_cgit = self._is_retired_in_cgit(
135+
retired_in_dist_git = _is_retired_in_dist_git(
170136
namespace=self._pdc_to_namespace(branch['type']),
171137
repo=branch['global_component'],
172138
branch=branch['name'],
173139
requests_session=session
174140
)
175141

176-
if retired_in_cgit:
177-
self._retire_branch(pdc, branch)
142+
if retired_in_dist_git:
143+
_retire_branch(pdc, branch)
144+
145+
@pdcupdater.utils.retry(wait_on=requests.exceptions.ConnectionError)
146+
def _is_retired_in_dist_git(namespace, repo, branch, requests_session=None):
147+
if requests_session is None:
148+
requests_session = requests.Session()
149+
150+
base = 'https://src.fedoraproject.org/'
151+
# Check to see if they have a dead.package file in dist-git
152+
url = '{base}/{namespace}/{repo}/raw/{branch}/f/dead.package'
153+
response = requests_session.head(url.format(
154+
base=base,
155+
namespace=namespace,
156+
repo=repo,
157+
branch=branch,
158+
))
159+
160+
# If there is a dead.package, then the branch is retired in dist_git
161+
if response.status_code in [200, 404]:
162+
return response.status_code == 200
163+
else:
164+
raise ValueError(
165+
'The connection to dist_git failed. Retirement status could not '
166+
'be determined. The status code was: {0}. The content was: '
167+
'{1}'.format(response.status_code, response.content))
168+
169+
170+
@pdcupdater.utils.retry(wait_on=requests.exceptions.ConnectionError)
171+
def _retire_branch(pdc, branch):
172+
""" Internal method for retiring a branch in PDC. """
173+
today = datetime.utcnow().date()
174+
for sla in branch['slas']:
175+
sla_eol = datetime.strptime(sla['eol'], '%Y-%m-%d').date()
176+
if sla_eol > today:
177+
pdc['component-branch-slas'][sla['id']]._ \
178+
+= {'eol': str(today)}

pdcupdater/tests/handler_tests/test_retirement.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from pdcupdater.tests.handler_tests import BaseHandlerTest, mock_pdc
44
import pdcupdater.services
5+
import pdcupdater.handlers.retirement
56
from pdcupdater.handlers.retirement import RetireComponentHandler
67

78

@@ -178,7 +179,7 @@ def test_audit(self, pdc):
178179

179180

180181
@mock_pdc
181-
def test_audit_retired_in_pdc_not_cgit(self, pdc):
182+
def test_audit_retired_in_pdc_not_git(self, pdc):
182183
pdc.add_endpoint('component-branches', 'GET', [
183184
{
184185
"id": 155867,
@@ -234,7 +235,7 @@ def test_audit_retired_in_pdc_not_cgit(self, pdc):
234235
self.assertEquals(absent, set())
235236

236237
@mock_pdc
237-
def test_audit_retired_in_cgit_not_pdc(self, pdc):
238+
def test_audit_retired_in_git_not_pdc(self, pdc):
238239
pdc.add_endpoint('component-branches', 'GET', [
239240
{
240241
"id": 155867,
@@ -334,8 +335,8 @@ def test_initialize(self, pdc):
334335
}
335336
])
336337

337-
with mock.patch.object(RetireComponentHandler, '_retire_branch') as mock_retire_branch, \
338-
mock.patch.object(RetireComponentHandler, '_is_retired_in_cgit') as mock_is_retired_in_cgit:
339-
mock_is_retired_in_cgit.side_effect = [True, False]
338+
with mock.patch.object(pdcupdater.handlers.retirement, '_retire_branch') as mock_retire_branch, \
339+
mock.patch.object(pdcupdater.handlers.retirement, '_is_retired_in_dist_git') as mock_is_retired_in_dist_git:
340+
mock_is_retired_in_dist_git.side_effect = [True, False]
340341
self.handler.initialize(pdc)
341342
self.assertEqual(mock_retire_branch.call_count, 1)

0 commit comments

Comments
 (0)