Skip to content

Commit

Permalink
Fix race condition causing ENOPRSTATS
Browse files Browse the repository at this point in the history
  • Loading branch information
majamassarini committed Dec 19, 2023
1 parent 7a7dd21 commit cce5f90
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
29 changes: 27 additions & 2 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,7 +192,20 @@ 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:
def get_files_diff(
project: "ogr_pagure.PagureProject",
pr_id: int,
retries=3,
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 = 0
try:
return project._call_project_api(
"pull-request",
Expand All @@ -201,7 +215,18 @@ def get_files_diff(project: "ogr_pagure.PagureProject", pr_id: int) -> dict:
)
except PagureAPIException as ex:
if "No statistics" in ex.pagure_error:
return {}
# this may be a race condition, try once more
logger.error(
f"While retrieving PR diff Pagure returned ENOPRSTATS. {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
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) as _:
self.ogr_project.get_pr_files_diff(6, retries=1)

0 comments on commit cce5f90

Please sign in to comment.