Skip to content

Commit cc5d2f9

Browse files
committed
Hide outdated comments
1 parent 9c15d46 commit cc5d2f9

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

image/src/github_actions/api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
IssueUrl = NewType('IssueUrl', GitHubUrl)
1414
CommentUrl = NewType('CommentUrl', GitHubUrl)
1515
CommentReactionUrl = NewType('CommentReactionUrl', GitHubUrl)
16+
NodeId = NewType('NodeId', str)
1617

1718

1819
class GithubApi:

image/src/github_pr_comment/__main__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from github_pr_comment.backend_config import complete_config, partial_config
2020
from github_pr_comment.backend_fingerprint import fingerprint
2121
from github_pr_comment.cmp import plan_cmp, remove_warnings, remove_unchanged_attributes
22-
from github_pr_comment.comment import find_comment, TerraformComment, update_comment, serialize, deserialize
22+
from github_pr_comment.comment import find_comment, TerraformComment, update_comment, serialize, deserialize, hide_comment
2323
from github_pr_comment.hash import comment_hash, plan_hash, plan_out_hash
2424
from github_pr_comment.plan_formatting import format_diff
2525
from plan_renderer.outputs import render_outputs
@@ -323,6 +323,7 @@ def new_pr_comment(backend_fingerprint: bytes) -> TerraformComment:
323323
return TerraformComment(
324324
issue_url=issue_url,
325325
comment_url=None,
326+
node_id=None,
326327
headers={k: v for k, v in headers.items() if v is not None},
327328
description='',
328329
summary='',
@@ -497,6 +498,11 @@ def main() -> int:
497498
headers=comment.headers | {'closed': True},
498499
status=':spider_web: Plan is outdated'
499500
)
501+
hide_comment(
502+
github,
503+
comment,
504+
'OUTDATED'
505+
)
500506

501507
# Create the replacement comment
502508
comment = new_pr_comment(backend_fingerprint)

image/src/github_pr_comment/comment.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from json import JSONDecodeError
55
from typing import Optional, Any
66

7-
from github_actions.api import IssueUrl, GithubApi, CommentUrl
7+
from github_actions.api import IssueUrl, GithubApi, CommentUrl, NodeId
88
from github_actions.debug import debug
99

1010
try:
@@ -43,9 +43,10 @@ class TerraformComment:
4343
4444
"""
4545

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 = ''):
4747
self._issue_url = issue_url
4848
self._comment_url = comment_url
49+
self._node_id = node_id
4950
self._headers = headers
5051
self._description = description.strip()
5152
self._summary = summary.strip()
@@ -60,6 +61,7 @@ def __eq__(self, other):
6061
return (
6162
self._issue_url == other._issue_url and
6263
self._comment_url == other._comment_url and
64+
self._node_id == other._node_id and
6365
self._headers == other._headers and
6466
self._description == other._description and
6567
self._summary == other._summary and
@@ -72,7 +74,7 @@ def __ne__(self, other):
7274
return not self.__eq__(other)
7375

7476
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})'
7678

7779
@property
7880
def comment_url(self) -> Optional[CommentUrl]:
@@ -84,6 +86,16 @@ def comment_url(self, comment_url: CommentUrl) -> None:
8486
raise Exception('Can only set url for comments that don\'t exist yet')
8587
self._comment_url = comment_url
8688

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+
8799
@property
88100
def issue_url(self) -> IssueUrl:
89101
return self._issue_url
@@ -116,6 +128,7 @@ def serialize(comment: TerraformComment) -> str:
116128
return json.dumps({
117129
'issue_url': comment.issue_url,
118130
'comment_url': comment.comment_url,
131+
'node_id': comment.node_id,
119132
'headers': comment.headers,
120133
'description': comment.description,
121134
'summary': comment.summary,
@@ -130,6 +143,7 @@ def deserialize(s) -> TerraformComment:
130143
return TerraformComment(
131144
issue_url=j['issue_url'],
132145
comment_url=j['comment_url'],
146+
node_id=j.get('node_id'),
133147
headers=j['headers'],
134148
description=j['description'],
135149
summary=j['summary'],
@@ -176,6 +190,7 @@ def _from_api_payload(comment: dict[str, Any]) -> Optional[TerraformComment]:
176190
return TerraformComment(
177191
issue_url=comment['issue_url'],
178192
comment_url=comment['url'],
193+
node_id=comment.get('node_id'),
179194
headers=_parse_comment_header(match.group('headers')),
180195
description=match.group('description').strip(),
181196
summary=match.group('summary').strip(),
@@ -290,6 +305,7 @@ def find_comment(github: GithubApi, issue_url: IssueUrl, username: str, headers:
290305
return TerraformComment(
291306
issue_url=backup_comment.issue_url,
292307
comment_url=backup_comment.comment_url,
308+
node_id=backup_comment.node_id,
293309
headers=backup_comment.headers | headers,
294310
description=backup_comment.description,
295311
summary=backup_comment.summary,
@@ -305,6 +321,7 @@ def find_comment(github: GithubApi, issue_url: IssueUrl, username: str, headers:
305321
return TerraformComment(
306322
issue_url=legacy_comment.issue_url,
307323
comment_url=legacy_comment.comment_url,
324+
node_id=legacy_comment.node_id,
308325
headers={k: v for k, v in headers.items() if v is not None},
309326
description=legacy_comment.description,
310327
summary=legacy_comment.summary,
@@ -317,6 +334,7 @@ def find_comment(github: GithubApi, issue_url: IssueUrl, username: str, headers:
317334
return TerraformComment(
318335
issue_url=issue_url,
319336
comment_url=None,
337+
node_id=None,
320338
headers={k: v for k, v in headers.items() if v is not None},
321339
description='',
322340
summary='',
@@ -344,6 +362,7 @@ def update_comment(
344362
new_comment = TerraformComment(
345363
issue_url=comment.issue_url,
346364
comment_url=comment.comment_url,
365+
node_id=comment.node_id,
347366
headers=new_headers,
348367
description=description if description is not None else comment.description,
349368
summary=summary if summary is not None else comment.summary,
@@ -359,5 +378,27 @@ def update_comment(
359378
response = github.post(comment.issue_url + '/comments', json={'body': _to_api_payload(new_comment)})
360379
response.raise_for_status()
361380
new_comment.comment_url = response.json()['url']
381+
new_comment.node_id = response.json().get('node_id')
362382

363383
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

Comments
 (0)