From a526e11c011bdcf25ad3ed45a5082642c79020b3 Mon Sep 17 00:00:00 2001 From: Frantisek Lachman Date: Wed, 14 Oct 2020 10:32:50 +0200 Subject: [PATCH] Implement project.exists for GitLab Signed-off-by: Frantisek Lachman --- ogr/abstract.py | 8 + ogr/services/gitlab/project.py | 11 +- ogr/services/pagure/project.py | 2 +- ...s.GenericCommands.test_project_exists.yaml | 250 ++++++++++++++++++ ...nericCommands.test_project_not_exists.yaml | 153 +++++++++++ .../gitlab/test_generic_commands.py | 14 +- 6 files changed, 433 insertions(+), 5 deletions(-) create mode 100644 tests/integration/gitlab/test_data/test_generic_commands/tests.integration.gitlab.test_generic_commands.GenericCommands.test_project_exists.yaml create mode 100644 tests/integration/gitlab/test_data/test_generic_commands/tests.integration.gitlab.test_generic_commands.GenericCommands.test_project_not_exists.yaml diff --git a/ogr/abstract.py b/ogr/abstract.py index df57ed22..ae5ea2ea 100644 --- a/ogr/abstract.py +++ b/ogr/abstract.py @@ -803,6 +803,14 @@ def __init__(self, repo: str, service: GitService, namespace: str) -> None: self.repo = repo self.namespace = namespace + def exists(self) -> bool: + """ + Check the existence of the repo. + + :return: True if the project exists, False otherwise + """ + raise NotImplementedError() + def is_private(self) -> bool: """ Is this repo private (accessible only by users with permissions) diff --git a/ogr/services/gitlab/project.py b/ogr/services/gitlab/project.py index f98e7572..3cde1596 100644 --- a/ogr/services/gitlab/project.py +++ b/ogr/services/gitlab/project.py @@ -24,7 +24,7 @@ from typing import List, Optional, Dict, Set, Union import gitlab -from gitlab.v4.objects import Project as GitlabObjectsProject +from gitlab.v4.objects import GitlabGetError, Project as GitlabObjectsProject from ogr.abstract import ( PullRequest, @@ -120,6 +120,15 @@ def _construct_fork_project(self) -> Optional["GitlabProject"]: logger.debug(f"Project {self.repo}/{user_login} does not exist: {ex}") return None + def exists(self) -> bool: + try: + _ = self.gitlab_repo + return True + except GitlabGetError as ex: + if "404 Project Not Found" in str(ex): + return False + raise GitlabAPIException from ex + def is_private(self) -> bool: """ Is this repo private? (accessible only by users with granted access) diff --git a/ogr/services/pagure/project.py b/ogr/services/pagure/project.py index 9b75cf0d..935b3be9 100644 --- a/ogr/services/pagure/project.py +++ b/ogr/services/pagure/project.py @@ -307,7 +307,7 @@ def get_fork(self, create: bool = True) -> Optional["PagureProject"]: return None return self._construct_fork_project() - def exists(self): + def exists(self) -> bool: response = self._call_project_api_raw() return response.ok diff --git a/tests/integration/gitlab/test_data/test_generic_commands/tests.integration.gitlab.test_generic_commands.GenericCommands.test_project_exists.yaml b/tests/integration/gitlab/test_data/test_generic_commands/tests.integration.gitlab.test_generic_commands.GenericCommands.test_project_exists.yaml new file mode 100644 index 00000000..9413ec23 --- /dev/null +++ b/tests/integration/gitlab/test_data/test_generic_commands/tests.integration.gitlab.test_generic_commands.GenericCommands.test_project_exists.yaml @@ -0,0 +1,250 @@ +_requre: + DataTypes: 1 + key_strategy: StorageKeysInspectSimple + version_storage_file: 2 +requests.sessions: + send: + GET: + https://gitlab.com/api/v4/projects/packit-service%2Fogr-tests: + - metadata: + latency: 0.4326820373535156 + module_call_list: + - unittest.main + - teamcity.unittestpy + - unittest.runner + - unittest.suite + - unittest.case + - requre.online_replacing + - test_generic_commands + - ogr.services.gitlab.project + - gitlab.exceptions + - gitlab.mixins + - gitlab + - requre.objects + - requre.cassette + - requests.sessions + - send + output: + __store_indicator: 2 + _content: + _links: + events: https://gitlab.com/api/v4/projects/14233409/events + issues: https://gitlab.com/api/v4/projects/14233409/issues + labels: https://gitlab.com/api/v4/projects/14233409/labels + members: https://gitlab.com/api/v4/projects/14233409/members + merge_requests: https://gitlab.com/api/v4/projects/14233409/merge_requests + repo_branches: https://gitlab.com/api/v4/projects/14233409/repository/branches + self: https://gitlab.com/api/v4/projects/14233409 + allow_merge_on_skipped_pipeline: null + approvals_before_merge: 0 + archived: false + auto_cancel_pending_pipelines: enabled + auto_devops_deploy_strategy: continuous + auto_devops_enabled: false + autoclose_referenced_issues: true + avatar_url: null + build_coverage_regex: null + build_git_strategy: fetch + build_timeout: 3600 + builds_access_level: enabled + can_create_merge_request_in: true + ci_config_path: null + ci_default_git_depth: 50 + compliance_frameworks: [] + container_registry_enabled: true + created_at: '2019-09-10T10:28:09.946Z' + creator_id: 433670 + default_branch: master + description: Testing repository for python-ogr package. | https://github.com/packit-service/ogr + emails_disabled: null + empty_repo: false + external_authorization_classification_label: '' + forking_access_level: enabled + forks_count: 4 + http_url_to_repo: https://gitlab.com/packit-service/ogr-tests.git + id: 14233409 + import_error: null + import_status: none + issues_access_level: enabled + issues_enabled: true + jobs_enabled: true + last_activity_at: '2020-10-12T10:27:00.646Z' + lfs_enabled: true + marked_for_deletion_at: null + marked_for_deletion_on: null + merge_method: merge + merge_requests_access_level: enabled + merge_requests_enabled: true + mirror: false + name: ogr-tests + name_with_namespace: packit-service / ogr-tests + namespace: + avatar_url: /uploads/-/system/group/avatar/6032704/packit-logo.png + full_path: packit-service + id: 6032704 + kind: group + name: packit-service + parent_id: null + path: packit-service + web_url: https://gitlab.com/groups/packit-service + only_allow_merge_if_all_discussions_are_resolved: false + only_allow_merge_if_pipeline_succeeds: false + open_issues_count: 57 + packages_enabled: true + pages_access_level: enabled + path: ogr-tests + path_with_namespace: packit-service/ogr-tests + permissions: + group_access: + access_level: 50 + notification_level: 3 + project_access: null + printing_merge_request_link_enabled: true + public_jobs: true + readme_url: https://gitlab.com/packit-service/ogr-tests/-/blob/master/README.md + remove_source_branch_after_merge: null + repository_access_level: enabled + request_access_enabled: false + resolve_outdated_diff_discussions: false + runners_token: VjGdzzwZbsTY37sxUeWL + service_desk_address: incoming+packit-service-ogr-tests-14233409-issue-@incoming.gitlab.com + service_desk_enabled: true + shared_runners_enabled: true + shared_with_groups: [] + snippets_access_level: enabled + snippets_enabled: true + ssh_url_to_repo: git@gitlab.com:packit-service/ogr-tests.git + star_count: 0 + suggestion_commit_message: null + tag_list: [] + visibility: public + web_url: https://gitlab.com/packit-service/ogr-tests + wiki_access_level: enabled + wiki_enabled: true + _next: null + elapsed: 0.2 + encoding: null + headers: + CF-Cache-Status: DYNAMIC + CF-RAY: 5e1fede9dc4327a0-PRG + Cache-Control: max-age=0, private, must-revalidate + Connection: keep-alive + Content-Encoding: gzip + Content-Type: application/json + Date: Fri, 01 Nov 2019 13-36-03 GMT + Etag: W/"8fa6c889a7a81210afaac7009d5f98fa" + Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" + GitLab-LB: fe-10-lb-gprd + GitLab-SV: localhost + RateLimit-Limit: '600' + RateLimit-Observed: '6' + RateLimit-Remaining: '594' + RateLimit-Reset: '1602664192' + RateLimit-ResetTime: Wed, 14 Oct 2020 08:29:52 GMT + Referrer-Policy: strict-origin-when-cross-origin + Server: cloudflare + Strict-Transport-Security: max-age=31536000 + Transfer-Encoding: chunked + Vary: Accept-Encoding, Origin + X-Content-Type-Options: nosniff + X-Frame-Options: SAMEORIGIN + X-Request-Id: jbWNNpu98k5 + X-Runtime: '0.076490' + cf-request-id: 05c7d30626000027a0ba910200000001 + raw: !!binary "" + reason: OK + status_code: 200 + https://gitlab.com/api/v4/user: + - metadata: + latency: 0.4252171516418457 + module_call_list: + - unittest.main + - teamcity.unittestpy + - unittest.runner + - unittest.suite + - unittest.case + - requre.online_replacing + - test_generic_commands + - ogr.services.gitlab.project + - ogr.services.gitlab.service + - gitlab + - gitlab.exceptions + - gitlab.mixins + - gitlab + - requre.objects + - requre.cassette + - requests.sessions + - send + output: + __store_indicator: 2 + _content: + avatar_url: https://assets.gitlab-static.net/uploads/-/system/user/avatar/433670/avatar.png + bio: '' + bio_html: '' + can_create_group: true + can_create_project: true + color_scheme_id: 5 + confirmed_at: '2016-03-01T11:08:34.779Z' + created_at: '2016-03-01T11:08:00.390Z' + current_sign_in_at: '2020-09-14T09:21:53.853Z' + email: lachmanfrantisek@gmail.com + external: false + extra_shared_runners_minutes_limit: null + id: 433670 + identities: [] + job_title: '' + last_activity_on: '2020-10-14' + last_sign_in_at: '2020-09-07T09:53:44.736Z' + linkedin: '' + location: '' + name: "Franti\u0161ek Lachman" + organization: '' + private_profile: false + projects_limit: 100000 + public_email: lachmanfrantisek@gmail.com + shared_runners_minutes_limit: 2000 + skype: '' + state: active + theme_id: 2 + twitter: '' + two_factor_enabled: true + username: lachmanfrantisek + web_url: https://gitlab.com/lachmanfrantisek + website_url: lachman.tk + work_information: null + _next: null + elapsed: 0.2 + encoding: null + headers: + CF-Cache-Status: DYNAMIC + CF-RAY: 5e1fede3cba727a0-PRG + Cache-Control: max-age=0, private, must-revalidate + Connection: keep-alive + Content-Encoding: gzip + Content-Type: application/json + Date: Fri, 01 Nov 2019 13-36-03 GMT + Etag: W/"e5fffd1b75a481aa7959b34bc1c7b3b4" + Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" + GitLab-LB: fe-10-lb-gprd + GitLab-SV: localhost + RateLimit-Limit: '600' + RateLimit-Observed: '5' + RateLimit-Remaining: '595' + RateLimit-Reset: '1602664191' + RateLimit-ResetTime: Wed, 14 Oct 2020 08:29:51 GMT + Referrer-Policy: strict-origin-when-cross-origin + Server: cloudflare + Set-Cookie: __cfduid=d36a65c61db92895ecc95a0d067bb7bca1602664131; expires=Fri, + 13-Nov-20 08:28:51 GMT; path=/; domain=.gitlab.com; HttpOnly; SameSite=Lax; + Secure + Strict-Transport-Security: max-age=31536000 + Transfer-Encoding: chunked + Vary: Accept-Encoding, Origin + X-Content-Type-Options: nosniff + X-Frame-Options: SAMEORIGIN + X-Request-Id: IWwJIg1d4i + X-Runtime: '0.029543' + cf-request-id: 05c7d3025a000027a0ba8d6200000001 + raw: !!binary "" + reason: OK + status_code: 200 diff --git a/tests/integration/gitlab/test_data/test_generic_commands/tests.integration.gitlab.test_generic_commands.GenericCommands.test_project_not_exists.yaml b/tests/integration/gitlab/test_data/test_generic_commands/tests.integration.gitlab.test_generic_commands.GenericCommands.test_project_not_exists.yaml new file mode 100644 index 00000000..19db5e28 --- /dev/null +++ b/tests/integration/gitlab/test_data/test_generic_commands/tests.integration.gitlab.test_generic_commands.GenericCommands.test_project_not_exists.yaml @@ -0,0 +1,153 @@ +_requre: + DataTypes: 1 + key_strategy: StorageKeysInspectSimple + version_storage_file: 2 +requests.sessions: + send: + GET: + https://gitlab.com/api/v4/projects/some-none-existing-namespace%2Fsome-non-existing-repo: + - metadata: + latency: 0.3785989284515381 + module_call_list: + - unittest.main + - teamcity.unittestpy + - unittest.runner + - unittest.suite + - unittest.case + - requre.online_replacing + - test_generic_commands + - ogr.services.gitlab.project + - gitlab.exceptions + - gitlab.mixins + - gitlab + - requre.objects + - requre.cassette + - requests.sessions + - send + output: + __store_indicator: 2 + _content: + message: 404 Project Not Found + _next: null + elapsed: 0.2 + encoding: null + headers: + CF-Cache-Status: DYNAMIC + CF-RAY: 5e1feed7aa2927b8-PRG + Cache-Control: no-cache + Connection: keep-alive + Content-Length: '35' + Content-Type: application/json + Date: Fri, 01 Nov 2019 13-36-03 GMT + Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" + GitLab-LB: fe-07-lb-gprd + GitLab-SV: localhost + RateLimit-Limit: '600' + RateLimit-Observed: '5' + RateLimit-Remaining: '595' + RateLimit-Reset: '1602664230' + RateLimit-ResetTime: Wed, 14 Oct 2020 08:30:30 GMT + Server: cloudflare + Vary: Origin + X-Content-Type-Options: nosniff + X-Frame-Options: SAMEORIGIN + X-Request-Id: qWtcSowYEj2 + X-Runtime: '0.025590' + cf-request-id: 05c7d39ac8000027b8642d9200000001 + raw: !!binary "" + reason: Not Found + status_code: 404 + https://gitlab.com/api/v4/user: + - metadata: + latency: 0.41721367835998535 + module_call_list: + - unittest.main + - teamcity.unittestpy + - unittest.runner + - unittest.suite + - unittest.case + - requre.online_replacing + - test_generic_commands + - ogr.services.gitlab.project + - ogr.services.gitlab.service + - gitlab + - gitlab.exceptions + - gitlab.mixins + - gitlab + - requre.objects + - requre.cassette + - requests.sessions + - send + output: + __store_indicator: 2 + _content: + avatar_url: https://assets.gitlab-static.net/uploads/-/system/user/avatar/433670/avatar.png + bio: '' + bio_html: '' + can_create_group: true + can_create_project: true + color_scheme_id: 5 + confirmed_at: '2016-03-01T11:08:34.779Z' + created_at: '2016-03-01T11:08:00.390Z' + current_sign_in_at: '2020-09-14T09:21:53.853Z' + email: lachmanfrantisek@gmail.com + external: false + extra_shared_runners_minutes_limit: null + id: 433670 + identities: [] + job_title: '' + last_activity_on: '2020-10-14' + last_sign_in_at: '2020-09-07T09:53:44.736Z' + linkedin: '' + location: '' + name: "Franti\u0161ek Lachman" + organization: '' + private_profile: false + projects_limit: 100000 + public_email: lachmanfrantisek@gmail.com + shared_runners_minutes_limit: 2000 + skype: '' + state: active + theme_id: 2 + twitter: '' + two_factor_enabled: true + username: lachmanfrantisek + web_url: https://gitlab.com/lachmanfrantisek + website_url: lachman.tk + work_information: null + _next: null + elapsed: 0.2 + encoding: null + headers: + CF-Cache-Status: DYNAMIC + CF-RAY: 5e1feed25b8827b8-PRG + Cache-Control: max-age=0, private, must-revalidate + Connection: keep-alive + Content-Encoding: gzip + Content-Type: application/json + Date: Fri, 01 Nov 2019 13-36-03 GMT + Etag: W/"e5fffd1b75a481aa7959b34bc1c7b3b4" + Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" + GitLab-LB: fe-02-lb-gprd + GitLab-SV: localhost + RateLimit-Limit: '600' + RateLimit-Observed: '4' + RateLimit-Remaining: '596' + RateLimit-Reset: '1602664229' + RateLimit-ResetTime: Wed, 14 Oct 2020 08:30:29 GMT + Referrer-Policy: strict-origin-when-cross-origin + Server: cloudflare + Set-Cookie: __cfduid=d92c9f8dbc373bdccd169441e9bfaadd41602664169; expires=Fri, + 13-Nov-20 08:29:29 GMT; path=/; domain=.gitlab.com; HttpOnly; SameSite=Lax; + Secure + Strict-Transport-Security: max-age=31536000 + Transfer-Encoding: chunked + Vary: Accept-Encoding, Origin + X-Content-Type-Options: nosniff + X-Frame-Options: SAMEORIGIN + X-Request-Id: so6050nRPR9 + X-Runtime: '0.024519' + cf-request-id: 05c7d39779000027b864291200000001 + raw: !!binary "" + reason: OK + status_code: 200 diff --git a/tests/integration/gitlab/test_generic_commands.py b/tests/integration/gitlab/test_generic_commands.py index b8dc57ec..336911db 100644 --- a/tests/integration/gitlab/test_generic_commands.py +++ b/tests/integration/gitlab/test_generic_commands.py @@ -1,11 +1,11 @@ from datetime import datetime import pytest -from requre.online_replacing import record_requests_for_all_methods -from tests.integration.gitlab.base import GitlabTests -from ogr.abstract import CommitStatus, AccessLevel +from ogr.abstract import AccessLevel, CommitStatus from ogr.exceptions import GitlabAPIException +from requre.online_replacing import record_requests_for_all_methods +from tests.integration.gitlab.base import GitlabTests @record_requests_for_all_methods() @@ -159,6 +159,14 @@ def test_get_web_url(self): def test_full_repo_name(self): assert self.project.full_repo_name == "packit-service/ogr-tests" + def test_project_exists(self): + assert self.project.exists() + + def test_project_not_exists(self): + assert not self.service.get_project( + repo="some-non-existing-repo", namespace="some-none-existing-namespace" + ).exists() + def test_get_owners(self): owners = self.project.get_owners() assert set(("lachmanfrantisek", "lbarcziova")).issubset(set(owners))