55import re
66import sys
77import datetime
8+ import hashlib
89from typing import Optional , Dict , Iterable
910
1011import requests
@@ -98,16 +99,37 @@ def find_pr() -> str:
9899 raise Exception (f"The { event_type } event doesn\' t relate to a Pull Request." )
99100
100101def current_user () -> str :
102+
103+ token_hash = hashlib .sha256 (os .environ ["GITHUB_TOKEN" ].encode ()).hexdigest ()
104+
105+ try :
106+ with open (f'.dflook-terraform/token-cache/{ token_hash } ' ) as f :
107+ username = f .read ()
108+ debug (f'GITHUB_TOKEN username: { username } ' )
109+ return username
110+ except Exception as e :
111+ debug (str (e ))
112+
101113 response = github_api_request ('get' , 'https://api.github.com/user' )
102114 if response .status_code != 403 :
103115 user = response .json ()
104116 debug ('GITHUB_TOKEN user:' )
105117 debug (json .dumps (user ))
106118
107- return user ['login' ]
119+ username = user ['login' ]
120+ else :
121+ # Assume this is the github actions app token
122+ username = 'github-actions[bot]'
123+
124+ try :
125+ os .makedirs ('.dflook-terraform/token-cache' , exist_ok = True )
126+ with open (f'.dflook-terraform/token-cache/{ token_hash } ' , 'w' ) as f :
127+ f .write (username )
128+ except Exception as e :
129+ debug (str (e ))
108130
109- # Assume this is the github actions app token
110- return 'github-actions[bot]'
131+ debug ( f'GITHUB_TOKEN username: { username } ' )
132+ return username
111133
112134class TerraformComment :
113135 """
@@ -129,10 +151,12 @@ def __init__(self, pr_url: str=None):
129151 response = github_api_request ('get' , self ._issue_url )
130152 response .raise_for_status ()
131153
154+ username = current_user ()
155+
132156 debug ('Looking for an existing comment:' )
133157 for comment in response .json ():
134158 debug (json .dumps (comment ))
135- if comment ['user' ]['login' ] == current_user () :
159+ if comment ['user' ]['login' ] == username :
136160 match = re .match (rf'{ re .escape (self ._comment_identifier )} .*```(?:hcl)?(.*?)```.*' , comment ['body' ], re .DOTALL )
137161
138162 if not match :
0 commit comments