Skip to content
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

Add support for environments #2223

Merged
merged 6 commits into from
Jun 13, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
123 changes: 123 additions & 0 deletions github/Environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
############################ Copyrights and license ############################
# #
# Copyright 2022 Alson van der Meulen <alson.vandermeulen@dearhealth.com> #
# #
# This file is part of PyGithub. #
# http://pygithub.readthedocs.io/ #
# #
# PyGithub is free software: you can redistribute it and/or modify it under #
# the terms of the GNU Lesser General Public License as published by the Free #
# Software Foundation, either version 3 of the License, or (at your option) #
# any later version. #
# #
# PyGithub 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 Lesser General Public License for more #
# details. #
# #
# You should have received a copy of the GNU Lesser General Public License #
# along with PyGithub. If not, see <http://www.gnu.org/licenses/>. #
# #
################################################################################

import datetime
from typing import List

import github.EnvironmentDeploymentBranchPolicy
import github.EnvironmentProtectionRule
import github.GithubObject


class Environment(github.GithubObject.CompletableGithubObject):
"""
This class represents Environment. The reference can be found here https://docs.github.com/en/rest/reference/deployments#environments
"""

def __repr__(self):
return self.get__repr__({"name": self._name.value})

@property
def created_at(self) -> datetime.datetime:
self._completeIfNotSet(self._created_at)
return self._created_at.value

@property
def html_url(self) -> str:
self._completeIfNotSet(self._html_url)
return self._html_url.value

@property
def id(self) -> int:
self._completeIfNotSet(self._id)
return self._id.value

@property
def name(self) -> str:
self._completeIfNotSet(self._name)
return self._name.value

@property
def node_id(self) -> str:
self._completeIfNotSet(self._node_id)
return self._node_id.value

@property
def protection_rules(
self,
) -> List[github.EnvironmentProtectionRule.EnvironmentProtectionRule]:
self._completeIfNotSet(self._protection_rules)
return self._protection_rules.value

@property
def updated_at(self) -> datetime.datetime:
self._completeIfNotSet(self._updated_at)
return self._updated_at.value

@property
def url(self) -> str:
self._completeIfNotSet(self._url)
return self._url.value

@property
def deployment_branch_policy(
self,
) -> github.EnvironmentDeploymentBranchPolicy.EnvironmentDeploymentBranchPolicy:
self._completeIfNotSet(self._deployment_branch_policy)
return self._deployment_branch_policy.value

def _initAttributes(self):
self._created_at = github.GithubObject.NotSet
self._html_url = github.GithubObject.NotSet
self._id = github.GithubObject.NotSet
self._name = github.GithubObject.NotSet
self._node_id = github.GithubObject.NotSet
self._protection_rules = github.GithubObject.NotSet
self._updated_at = github.GithubObject.NotSet
self._url = github.GithubObject.NotSet
self._deployment_branch_policy = github.GithubObject.NotSet

def _useAttributes(self, attributes):
if "created_at" in attributes: # pragma no branch
self._created_at = self._makeDatetimeAttribute(attributes["created_at"])
if "html_url" in attributes: # pragma no branch
self._html_url = self._makeStringAttribute(attributes["html_url"])
if "id" in attributes: # pragma no branch
self._id = self._makeIntAttribute(attributes["id"])
if "name" in attributes: # pragma no branch
self._name = self._makeStringAttribute(attributes["name"])
if "node_id" in attributes: # pragma no branch
self._node_id = self._makeStringAttribute(attributes["node_id"])
if "protection_rules" in attributes: # pragma no branch
self._protection_rules = self._makeListOfClassesAttribute(
github.EnvironmentProtectionRule.EnvironmentProtectionRule,
attributes["protection_rules"],
)
if "updated_at" in attributes: # pragma no branch
self._updated_at = self._makeDatetimeAttribute(attributes["updated_at"])
if "url" in attributes: # pragma no branch
self._url = self._makeStringAttribute(attributes["url"])
if "deployment_branch_policy" in attributes: # pragma no branch
self._deployment_branch_policy = self._makeClassAttribute(
github.EnvironmentDeploymentBranchPolicy.EnvironmentDeploymentBranchPolicy,
attributes["deployment_branch_policy"],
)
75 changes: 75 additions & 0 deletions github/EnvironmentDeploymentBranchPolicy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
############################ Copyrights and license ############################
# #
# Copyright 2022 Alson van der Meulen <alson.vandermeulen@dearhealth.com> #
# #
# This file is part of PyGithub. #
# http://pygithub.readthedocs.io/ #
# #
# PyGithub is free software: you can redistribute it and/or modify it under #
# the terms of the GNU Lesser General Public License as published by the Free #
# Software Foundation, either version 3 of the License, or (at your option) #
# any later version. #
# #
# PyGithub 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 Lesser General Public License for more #
# details. #
# #
# You should have received a copy of the GNU Lesser General Public License #
# along with PyGithub. If not, see <http://www.gnu.org/licenses/>. #
# #
################################################################################

import github.EnvironmentProtectionRuleReviewer
import github.GithubObject


class EnvironmentDeploymentBranchPolicy(github.GithubObject.NonCompletableGithubObject):
"""
This class represents a deployment branch policy for an environment. The reference can be found here https://docs.github.com/en/rest/reference/deployments#environments
"""

def __repr__(self):
return self.get__repr__({})

@property
def protected_branches(self) -> bool:
return self._protected_branches.value

@property
def custom_branch_policies(self) -> bool:
return self._custom_branch_policies.value

def _initAttributes(self):
self._protected_branches = github.GithubObject.NotSet
self._custom_branch_policies = github.GithubObject.NotSet

