Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

feat: Adds permission check shortcut using repo.installed_by #33

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions src/app/api/api_v1/endpoints/guidelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
telemetry_client.capture(user.id, event="guideline-creation", properties={"repo_id": payload.repo_id})
# Check if user is allowed
repo = cast(Repository, await repos.get(payload.repo_id, strict=True))
gh_client.check_user_permission(user, repo.full_name, repo.owner_id, payload.github_token)
gh_client.check_user_permission(user, repo.full_name, repo.owner_id, payload.github_token, repo.installed_by)
return await guidelines.create(GuidelineCreation(**payload.dict()))


Expand Down Expand Up @@ -65,7 +65,7 @@
telemetry_client.capture(user.id, event="guideline-content", properties={"repo_id": guideline.repo_id})
# Check if user is allowed
repo = cast(Repository, await repos.get(guideline.repo_id, strict=True))
gh_client.check_user_permission(user, repo.full_name, repo.owner_id, payload.github_token)
gh_client.check_user_permission(user, repo.full_name, repo.owner_id, payload.github_token, repo.installed_by)

Check warning on line 68 in src/app/api/api_v1/endpoints/guidelines.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/guidelines.py#L68

Added line #L68 was not covered by tests
return guideline


Expand All @@ -82,7 +82,7 @@
telemetry_client.capture(user.id, event="guideline-order", properties={"repo_id": guideline.repo_id})
# Check if user is allowed
repo = cast(Repository, await repos.get(guideline.repo_id, strict=True))
gh_client.check_user_permission(user, repo.full_name, repo.owner_id, payload.github_token)
gh_client.check_user_permission(user, repo.full_name, repo.owner_id, payload.github_token, repo.installed_by)

Check warning on line 85 in src/app/api/api_v1/endpoints/guidelines.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/guidelines.py#L85

Added line #L85 was not covered by tests
return guideline


Expand All @@ -98,5 +98,5 @@
telemetry_client.capture(user.id, event="guideline-deletion", properties={"repo_id": guideline.repo_id})
# Check if user is allowed
repo = cast(Repository, await repos.get(guideline.repo_id, strict=True))
gh_client.check_user_permission(user, repo.full_name, repo.owner_id, payload.github_token)
gh_client.check_user_permission(user, repo.full_name, repo.owner_id, payload.github_token, repo.installed_by)
await guidelines.delete(guideline_id)
6 changes: 3 additions & 3 deletions src/app/api/api_v1/endpoints/repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Guideline IDs for that repo don't match."
)
# Check if user is allowed
gh_client.check_user_permission(user, repo.full_name, repo.owner_id, payload.github_token)
gh_client.check_user_permission(user, repo.full_name, repo.owner_id, payload.github_token, repo.installed_by)

Check warning on line 84 in src/app/api/api_v1/endpoints/repos.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/repos.py#L84

Added line #L84 was not covered by tests
# Update all order
return [
await guidelines.update(guideline_id, OrderUpdate(order=order_idx, updated_at=datetime.utcnow()))
Expand All @@ -99,7 +99,7 @@
telemetry_client.capture(user.id, event="repo-disable", properties={"repo_id": repo_id})
# Check if user is allowed
repo = cast(Repository, await repos.get(repo_id, strict=True))
gh_client.check_user_permission(user, repo.full_name, repo.owner_id, payload.github_token)
gh_client.check_user_permission(user, repo.full_name, repo.owner_id, payload.github_token, repo.installed_by)
return await repos.update(repo_id, RepoUpdate(is_active=False))


Expand All @@ -113,7 +113,7 @@
telemetry_client.capture(user.id, event="repo-enable", properties={"repo_id": repo_id})
# Check if user is allowed
repo = cast(Repository, await repos.get(repo_id, strict=True))
gh_client.check_user_permission(user, repo.full_name, repo.owner_id, payload.github_token)
gh_client.check_user_permission(user, repo.full_name, repo.owner_id, payload.github_token, repo.installed_by)
return await repos.update(repo_id, RepoUpdate(is_active=True))


Expand Down
13 changes: 11 additions & 2 deletions src/app/services/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,18 @@ def get_permission(self, repo_name: str, user_name: str, github_token: str) -> s
return self._get(f"repos/{repo_name}/collaborators/{user_name}/permission", github_token)["role_name"]

def check_user_permission(
self, user: User, repo_full_name: str, repo_owner_id: int, github_token: Union[str, None]
self,
user: User,
repo_full_name: str,
repo_owner_id: int,
github_token: Union[str, None],
repo_installer_id: Union[int, None] = None,
) -> None:
if user.scope != UserScope.ADMIN and repo_owner_id != user.id:
if (
user.scope != UserScope.ADMIN
and repo_owner_id != user.id
and (not isinstance(repo_installer_id, int) or repo_installer_id != user.id)
):
if not isinstance(github_token, str):
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Expected `github_token` to check access."
Expand Down
Loading