Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USHIFT-2474: remove gh dependency in release notes script #3096

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
89 changes: 64 additions & 25 deletions scripts/release-notes/gen_ec_release_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

*Draft Release*

After the tag is present, running the script again causes it to use gh
to produce a draft release with a preamble that includes download URLs
and a body that is auto-generated by GitHub's service based on the
pull requests that have merged since the last tagged release.
After the tag is present, running the script again causes it produce a
draft release with a preamble that includes download URLs and a body
that is auto-generated by GitHub's service based on the pull requests
that have merged since the last tagged release.

*Publishing Release*

Expand All @@ -34,8 +34,8 @@

NOTE:

To use this script, you must have the GitHub command line tool "gh"
installed and you must have enough privileges on the
To use this script, you must have a GitHub token configured in the
environment variable GITHUB_TOKEN with enough privileges on the
openshift/microshift repository to create releases.

"""
Expand All @@ -44,6 +44,7 @@
import collections
import datetime
import html.parser
import json
import os
import re
import subprocess
Expand Down Expand Up @@ -73,10 +74,13 @@
re.VERBOSE,
)

# Save our token for use later
GITHUB_TOKEN = os.environ.get('GITHUB_TOKEN')

# Include the major.minor version string in this list to ignore
# processing very old versions for which we do not anticipate future
# candidate builds.
OLD_VERSIONS = ['4.12', '4.13', '4.14']
OLD_VERSIONS = ['4.12', '4.13', '4.14', '4.15']


# Representation of one release
Expand All @@ -91,6 +95,7 @@ def main():
The main function of the script. It runs the `check_one()` function for both 'ocp-dev-preview'
and 'ocp' release types and for a specified version depending upon provided arguments.
"""

parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
Expand Down Expand Up @@ -129,6 +134,9 @@ def main():
)
args = parser.parse_args()

if not GITHUB_TOKEN:
raise RuntimeError('GITHUB_TOKEN does not appear to be set')

new_releases = []
if args.ec:
new_releases.extend(find_new_releases(URL_BASE, 'ocp-dev-preview'))
Expand Down Expand Up @@ -290,17 +298,10 @@ def check_for_new_releases(url_base, release_type, version):

# Check if the release already exists
print(f"Checking for release {release_name}...")
try:
subprocess.run(["gh", "release", "view", release_name],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except subprocess.CalledProcessError:
print("Not found")
else:
if github_release_exists(release_name):
print("Found an existing release, no work to do")
return None
print("Not found")

return Release(
release_name,
Expand Down Expand Up @@ -378,15 +379,53 @@ def publish_release(new_release, take_action):

# Create draft release with message that includes download URLs and history
try:
subprocess.run(["gh", "release", "create",
"--prerelease",
"--notes", notes,
"--generate-notes",
release_name,
],
check=True)
except subprocess.CalledProcessError as err:
print(f"Failed to create the release: {err}")
github_release_create(release_name, notes)
except Exception as err:
print(f"Failed to create the release {release_name}: {err}")
raise


def github_release_create(tag, notes_header):
results = github(
'/repos/openshift/microshift/releases',
tag_name=tag,
name=tag,
body=notes_header,
draft=False,
prerelease=True,
generate_release_notes=True,
)
print(f'Created new release {tag}')
print()
print(results['html_url'])
print()
print(results['body'])


def github_release_exists(tag):
try:
github(f'/repos/openshift/microshift/releases/tags/{tag}')
return True
except Exception:
return False


def github(path, **data):
url = f'https://api.github.com/{path.lstrip("/")}'
if data:
r = request.Request(
url=url,
data=json.dumps(data).encode('utf-8'),
)
else:
r = request.Request(url=url)
print(r.get_method(), url, data)
r.add_header('Accept', 'application/vnd.github+json')
r.add_header('User-agent', 'microshift-release-notes')
r.add_header('Authorization', f'Bearer {GITHUB_TOKEN}')
r.add_header('X-GitHub-Api-Version', '2022-11-28')
response = request.urlopen(r)
return json.loads(response.read().decode('utf-8'))


if __name__ == "__main__":
Expand Down