-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
163 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: Test Reaction | ||
|
||
on: [issue_comment] | ||
|
||
jobs: | ||
issue_comment: | ||
if: ${{ github.event.issue.pull_request && contains(github.event.comment.user.login, 'dflook') }} | ||
runs-on: ubuntu-latest | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
steps: | ||
- name: Plan | ||
uses: ./terraform-version | ||
with: | ||
path: tests/plan/no_changes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#!/usr/bin/python3 | ||
|
||
import datetime | ||
import json | ||
import os | ||
import sys | ||
from typing import Optional, cast, NewType, TypedDict | ||
|
||
import requests | ||
|
||
GitHubUrl = NewType('GitHubUrl', str) | ||
CommentReactionUrl = NewType('CommentReactionUrl', GitHubUrl) | ||
|
||
|
||
class GitHubActionsEnv(TypedDict): | ||
""" | ||
Environment variables that are set by the actions runner | ||
""" | ||
GITHUB_API_URL: str | ||
GITHUB_TOKEN: str | ||
GITHUB_EVENT_PATH: str | ||
GITHUB_EVENT_NAME: str | ||
GITHUB_REPOSITORY: str | ||
GITHUB_SHA: str | ||
|
||
|
||
job_tmp_dir = os.environ.get('JOB_TMP_DIR', '.') | ||
step_tmp_dir = os.environ.get('STEP_TMP_DIR', '.') | ||
|
||
env = cast(GitHubActionsEnv, os.environ) | ||
|
||
|
||
def github_session(github_env: GitHubActionsEnv) -> requests.Session: | ||
""" | ||
A request session that is configured for the github API | ||
""" | ||
session = requests.Session() | ||
session.headers['authorization'] = f'token {github_env["GITHUB_TOKEN"]}' | ||
session.headers['user-agent'] = 'terraform-github-actions' | ||
session.headers['accept'] = 'application/vnd.github.v3+json' | ||
return session | ||
|
||
|
||
def github_api_request(method: str, *args, **kwargs) -> requests.Response: | ||
response = github.request(method, *args, **kwargs) | ||
|
||
if 400 <= response.status_code < 500: | ||
debug(str(response.headers)) | ||
|
||
try: | ||
message = response.json()['message'] | ||
|
||
if response.headers['X-RateLimit-Remaining'] == '0': | ||
limit_reset = datetime.datetime.fromtimestamp(int(response.headers['X-RateLimit-Reset'])) | ||
sys.stdout.write(message) | ||
sys.stdout.write(f' Try again when the rate limit resets at {limit_reset} UTC.\n') | ||
exit(1) | ||
|
||
if message != 'Resource not accessible by integration': | ||
sys.stdout.write(message) | ||
sys.stdout.write('\n') | ||
debug(response.content.decode()) | ||
|
||
except Exception: | ||
sys.stdout.write(response.content.decode()) | ||
sys.stdout.write('\n') | ||
raise | ||
|
||
return response | ||
|
||
|
||
def debug(msg: str) -> None: | ||
sys.stderr.write(msg) | ||
sys.stderr.write('\n') | ||
|
||
|
||
def find_reaction_url(actions_env: GitHubActionsEnv) -> Optional[CommentReactionUrl]: | ||
event_type = actions_env['GITHUB_EVENT_NAME'] | ||
|
||
if event_type not in ['issue_comment', 'pull_request_review_comment']: | ||
return None | ||
|
||
with open(actions_env['GITHUB_EVENT_PATH']) as f: | ||
event = json.load(f) | ||
|
||
return event['comment']['reactions']['url'] | ||
|
||
|
||
def react(comment_reaction_url: CommentReactionUrl, reaction_type: str) -> None: | ||
github_api_request('post', comment_reaction_url, json={'content': reaction_type}) | ||
|
||
|
||
def main() -> None: | ||
if len(sys.argv) < 2: | ||
print(f'''Usage: | ||
{sys.argv[0]} <reaction type>''') | ||
|
||
debug(repr(sys.argv)) | ||
|
||
reaction_url = find_reaction_url(env) | ||
if reaction_url is not None: | ||
react(reaction_url, sys.argv[1]) | ||
|
||
|
||
if __name__ == '__main__': | ||
if 'GITHUB_TOKEN' not in env: | ||
exit(0) | ||
github = github_session(env) | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters