/
GistComment.py
232 lines (195 loc) · 6.77 KB
/
GistComment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python3.7
# -*- coding: utf-8 -*-
"""
GistComment.py - a Github APIv4 object
TODO: This class is still a Work in Progress
"""
import json
import os
import requests
from .GithubObject import GithubObject
from .decorators import connection
GRAPHQL_DIR = ''.join([os.path.dirname(os.path.realpath(__file__)), '/graphql/'])
class GistComment(GithubObject):
"""This class represents a comment on a Gist. Upstream reference is at
https://developer.github.com/v4/object/gistcomment/"""
def __init__(self, data):
"""
Arguments:
data (str) - a json-formatted gist comment node from a user.gistComments query
"""
super(Gist, self).__init__()
self.data = data
def __repr__(self):
return 'GistComment(id={!r}, createdAt={!r})'.format(self.id, self.createdAt)
def __eq__(self, other):
if other.__class__ is not self.__class__:
return NotImplemented
return self.id == other.id
def __ne__(self, other):
if other.__class__ is not self.__class__:
return NotImplemented
return self.id != other.id
@connection
def userContentEdits(self, **kwargs):
"""A list of edits to this content
Keyword Arguments:
after (str) - Returns the elements in the list that come after the specified cursor.
before (str) - Returns the elements in the list that come before the specified cursor.
first (int) - Returns the first n elements from the list.
last (int) - Returns the last n elements from the list.
WIP: This function is a work in progress.
TODO: This function should return a GithubObject, GistComment instead of json eventually.
see https://developer.github.com/v4/object/gistcomment/
"""
filters = []
for key, value in kwargs.items():
filters.append("{}: {}, ".format(key, value))
filters = ''.join(filters)
filters = filters.rstrip(', ')
try:
gist_owner = self.gist['owner']['login']
gist_name = self.gist['name']
except KeyError:
return False # the gist property of the query MUST return an owner and a gist name
query = {"query" : '{ user(login: "%s") { gist(name: "%s") { comments(%s) { totalCount,
edges { cursor, node { userContentEdits(%s){}}}, pageInfo { startCursor, endCursor, hasNextPage,
hasPreviousPage } } } } }' }
query = {"query" : open(GRAPHQL_DIR+'gh_gist_comment_user_content_edits.graphql', 'r').read() % (gist_owner, gist_name, filters)}
user_content_edits = requests.post(url=self.endpoint, json=query, headers=self.headers)
if self._errors_exist(user_content_edits):
return False
return user_content_edits
@property
def author(self):
"""The actor who authored the comment.
:type dict
"""
return self.data['node']['author']
@property
def authorAssociation(self):
"""Author's association with the gist.
:type str
"""
return self.data['node']['authorAssocation']
@property
def body(self):
"""Identifies the comment body.
:type str
"""
return self.data['node']['body']
@property
def bodyHTML(self):
"""The comment body rendered to HTML.
:type str
"""
return self.data['node']['bodyHTML']
@property
def bodyText(self):
"""The body rendered to text.
:type str
"""
return self.data['node']['bodyText']
@property
def createdAt(self):
"""Identifies the date and time when the object was created.
:type: string (An ISO-8601 encoded UTC date string)
"""
return self.data['node']['createdAt']
@property
def createdViaEmail(self):
"""Check if this comment was created via an email reply.
:type: Boolean
"""
return self.data['node']['createdViaEmail']
@property
def databaseId(self):
"""Identifies the primary key from the database.
:type str
"""
return self.data['node']['databaseId']
@property
def editor(self):
"""The actor who edited the comment.
:type dict
"""
return self.data['node']['editor']
@property
def gist(self):
"""The associated gist
:type dict
"""
return self.data['node']['gist']
@property
def id(self):
"""
:type str
"""
return self.data['node']['id']
@property
def includesCreatedEdit(self):
"""
Check if this comment was edited and includes an edit with the creation data
:type: Boolean
"""
return self.data['node']['includesCreatedEdit']
@property
def isMinimized(self):
"""Returns whether or not a comment has been minimized.
:type: Boolean
"""
return self.data['node']['isMinimized']
@property
def lastEditedAt(self):
"""The moment the editor made the last edit
:type: string (An ISO-8601 encoded UTC date string)
"""
return self.data['node']['lastEditedAt']
@property
def minimizedReason(self):
"""Returns why the comment was minimized.
:type: str
"""
return self.data['node']['minimizedReason']
@property
def publishedAt(self):
"""Identifies when the comment was published at.
:type: str (An ISO-8601 encoded UTC date string)
"""
return self.data['node']['publishedAt']
@property
def updatedAt(self):
"""Identifies the date and time when the object was last updated
:type: str (An ISO-8601 encoded UTC date string)
"""
return self.data['node']['updatedAt']
@property
def viewerCanDelete(self):
"""Check if the current viewer can delete this object.
:type: boolean
"""
return self.data['node']['viewerCanDelete']
@property
def viewerCanMinimize(self):
"""Check if the current viewer can minimize this object.
:type: boolean
"""
return self.data['node']['viewerCanDelete']
@property
def viewerCanUpdate(self):
"""Check if the current viewer can update this object.
:type: boolean
"""
return self.data['node']['viewerCanUpdate']
@property
def viewerCannotUpdateReasons(self):
"""Reasons why the current viewer can not update this comment.
:type: str
"""
return self.data['node']['viewerCannotUpdateReasons']
@property
def viewerDidAuthor(self):
"""Did the viewer author this comment.
:type: Boolean
"""
return self.data['node']['viewerDidAuthor']