From 0a18fb732bafc136395a4ebf7f03e020d294f84a Mon Sep 17 00:00:00 2001 From: Emilio Reyes Date: Fri, 22 Mar 2024 14:21:33 -0700 Subject: [PATCH] fix: retry rate limit error (#21) * build: update build version * fix: retry rate limit error * ci: use pyb code coverage --------- Signed-off-by: Emilio Reyes --- .github/workflows/main.yml | 34 ------------------------- README.md | 3 ++- build.py | 3 +-- src/main/python/github3api/githubapi.py | 3 ++- src/unittest/python/test_githubapi.py | 10 ++++++-- 5 files changed, 13 insertions(+), 40 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8fd4d8f..08be898 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,37 +20,3 @@ jobs: - name: build github3api ${{ matrix.version }} image run: docker image build --target build-image --build-arg PYTHON_VERSION=${{ matrix.version }} -t github3api:${{ matrix.version }} . - - name: save github3api ${{ matrix.version }} image - if: ${{ matrix.version == '3.9' }} - run: | - mkdir -p images - docker save --output images/github3api-${{ matrix.version }}.tar github3api:${{ matrix.version }} - - name: upload github3api ${{ matrix.version }} image artifact - if: ${{ matrix.version == '3.9' }} - uses: actions/upload-artifact@v2 - with: - name: image - path: images/github3api-${{ matrix.version }}.tar - coverage: - name: Publish Code Coverage Report - needs: build-images - runs-on: ubuntu-20.04 - steps: - - name: download image artifact - uses: actions/download-artifact@v2 - with: - name: image - path: images/ - - name: load image - run: - docker load --input images/github3api-3.9.tar - - name: prepare report - run: | - ID=$(docker create github3api:3.9) - docker cp $ID:/code/target/reports/github3api_coverage.xml github3api_coverage.xml - sed -i -e 's,filename="github3api/,filename="src/main/python/github3api/,g' github3api_coverage.xml - - name: upload report - uses: codecov/codecov-action@v1 - with: - token: ${{ secrets.CODECOV_TOKEN }} - file: github3api_coverage.xml diff --git a/README.md b/README.md index 2147cfe..dfffa14 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # github3api [![GitHub Workflow Status](https://github.com/soda480/github3api/workflows/build/badge.svg)](https://github.com/soda480/github3api/actions) -[![Code Coverage](https://codecov.io/gh/soda480/github3api/branch/master/graph/badge.svg)](https://codecov.io/gh/soda480/github3api) +[![coverage](https://img.shields.io/badge/coverage-100%25-brightgreen)](https://pybuilder.io/) +[![complexity](https://img.shields.io/badge/complexity-A-brightgreen)](https://radon.readthedocs.io/en/latest/api.html#module-radon.complexity) [![vulnerabilities](https://img.shields.io/badge/vulnerabilities-None-brightgreen)](https://pypi.org/project/bandit/) [![PyPI version](https://badge.fury.io/py/github3api.svg)](https://app.codiga.io/public/project/13337/github3api/dashboard) [![python](https://img.shields.io/badge/python-3.7%20%7C%203.8%20%7C%203.9%20%7C%203.10-teal)](https://www.python.org/downloads/) diff --git a/build.py b/build.py index 6043b19..b4070ae 100644 --- a/build.py +++ b/build.py @@ -30,7 +30,7 @@ authors = [Author('Emilio Reyes', 'emilio.reyes@intel.com')] summary = 'An advanced REST client for the GitHub API' url = 'https://github.com/soda480/github3api' -version = '0.3.1' +version = '0.3.2' default_task = [ 'clean', 'analyze', @@ -76,4 +76,3 @@ def set_properties(project): project.set_property('radon_break_build_average_complexity_threshold', 3.6) project.set_property('radon_break_build_complexity_threshold', 14) project.set_property('bandit_break_build', True) - project.set_property('anybadge_exclude', 'coverage, complexity') diff --git a/src/main/python/github3api/githubapi.py b/src/main/python/github3api/githubapi.py index 6eff222..7298dde 100644 --- a/src/main/python/github3api/githubapi.py +++ b/src/main/python/github3api/githubapi.py @@ -153,7 +153,8 @@ def retry_ratelimit_error(exception): """ logger.debug(f"checking if '{type(exception).__name__}' exception is a ratelimit error") if isinstance(exception, HTTPError): - if exception.response.status_code == 403: + logger.debug(exception.response.reason) + if exception.response.status_code == 403 and 'rate limit exceeded' in exception.response.reason.lower(): logger.info('ratelimit error encountered - retrying request in 60 seconds') return True logger.debug(f'exception is not a ratelimit error: {exception}') diff --git a/src/unittest/python/test_githubapi.py b/src/unittest/python/test_githubapi.py index 1561c9a..730cbd9 100644 --- a/src/unittest/python/test_githubapi.py +++ b/src/unittest/python/test_githubapi.py @@ -109,18 +109,24 @@ def test__retry_ratelimit_error_Should_Return_False_When_NotHttpError(self, *pat self.assertFalse(GitHubAPI.retry_ratelimit_error(Exception('test'))) - def test__retry_ratelimit_error_Should_Return_True_When_HttpErrorNoStatusCodeMatch(self, *patches): + def test__retry_ratelimit_error_Should_Return_False_When_HttpErrorNoStatusCodeMatch(self, *patches): response_mock = Mock(status_code=404) http_error_mock = HTTPError(Mock()) http_error_mock.response = response_mock self.assertFalse(GitHubAPI.retry_ratelimit_error(http_error_mock)) def test__retry_ratelimit_error_Should_Return_True_When_Match(self, *patches): - response_mock = Mock(status_code=403) + response_mock = Mock(status_code=403, reason='API Rate Limit Exceeded') http_error_mock = HTTPError(Mock()) http_error_mock.response = response_mock self.assertTrue(GitHubAPI.retry_ratelimit_error(http_error_mock)) + def test__retry_ratelimit_error_Should_Return_False_When_403NotRateLimit(self, *patches): + response_mock = Mock(status_code=403, reason='Forbidden') + http_error_mock = HTTPError(Mock()) + http_error_mock.response = response_mock + self.assertFalse(GitHubAPI.retry_ratelimit_error(http_error_mock)) + @patch('github3api.githubapi.GitHubAPI.log_ratelimit') @patch('github3api.githubapi.GitHubAPI.get_ratelimit') def test__get_response_Should_CallExpected_When_RateLimit(self, get_ratelimit_patch, log_ratelimit_patch, *patches):