Skip to content

Commit 15dad01

Browse files
committed
Added the set_permissions function.
1 parent bff7e63 commit 15dad01

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

src/pygobbler/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@
3939
from .refresh_usage import refresh_usage
4040
from .approve_probation import approve_probation
4141
from .reject_probation import reject_probation
42+
from .set_permissions import set_permissions

src/pygobbler/set_permissions.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from typing import Optional, List, Dict
2+
from . import _utils as ut
3+
from . import fetch_permissions
4+
5+
6+
def set_permissions(project: str, registry: str, staging: str, url: str, owners: Optional[List] = None, uploaders: Optional[Dict] = None, append: bool = True):
7+
"""
8+
Set the owner and uploader permissions for a project.
9+
10+
Args:
11+
project:
12+
Name of the project.
13+
14+
registry:
15+
Path to the Gobbler registry.
16+
17+
staging:
18+
Path to the staging directory.
19+
20+
url:
21+
URL of the REST API.
22+
23+
owners:
24+
List of user IDs for owners of this project. If None, no change is
25+
made to the existing owners in the project permissions.
26+
27+
uploaders:
28+
List of dictionaries specifying the authorized uploaders for this
29+
project. See the ``uploaders`` field in the return value of
30+
:py:func:`~fetch_permissions.fetch_permissions` for the expected
31+
format. If None, no change is made to the existing uploaders.
32+
33+
append:
34+
Whether ``owners`` and ``uploaders`` should be appended to the
35+
existing owners and uploaders, respectively. If False, the
36+
``owners`` and ``uploaders`` are used to replace the existing
37+
values in the project permissions.
38+
"""
39+
perms = {}
40+
41+
if append:
42+
oldperms = fetch_permissions(project, registry=registry, url=url)
43+
if owners is not None:
44+
oldset = set(oldperms["owners"])
45+
perms["owners"] = oldperms["owners"] + list(filter(lambda x : x not in oldset, owners))
46+
if uploaders is not None:
47+
perms["uploaders"] = oldperms["uploaders"] + uploaders
48+
else:
49+
if owners is not None:
50+
perms["owners"] = owners
51+
if uploaders is not None:
52+
perms["uploaders"] = uploaders
53+
54+
ut.dump_request(staging, url, "set_permissions", { "project": project, "permissions": perms })

tests/test_set_permissions.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import pygobbler as pyg
2+
3+
4+
def test_set_permissions():
5+
_, staging, registry, url = pyg.start_gobbler()
6+
pyg.remove_project("test-perms", staging=staging, url=url)
7+
pyg.create_project("test-perms", staging=staging, url=url, owners=["LTLA"])
8+
9+
until = "2022-02-02T02:20:02.02Z"
10+
pyg.set_permissions("test-perms",
11+
owners=["jkanche"],
12+
uploaders=[ { "id": "lawremi", "until": until } ],
13+
staging=staging,
14+
url=url,
15+
registry=registry
16+
)
17+
18+
perms = pyg.fetch_permissions("test-perms", registry=registry, url=url)
19+
assert perms["owners"] == [ "LTLA", "jkanche" ]
20+
assert len(perms["uploaders"]) == 1
21+
assert perms["uploaders"][0]["id"] == "lawremi"
22+
assert perms["uploaders"][0]["until"] == until
23+
24+
# Checking uploader appending, while also checking owners=NULL.
25+
pyg.set_permissions("test-perms", uploaders=[ { "id": "ArtifactDB-bot", "trusted": True } ], staging=staging, url=url, registry=registry)
26+
perms = pyg.fetch_permissions("test-perms", registry=registry, url=url)
27+
assert perms["owners"] == [ "LTLA", "jkanche" ]
28+
assert len(perms["uploaders"]) == 2
29+
assert perms["uploaders"][0]["id"] == "lawremi"
30+
assert perms["uploaders"][1]["id"] == "ArtifactDB-bot"
31+
assert perms["uploaders"][1]["trusted"]
32+
33+
# Checking union of owners, and also that uploaders=NULL works.
34+
pyg.set_permissions("test-perms", owners=[ "PeteHaitch", "LTLA" ], staging=staging, url=url, registry=registry)
35+
perms = pyg.fetch_permissions("test-perms", registry=registry, url=url)
36+
assert perms["owners"] == [ "LTLA", "jkanche", "PeteHaitch" ]
37+
assert len(perms["uploaders"]) == 2
38+
39+
# Resetting the owners back.
40+
pyg.set_permissions("test-perms", owners=[ "LTLA" ], append=False, staging=staging, url=url, registry=registry)
41+
perms = pyg.fetch_permissions("test-perms", registry=registry, url=url)
42+
assert perms["owners"] == [ "LTLA" ]
43+
assert len(perms["uploaders"]) == 2
44+
45+
# Now resetting the uploaders.
46+
pyg.set_permissions("test-perms", uploaders=[], append=False, staging=staging, url=url, registry=registry)
47+
perms = pyg.fetch_permissions("test-perms", registry=registry, url=url)
48+
assert perms["owners"] == [ "LTLA" ]
49+
assert len(perms["uploaders"]) == 0

0 commit comments

Comments
 (0)