Skip to content

Commit

Permalink
Add: API call path_exists() to GitHub API (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
y0urself authored Sep 28, 2022
1 parent 48ced0a commit 9b5cc82
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pontos/github/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from pontos.github.api.artifacts import GitHubRESTArtifactsMixin
from pontos.github.api.branch import GitHubRESTBranchMixin
from pontos.github.api.contents import GitHubRESTContentMixin
from pontos.github.api.helper import (
DEFAULT_GITHUB_API_URL,
DEFAULT_TIMEOUT_CONFIG,
Expand All @@ -40,6 +41,7 @@ class GitHubRESTApi(
GitHubRESTReleaseMixin,
GitHubRESTArtifactsMixin,
GitHubRESTBranchMixin,
GitHubRESTContentMixin,
GitHubRESTLabelsMixin,
GitHubAPIWorkflowsMixin,
GitHubRESTOrganizationsMixin,
Expand Down
40 changes: 40 additions & 0 deletions pontos/github/api/contents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (C) 2022 Greenbone Networks GmbH
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


import httpx


class GitHubRESTContentMixin:
def path_exits(self, repo: str, path: str, branch: str = None) -> bool:
"""
Check if a path exists in a branch of a repository
Args:
repo: GitHub repository (owner/name) to use
path: to the file/directory in question
branch: Branch to check, defaults to default branch (:
Returns:
True if existing, False else
"""
api = f"/repos/{repo}/contents/{path}"
params = {}
if branch:
params["ref"] = branch
response: httpx.Response = self._request(api, params=params)
return response.is_success
63 changes: 63 additions & 0 deletions tests/github/api/test_contents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright (C) 2022 Greenbone Networks GmbH
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import unittest
from pathlib import Path
from unittest.mock import MagicMock, patch

from pontos.github.api import GitHubRESTApi
from tests.github.api import default_request

here = Path(__file__).parent


class GitHubContentTestCase(unittest.TestCase):
@patch("pontos.github.api.api.httpx.get")
def test_path_exists(self, requests_mock: MagicMock):
response = MagicMock()
response.ok = True
requests_mock.return_value = response

api = GitHubRESTApi("12345")
exists = api.path_exits(repo="foo/bar", path="baz", branch="main")

args, kwargs = default_request(
"https://api.github.com/repos/foo/bar/contents/baz",
params={
"ref": "main",
},
)
requests_mock.assert_called_once_with(*args, **kwargs)
self.assertTrue(exists)

@patch("pontos.github.api.api.httpx.get")
def test_path_not_exists(self, requests_mock: MagicMock):
response = MagicMock()
response.is_success = False
requests_mock.return_value = response

api = GitHubRESTApi("12345")
exists = api.path_exits(repo="foo/bar", path="baz", branch="main")

args, kwargs = default_request(
"https://api.github.com/repos/foo/bar/contents/baz",
params={
"ref": "main",
},
)
requests_mock.assert_called_once_with(*args, **kwargs)
self.assertFalse(exists)

0 comments on commit 9b5cc82

Please sign in to comment.