def _useAttributes(self, attributes):
if "protected_branches" in attributes: # pragma no branch
self._protected_branches = self._makeBoolAttribute(
attributes["protected_branches"]
)
if "custom_branch_policies" in attributes: # pragma no branch
self._custom_branch_policies = self._makeBoolAttribute(
attributes["custom_branch_policies"]
)


class EnvironmentDeploymentBranchPolicyParams:
"""
This class presents the deployment branch policy parameters as can be configured for an Environment.
"""

def __init__(
self, protected_branches: bool = False, custom_branch_policies: bool = False
):
assert isinstance(protected_branches, bool)
assert isinstance(custom_branch_policies, bool)
self.protected_branches = protected_branches
self.custom_branch_policies = custom_branch_policies

def _asdict(self) -> dict:
return {
"protected_branches": self.protected_branches,
"custom_branch_policies": self.custom_branch_policies,
}
81 changes: 81 additions & 0 deletions github/EnvironmentProtectionRule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
############################ Copyrights and license ############################
# #
# Copyright 2022 Alson van der Meulen <alson.vandermeulen@dearhealth.com> #
# #
# This file is part of PyGithub. #
# http://pygithub.readthedocs.io/ #
# #
# PyGithub is free software: you can redistribute it and/or modify it under #
# the terms of the GNU Lesser General Public License as published by the Free #
# Software Foundation, either version 3 of the License, or (at your option) #
# any later version. #
# #
# PyGithub 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 Lesser General Public License for more #
# details. #
# #
# You should have received a copy of the GNU Lesser General Public License #
# along with PyGithub. If not, see <http://www.gnu.org/licenses/>. #
# #
################################################################################

from typing import List

import github.EnvironmentProtectionRuleReviewer
import github.GithubObject


class EnvironmentProtectionRule(github.GithubObject.NonCompletableGithubObject):
"""
This class represents a protection rule for an environment. The reference can be found here https://docs.github.com/en/rest/reference/deployments#environments
"""

def __repr__(self):
return self.get__repr__({"id": self._id.value})

@property
def id(self) -> int:
return self._id.value

@property
def node_id(self) -> str:
return self._node_id.value

@property
def type(self) -> str:
return self._type.value

@property
def reviewers(
self,
) -> List[
github.EnvironmentProtectionRuleReviewer.EnvironmentProtectionRuleReviewer
]:
return self._reviewers.value

@property
def wait_timer(self) -> int:
return self._wait_timer.value

def _initAttributes(self):
self._id = github.GithubObject.NotSet
self._node_id = github.GithubObject.NotSet
self._type = github.GithubObject.NotSet
self._reviewers = github.GithubObject.NotSet
self._wait_timer = github.GithubObject.NotSet

def _useAttributes(self, attributes):
if "id" in attributes: # pragma no branch
self._id = self._makeIntAttribute(attributes["id"])
if "node_id" in attributes: # pragma no branch
self._node_id = self._makeStringAttribute(attributes["node_id"])
if "type" in attributes: # pragma no branch
self._type = self._makeStringAttribute(attributes["type"])
if "reviewers" in attributes: # pragma no branch
self._reviewers = self._makeListOfClassesAttribute(
github.EnvironmentProtectionRuleReviewer.EnvironmentProtectionRuleReviewer,
attributes["reviewers"],
)
if "wait_timer" in attributes: # pragma no branch
self._wait_timer = self._makeIntAttribute(attributes["wait_timer"])
80 changes: 80 additions & 0 deletions github/EnvironmentProtectionRuleReviewer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
############################ Copyrights and license ############################
# #
# Copyright 2022 Alson van der Meulen <alson.vandermeulen@dearhealth.com> #
# #
# This file is part of PyGithub. #
# http://pygithub.readthedocs.io/ #
# #
# PyGithub is free software: you can redistribute it and/or modify it under #
# the terms of the GNU Lesser General Public License as published by the Free #
# Software Foundation, either version 3 of the License, or (at your option) #
# any later version. #
# #
# PyGithub 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 Lesser General Public License for more #
# details. #
# #
# You should have received a copy of the GNU Lesser General Public License #
# along with PyGithub. If not, see <http://www.gnu.org/licenses/>. #
# #
################################################################################

from __future__ import annotations

import github.GithubObject
import github.NamedUser
import github.Team


class EnvironmentProtectionRuleReviewer(github.GithubObject.NonCompletableGithubObject):
"""
This class represents a reviewer for an EnvironmentProtectionRule. The reference can be found here https://docs.github.com/en/rest/reference/deployments#environments
"""

def __repr__(self):
return self.get__repr__({"type": self._type.value})

@property
def type(self) -> str:
return self._type.value

@property
def reviewer(self) -> github.NamedUser.NamedUser | github.Team.Team:
return self._reviewer.value

def _initAttributes(self):
self._type = github.GithubObject.NotSet
self._reviewer = github.GithubObject.NotSet

def _useAttributes(self, attributes):
if "type" in attributes: # pragma no branch
self._type = self._makeStringAttribute(attributes["type"])
if "reviewer" in attributes: # pragma no branch
assert self._type.value in ("User", "Team")
if self._type.value == "User":
self._reviewer = self._makeClassAttribute(
github.NamedUser.NamedUser, attributes["reviewer"]
)
elif self._type.value == "Team":
self._reviewer = self._makeClassAttribute(
github.Team.Team, attributes["reviewer"]
)


class ReviewerParams:
"""
This class presents reviewers as can be configured for an Environment.
"""

def __init__(self, type_: str, id_: int):
assert isinstance(type_, str) and type_ in ("User", "Team")
assert isinstance(id_, int)
self.type = type_
self.id = id_

def _asdict(self) -> dict:
return {
"type": self.type,
"id": self.id,
}