-
Notifications
You must be signed in to change notification settings - Fork 21
/
entrypoint.py
99 lines (72 loc) · 2.15 KB
/
entrypoint.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import json
import os
from github import Github
def read_json(filepath):
"""
Read a json file as a dictionary.
Parameters
----------
filepath : str
Returns
-------
data : dict
"""
with open(filepath, 'r') as f:
return json.load(f)
def get_actions_input(input_name):
"""
Get a Github actions input by name.
Parameters
----------
input_name : str
Returns
-------
action_input : str
Notes
-----
GitHub Actions creates an environment variable for the input with the name:
INPUT_<CAPITALIZED_VARIABLE_NAME> (e.g. "INPUT_FOO" for "foo")
References
----------
.. [1] https://help.github.com/en/actions/automating-your-workflow-with-github-actions/metadata-syntax-for-github-actions#example # noqa: E501
"""
return os.getenv('INPUT_{}'.format(input_name).upper())
def load_template(filename):
"""
Load a template.
Parameters
----------
filename : template file name
Returns
-------
template : str
"""
template_path = os.path.join('.github/workflows', filename)
with open(template_path, 'r') as f:
return f.read()
def main():
# search a pull request that triggered this action
gh = Github(os.getenv('GITHUB_TOKEN'))
event = read_json(os.getenv('GITHUB_EVENT_PATH'))
branch_label = event['pull_request']['head']['label'] # author:branch
branch_name = branch_label.split(':')[-1]
repo = gh.get_repo(event['repository']['full_name'])
prs = repo.get_pulls(state='open', sort='created', head=branch_label)
pr = prs[0]
# load template
template = load_template(get_actions_input('filename'))
# build a comment
pr_info = {
'pull_id': pr.number,
'branch_name': branch_name
}
new_comment = template.format(**pr_info)
# check if this pull request has a duplicated comment
old_comments = [c.body for c in pr.get_issue_comments()]
if new_comment in old_comments:
print('This pull request already a duplicated comment.')
exit(0)
# add the comment
pr.create_issue_comment(new_comment)
if __name__ == '__main__':
main()