This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
forked from dora-team/fourkeys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_data.py
executable file
·279 lines (234 loc) · 8.42 KB
/
generate_data.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import datetime
import hmac
import json
import os
import random
import secrets
import time
import sys
from hashlib import sha1
from urllib.request import Request, urlopen
def make_changes(num_changes, vcs, event_timespan):
changes = []
max_time = time.time() - event_timespan
head_commit = None
event = None
for x in range(num_changes):
change_id = secrets.token_hex(20)
unix_timestamp = time.time() - random.randrange(0, event_timespan)
change = {
"id": change_id,
"timestamp": datetime.datetime.fromtimestamp(unix_timestamp),
}
if unix_timestamp > max_time:
max_time = unix_timestamp
head_commit = change
changes.append(change)
if vcs == "gitlab":
event = {
"object_kind": "push",
"checkout_sha": head_commit["id"],
"commits": changes,
}
if vcs == "github":
event = {"head_commit": head_commit, "commits": changes}
return event
def create_github_deploy_event(change):
deployment = {
"deployment_status": {
"updated_at": change["timestamp"],
"id": secrets.token_hex(20),
"state": "success",
},
"deployment": {
"sha": change["id"],
},
}
return deployment
def create_gitlab_pipeline_event(changes):
pipeline = None
checkout_sha = changes["checkout_sha"]
for c in changes["commits"]:
if c["id"] == checkout_sha:
pipeline = {
"object_kind": "pipeline",
"object_attributes": {
"created_at": c["timestamp"],
"id": random.randrange(0, 1000),
"status": "success",
},
"commit": c,
}
return pipeline
def create_gitlab_deploy_event(changes):
deployment = None
checkout_sha = changes["checkout_sha"]
for c in changes["commits"]:
if c["id"] == checkout_sha:
deployment = {
"object_kind": "deployment",
"status": "success",
"status_changed_at": c["timestamp"].strftime("%F %T +0200"),
"deployment_id": random.randrange(0, 1000),
"commit_url": f"http://example.com/root/test/commit/{checkout_sha}",
}
return deployment
def make_github_issue(root_cause):
event = {
"issue": {
"created_at": root_cause["timestamp"],
"updated_at": datetime.datetime.now(),
"closed_at": datetime.datetime.now(),
"number": random.randrange(0, 1000),
"labels": [{"name": "Incident"}],
"body": "root cause: %s" % root_cause["id"],
},
"repository": {"name": "foobar"},
}
return event
def make_gitlab_issue(changes):
issue = None
checkout_sha = changes["checkout_sha"]
for c in changes["commits"]:
if c["id"] == checkout_sha:
issue = {
"object_kind": "issue",
"object_attributes": {
"created_at": c["timestamp"],
"updated_at": datetime.datetime.now(),
"closed_at": datetime.datetime.now(),
"id": random.randrange(0, 1000),
"labels": [{"title": "Incident"}],
"description": "root cause: %s" % c["id"],
},
}
return issue
def make_webhook_request(vcs, webhook_url, secret, event_type, data, token=None):
data = json.dumps(data, default=str).encode()
request = Request(webhook_url, data)
if vcs == "github":
signature = hmac.new(secret.encode(), data, sha1)
request.add_header("X-Github-Event", event_type)
request.add_header("X-Hub-Signature", "sha1=" + signature.hexdigest())
request.add_header("User-Agent", "GitHub-Hookshot/mock")
if vcs == "gitlab":
request.add_header("X-Gitlab-Event", event_type)
request.add_header("X-Gitlab-Token", secret)
request.add_header("Content-Type", "application/json")
request.add_header("Mock", True)
if token:
request.add_header("Authorization", f"Bearer {token}")
return request
def post_to_webhook(vcs, webhook_url, secret, event_type, data, token=None):
request = make_webhook_request(vcs, webhook_url, secret, event_type, data, token)
response = urlopen(request)
if response.getcode() == 204:
return 1
else:
return 0
if __name__ == "__main__":
# parse arguments
parser = argparse.ArgumentParser()
parser.add_argument(
"--event_timespan",
"-t",
type=int,
default=604800,
help="time duration (in seconds) of timestamps of generated events \
(from [Now-timespan] to [Now]); default=604800 (1 week)",
)
parser.add_argument(
"--num_events",
"-e",
type=int,
default=20,
help="number of events to generate; default=20",
)
parser.add_argument(
"--num_issues",
"-i",
type=int,
default=2,
help="number of issues to generate; default=2",
)
parser.add_argument(
"--vc_system",
"-v",
required=True,
choices=["gitlab", "github"],
help="version control system (e.g. 'github', 'gitlab')",
)
args = parser.parse_args()
if args.num_issues > args.num_events:
print("Error: num_issues cannot be greater than num_events")
sys.exit()
# get environment vars
webhook_url = os.environ.get("WEBHOOK")
secret = os.environ.get("SECRET")
token = os.environ.get("TOKEN")
if not webhook_url or not secret:
print(
"Error: please ensure the following environment variables are set: WEBHOOK, SECRET"
)
sys.exit()
all_changesets = []
changes_sent = 0
for x in range(args.num_events):
# make a change set containing a random number of changes
changeset = make_changes(
random.randrange(1, 5),
args.vc_system,
args.event_timespan,
)
# Send individual changes data
for c in changeset["commits"]:
curr_change = None
if args.vc_system == "gitlab":
curr_change = {
"object_kind": "push",
"checkout_sha": c["id"],
"commits": [c],
}
if args.vc_system == "github":
curr_change = {"head_commit": c, "commits": [c]}
changes_sent += post_to_webhook(
args.vc_system, webhook_url, secret, "push", curr_change, token
)
# Send fully associated push event
post_to_webhook(args.vc_system, webhook_url, secret, "push", changeset, token)
# Make and send a deployment
if args.vc_system == "gitlab":
deploy = create_gitlab_deploy_event(changeset)
post_to_webhook(
args.vc_system, webhook_url, secret, "deployment", deploy, token
)
if args.vc_system == "github":
deploy = create_github_deploy_event(changeset["head_commit"])
post_to_webhook(
args.vc_system, webhook_url, secret, "deployment_status", deploy, token
)
all_changesets.append(changeset)
# randomly create incidents associated to changes
changesets_with_issues = random.sample(all_changesets, args.num_issues)
for changeset in changesets_with_issues:
issue = None
if args.vc_system == "gitlab":
issue = make_gitlab_issue(changeset)
if args.vc_system == "github":
issue = make_github_issue(changeset["head_commit"])
post_to_webhook(args.vc_system, webhook_url, secret, "issues", issue, token)
print(f"{changes_sent} changes successfully sent to event-handler")