/ github / OrganizationVariable.py
OrganizationVariable.py
  1  ############################ Copyrights and license ############################
  2  #                                                                              #
  3  # Copyright 2023 Mauricio Martinez <mauricio.martinez@premise.com>             #
  4  #                                                                              #
  5  # This file is part of PyGithub.                                               #
  6  # http://pygithub.readthedocs.io/                                              #
  7  #                                                                              #
  8  # PyGithub is free software: you can redistribute it and/or modify it under    #
  9  # the terms of the GNU Lesser General Public License as published by the Free  #
 10  # Software Foundation, either version 3 of the License, or (at your option)    #
 11  # any later version.                                                           #
 12  #                                                                              #
 13  # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY  #
 14  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    #
 15  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
 16  # details.                                                                     #
 17  #                                                                              #
 18  # You should have received a copy of the GNU Lesser General Public License     #
 19  # along with PyGithub. If not, see <http://www.gnu.org/licenses/>.             #
 20  #                                                                              #
 21  ################################################################################
 22  
 23  from datetime import datetime
 24  from typing import Any, Dict
 25  
 26  from github.GithubObject import Attribute, NotSet
 27  from github.PaginatedList import PaginatedList
 28  from github.Repository import Repository
 29  from github.Variable import Variable
 30  
 31  
 32  class OrganizationVariable(Variable):
 33      """
 34      This class represents a org level GitHub variable. The reference can be found here https://docs.github.com/en/rest/actions/variables
 35      """
 36  
 37      def _initAttributes(self) -> None:
 38          self._name: Attribute[str] = NotSet
 39          self._created_at: Attribute[datetime] = NotSet
 40          self._updated_at: Attribute[datetime] = NotSet
 41          self._visibility: Attribute[str] = NotSet
 42          self._selected_repositories: Attribute[PaginatedList[Repository]] = NotSet
 43          self._selected_repositories_url: Attribute[str] = NotSet
 44          self._url: Attribute[str] = NotSet
 45  
 46      @property
 47      def visibility(self) -> str:
 48          """
 49          :type: string
 50          """
 51          self._completeIfNotSet(self._visibility)
 52          return self._visibility.value
 53  
 54      @property
 55      def selected_repositories(self) -> PaginatedList[Repository]:
 56          return PaginatedList(
 57              Repository,
 58              self._requester,
 59              self._selected_repositories_url.value,
 60              None,
 61              list_item="repositories",
 62          )
 63  
 64      def edit(
 65          self,
 66          value: str,
 67          visibility: str = "all",
 68      ) -> bool:
 69          """
 70          :calls: `PATCH /orgs/{org}/actions/variables/{variable_name} <https://docs.github.com/en/rest/reference/actions/variables#update-an-organization-variable>`_
 71          :param variable_name: string
 72          :param value: string
 73          :param visibility: string
 74          :rtype: bool
 75          """
 76          assert isinstance(value, str), value
 77          assert isinstance(visibility, str), visibility
 78  
 79          patch_parameters: Dict[str, Any] = {
 80              "name": self.name,
 81              "value": value,
 82              "visibility": visibility,
 83          }
 84  
 85          status, _, _ = self._requester.requestJson(
 86              "PATCH",
 87              f"{self.url}/actions/variables/{self.name}",
 88              input=patch_parameters,
 89          )
 90          return status == 204
 91  
 92      def add_repo(self, repo: Repository) -> bool:
 93          """
 94          :calls: 'PUT {org_url}/actions/variables/{variable_name} <https://docs.github.com/en/rest/actions/variables#add-selected-repository-to-an-organization-secret>`_
 95          :param repo: github.Repository.Repository
 96          :rtype: bool
 97          """
 98          if self.visibility != "selected":
 99              return False
100          self._requester.requestJsonAndCheck("PUT", f"{self._selected_repositories_url.value}/{repo.id}")
101          return True
102  
103      def remove_repo(self, repo: Repository) -> bool:
104          """
105          :calls: 'DELETE {org_url}/actions/variables/{variable_name} <https://docs.github.com/en/rest/actions/variables#add-selected-repository-to-an-organization-secret>`_
106          :param repo: github.Repository.Repository
107          :rtype: bool
108          """
109          if self.visibility != "selected":
110              return False
111          self._requester.requestJsonAndCheck("DELETE", f"{self._selected_repositories_url.value}/{repo.id}")
112          return True
113  
114      def _useAttributes(self, attributes: Dict[str, Any]) -> None:
115          if "name" in attributes:
116              self._name = self._makeStringAttribute(attributes["name"])
117          if "created_at" in attributes:
118              self._created_at = self._makeDatetimeAttribute(attributes["created_at"])
119          if "updated_at" in attributes:
120              self._updated_at = self._makeDatetimeAttribute(attributes["updated_at"])
121          if "visibility" in attributes:
122              self._visibility = self._makeStringAttribute(attributes["visibility"])
123          if "selected_repositories_url" in attributes:
124              self._selected_repositories_url = self._makeStringAttribute(attributes["selected_repositories_url"])
125          if "url" in attributes:
126              self._url = self._makeStringAttribute(attributes["url"])