Skip to content

test: add api integration test for ci_cd_token allowlist #2797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
48726fd
chore(deps): update all non-major dependencies
renovate[bot] Jan 26, 2024
f59aee3
chore(deps): update dependency black to v24
renovate[bot] Jan 29, 2024
4e68d32
chore: adapt style for black v24
nejch Jan 29, 2024
7ec3189
fix(cli): allow exclusive arguments as optional (#2770)
Sjord Jan 29, 2024
d2be1f7
chore(deps): update codecov/codecov-action action to v4
renovate[bot] Feb 5, 2024
7dc2fa6
chore(deps): update all non-major dependencies
renovate[bot] Feb 5, 2024
3501716
chore: update version of `black` for `pre-commit`
JohnVillalovos Feb 5, 2024
35d2aec
chore(deps): update dependency pytest-docker to v3
renovate[bot] Feb 5, 2024
253babb
chore(deps): update dependency pytest to v8
renovate[bot] Feb 5, 2024
f6fd02d
chore(deps): update dependency furo to v2024
renovate[bot] Feb 5, 2024
c631eeb
docs(artifacts): Fix argument indentation
maxnoe Feb 8, 2024
9440a32
fix(api): update manual job status when playing it
1ace Feb 4, 2024
a867c48
feat(api): allow updating protected branches (#2771)
Sjord Feb 10, 2024
3c4b27e
chore(deps): update all non-major dependencies
renovate[bot] Feb 12, 2024
e11d889
chore(deps): update python-semantic-release/python-semantic-release a…
renovate[bot] Feb 12, 2024
9596e09
feat(job_token_scope): support job token access allowlist API
nejch Jan 19, 2024
4203bec
test: add api integration test for ci_cd_token allowlist
TimKnight-DWP Feb 13, 2024
e2cae65
test: skip in Gitlab 15
TimKnight-DWP Feb 14, 2024
44a354a
Merge branch 'feat/job-token-scope-allowlist' into feat/job-token-sco…
TimKnight-DWP Mar 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
TOXENV: docs
run: tox
- name: Archive generated docs
uses: actions/upload-artifact@v4.1.0
uses: actions/upload-artifact@v4.3.1
with:
name: html-docs
path: build/sphinx/html/
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

- name: Python Semantic Release
id: release
uses: python-semantic-release/python-semantic-release@v8.7.2
uses: python-semantic-release/python-semantic-release@v9.0.3
with:
github_token: ${{ secrets.RELEASE_GITHUB_TOKEN }}

Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
TOXENV: ${{ matrix.toxenv }}
run: tox -- --override-ini='log_cli=True'
- name: Upload codecov coverage
uses: codecov/codecov-action@v3.1.4
uses: codecov/codecov-action@v4.0.1
with:
files: ./coverage.xml
flags: ${{ matrix.toxenv }}
Expand All @@ -100,7 +100,7 @@ jobs:
TOXENV: cover
run: tox
- name: Upload codecov coverage
uses: codecov/codecov-action@v3.1.4
uses: codecov/codecov-action@v4.0.1
with:
files: ./coverage.xml
flags: unit
Expand All @@ -119,7 +119,7 @@ jobs:
pip install -r requirements-test.txt
- name: Build package
run: python -m build -o dist/
- uses: actions/upload-artifact@v4.1.0
- uses: actions/upload-artifact@v4.3.1
with:
name: dist
path: dist
Expand All @@ -133,7 +133,7 @@ jobs:
uses: actions/setup-python@v5.0.0
with:
python-version: '3.12'
- uses: actions/download-artifact@v4.1.1
- uses: actions/download-artifact@v4.1.2
with:
name: dist
path: dist
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ default_language_version:

repos:
- repo: https://github.com/psf/black
rev: 23.12.1
rev: 24.1.1
hooks:
- id: black
- repo: https://github.com/commitizen-tools/commitizen
rev: v3.13.0
rev: v3.14.1
hooks:
- id: commitizen
stages: [commit-msg]
Expand Down Expand Up @@ -47,6 +47,6 @@ repos:
- id: rst-directive-colons
- id: rst-inline-touching-normal
- repo: https://github.com/maxbrunet/pre-commit-renovate
rev: 37.131.0
rev: 37.183.0
hooks:
- id: renovate-config-validator
5 changes: 5 additions & 0 deletions docs/gl_objects/protected_branches.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ Get a single protected branch::

p_branch = project.protectedbranches.get('main')

Update a protected branch:

p_branch.allow_force_push = True
p_branch.save()

Create a protected branch::

p_branch = project.protectedbranches.create({
Expand Down
6 changes: 2 additions & 4 deletions gitlab/_backends/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

class BackendResponse(Protocol):
@abc.abstractmethod
def __init__(self, response: requests.Response) -> None:
...
def __init__(self, response: requests.Response) -> None: ...


class Backend(Protocol):
Expand All @@ -30,5 +29,4 @@ def http_request(
verify: Optional[Union[bool, str]],
stream: Optional[bool],
**kwargs: Any,
) -> BackendResponse:
...
) -> BackendResponse: ...
1 change: 0 additions & 1 deletion gitlab/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@


class Gitlab:

"""Represents a GitLab server connection.

Args:
Expand Down
9 changes: 9 additions & 0 deletions gitlab/v4/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ def _populate_sub_parser_by_class(
sub_parser_action.add_argument(
f"--{x.replace('_', '-')}", required=False
)
if mgr_cls._create_attrs.exclusive:
group = sub_parser_action.add_mutually_exclusive_group()
for x in mgr_cls._create_attrs.exclusive:
group.add_argument(f"--{x.replace('_', '-')}")

if action_name == "update":
if cls._id_attr is not None:
Expand All @@ -280,6 +284,11 @@ def _populate_sub_parser_by_class(
f"--{x.replace('_', '-')}", required=False
)

if mgr_cls._update_attrs.exclusive:
group = sub_parser_action.add_mutually_exclusive_group()
for x in mgr_cls._update_attrs.exclusive:
group.add_argument(f"--{x.replace('_', '-')}")

if cls.__name__ in cli.custom_actions:
name = cls.__name__
for action_name in cli.custom_actions[name]:
Expand Down
3 changes: 2 additions & 1 deletion gitlab/v4/objects/artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
GitLab API:
https://docs.gitlab.com/ee/api/job_artifacts.html
"""

from typing import Any, Callable, Iterator, Optional, TYPE_CHECKING, Union

import requests
Expand Down Expand Up @@ -61,7 +62,7 @@ def download(

Args:
ref_name: Branch or tag name in repository. HEAD or SHA references
are not supported.
are not supported.
job: The name of the job.
job_token: Job token for multi-project pipeline triggers.
streamed: If True the data will be processed by chunks of
Expand Down
1 change: 1 addition & 0 deletions gitlab/v4/objects/audit_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
GitLab API:
https://docs.gitlab.com/ee/api/audit_events.html
"""

from typing import Any, cast, Union

from gitlab.base import RESTManager, RESTObject
Expand Down
13 changes: 10 additions & 3 deletions gitlab/v4/objects/branches.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from typing import Any, cast, Union

from gitlab.base import RESTManager, RESTObject
from gitlab.mixins import NoUpdateMixin, ObjectDeleteMixin
from gitlab.mixins import (
CRUDMixin,
NoUpdateMixin,
ObjectDeleteMixin,
SaveMixin,
UpdateMethod,
)
from gitlab.types import RequiredOptional

__all__ = [
Expand All @@ -28,11 +34,11 @@ def get(
return cast(ProjectBranch, super().get(id=id, lazy=lazy, **kwargs))


class ProjectProtectedBranch(ObjectDeleteMixin, RESTObject):
class ProjectProtectedBranch(SaveMixin, ObjectDeleteMixin, RESTObject):
_id_attr = "name"


class ProjectProtectedBranchManager(NoUpdateMixin, RESTManager):
class ProjectProtectedBranchManager(CRUDMixin, RESTManager):
_path = "/projects/{project_id}/protected_branches"
_obj_cls = ProjectProtectedBranch
_from_parent_attrs = {"project_id": "id"}
Expand All @@ -49,6 +55,7 @@ class ProjectProtectedBranchManager(NoUpdateMixin, RESTManager):
"code_owner_approval_required",
),
)
_update_method = UpdateMethod.PATCH

def get(
self, id: Union[str, int], lazy: bool = False, **kwargs: Any
Expand Down
1 change: 1 addition & 0 deletions gitlab/v4/objects/deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
GitLab API:
https://docs.gitlab.com/ee/api/deployments.html
"""

from typing import Any, cast, Dict, Optional, TYPE_CHECKING, Union

from gitlab import cli
Expand Down
1 change: 1 addition & 0 deletions gitlab/v4/objects/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
GitLab API:
https://docs.gitlab.com/ee/api/features.html
"""

from typing import Any, Optional, TYPE_CHECKING, Union

from gitlab import exceptions as exc
Expand Down
1 change: 0 additions & 1 deletion gitlab/v4/objects/job_token_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
)
from gitlab.types import RequiredOptional


__all__ = [
"ProjectJobTokenScope",
"ProjectJobTokenScopeManager",
Expand Down
5 changes: 4 additions & 1 deletion gitlab/v4/objects/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ def play(self, **kwargs: Any) -> None:
GitlabJobPlayError: If the job could not be triggered
"""
path = f"{self.manager.path}/{self.encoded_id}/play"
self.manager.gitlab.http_post(path, **kwargs)
result = self.manager.gitlab.http_post(path, **kwargs)
if TYPE_CHECKING:
assert isinstance(result, dict)
self._update_attrs(result)

@cli.register_custom_action("ProjectJob")
@exc.on_http_error(exc.GitlabJobEraseError)
Expand Down
1 change: 1 addition & 0 deletions gitlab/v4/objects/merge_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
https://docs.gitlab.com/ee/api/merge_requests.html
https://docs.gitlab.com/ee/api/merge_request_approvals.html
"""

from typing import Any, cast, Dict, Optional, TYPE_CHECKING, Union

import requests
Expand Down
1 change: 1 addition & 0 deletions gitlab/v4/objects/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
GitLab API:
https://docs.gitlab.com/ee/api/projects.html
"""

from typing import (
Any,
Callable,
Expand Down
1 change: 1 addition & 0 deletions gitlab/v4/objects/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

Currently this module only contains repository-related methods for projects.
"""

from typing import Any, Callable, Dict, Iterator, List, Optional, TYPE_CHECKING, Union

import requests
Expand Down
1 change: 1 addition & 0 deletions gitlab/v4/objects/secure_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
GitLab API:
https://docs.gitlab.com/ee/api/secure_files.html
"""

from typing import Any, Callable, cast, Iterator, Optional, TYPE_CHECKING, Union

import requests
Expand Down
1 change: 1 addition & 0 deletions gitlab/v4/objects/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
https://docs.gitlab.com/ee/api/users.html
https://docs.gitlab.com/ee/api/projects.html#list-projects-starred-by-a-user
"""

from typing import Any, cast, Dict, List, Optional, Union

import requests
Expand Down
1 change: 1 addition & 0 deletions gitlab/v4/objects/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
https://docs.gitlab.com/ee/api/project_level_variables.html
https://docs.gitlab.com/ee/api/group_level_variables.html
"""

from typing import Any, cast, Union

from gitlab.base import RESTManager, RESTObject
Expand Down
2 changes: 1 addition & 1 deletion requirements-docker.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
-r requirements.txt
-r requirements-test.txt
pytest-docker==2.0.1
pytest-docker==3.1.1
2 changes: 1 addition & 1 deletion requirements-docs.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-r requirements.txt
furo==2023.9.10
furo==2024.1.29
jinja2==3.1.3
myst-parser==2.0.0
sphinx==7.2.6
Expand Down
10 changes: 5 additions & 5 deletions requirements-lint.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
-r requirements.txt
argcomplete==2.0.0
black==23.12.1
commitizen==3.13.0
black==24.1.1
commitizen==3.14.1
flake8==7.0.0
isort==5.13.2
mypy==1.8.0
pylint==3.0.3
pytest==7.4.4
pytest==8.0.0
responses==0.24.1
types-PyYAML==6.0.12.12
types-requests==2.31.0.20240106
types-setuptools==69.0.0.20240106
types-requests==2.31.0.20240125
types-setuptools==69.0.0.20240125
2 changes: 1 addition & 1 deletion requirements-precommit.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pre-commit==3.6.0
pre-commit==3.6.1
4 changes: 2 additions & 2 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
-r requirements.txt
build==1.0.3
coverage==7.4.0
coverage==7.4.1
pytest-console-scripts==1.4.1
pytest-cov==4.1.0
pytest-github-actions-annotate-failures==0.2.0
pytest==7.4.4
pytest==8.0.0
PyYaml==6.0.1
responses==0.24.1
wheel==0.42.0
1 change: 1 addition & 0 deletions tests/functional/api/test_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
GitLab API:
https://docs.gitlab.com/ce/api/keys.html
"""

import base64
import hashlib

Expand Down
1 change: 1 addition & 0 deletions tests/functional/api/test_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
https://docs.gitlab.com/ce/api/packages.html
https://docs.gitlab.com/ee/user/packages/generic_packages
"""

from collections.abc import Iterator

from gitlab.v4.objects import GenericPackage
Expand Down
50 changes: 50 additions & 0 deletions tests/functional/api/test_project_job_token_scope.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest


# TODO: can be enabled when https://github.com/python-gitlab/python-gitlab/pull/2790 merged
@pytest.mark.xfail(reason="project job_token_scope api only in 16.*")
def test_add_project_to_job_token_scope_allowlist(gl, project):
project_to_add = gl.projects.create({"name": "Ci_Cd_token_add_proj"})

scope = project.job_token_scope.get()
resp = scope.allowlist.create({"target_project_id": project_to_add.id})

assert resp.source_project_id == project.id
assert resp.target_project_id == project_to_add.id

project_to_add.delete()


@pytest.mark.xfail(reason="project job_token_scope api only in 16.*")
def test_projects_job_token_scope_allowlist_contains_added_project_name(gl, project):
scope = project.job_token_scope.get()
assert len(scope.allowlist.list()) == 0

project_name = "Ci_Cd_token_named_proj"
project_to_add = gl.projects.create({"name": project_name})
scope.allowlist.create({"target_project_id": project_to_add.id})

scope.refresh()
assert any(allowed.name == project_name for allowed in scope.allowlist.list())

project_to_add.delete()


@pytest.mark.xfail(reason="project job_token_scope api only in 16.*")
def test_remove_project_by_id_from_projects_job_token_scope_allowlist(gl, project):
scope = project.job_token_scope.get()
assert len(scope.allowlist.list()) == 0

project_to_add = gl.projects.create({"name": "Ci_Cd_token_remove_proj"})

scope.allowlist.create({"target_project_id": project_to_add.id})

scope.refresh()
assert len(scope.allowlist.list()) != 0

scope.allowlist.remove(project_to_add.id)

scope.refresh()
assert len(scope.allowlist.list()) == 0

project_to_add.delete()
Loading