Skip to content

Commit db316e4

Browse files
committed
🐛 Search for PRs from the current token user
Don't assume the default github actions token before checking if it is a user PAT
1 parent 907ccdf commit db316e4

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

.github/workflows/test-apply.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,32 @@ jobs:
343343
echo "Apply did not fail correctly"
344344
exit 1
345345
fi
346+
347+
apply_user_token:
348+
runs-on: ubuntu-latest
349+
name: Apply using a personal access token
350+
env:
351+
GITHUB_TOKEN: ${{ secrets.USER_GITHUB_TOKEN }}
352+
steps:
353+
- name: Checkout
354+
uses: actions/checkout@v2
355+
356+
- name: Plan
357+
uses: ./terraform-plan
358+
with:
359+
label: User PAT
360+
path: tests/apply/changes
361+
362+
- name: Apply
363+
uses: ./terraform-apply
364+
id: output
365+
with:
366+
label: User PAT
367+
path: tests/apply/changes
368+
369+
- name: Verify outputs
370+
run: |
371+
if [[ "${{ steps.output.outputs.output_string }}" != "the_string" ]]; then
372+
echo "::error:: output s not set correctly"
373+
exit 1
374+
fi

image/tools/github_pr_comment.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ def find_pr() -> str:
7070
else:
7171
raise Exception(f"The {event_type} event doesn\'t relate to a Pull Request.")
7272

73+
def current_user() -> str:
74+
response = github.get('https://api.github.com/user')
75+
if response.status_code != 403:
76+
user = response.json()
77+
debug('GITHUB_TOKEN user:')
78+
debug(json.dumps(user))
79+
80+
return user['login']
81+
82+
# Assume this is the github actions app token
83+
return 'github-actions[bot]'
84+
7385
class TerraformComment:
7486
"""
7587
The GitHub comment for this specific terraform plan
@@ -90,7 +102,7 @@ def __init__(self, pr_url: str):
90102
debug('Looking for an existing comment:')
91103
for comment in response.json():
92104
debug(json.dumps(comment))
93-
if comment['user']['login'] == 'github-actions[bot]':
105+
if comment['user']['login'] == current_user():
94106
match = re.match(rf'{re.escape(self._comment_identifier)}\n```(.*?)```(.*)', comment['body'], re.DOTALL)
95107

96108
if not match:

tests/terraform-cloud/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ terraform {
66
prefix = "github-actions-"
77
}
88
}
9-
required_version = "~> 0.13.0"
9+
required_version = "0.13.0"
1010
}
1111

1212
resource "random_id" "the_id" {

0 commit comments

Comments
 (0)