2019-11-15 04:34:32 +00:00
|
|
|
#!/usr/bin/env python3
|
2021-07-16 14:15:31 +01:00
|
|
|
# SPDX-License-Identifier: MIT
|
2019-11-15 04:34:32 +00:00
|
|
|
|
2017-04-27 09:02:32 +01:00
|
|
|
import os
|
|
|
|
import sys
|
2021-09-03 13:10:24 +01:00
|
|
|
import requests
|
2019-11-15 04:34:32 +00:00
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
BOT_URL = "https://tldr-bot.starbeamrainbowlabs.com"
|
2019-11-15 04:34:32 +00:00
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
COMMENT_ERROR = """
|
2020-06-09 15:03:35 +01:00
|
|
|
The [build](https://github.com/tldr-pages/tldr/actions/runs/{build_id}) for this PR failed with the following error(s):
|
2019-11-15 04:34:32 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
{content}
|
|
|
|
```
|
|
|
|
|
|
|
|
Please fix the error(s) and push again.
|
|
|
|
"""
|
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
COMMENT_CHECK = """
|
2019-11-15 04:34:32 +00:00
|
|
|
Hello! I've noticed something unusual when checking this PR:
|
|
|
|
|
|
|
|
{content}
|
|
|
|
|
2020-01-13 19:45:31 +00:00
|
|
|
Is this intended? If so, just ignore this comment. Otherwise, please double-check the commits.
|
2019-11-15 04:34:32 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
|
2019-11-15 04:34:32 +00:00
|
|
|
def post_comment(pr_id, body, once):
|
2023-10-04 09:32:15 +01:00
|
|
|
endpoint = f"{BOT_URL}/comment"
|
2021-09-03 16:17:51 +01:00
|
|
|
|
|
|
|
if once:
|
|
|
|
endpoint += "/once"
|
|
|
|
|
|
|
|
data = {"pr_id": pr_id, "body": body}
|
|
|
|
|
|
|
|
try:
|
|
|
|
with requests.post(endpoint, json=data) as r:
|
|
|
|
if r.status_code != requests.codes.ok:
|
|
|
|
print(
|
|
|
|
"Error: tldr-bot responded with code",
|
|
|
|
r.status_code,
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
|
|
|
print(r.text, file=sys.stderr)
|
|
|
|
return False
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
|
|
print("Error sending data to tldr-bot:", str(e), file=sys.stderr)
|
|
|
|
return False
|
2019-11-18 21:55:29 +00:00
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
return True
|
2019-11-15 04:34:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main(action):
|
2021-09-03 16:17:51 +01:00
|
|
|
if action not in ("report-errors", "report-check-results"):
|
|
|
|
print("Unknown action:", action, file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
content = sys.stdin.read().strip()
|
2019-11-15 04:34:32 +00:00
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
if action == "report-errors":
|
|
|
|
comment_body = COMMENT_ERROR.format(build_id=BUILD_ID, content=content)
|
|
|
|
comment_once = False
|
|
|
|
elif action == "report-check-results":
|
|
|
|
comment_body = COMMENT_CHECK.format(content=content)
|
|
|
|
comment_once = True
|
2019-11-15 04:34:32 +00:00
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
if post_comment(PR_ID, comment_body, comment_once):
|
|
|
|
print("Success.")
|
|
|
|
else:
|
|
|
|
print("Error sending data to tldr-bot!", file=sys.stderr)
|
2019-11-15 04:34:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
REPO_SLUG = os.environ.get("GITHUB_REPOSITORY")
|
|
|
|
PR_ID = os.environ.get("PULL_REQUEST_ID")
|
|
|
|
BUILD_ID = os.environ.get("GITHUB_RUN_ID")
|
2019-11-15 04:34:32 +00:00
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
if PR_ID is None or BUILD_ID is None or REPO_SLUG is None:
|
|
|
|
print("Needed environment variables are not set.", file=sys.stderr)
|
|
|
|
sys.exit(1)
|
2019-11-15 04:34:32 +00:00
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
if PR_ID is None or PR_ID == "false":
|
|
|
|
print("Not a pull request, refusing to run.", file=sys.stderr)
|
|
|
|
sys.exit(0)
|
2019-11-15 04:34:32 +00:00
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
if len(sys.argv) != 2:
|
|
|
|
print("Usage:", sys.argv[0], "<ACTION>", file=sys.stderr)
|
|
|
|
sys.exit(1)
|
2019-11-15 04:34:32 +00:00
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
main(sys.argv[1])
|