|
5 | 5 | import sys
|
6 | 6 | import requests
|
7 | 7 |
|
8 |
| -BOT_URL = 'https://tldr-bot.starbeamrainbowlabs.com' |
| 8 | +BOT_URL = "https://tldr-bot.starbeamrainbowlabs.com" |
9 | 9 |
|
10 |
| -COMMENT_ERROR=""" |
| 10 | +COMMENT_ERROR = """ |
11 | 11 | The [build](https://github.com/tldr-pages/tldr/actions/runs/{build_id}) for this PR failed with the following error(s):
|
12 | 12 |
|
13 | 13 | ```
|
|
17 | 17 | Please fix the error(s) and push again.
|
18 | 18 | """
|
19 | 19 |
|
20 |
| -COMMENT_CHECK=""" |
| 20 | +COMMENT_CHECK = """ |
21 | 21 | Hello! I've noticed something unusual when checking this PR:
|
22 | 22 |
|
23 | 23 | {content}
|
|
27 | 27 |
|
28 | 28 | ################################################################################
|
29 | 29 |
|
| 30 | + |
30 | 31 | def post_comment(pr_id, body, once):
|
31 |
| - endpoint = BOT_URL + '/comment' |
| 32 | + endpoint = BOT_URL + "/comment" |
| 33 | + |
| 34 | + if once: |
| 35 | + endpoint += "/once" |
| 36 | + |
| 37 | + data = {"pr_id": pr_id, "body": body} |
| 38 | + |
| 39 | + try: |
| 40 | + with requests.post(endpoint, json=data) as r: |
| 41 | + if r.status_code != requests.codes.ok: |
| 42 | + print( |
| 43 | + "Error: tldr-bot responded with code", |
| 44 | + r.status_code, |
| 45 | + file=sys.stderr, |
| 46 | + ) |
| 47 | + print(r.text, file=sys.stderr) |
| 48 | + return False |
| 49 | + except requests.exceptions.RequestException as e: |
| 50 | + print("Error sending data to tldr-bot:", str(e), file=sys.stderr) |
| 51 | + return False |
32 | 52 |
|
33 |
| - if once: |
34 |
| - endpoint += '/once' |
| 53 | + return True |
35 | 54 |
|
36 |
| - data = {'pr_id': pr_id, 'body': body} |
37 |
| - |
38 |
| - try: |
39 |
| - with requests.post(endpoint, json=data) as r: |
40 |
| - if r.status_code != requests.codes.ok: |
41 |
| - print('Error: tldr-bot responded with code', r.status_code, file=sys.stderr) |
42 |
| - print(r.text, file=sys.stderr) |
43 |
| - return False |
44 |
| - except requests.exceptions.RequestException as e: |
45 |
| - print('Error sending data to tldr-bot:', str(e), file=sys.stderr) |
46 |
| - return False |
47 |
| - |
48 |
| - return True |
49 | 55 |
|
50 | 56 | def main(action):
|
51 |
| - if action not in ('report-errors', 'report-check-results'): |
52 |
| - print('Unknown action:', action, file=sys.stderr) |
53 |
| - sys.exit(1) |
| 57 | + if action not in ("report-errors", "report-check-results"): |
| 58 | + print("Unknown action:", action, file=sys.stderr) |
| 59 | + sys.exit(1) |
| 60 | + |
| 61 | + content = sys.stdin.read().strip() |
54 | 62 |
|
55 |
| - content = sys.stdin.read().strip() |
| 63 | + if action == "report-errors": |
| 64 | + comment_body = COMMENT_ERROR.format(build_id=BUILD_ID, content=content) |
| 65 | + comment_once = False |
| 66 | + elif action == "report-check-results": |
| 67 | + comment_body = COMMENT_CHECK.format(content=content) |
| 68 | + comment_once = True |
56 | 69 |
|
57 |
| - if action == 'report-errors': |
58 |
| - comment_body = COMMENT_ERROR.format(build_id=BUILD_ID, content=content) |
59 |
| - comment_once = False |
60 |
| - elif action == 'report-check-results': |
61 |
| - comment_body = COMMENT_CHECK.format(content=content) |
62 |
| - comment_once = True |
| 70 | + if post_comment(PR_ID, comment_body, comment_once): |
| 71 | + print("Success.") |
| 72 | + else: |
| 73 | + print("Error sending data to tldr-bot!", file=sys.stderr) |
63 | 74 |
|
64 |
| - if post_comment(PR_ID, comment_body, comment_once): |
65 |
| - print('Success.') |
66 |
| - else: |
67 |
| - print('Error sending data to tldr-bot!', file=sys.stderr) |
68 | 75 |
|
69 | 76 | ################################################################################
|
70 | 77 |
|
71 |
| -if __name__ == '__main__': |
72 |
| - REPO_SLUG = os.environ.get('GITHUB_REPOSITORY') |
73 |
| - PR_ID = os.environ.get('PULL_REQUEST_ID') |
74 |
| - BUILD_ID = os.environ.get('GITHUB_RUN_ID') |
| 78 | +if __name__ == "__main__": |
| 79 | + REPO_SLUG = os.environ.get("GITHUB_REPOSITORY") |
| 80 | + PR_ID = os.environ.get("PULL_REQUEST_ID") |
| 81 | + BUILD_ID = os.environ.get("GITHUB_RUN_ID") |
75 | 82 |
|
76 |
| - if PR_ID is None or BUILD_ID is None or REPO_SLUG is None: |
77 |
| - print('Needed environment variables are not set.', file=sys.stderr) |
78 |
| - sys.exit(1) |
| 83 | + if PR_ID is None or BUILD_ID is None or REPO_SLUG is None: |
| 84 | + print("Needed environment variables are not set.", file=sys.stderr) |
| 85 | + sys.exit(1) |
79 | 86 |
|
80 |
| - if PR_ID is None or PR_ID == 'false': |
81 |
| - print('Not a pull request, refusing to run.', file=sys.stderr) |
82 |
| - sys.exit(0) |
| 87 | + if PR_ID is None or PR_ID == "false": |
| 88 | + print("Not a pull request, refusing to run.", file=sys.stderr) |
| 89 | + sys.exit(0) |
83 | 90 |
|
84 |
| - if len(sys.argv) != 2: |
85 |
| - print('Usage:', sys.argv[0], '<ACTION>', file=sys.stderr) |
86 |
| - sys.exit(1) |
| 91 | + if len(sys.argv) != 2: |
| 92 | + print("Usage:", sys.argv[0], "<ACTION>", file=sys.stderr) |
| 93 | + sys.exit(1) |
87 | 94 |
|
88 |
| - main(sys.argv[1]) |
| 95 | + main(sys.argv[1]) |
0 commit comments