4
4
from json import JSONDecodeError
5
5
from typing import Optional , Any
6
6
7
- from github_actions .api import IssueUrl , GithubApi , CommentUrl
7
+ from github_actions .api import IssueUrl , GithubApi , CommentUrl , NodeId
8
8
from github_actions .debug import debug
9
9
10
10
try :
@@ -43,9 +43,10 @@ class TerraformComment:
43
43
44
44
"""
45
45
46
- def __init__ (self , * , issue_url : IssueUrl , comment_url : Optional [CommentUrl ], headers : dict [str , str ], description : str , summary : str , body : str , status : str , body_highlighting : str = '' ):
46
+ def __init__ (self , * , issue_url : IssueUrl , comment_url : Optional [CommentUrl ], node_id : Optional [ NodeId ], headers : dict [str , str ], description : str , summary : str , body : str , status : str , body_highlighting : str = '' ):
47
47
self ._issue_url = issue_url
48
48
self ._comment_url = comment_url
49
+ self ._node_id = node_id
49
50
self ._headers = headers
50
51
self ._description = description .strip ()
51
52
self ._summary = summary .strip ()
@@ -60,6 +61,7 @@ def __eq__(self, other):
60
61
return (
61
62
self ._issue_url == other ._issue_url and
62
63
self ._comment_url == other ._comment_url and
64
+ self ._node_id == other ._node_id and
63
65
self ._headers == other ._headers and
64
66
self ._description == other ._description and
65
67
self ._summary == other ._summary and
@@ -72,7 +74,7 @@ def __ne__(self, other):
72
74
return not self .__eq__ (other )
73
75
74
76
def __repr__ (self ):
75
- return f'TerraformComment(issue_url={ self ._issue_url !r} , comment_url={ self ._comment_url !r} , headers={ self ._headers !r} , description={ self ._description !r} , summary={ self ._summary !r} , body={ self ._body !r} , status={ self ._status !r} , body_highlighting={ self ._body_highlighting !r} )'
77
+ return f'TerraformComment(issue_url={ self ._issue_url !r} , comment_url={ self ._comment_url !r} , node_id= { self . node_id } , headers={ self ._headers !r} , description={ self ._description !r} , summary={ self ._summary !r} , body={ self ._body !r} , status={ self ._status !r} , body_highlighting={ self ._body_highlighting !r} )'
76
78
77
79
@property
78
80
def comment_url (self ) -> Optional [CommentUrl ]:
@@ -84,6 +86,16 @@ def comment_url(self, comment_url: CommentUrl) -> None:
84
86
raise Exception ('Can only set url for comments that don\' t exist yet' )
85
87
self ._comment_url = comment_url
86
88
89
+ @property
90
+ def node_id (self ) -> Optional [NodeId ]:
91
+ return self ._node_id
92
+
93
+ @node_id .setter
94
+ def node_id (self , node_id : NodeId ) -> None :
95
+ if self ._node_id is not None :
96
+ raise Exception ('Can only set node_id for comments that don\' t exist yet' )
97
+ self ._node_id = node_id
98
+
87
99
@property
88
100
def issue_url (self ) -> IssueUrl :
89
101
return self ._issue_url
@@ -116,6 +128,7 @@ def serialize(comment: TerraformComment) -> str:
116
128
return json .dumps ({
117
129
'issue_url' : comment .issue_url ,
118
130
'comment_url' : comment .comment_url ,
131
+ 'node_id' : comment .node_id ,
119
132
'headers' : comment .headers ,
120
133
'description' : comment .description ,
121
134
'summary' : comment .summary ,
@@ -130,6 +143,7 @@ def deserialize(s) -> TerraformComment:
130
143
return TerraformComment (
131
144
issue_url = j ['issue_url' ],
132
145
comment_url = j ['comment_url' ],
146
+ node_id = j .get ('node_id' ),
133
147
headers = j ['headers' ],
134
148
description = j ['description' ],
135
149
summary = j ['summary' ],
@@ -176,6 +190,7 @@ def _from_api_payload(comment: dict[str, Any]) -> Optional[TerraformComment]:
176
190
return TerraformComment (
177
191
issue_url = comment ['issue_url' ],
178
192
comment_url = comment ['url' ],
193
+ node_id = comment .get ('node_id' ),
179
194
headers = _parse_comment_header (match .group ('headers' )),
180
195
description = match .group ('description' ).strip (),
181
196
summary = match .group ('summary' ).strip (),
@@ -290,6 +305,7 @@ def find_comment(github: GithubApi, issue_url: IssueUrl, username: str, headers:
290
305
return TerraformComment (
291
306
issue_url = backup_comment .issue_url ,
292
307
comment_url = backup_comment .comment_url ,
308
+ node_id = backup_comment .node_id ,
293
309
headers = backup_comment .headers | headers ,
294
310
description = backup_comment .description ,
295
311
summary = backup_comment .summary ,
@@ -305,6 +321,7 @@ def find_comment(github: GithubApi, issue_url: IssueUrl, username: str, headers:
305
321
return TerraformComment (
306
322
issue_url = legacy_comment .issue_url ,
307
323
comment_url = legacy_comment .comment_url ,
324
+ node_id = legacy_comment .node_id ,
308
325
headers = {k : v for k , v in headers .items () if v is not None },
309
326
description = legacy_comment .description ,
310
327
summary = legacy_comment .summary ,
@@ -317,6 +334,7 @@ def find_comment(github: GithubApi, issue_url: IssueUrl, username: str, headers:
317
334
return TerraformComment (
318
335
issue_url = issue_url ,
319
336
comment_url = None ,
337
+ node_id = None ,
320
338
headers = {k : v for k , v in headers .items () if v is not None },
321
339
description = '' ,
322
340
summary = '' ,
@@ -344,6 +362,7 @@ def update_comment(
344
362
new_comment = TerraformComment (
345
363
issue_url = comment .issue_url ,
346
364
comment_url = comment .comment_url ,
365
+ node_id = comment .node_id ,
347
366
headers = new_headers ,
348
367
description = description if description is not None else comment .description ,
349
368
summary = summary if summary is not None else comment .summary ,
@@ -359,5 +378,27 @@ def update_comment(
359
378
response = github .post (comment .issue_url + '/comments' , json = {'body' : _to_api_payload (new_comment )})
360
379
response .raise_for_status ()
361
380
new_comment .comment_url = response .json ()['url' ]
381
+ new_comment .node_id = response .json ().get ('node_id' )
362
382
363
383
return new_comment
384
+
385
+ def hide_comment (
386
+ github : GithubApi ,
387
+ comment : TerraformComment ,
388
+ classifier : str
389
+ ) -> None :
390
+
391
+ graphql_url = os .environ .get ('GITHUB_GRAPHQL_URL' , f'{ os .environ ["GITHUB_API_URL" ]} /graphql' )
392
+
393
+ response = github .post (
394
+ graphql_url , json = {
395
+ 'query' : '''
396
+ mutation {
397
+ minimizeComment(input: {subjectId: "''' + comment .node_id + '''", classifier: ''' + classifier + '''}) {
398
+ clientMutationId
399
+ }
400
+ }
401
+ '''
402
+ }
403
+ )
404
+ debug (f'graphql response: { response .content } ' )
0 commit comments