|
6 | 6 | import argparse |
7 | 7 | import logging |
8 | 8 | import os |
| 9 | +import hashlib |
9 | 10 |
|
10 | 11 | from github import Github, GithubException, UnknownObjectException |
11 | 12 |
|
|
15 | 16 | CLOUDFLARED_REPO = os.environ.get("GITHUB_REPO", "cloudflare/cloudflared") |
16 | 17 | GITHUB_CONFLICT_CODE = "already_exists" |
17 | 18 |
|
| 19 | +def get_sha256(filename): |
| 20 | + """ get the sha256 of a file """ |
| 21 | + sha256_hash = hashlib.sha256() |
| 22 | + with open(filename,"rb") as f: |
| 23 | + for byte_block in iter(lambda: f.read(4096),b""): |
| 24 | + sha256_hash.update(byte_block) |
| 25 | + return sha256_hash.hexdigest() |
| 26 | + |
| 27 | + |
| 28 | +def update_or_add_message(msg, name, sha): |
| 29 | + """ |
| 30 | + updates or builds the github version message for each new asset's sha256. |
| 31 | + Searches the existing message string to update or create. |
| 32 | + """ |
| 33 | + new_text = '{0}: {1}\n'.format(name, sha) |
| 34 | + start = msg.find(name) |
| 35 | + if (start != -1): |
| 36 | + end = msg.find("\n", start) |
| 37 | + if (end != -1): |
| 38 | + return msg.replace(msg[start:end+1], new_text) |
| 39 | + back = msg.rfind("```") |
| 40 | + if (back != -1): |
| 41 | + return '{0}{1}```'.format(msg[:back], new_text) |
| 42 | + return '{0} \n### SHA256 Checksums:\n```\n {1}```'.format(msg, new_text) |
18 | 43 |
|
19 | 44 | def assert_tag_exists(repo, version): |
20 | 45 | """ Raise exception if repo does not contain a tag matching version """ |
@@ -120,6 +145,14 @@ def main(): |
120 | 145 | return |
121 | 146 |
|
122 | 147 | release.upload_asset(args.path, name=args.name) |
| 148 | + |
| 149 | + # add the sha256 of the new artifact to the release message body |
| 150 | + pkg_hash = get_sha256(args.path) |
| 151 | + |
| 152 | + # update the release body text |
| 153 | + msg = update_or_add_message(release.body, args.name, pkg_hash) |
| 154 | + release.update_release(version, version, msg) |
| 155 | + |
123 | 156 | except Exception as e: |
124 | 157 | logging.exception(e) |
125 | 158 | exit(1) |
|
0 commit comments