Skip to content

Commit 63b84db

Browse files
committed
Cache the token username
This can reduce the number of github api requests
1 parent 166dcff commit 63b84db

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ When using an action you can specify the version as:
1212
- `@v1.6` to use the latest patch release for the specific minor version
1313
- `@v1` to use the latest patch release for the specific major version
1414

15-
## [1.6.0] - 2021-02-24
15+
## [1.6.0] - 2021-02-25
1616

1717
### Added
1818
- PR comments use a one line summary of the terraform output, with the full output in a collapsable pane.
1919

2020
If a plan is short the output is shown by default. This can be controlled with the `TF_PLAN_COLLAPSE_LENGTH` environment
2121
variable for the [dflook/terraform-plan](terraform-plan) action.
2222

23+
### Fixed
24+
- Now makes far fewer github api requests to avoid rate limiting.
25+
2326
## [1.5.2] - 2021-01-16
2427

2528
### Fixed

image/tools/github_pr_comment.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import re
66
import sys
77
import datetime
8+
import hashlib
89
from typing import Optional, Dict, Iterable
910

1011
import 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

100101
def 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

112134
class 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

Comments
 (0)