/ github / ProjectColumn.py
ProjectColumn.py
  1  ############################ Copyrights and license ############################
  2  #                                                                              #
  3  # Copyright 2018 bbi-yggy <yossarian@blackbirdinteractive.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  from __future__ import annotations
 23  
 24  from datetime import datetime
 25  from typing import Any
 26  
 27  import github.GithubObject
 28  import github.Project
 29  import github.ProjectCard
 30  from github.GithubObject import Attribute, CompletableGithubObject, NotSet, Opt
 31  from github.PaginatedList import PaginatedList
 32  
 33  from . import Consts
 34  
 35  
 36  class ProjectColumn(CompletableGithubObject):
 37      """
 38      This class represents Project Columns. The reference can be found here https://docs.github.com/en/rest/reference/projects#columns
 39      """
 40  
 41      def _initAttributes(self) -> None:
 42          self._cards_url: Attribute[str] = NotSet
 43          self._created_at: Attribute[datetime] = NotSet
 44          self._id: Attribute[int] = NotSet
 45          self._name: Attribute[str] = NotSet
 46          self._node_id: Attribute[str] = NotSet
 47          self._project_url: Attribute[str] = NotSet
 48          self._updated_at: Attribute[datetime] = NotSet
 49          self._url: Attribute[str] = NotSet
 50  
 51      def __repr__(self) -> str:
 52          return self.get__repr__({"name": self._name.value})
 53  
 54      @property
 55      def cards_url(self) -> str:
 56          return self._cards_url.value
 57  
 58      @property
 59      def created_at(self) -> datetime:
 60          return self._created_at.value
 61  
 62      @property
 63      def id(self) -> int:
 64          return self._id.value
 65  
 66      @property
 67      def name(self) -> str:
 68          return self._name.value
 69  
 70      @property
 71      def node_id(self) -> str:
 72          return self._node_id.value
 73  
 74      @property
 75      def project_url(self) -> str:
 76          return self._project_url.value
 77  
 78      @property
 79      def updated_at(self) -> datetime:
 80          return self._updated_at.value
 81  
 82      @property
 83      def url(self) -> str:
 84          return self._url.value
 85  
 86      def get_cards(self, archived_state: Opt[str] = NotSet) -> PaginatedList[github.ProjectCard.ProjectCard]:
 87          """
 88          :calls: `GET /projects/columns/{column_id}/cards <https://docs.github.com/en/rest/reference/projects#list-project-cards>`_
 89          """
 90          assert archived_state is NotSet or isinstance(archived_state, str), archived_state
 91  
 92          url_parameters = dict()
 93          if archived_state is not NotSet:
 94              url_parameters["archived_state"] = archived_state
 95  
 96          return PaginatedList(
 97              github.ProjectCard.ProjectCard,
 98              self._requester,
 99              f"{self.url}/cards",
100              url_parameters,
101              {"Accept": Consts.mediaTypeProjectsPreview},
102          )
103  
104      def create_card(
105          self,
106          note: Opt[str] = NotSet,
107          content_id: Opt[int] = NotSet,
108          content_type: Opt[str] = NotSet,
109      ) -> github.ProjectCard.ProjectCard:
110          """
111          :calls: `POST /projects/columns/{column_id}/cards <https://docs.github.com/en/rest/reference/projects#create-a-project-card>`_
112          """
113          if isinstance(note, str):
114              assert content_id is NotSet, content_id
115              assert content_type is NotSet, content_type
116              post_parameters: dict[str, Any] = {"note": note}
117          else:
118              assert note is NotSet, note
119              assert isinstance(content_id, int), content_id
120              assert isinstance(content_type, str), content_type
121              post_parameters = {"content_id": content_id, "content_type": content_type}
122  
123          import_header = {"Accept": Consts.mediaTypeProjectsPreview}
124          headers, data = self._requester.requestJsonAndCheck(
125              "POST", f"{self.url}/cards", headers=import_header, input=post_parameters
126          )
127          return github.ProjectCard.ProjectCard(self._requester, headers, data, completed=True)
128  
129      def move(self, position: str) -> bool:
130          """
131          :calls: `POST POST /projects/columns/{column_id}/moves <https://docs.github.com/en/rest/reference/projects#move-a-project-column>`_
132          """
133          assert isinstance(position, str), position
134          post_parameters = {"position": position}
135          status, _, _ = self._requester.requestJson(
136              "POST",
137              f"{self.url}/moves",
138              input=post_parameters,
139              headers={"Accept": Consts.mediaTypeProjectsPreview},
140          )
141          return status == 201
142  
143      def delete(self) -> bool:
144          """
145          :calls: `DELETE /projects/columns/{column_id} <https://docs.github.com/en/rest/reference/projects#delete-a-project-column>`_
146          """
147          status, _, _ = self._requester.requestJson(
148              "DELETE",
149              self.url,
150              headers={"Accept": Consts.mediaTypeProjectsPreview},
151          )
152          return status == 204
153  
154      def edit(self, name: str) -> None:
155          """
156          :calls: `PATCH /projects/columns/{column_id} <https://docs.github.com/en/rest/reference/projects#update-an-existing-project-column>`_
157          """
158          assert isinstance(name, str), name
159          patch_parameters = {"name": name}
160  
161          headers, data = self._requester.requestJsonAndCheck(
162              "PATCH",
163              self.url,
164              input=patch_parameters,
165              headers={"Accept": Consts.mediaTypeProjectsPreview},
166          )
167  
168          self._useAttributes(data)
169  
170      def _useAttributes(self, attributes: dict[str, Any]) -> None:
171          if "cards_url" in attributes:  # pragma no branch
172              self._cards_url = self._makeStringAttribute(attributes["cards_url"])
173          if "created_at" in attributes:  # pragma no branch
174              self._created_at = self._makeDatetimeAttribute(attributes["created_at"])
175          if "id" in attributes:  # pragma no branch
176              self._id = self._makeIntAttribute(attributes["id"])
177          if "name" in attributes:  # pragma no branch
178              self._name = self._makeStringAttribute(attributes["name"])
179          if "node_id" in attributes:  # pragma no branch
180              self._node_id = self._makeStringAttribute(attributes["node_id"])
181          if "project_url" in attributes:  # pragma no branch
182              self._project_url = self._makeStringAttribute(attributes["project_url"])
183          if "updated_at" in attributes:  # pragma no branch
184              self._updated_at = self._makeDatetimeAttribute(attributes["updated_at"])
185          if "url" in attributes:  # pragma no branch
186              self._url = self._makeStringAttribute(attributes["url"])