|
8 | 8 | from os import makedirs |
9 | 9 | from os.path import expanduser |
10 | 10 | from datetime import datetime |
11 | | - |
| 11 | +from hashlib import sha1 |
| 12 | + |
| 13 | +# Using this script |
| 14 | +# Run it from destination repository root with: |
| 15 | +# python /path/to/elastic-agent-changelog-tool/tools/asciidoc_to_fragments.py --path CHANGELOG.next.asciidoc --workdir $PWD |
| 16 | +# |
| 17 | +# If errors arise you should at first try to solve them in the source changelog, |
| 18 | +# so that if you re-run the script you are not required to apply the same fixes |
| 19 | +# again. |
| 20 | +# Fixable errors: |
| 21 | +# - look for duplicated entries |
| 22 | +# - no response from Github API: look for missing or wrong data (es issue number instead of PR number) in {pull} |
| 23 | +# - no PR/issue fields: no {pull} or {issue field present} |
| 24 | +# - multiple PRs/issues found: the tool does not support multiple {pull} or {issue} on the same line; split them or remove all but one {issue} and one {pull} |
| 25 | +# - issue info lost due to multiple repositories: remove the one referring to an external repository |
| 26 | +# - look for files starting with "1000000*", as this timestamp means something is wrong (missing {pull} maybe?) |
| 27 | +# |
| 28 | +# For the remaining errors, fix them in the created fragments. |
12 | 29 |
|
13 | 30 | api_url = "https://api.github.com/repos/" |
14 | 31 | github_token_location = "/.elastic/github.token" |
|
30 | 47 | kind_token = "====" |
31 | 48 | field_token = "-" |
32 | 49 |
|
33 | | -def write_fragment(title, fragment_timestamp, fragment_dict): |
| 50 | +def write_fragment(filename, fragment_timestamp, fragment_dict): |
34 | 51 | if not fragment_timestamp: |
35 | 52 | fragment_timestamp = str(1000000000 + fragments_counter) |
36 | 53 |
|
37 | 54 | path = "".join([fragments_path, |
38 | 55 | fragment_timestamp, |
39 | 56 | "-", |
40 | | - title, |
| 57 | + filename, |
41 | 58 | ".yaml"]) |
42 | 59 |
|
43 | 60 | with open(path, 'w+') as f: |
44 | 61 | for k, v in fragment_dict.items(): |
45 | 62 | f.write(f"{k}: {v}\n") |
46 | 63 |
|
| 64 | + # print path and SHA1 of it's content, for verification purposes |
| 65 | + with open(path, 'r') as f: |
| 66 | + content = f.read() |
| 67 | + hash_object = sha1(content.encode('utf-8')) |
| 68 | + print(path, hash_object.hexdigest()) |
| 69 | + |
47 | 70 | def get_event_timestamp(repository, event, number): |
48 | 71 | token_path = ''.join([expanduser("~"), github_token_location]) |
49 | 72 | with open(token_path, 'r') as f: |
@@ -106,10 +129,8 @@ def parse_line(line, kind): |
106 | 129 | issue_number, issue_repo = number, repo_link |
107 | 130 |
|
108 | 131 | if pr_repo: |
109 | | - fragment_dict["repository"] = pr_repo |
110 | 132 | fragment_timestamp = get_event_timestamp(pr_repo, "pulls", pr_number) |
111 | 133 | elif issue_repo: |
112 | | - fragment_dict["repository"] = issue_repo |
113 | 134 | fragment_timestamp = get_event_timestamp(issue_repo, "issues", issue_number) |
114 | 135 |
|
115 | 136 | if fragment_timestamp == "not_found": |
|
0 commit comments