|
1 |
| -import json |
2 | 1 | import os
|
3 | 2 | import re
|
4 | 3 |
|
|
7 | 6 | DEPLOY_OUTPUT = os.environ['DEPLOY_OUTPUT']
|
8 | 7 | GITHUB_TOKEN = os.environ['GITHUB_TOKEN']
|
9 | 8 | REPOSITORY = os.environ['REPOSITORY']
|
10 |
| -REF = os.environ['REF'] |
11 |
| -ENVIRONMENT = 'deploy-preview' |
| 9 | +PULL_REQUEST_NUMBER = os.environ['PULL_REQUEST_NUMBER'] |
12 | 10 |
|
13 | 11 | m = re.search(r'https://(\S+)\.workers\.dev', DEPLOY_OUTPUT)
|
14 | 12 | assert m, f'Could not find worker URL in {DEPLOY_OUTPUT!r}'
|
|
19 | 17 |
|
20 | 18 | version_id = m.group(1)
|
21 | 19 | preview_url = f'https://{version_id}-{worker_name}.workers.dev'
|
| 20 | +print('Docs preview URL:', preview_url) |
22 | 21 |
|
23 | 22 | gh_headers = {
|
24 | 23 | 'Accept': 'application/vnd.github+json',
|
25 | 24 | 'Authorization': f'Bearer {GITHUB_TOKEN}',
|
26 | 25 | 'X-GitHub-Api-Version': '2022-11-28',
|
27 | 26 | }
|
28 | 27 |
|
29 |
| -deployment_url = f'https://api.github.com/repos/{REPOSITORY}/deployments' |
30 |
| -deployment_data = { |
31 |
| - 'ref': REF, |
32 |
| - 'task': f'docs preview {version_id}', |
33 |
| - 'environment': ENVIRONMENT, |
34 |
| - 'auto_merge': False, |
35 |
| - 'required_contexts': [], |
36 |
| - 'payload': json.dumps({ |
37 |
| - 'preview_url': preview_url, |
38 |
| - 'worker_name': worker_name, |
39 |
| - 'version_id': version_id, |
40 |
| - }) |
41 |
| -} |
42 |
| -r = httpx.post(deployment_url, headers=gh_headers, json=deployment_data) |
43 |
| -print(f'POST {deployment_url}: {r.status_code}') # {r.text} |
44 |
| -r.raise_for_status() |
45 |
| -deployment_id = r.json()['id'] |
46 |
| - |
47 |
| -status_url = f'https://api.github.com/repos/{REPOSITORY}/deployments/{deployment_id}/statuses' |
48 |
| -status_data = { |
49 |
| - 'environment': ENVIRONMENT, |
50 |
| - 'environment_url': preview_url, |
51 |
| - 'state': 'success', |
52 |
| -} |
53 |
| -r = httpx.post(status_url, headers=gh_headers, json=status_data) |
54 |
| -print(f'POST {status_url}: {r.status_code}') # {r.text} |
| 28 | +# now create or update a comment on the PR with the preview URL |
| 29 | + |
| 30 | +comments_url = f'https://api.github.com/repos/{REPOSITORY}/issues/{PULL_REQUEST_NUMBER}/comments' |
| 31 | +r = httpx.get(comments_url, headers=gh_headers) |
| 32 | +print(f'GET {comments_url} {r.status_code}') |
| 33 | +comment_update_url = None |
| 34 | + |
| 35 | +for comment in r.json(): |
| 36 | + if comment['user']['login'] == 'github-actions[bot]' and comment['body'].startswith('## Docs Preview'): |
| 37 | + comment_update_url = comment['url'] |
| 38 | + break |
| 39 | + |
| 40 | +body = f"""\ |
| 41 | +## Docs Preview |
| 42 | +
|
| 43 | +<table> |
| 44 | +<tr> |
| 45 | +<td><strong>Preview URL:</strong></td> |
| 46 | +<td><a href="{preview_url}">{preview_url}</a></td> |
| 47 | +</tr> |
| 48 | +</table> |
| 49 | +""" |
| 50 | +comment_data = {'body': body} |
| 51 | + |
| 52 | +if comment_update_url: |
| 53 | + print('Updating existing comment...') |
| 54 | + r = httpx.patch(comment_update_url, headers=gh_headers, json=comment_data) |
| 55 | +else: |
| 56 | + print('Creating new comment...') |
| 57 | + r = httpx.post(comments_url, headers=gh_headers, json=comment_data) |
| 58 | + |
| 59 | +print(f'{r.request.method} {comments_url} {r.status_code}') |
55 | 60 | r.raise_for_status()
|
0 commit comments