-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
176 lines (141 loc) · 5.18 KB
/
main.py
File metadata and controls
176 lines (141 loc) · 5.18 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import json
import os
import sys
from http import HTTPStatus
from typing import Optional, Tuple
import requests
GITHUB_API_BASE_URL = os.environ.get('GITHUB_API_URL', 'https://api.github.com')
STEPSECURITY_API_BASE_URL = 'https://agent.api.stepsecurity.io'
def escape(v: str) -> str:
return repr(v)[1:-1]
def set_action_output(name: str, value: str):
github_output = os.environ.get('GITHUB_OUTPUT', '')
if github_output:
with open(github_output, 'a') as f:
f.write(f'{name}={value}\n')
else:
sys.stdout.write(f'::set-output name={name}::{escape(value)}\n')
def print_action_error(msg: str):
sys.stdout.write(f'::error file={__name__}::{escape(msg)}\n')
def print_action_debug(msg: str):
sys.stdout.write(f'::debug file={__name__}::{escape(msg)}\n')
def get_action_input(
name: str, required: bool = False, default: Optional[str] = None
) -> str:
v = os.environ.get(f'INPUT_{name.upper()}', '')
if v == '' and default:
v = default
if required and v == '':
print_action_error(f'input required and not supplied: {name}')
exit(1)
return v
def create(token, repo, body, issue_number) -> Tuple[str, str]:
headers = {
'Authorization': f'token {token}',
}
data = {
'body': body,
}
resp = requests.post(
f'{GITHUB_API_BASE_URL}/repos/{repo}/issues/{issue_number}/comments',
headers=headers,
json=data,
)
if resp.status_code != HTTPStatus.CREATED:
print_action_error(f'cannot create comment')
print_action_debug(f'status code: {resp.status_code}')
print_action_debug(f'response body: {resp.text}')
exit(1)
json = resp.json()
return str(json['id']), body
def edit(token, repo, body, comment_id) -> Tuple[str, str]:
headers = {
'Authorization': f'token {token}',
}
data = {
'body': body,
}
resp = requests.patch(
f'{GITHUB_API_BASE_URL}/repos/{repo}/issues/comments/{comment_id}',
headers=headers,
json=data,
)
if resp.status_code != HTTPStatus.OK:
print_action_error(f'cannot edit comment')
print_action_debug(f'status code: {resp.status_code}')
print_action_debug(f'response body: {resp.text}')
exit(1)
json = resp.json()
return str(json['id']), body
def delete(token, repo, comment_id) -> Tuple[str, str]:
headers = {
'Authorization': f'token {token}',
}
resp = requests.delete(
f'{GITHUB_API_BASE_URL}/repos/{repo}/issues/comments/{comment_id}',
headers=headers,
)
if resp.status_code != HTTPStatus.NO_CONTENT:
print_action_error(f'cannot delete comment')
print_action_debug(f'status code: {resp.status_code}')
print_action_debug(f'response body: {resp.text}')
exit(1)
return '', ''
def check_subscription():
upstream = 'winterjung/comment'
docs_url = 'https://docs.stepsecurity.io/actions/stepsecurity-maintained-actions'
action = os.environ.get('GITHUB_ACTION_REPOSITORY', '')
repo = os.environ.get('GITHUB_REPOSITORY', '')
repo_private = None
event_path = os.environ.get('GITHUB_EVENT_PATH', '')
if event_path:
try:
with open(event_path) as f:
payload = json.load(f)
repo_private = payload.get('repository', {}).get('private')
except Exception:
pass
print()
print('\033[1;36mStepSecurity Maintained Action\033[0m')
print(f'Secure drop-in replacement for {upstream}')
if repo_private is False:
print('\033[32m\u2713 Free for public repositories\033[0m')
print(f'\033[36mLearn more:\033[0m {docs_url}')
print()
if repo_private is False:
return
server_url = os.environ.get('GITHUB_SERVER_URL', 'https://github.com')
body = {'action': action}
if server_url != 'https://github.com':
body['ghes_server'] = server_url
try:
resp = requests.post(
f'{STEPSECURITY_API_BASE_URL}/v1/github/{repo}/actions/maintained-actions-subscription',
json=body,
timeout=3,
)
if resp.status_code == 403:
print('\033[1;31mThis action requires a StepSecurity subscription for private repositories.\033[0m', file=sys.stderr)
print(f'\033[31mLearn how to enable a subscription: {docs_url}\033[0m', file=sys.stderr)
exit(1)
except Exception:
print('Timeout or API not reachable. Continuing to next step.')
def main():
check_subscription()
repo = os.environ['GITHUB_REPOSITORY']
action_type = get_action_input('type', required=True)
token = get_action_input('token', required=True)
body = get_action_input('body')
comment_id = get_action_input('comment_id')
issue_number = get_action_input('issue_number')
_id, _body = '', ''
if action_type == 'create':
_id, _body = create(token, repo, body, issue_number)
elif action_type == 'edit':
_id, _body = edit(token, repo, body, comment_id)
elif action_type == 'delete':
_id, _body = delete(token, repo, comment_id)
set_action_output('id', _id)
set_action_output('body', _body)
if __name__ == '__main__':
main()