Skip to content

Commit

Permalink
Fix race condition causing ENOPRSTATS
Browse files Browse the repository at this point in the history
Co-authored-by: Nikola Forró <nforro@redhat.com>
  • Loading branch information
majamassarini and nforro committed Dec 19, 2023
1 parent 7a7dd21 commit 280368a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
50 changes: 38 additions & 12 deletions ogr/services/pagure/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import datetime
import logging
from time import sleep
from typing import Any, Optional, Union

from ogr.abstract import CommitFlag, CommitStatus, PRComment, PRStatus, PullRequest
Expand Down Expand Up @@ -191,18 +192,43 @@ def get(project: "ogr_pagure.PagureProject", pr_id: int) -> "PullRequest":
return PagurePullRequest(raw_pr, project)

@staticmethod
def get_files_diff(project: "ogr_pagure.PagureProject", pr_id: int) -> dict:
try:
return project._call_project_api(
"pull-request",
str(pr_id),
"diffstats",
method="GET",
)
except PagureAPIException as ex:
if "No statistics" in ex.pagure_error:
return {}
raise ex
def get_files_diff(
project: "ogr_pagure.PagureProject",
pr_id: int,
retries=0,
wait_seconds=3,
) -> dict:
"""Collect all the changes in the PR and deal with ENOPRSTATS exception.
While the PR is changing its status from open to merged
exception can be raised by the Pagure service.
Wait for a while to see if we can manage to access changes adjusting the
retries and wait_seconds parameters.
"""
attempt = 1
while True:
try:
return project._call_project_api(
"pull-request",
str(pr_id),
"diffstats",
method="GET",
)
except PagureAPIException as ex: # noqa PERF203
if "No statistics" in ex.pagure_error:
# this may be a race condition, try once more
logger.error(
f"While retrieving PR diffstats Pagure returned ENOPRSTATS. \n{ex}",
)
if attempt <= retries:
logger.error(
f"Trying again; attempt={attempt} after {wait_seconds}seconds",
)
attempt += 1
sleep(wait_seconds)
else:
raise ex
raise ex

@staticmethod
def get_list(
Expand Down
7 changes: 4 additions & 3 deletions tests/integration/pagure/test_pull_requests.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT

import pytest
from requre.online_replacing import record_requests_for_all_methods

from ogr.abstract import CommitStatus, PRStatus
from ogr.exceptions import PagureAPIException
from tests.integration.pagure.base import PagureTests


Expand Down Expand Up @@ -125,6 +127,5 @@ def test_pr_diff(self):
assert "README.md" in diff

def test_pr_diff_empty_diff(self):
diff = self.ogr_project.get_pr_files_diff(6)
assert isinstance(diff, dict)
assert diff == {}
with pytest.raises(PagureAPIException):
self.ogr_project.get_pr_files_diff(6)

0 comments on commit 280368a

Please sign in to comment.