1# -*- coding: utf-8 -*-
2
3############################ Copyrights and license ############################
4#                                                                              #
5# Copyright 2012 Vincent Jacques <vincent@vincent-jacques.net>                 #
6# Copyright 2012 Zearin <zearin@gonk.net>                                      #
7# Copyright 2013 AKFish <akfish@gmail.com>                                     #
8# Copyright 2013 Vincent Jacques <vincent@vincent-jacques.net>                 #
9# Copyright 2014 Vincent Jacques <vincent@vincent-jacques.net>                 #
10# Copyright 2016 Jannis Gebauer <ja.geb@me.com>                                #
11# Copyright 2016 Peter Buckley <dx-pbuckley@users.noreply.github.com>          #
12# Copyright 2017 Wan Liuyang <tsfdye@gmail.com>                                #
13# Copyright 2018 Wan Liuyang <tsfdye@gmail.com>                                #
14# Copyright 2018 sfdye <tsfdye@gmail.com>                                      #
15#                                                                              #
16# This file is part of PyGithub.                                               #
17# http://pygithub.readthedocs.io/                                              #
18#                                                                              #
19# PyGithub is free software: you can redistribute it and/or modify it under    #
20# the terms of the GNU Lesser General Public License as published by the Free  #
21# Software Foundation, either version 3 of the License, or (at your option)    #
22# any later version.                                                           #
23#                                                                              #
24# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY  #
25# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    #
26# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
27# details.                                                                     #
28#                                                                              #
29# You should have received a copy of the GNU Lesser General Public License     #
30# along with PyGithub. If not, see <http://www.gnu.org/licenses/>.             #
31#                                                                              #
32################################################################################
33
34import github.GithubObject
35import github.HookResponse
36
37
38class Hook(github.GithubObject.CompletableGithubObject):
39    """
40    This class represents Hooks. The reference can be found here http://developer.github.com/v3/repos/hooks
41    """
42
43    def __repr__(self):
44        return self.get__repr__({"id": self._id.value, "url": self._url.value})
45
46    @property
47    def active(self):
48        """
49        :type: bool
50        """
51        self._completeIfNotSet(self._active)
52        return self._active.value
53
54    @property
55    def config(self):
56        """
57        :type: dict
58        """
59        self._completeIfNotSet(self._config)
60        return self._config.value
61
62    @property
63    def created_at(self):
64        """
65        :type: datetime.datetime
66        """
67        self._completeIfNotSet(self._created_at)
68        return self._created_at.value
69
70    @property
71    def events(self):
72        """
73        :type: list of string
74        """
75        self._completeIfNotSet(self._events)
76        return self._events.value
77
78    @property
79    def id(self):
80        """
81        :type: integer
82        """
83        self._completeIfNotSet(self._id)
84        return self._id.value
85
86    @property
87    def last_response(self):
88        """
89        :type: :class:`github.HookResponse.HookResponse`
90        """
91        self._completeIfNotSet(self._last_response)
92        return self._last_response.value
93
94    @property
95    def name(self):
96        """
97        :type: string
98        """
99        self._completeIfNotSet(self._name)
100        return self._name.value
101
102    @property
103    def test_url(self):
104        """
105        :type: string
106        """
107        self._completeIfNotSet(self._test_url)
108        return self._test_url.value
109
110    @property
111    def updated_at(self):
112        """
113        :type: datetime.datetime
114        """
115        self._completeIfNotSet(self._updated_at)
116        return self._updated_at.value
117
118    @property
119    def url(self):
120        """
121        :type: string
122        """
123        self._completeIfNotSet(self._url)
124        return self._url.value
125
126    @property
127    def ping_url(self):
128        """
129        :type: string
130        """
131        self._completeIfNotSet(self._ping_url)
132        return self._ping_url.value
133
134    def delete(self):
135        """
136        :calls: `DELETE /repos/:owner/:repo/hooks/:id <http://developer.github.com/v3/repos/hooks>`_
137        :rtype: None
138        """
139        headers, data = self._requester.requestJsonAndCheck("DELETE", self.url)
140
141    def edit(
142        self,
143        name,
144        config,
145        events=github.GithubObject.NotSet,
146        add_events=github.GithubObject.NotSet,
147        remove_events=github.GithubObject.NotSet,
148        active=github.GithubObject.NotSet,
149    ):
150        """
151        :calls: `PATCH /repos/:owner/:repo/hooks/:id <http://developer.github.com/v3/repos/hooks>`_
152        :param name: string
153        :param config: dict
154        :param events: list of string
155        :param add_events: list of string
156        :param remove_events: list of string
157        :param active: bool
158        :rtype: None
159        """
160        assert isinstance(name, str), name
161        assert isinstance(config, dict), config
162        assert events is github.GithubObject.NotSet or all(
163            isinstance(element, str) for element in events
164        ), events
165        assert add_events is github.GithubObject.NotSet or all(
166            isinstance(element, str) for element in add_events
167        ), add_events
168        assert remove_events is github.GithubObject.NotSet or all(
169            isinstance(element, str) for element in remove_events
170        ), remove_events
171        assert active is github.GithubObject.NotSet or isinstance(active, bool), active
172        post_parameters = {
173            "name": name,
174            "config": config,
175        }
176        if events is not github.GithubObject.NotSet:
177            post_parameters["events"] = events
178        if add_events is not github.GithubObject.NotSet:
179            post_parameters["add_events"] = add_events
180        if remove_events is not github.GithubObject.NotSet:
181            post_parameters["remove_events"] = remove_events
182        if active is not github.GithubObject.NotSet:
183            post_parameters["active"] = active
184        headers, data = self._requester.requestJsonAndCheck(
185            "PATCH", self.url, input=post_parameters
186        )
187        self._useAttributes(data)
188
189    def test(self):
190        """
191        :calls: `POST /repos/:owner/:repo/hooks/:id/tests <http://developer.github.com/v3/repos/hooks>`_
192        :rtype: None
193        """
194        headers, data = self._requester.requestJsonAndCheck("POST", self.url + "/tests")
195
196    def ping(self):
197        """
198        :calls: `POST /repos/:owner/:repo/hooks/:id/pings <http://developer.github.com/v3/repos/hooks>`_
199        :rtype: None
200        """
201        headers, data = self._requester.requestJsonAndCheck("POST", self.url + "/pings")
202
203    def _initAttributes(self):
204        self._active = github.GithubObject.NotSet
205        self._config = github.GithubObject.NotSet
206        self._created_at = github.GithubObject.NotSet
207        self._events = github.GithubObject.NotSet
208        self._id = github.GithubObject.NotSet
209        self._last_response = github.GithubObject.NotSet
210        self._name = github.GithubObject.NotSet
211        self._test_url = github.GithubObject.NotSet
212        self._updated_at = github.GithubObject.NotSet
213        self._url = github.GithubObject.NotSet
214        self._ping_url = github.GithubObject.NotSet
215
216    def _useAttributes(self, attributes):
217        if "active" in attributes:  # pragma no branch
218            self._active = self._makeBoolAttribute(attributes["active"])
219        if "config" in attributes:  # pragma no branch
220            self._config = self._makeDictAttribute(attributes["config"])
221        if "created_at" in attributes:  # pragma no branch
222            self._created_at = self._makeDatetimeAttribute(attributes["created_at"])
223        if "events" in attributes:  # pragma no branch
224            self._events = self._makeListOfStringsAttribute(attributes["events"])
225        if "id" in attributes:  # pragma no branch
226            self._id = self._makeIntAttribute(attributes["id"])
227        if "last_response" in attributes:  # pragma no branch
228            self._last_response = self._makeClassAttribute(
229                github.HookResponse.HookResponse, attributes["last_response"]
230            )
231        if "name" in attributes:  # pragma no branch
232            self._name = self._makeStringAttribute(attributes["name"])
233        if "test_url" in attributes:  # pragma no branch
234            self._test_url = self._makeStringAttribute(attributes["test_url"])
235        if "updated_at" in attributes:  # pragma no branch
236            self._updated_at = self._makeDatetimeAttribute(attributes["updated_at"])
237        if "url" in attributes:  # pragma no branch
238            self._url = self._makeStringAttribute(attributes["url"])
239        if "ping_url" in attributes:  # pragma no branch
240            self._ping_url = self._makeStringAttribute(attributes["ping_url"])
241