Skip to content

Commit c415579

Browse files
authored
Update fragment title in script with event timestamp (#93)
1 parent 625d77b commit c415579

File tree

3 files changed

+56
-18
lines changed

3 files changed

+56
-18
lines changed

tools/CHANGELOG.next.asciidoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- Allow the / char in variable names in eql and transpiler. {issue}715[715] {pull}718[718]
2020
- Fix data duplication for standalone agent on Kubernetes using the default manifest {issue-beats}31512[31512] {pull}742[742]
2121
- Agent updates will clean up unneeded artifacts. {issue}693[693] {issue}694[694] {pull}752[752]
22+
- Partial extracted beat result in failure to spawn beat {issue-beats}[21718]
2223

2324
==== New features
2425

@@ -30,3 +31,5 @@
3031
- Changed the default policy selection logic. When the agent has no policy id or name defined, it will fall back to defaults (defined by $FLEET_SERVER_POLICY_ID and $FLEET_DEFAULT_TOKEN_POLICY_NAME environment variables respectively). {issue-beats}[29774] {pull}226[226]
3132
- Support scheduled actions and cancellation of pending actions. {issue}393[393] {pull}419[419]
3233
- Bump node.js version for heartbeat/synthetics to 16.15.0
34+
- Test PR still open {pull}247[247]
35+
- Test PR does not exist {pull}100000[100000]

tools/asciidoc_to_fragments.py

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
# or more contributor license agreements. Licensed under the Elastic License 2.0;
33
# you may not use this file except in compliance with the Elastic License 2.0.
44

5-
import time
65
import argparse
6+
import requests
7+
78
from os import makedirs
9+
from os.path import expanduser
10+
from datetime import datetime
811

912

10-
timestamp = round(time.time())
13+
api_url = "https://api.github.com/repos/"
14+
github_token_location = "/.elastic/github.token"
1115

1216
fragments_path = "changelog/fragments/"
1317
fragments_counter = 0
@@ -26,10 +30,12 @@
2630
kind_token = "===="
2731
field_token = "-"
2832

33+
def write_fragment(title, fragment_timestamp, fragment_dict):
34+
if not fragment_timestamp:
35+
fragment_timestamp = str(1000000000 + fragments_counter)
2936

30-
def write_fragment(title, fragment_dict):
3137
path = "".join([fragments_path,
32-
str(timestamp + fragments_counter),
38+
fragment_timestamp,
3339
"-",
3440
title,
3541
".yaml"])
@@ -38,13 +44,33 @@ def write_fragment(title, fragment_dict):
3844
for k, v in fragment_dict.items():
3945
f.write(f"{k}: {v}\n")
4046

47+
def get_event_timestamp(repository, event, number):
48+
token_path = ''.join([expanduser("~"), github_token_location])
49+
with open(token_path, 'r') as f:
50+
token = f.read().rstrip()
51+
52+
owner, repo = repository.split("/")[-2:]
53+
event_url = f"{owner}/{repo}/{event}/{number}"
54+
url = f"{api_url}{event_url}"
55+
headers = {"Accept": "application/vnd.github+json", "Authorization": f"Bearer {token}"}
56+
response = requests.get(url, headers=headers)
57+
data = response.json()
58+
59+
if response.status_code == 404:
60+
return "not_found"
61+
elif data["closed_at"] is None:
62+
return "event_open"
63+
else:
64+
date = datetime.fromisoformat(data["closed_at"].replace('Z', '+00:00'))
65+
return str(int(datetime.timestamp(date)))
66+
4167
def parse_line(line, kind):
4268
global fragments_counter
4369
fragments_counter += 1
4470

4571
summary, *entries = line.split(" {")
4672
if len(entries) == 0:
47-
print(f"Warning: {line} has no PR/issue fields!\n")
73+
print(f"Warning: {line} -> no PR/issue fields\n")
4874

4975
fragment_dict = {"kind": kind}
5076
fragment_dict["summary"] = summary.lstrip(field_token).strip()
@@ -55,7 +81,7 @@ def parse_line(line, kind):
5581
title = title.replace("/", "|")
5682
title = title.rstrip(".")
5783

58-
pr_repo, issue_repo = "", ""
84+
pr_repo, issue_repo, fragment_timestamp = "", "", ""
5985

6086
for entry in entries:
6187
number = entry[entry.find("[")+1:entry.find("]")]
@@ -68,31 +94,39 @@ def parse_line(line, kind):
6894
except ValueError:
6995
fragment_field, repo_link = entry_data, default_repolink
7096

71-
fragment_field = fragment_field.replace("pull", "pr")
72-
7397
if fragment_field in fragment_dict.keys():
74-
print(f"Skipping {line} -> multiple PRs/issues found!\n")
98+
print(f"Skipping {line} -> multiple PRs/issues found\n")
7599
return
76100

77-
if fragment_field == "pr":
78-
fragment_dict[fragment_field] = number
79-
pr_repo = repo_link
101+
if fragment_field == "pull":
102+
fragment_dict["pr"] = ''.join([repo_link, '/pull/', number])
103+
pr_number, pr_repo = number, repo_link
80104
elif fragment_field == "issue":
81-
fragment_dict[fragment_field] = number
82-
issue_repo = repo_link
83-
105+
fragment_dict["issue"] = ''.join([repo_link, '/issue/', number])
106+
issue_number, issue_repo = number, repo_link
107+
84108
if pr_repo:
85109
fragment_dict["repository"] = pr_repo
110+
fragment_timestamp = get_event_timestamp(pr_repo, "pulls", pr_number)
86111
elif issue_repo:
87112
fragment_dict["repository"] = issue_repo
113+
fragment_timestamp = get_event_timestamp(issue_repo, "issues", issue_number)
114+
115+
if fragment_timestamp == "not_found":
116+
print(f"Skipping {line} -> no response from Github API\n")
117+
return
118+
if fragment_timestamp == "event_open":
119+
print(f"Skipping {line} -> PR/issue still open!\n")
120+
return
88121

89122
if issue_repo != pr_repo and pr_repo:
90123
try:
91124
del fragment_dict["issue"]
125+
print(f"Warning: {line} -> issue info lost due to multiple repositories\n")
92126
except KeyError:
93127
pass
94-
95-
write_fragment(title, fragment_dict)
128+
129+
write_fragment(title, fragment_timestamp, fragment_dict)
96130

97131
def iterate_lines(f, kind='', skip=True):
98132
line = next(f, None)
@@ -132,6 +166,6 @@ def iterate_lines(f, kind='', skip=True):
132166
except FileExistsError as e:
133167
pass
134168

135-
print("Skipped entries should be manually created")
169+
print("Skipped entries should be manually created and warnings should be checked")
136170
with open(args.path, 'r') as f:
137171
iterate_lines(f)

tools/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests==2.28.1

0 commit comments

Comments
 (0)