Skip to content

Commit

Permalink
Gate more things behind PROD env var
Browse files Browse the repository at this point in the history
  • Loading branch information
dhinakg committed Apr 28, 2023
1 parent c495c97 commit 86111f9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 58 deletions.
104 changes: 53 additions & 51 deletions add.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,30 +90,31 @@ def add_built(plugin, token):
release["date_authored"] = dateutil.parser.parse(commit_info["commit"]["author"]["date"]).isoformat()
release["source"] = "built"

releases_url = hammock("https://api.github.com/repos/dortania/build-repo/releases", auth=("github-actions", token))

# Delete previous releases
for i in paginate("https://api.github.com/repos/dortania/build-repo/releases", token):
if i["name"] == (name + " " + release["commit"]["sha"][:7]):
print("\tDeleting previous release...")
releases_url(i["id"]).DELETE()
if os.environ.get("PROD", "false") == "true":
releases_url = hammock("https://api.github.com/repos/dortania/build-repo/releases", auth=("github-actions", token))

# Delete previous releases
for i in paginate("https://api.github.com/repos/dortania/build-repo/releases", token):
if i["name"] == (name + " " + release["commit"]["sha"][:7]):
print("\tDeleting previous release...")
releases_url(i["id"]).DELETE()
time.sleep(3) # Prevent race conditions

# Delete tags
check_tag = hammock("https://api.github.com/repos/dortania/build-repo/git/refs/tags/" + name + "-" + release["commit"]["sha"][:7], auth=("github-actions", token))
if check_tag.GET().status_code != 404:
print("\tDeleting previous tag...")
check_tag.DELETE()
time.sleep(3) # Prevent race conditions

# Delete tags
check_tag = hammock("https://api.github.com/repos/dortania/build-repo/git/refs/tags/" + name + "-" + release["commit"]["sha"][:7], auth=("github-actions", token))
if check_tag.GET().status_code != 404:
print("\tDeleting previous tag...")
check_tag.DELETE()
time.sleep(3) # Prevent race conditions

# Create release
create_release = releases_url.POST(json={
"tag_name": name + "-" + release["commit"]["sha"][:7],
"target_commitish": "builds",
"name": name + " " + release["commit"]["sha"][:7]
})
# print(create_release.json()["id"])
release["release"] = {"id": create_release.json()["id"], "url": create_release.json()["html_url"]}
# Create release
create_release = releases_url.POST(json={
"tag_name": name + "-" + release["commit"]["sha"][:7],
"target_commitish": "builds",
"name": name + " " + release["commit"]["sha"][:7]
})
# print(create_release.json()["id"])
release["release"] = {"id": create_release.json()["id"], "url": create_release.json()["html_url"]}

if not release.get("hashes", None):
release["hashes"] = {"debug": {"sha256": ""}, "release": {"sha256": ""}}
Expand All @@ -125,35 +126,36 @@ def add_built(plugin, token):
for file in files["extras"]:
release["hashes"][file.name] = {"sha256": hash_file(file)}

if not release.get("links", None):
release["links"] = {}

for i in ["debug", "release"]:
release["links"][i] = upload_release_asset(release["release"]["id"], token, files[i])

if files["extras"]:
if not release.get("extras", None):
release["extras"] = {}
for file in files["extras"]:
release["extras"][file.name] = upload_release_asset(release["release"]["id"], token, file)
new_line = "\n" # No escapes in f-strings

release["release"]["description"] = f"""**Changes:**
{release['commit']['message'].strip()}
[View on GitHub]({release['commit']['url']}) ([browse tree]({release['commit']['tree_url']}))
**Hashes**:
**Debug:**
{files["debug"].name + ': ' + release['hashes']['debug']["sha256"]}
**Release:**
{files["release"].name + ': ' + release['hashes']['release']["sha256"]}
{'**Extras:**' if files["extras"] else ''}
{new_line.join([(file.name + ': ' + release['hashes'][file.name]['sha256']) for file in files["extras"]]) if files["extras"] else ''}
""".strip()

hammock("https://api.github.com/repos/dortania/build-repo/releases/" + str(release["release"]["id"]), auth=("github-actions", token)).POST(json={
"body": release["release"]["description"]
})
if os.environ.get("PROD", "false") == "true":
if not release.get("links", None):
release["links"] = {}

for i in ["debug", "release"]:
release["links"][i] = upload_release_asset(release["release"]["id"], token, files[i])

if files["extras"]:
if not release.get("extras", None):
release["extras"] = {}
for file in files["extras"]:
release["extras"][file.name] = upload_release_asset(release["release"]["id"], token, file)
new_line = "\n" # No escapes in f-strings

release["release"]["description"] = f"""**Changes:**
{release['commit']['message'].strip()}
[View on GitHub]({release['commit']['url']}) ([browse tree]({release['commit']['tree_url']}))
**Hashes**:
**Debug:**
{files["debug"].name + ': ' + release['hashes']['debug']["sha256"]}
**Release:**
{files["release"].name + ': ' + release['hashes']['release']["sha256"]}
{'**Extras:**' if files["extras"] else ''}
{new_line.join([(file.name + ': ' + release['hashes'][file.name]['sha256']) for file in files["extras"]]) if files["extras"] else ''}
""".strip()

hammock("https://api.github.com/repos/dortania/build-repo/releases/" + str(release["release"]["id"]), auth=("github-actions", token)).POST(json={
"body": release["release"]["description"]
})

config[name]["versions"].insert(0, release)
config[name]["versions"].sort(key=lambda x: (x["date_committed"], x["date_authored"]), reverse=True)
Expand Down
15 changes: 8 additions & 7 deletions notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ def get_current_run_link(token):


def notify(token, results, status):
results = dict(results)
results["status"] = status
results["job_url"] = get_current_run_link(token)
if results.get("files"):
results["files"] = {k: str(v) for k, v in results["files"].items()}

requests.post(webhook, data=fern.encrypt(json.dumps(results).encode()))
if os.environ.get("PROD", "false") == "true":
results = dict(results)
results["status"] = status
results["job_url"] = get_current_run_link(token)
if results.get("files"):
results["files"] = {k: str(v) for k, v in results["files"].items()}

requests.post(webhook, data=fern.encrypt(json.dumps(results).encode()))


def notify_success(token, results):
Expand Down

0 comments on commit 86111f9

Please sign in to comment.