/ github / Variable.py
Variable.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 __future__ import annotations
 24  
 25  from datetime import datetime
 26  from typing import Any
 27  
 28  from github.GithubObject import Attribute, CompletableGithubObject, NotSet
 29  
 30  
 31  class Variable(CompletableGithubObject):
 32      """
 33      This class represents a GitHub variable. The reference can be found here https://docs.github.com/en/rest/actions/variables
 34      """
 35  
 36      def _initAttributes(self) -> None:
 37          self._name: Attribute[str] = NotSet
 38          self._value: Attribute[str] = NotSet
 39          self._created_at: Attribute[datetime] = NotSet
 40          self._updated_at: Attribute[datetime] = NotSet
 41          self._url: Attribute[str] = NotSet
 42  
 43      def __repr__(self) -> str:
 44          return self.get__repr__({"name": self.name})
 45  
 46      @property
 47      def name(self) -> str:
 48          """
 49          :type: string
 50          """
 51          self._completeIfNotSet(self._name)
 52          return self._name.value
 53  
 54      @property
 55      def value(self) -> str:
 56          """
 57          :type: string
 58          """
 59          self._completeIfNotSet(self._value)
 60          return self._value.value
 61  
 62      @property
 63      def created_at(self) -> datetime:
 64          """
 65          :type: datetime.datetime
 66          """
 67          self._completeIfNotSet(self._created_at)
 68          return self._created_at.value
 69  
 70      @property
 71      def updated_at(self) -> datetime:
 72          """
 73          :type: datetime.datetime
 74          """
 75          self._completeIfNotSet(self._updated_at)
 76          return self._updated_at.value
 77  
 78      @property
 79      def url(self) -> str:
 80          """
 81          :type: string
 82          """
 83          return self._url.value
 84  
 85      def edit(self, value: str) -> bool:
 86          """
 87          :calls: `PATCH /repos/{owner}/{repo}/actions/variables/{variable_name} <https://docs.github.com/en/rest/reference/actions/variables#update-a-repository-variable>`_
 88          :param variable_name: string
 89          :param value: string
 90          :rtype: bool
 91          """
 92          assert isinstance(value, str), value
 93          patch_parameters = {
 94              "name": self.name,
 95              "value": value,
 96          }
 97          status, _, _ = self._requester.requestJson(
 98              "PATCH",
 99              f"{self.url}/actions/variables/{self.name}",
100              input=patch_parameters,
101          )
102          return status == 204
103  
104      def delete(self) -> None:
105          """
106          :calls: `DELETE {variable_url} <https://docs.github.com/en/rest/actions/variables>`_
107          :rtype: None
108          """
109          self._requester.requestJsonAndCheck("DELETE", f"{self.url}/actions/variables/{self.name}")
110  
111      def _useAttributes(self, attributes: dict[str, Any]) -> None:
112          if "name" in attributes:
113              self._name = self._makeStringAttribute(attributes["name"])
114          if "value" in attributes:
115              self._value = self._makeStringAttribute(attributes["value"])
116          if "created_at" in attributes:
117              self._created_at = self._makeDatetimeAttribute(attributes["created_at"])
118          if "updated_at" in attributes:
119              self._updated_at = self._makeDatetimeAttribute(attributes["updated_at"])
120          if "url" in attributes:
121              self._url = self._makeStringAttribute(attributes["url"])