Gautam Peri 449a4813a1
All checks were successful
Notify Remote Repositores About a Release / create-new-issue-in-remote-repo (release) Successful in 22s
Update .allspice/utils/entrypoint.py
2025-05-16 15:27:45 +00:00

77 lines
3.7 KiB
Python

import os
from allspice import AllSpice, Repository
from argparse import ArgumentParser
from pip._vendor.pkg_resources import issue_warning
###############################################################################
CREATE_ISSUE_ENDPOINT = """/repos/{owner}/{repo}/issues"""
###############################################################################
def create_issue(client, release_type, notifying_repo_owner, notifying_repo_name, notified_repo_owner, notified_repo_name, auth_token, tag="", title="", url="", body=""):
print("- Creating notification issue in " + repo_owner + "/" + repo_name)
reponse = client.requests_post(
CREATE_ISSUE_ENDPOINT.format(
owner=notified_repo_owner,
repo=notified_repo_name,
token=auth_token
),
data={
"Title": "[Release Notification] " + tag + " - by " + notifying_repo_owner + "/" + notifying_repo_name,
"Body": url + "\n\n" + body
},
)
###############################################################################
def notify_repos_about_release(client, auth_token, repo_urls, notifying_repo_owner, notifying_repo_name, release_type, tag, name, url, body):
for repo in repo_urls:
split_url = repo.removesuffix('.git').split('/')
repo_name = split_url[-1]
repo_owner = split_url[0].split(':')[1] if split_url[1] else split_url[-2]
create_issue(client, release_type, notifying_repo_owner, notifying_repo_name, repo_owner, repo_name, auth_token, tag=tag, title=name, url=url, body=body)
return
###############################################################################
if __name__ == "__main__":
# Initialize argument parser
parser = ArgumentParser()
parser.add_argument("repository", help="Repository object for the target repo")
parser.add_argument("release_type", help="Release type, i.e. published, edited, or deleted")
parser.add_argument("release_tag", help="Release tag for the release that triggered the action")
parser.add_argument("release_name", help="Release name for the release that triggered the action")
parser.add_argument("release_url", help="Release url for the release that triggered the action")
parser.add_argument("release_body", help="Release body text for the release that triggered the action")
parser.add_argument("notification_repo_file", help="File with list of AllSpice repo URLs to notify")
parser.add_argument(
"--allspice_hub_url",
help="The URL of your AllSpice Hub instance. Defaults to https://hub.allspice.io.",
)
args = parser.parse_args()
# Get auth token and hub url
auth_token = os.environ.get("ALLSPICE_AUTH_TOKEN")
if auth_token is None:
print("Please set the environment variable ALLSPICE_AUTH_TOKEN")
exit(1)
if args.allspice_hub_url is None:
allspice = AllSpice(token_text=auth_token)
else:
allspice = AllSpice(
token_text=auth_token,
allspice_hub_url=args.allspice_hub_url,
)
# Get the repository
repo_owner, repo_name = args.repository.split("/")
repository = allspice.get_repository(repo_owner, repo_name)
# Read file to ingest repos to notify
try:
with open(args.notification_repo_file, "r") as file:
notify_repo_urls = [line.rstrip() for line in file]
except FileNotFoundError:
print(f"Error: Notification repos file not found: {args.notification_repo_file}")
exit(1)
# Notify the repositories about the release
notify_repos_about_release(allspice, auth_token, notify_repo_urls, repo_owner, repo_name, args.release_type, args.release_tag, args.release_name, args.release_url, args.release_body)