-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.py
More file actions
83 lines (64 loc) · 2.61 KB
/
action.py
File metadata and controls
83 lines (64 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
import argparse
from github import Github
import os
import sys
NOTE = "\n\n*Note: This comment is automatically posted by the " \
"Documentation Publish GitHub Action.*"
PREFIX = "You can find the documentation preview for this PR at this " \
"<a href='{}'>link</a>." \
def main():
parser = argparse.ArgumentParser(
description="GH action to comment on a PR with the documentation preview URL",
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-u', '--urlroot', action='store', required=True,
help='Root of the URL were documentation preview is '
'located.')
parser.add_argument('-p', '--pr-prefix', action='store', required=False,
help='Prefix added to the contents of "--pr-file" '
'when generating the URL link.')
parser.add_argument('-f', '--pr-file', action='store', required=True,
help='File generated by the workflow containing the '
'number of the PR for which documentation '
'preview is being generated.')
token = os.environ.get('GITHUB_TOKEN', None)
if not token:
sys.exit('Github token not set in environment, please set the '
'GITHUB_TOKEN environment variable and retry.')
args = parser.parse_args()
urlroot = args.urlroot
pr_prefix = args.pr_prefix
pr_file = args.pr_file
action = os.environ.get('GITHUB_ACTION', None)
workflow = os.environ.get('GITHUB_WORKFLOW', None)
org_repo = os.environ.get('GITHUB_REPOSITORY', None)
print(f'Running action {action} from workflow {workflow} in {org_repo}')
try:
f = open(pr_file)
except FileNotFoundError:
print(f'Skipping comment because {pr_file} does not exist')
sys.exit(0)
pull_request = f.read()
gh = Github(token)
tk_usr = gh.get_user()
gh_repo = gh.get_repo(org_repo)
gh_pr = gh_repo.get_pull(int(pull_request))
if not urlroot.endswith('/'):
urlroot += '/'
if pr_prefix is None:
pr_prefix = ''
url = urlroot + pr_prefix + pull_request + '/'
message = PREFIX.format(url) + NOTE
comment = None
for c in gh_pr.get_issue_comments():
if c.user.login == tk_usr.login and NOTE in c.body:
comment = c
break
if comment is None:
print('Creating comment')
gh_pr.create_issue_comment(message)
else:
print('Comment already exists, skipping')
sys.exit(0)
if __name__ == '__main__':
main()