-
Notifications
You must be signed in to change notification settings - Fork 225
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
Autobuild: Cleanup analyze_git_references.py #2471
Merged
hoffie
merged 4 commits into
jamulussoftware:master
from
hoffie:autobuild-analyze_git_refs-cleanup
Mar 9, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
37d46d8
Autobuild: Refactor and clean up analyze_git_references.py
hoffie aab078f
Autobuild: Simplify variable outputs in analyze_git_references.py
hoffie 128f313
Autobuild: Handle non-version tag pushes consistently
hoffie 24db7f5
Autobuild: Error out if release tag pushes don't match Jamulus.pro VE…
hoffie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,89 @@ | ||
#!/usr/bin/python3 | ||
# This script is trigged from the Github Autobuild workflow. | ||
# It analyzes Jamulus.pro and git push details (tag vs. branch, etc.) to decide | ||
# - whether a release should be created, | ||
# - whether it is a pre-release, and | ||
# - what its title should be. | ||
|
||
# | ||
# on a triggered github workflow, this file does the decisions and variagles like | ||
# - shall the build be released (otherwise just run builds to check if there are errors) | ||
# - is it a prerelease | ||
# - title, tag etc of release_tag | ||
# | ||
# see the last lines of the file to see what variables are set | ||
# | ||
|
||
|
||
import sys | ||
import os | ||
import re | ||
import subprocess | ||
|
||
# get the jamulus version from the .pro file | ||
def get_jamulus_version(repo_path_on_disk): | ||
jamulus_version = "" | ||
with open (repo_path_on_disk + '/Jamulus.pro','r') as f: | ||
REPO_PATH = os.path.join(os.path.dirname(__file__), '..', '..') | ||
|
||
|
||
def get_version_from_jamulus_pro(): | ||
with open(REPO_PATH + '/Jamulus.pro', 'r') as f: | ||
pro_content = f.read() | ||
pro_content = pro_content.replace('\r','') | ||
pro_lines = pro_content.split('\n') | ||
for line in pro_lines: | ||
line = line.strip() | ||
VERSION_LINE_STARTSWITH = 'VERSION = ' | ||
if line.startswith(VERSION_LINE_STARTSWITH): | ||
jamulus_version = line[len(VERSION_LINE_STARTSWITH):] | ||
return jamulus_version | ||
return "UNKNOWN_VERSION" | ||
m = re.search(r'^VERSION\s*=\s*(\S+)$', pro_content, re.MULTILINE) | ||
if not m: | ||
raise Exception("Unable to determine Jamulus.pro VERSION") | ||
return m.group(1) | ||
|
||
|
||
def get_git_hash(): | ||
return subprocess.check_output(['git', 'describe', '--match=xxxxxxxxxxxxxxxxxxxx', '--always', '--abbrev', '--dirty']).decode('ascii').strip() | ||
#return subprocess.check_output(['git', 'rev-parse', 'HEAD']) | ||
#return subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']) | ||
|
||
if len(sys.argv) == 1: | ||
pass | ||
else: | ||
print('wrong number of arguments') | ||
print('Number of arguments:', len(sys.argv), 'arguments.') | ||
print('Argument List:', str(sys.argv)) | ||
raise Exception("wrong agruments") | ||
|
||
# derive workspace-path | ||
repo_path_on_disk = os.environ['GITHUB_WORKSPACE'] | ||
|
||
# derive git related variables | ||
version_from_changelog = get_jamulus_version(repo_path_on_disk) | ||
if "dev" in version_from_changelog: | ||
release_version_name = "{}-{}".format(version_from_changelog, get_git_hash()) | ||
print("building an intermediate version: ", release_version_name) | ||
else: | ||
release_version_name = version_from_changelog | ||
print("building a release version: ", release_version_name) | ||
|
||
|
||
fullref=os.environ['GITHUB_REF'] | ||
reflist = fullref.split("/", 2) | ||
pushed_name = reflist[2] | ||
|
||
|
||
# run Changelog-script | ||
os.system('perl "{}"/.github/actions_scripts/getChangelog.pl "{}"/ChangeLog "{}" > "{}"/autoLatestChangelog.md'.format( | ||
os.environ['GITHUB_WORKSPACE'], | ||
os.environ['GITHUB_WORKSPACE'], | ||
version_from_changelog, | ||
os.environ['GITHUB_WORKSPACE'] | ||
)) | ||
|
||
# decisions about release, prerelease, title and tag | ||
if fullref.startswith("refs/tags/"): | ||
print('this reference is a Tag') | ||
release_tag = pushed_name # tag already exists | ||
release_title="Release {} ({})".format(release_version_name, pushed_name) | ||
|
||
if pushed_name.startswith("r"): | ||
if re.match(r'^r\d+_\d+_\d+$', pushed_name): | ||
print('this reference is a Release-Tag') | ||
publish_to_release = True | ||
is_prerelease = False | ||
else: | ||
print('this reference is a Non-Release-Tag') | ||
publish_to_release = True | ||
is_prerelease = True | ||
else: | ||
print('this reference is a Non-Release-Tag') | ||
publish_to_release = False | ||
is_prerelease = True # just in case | ||
elif fullref.startswith("refs/heads/"): | ||
print('this reference is a Head/Branch') | ||
publish_to_release = False | ||
is_prerelease = True | ||
release_title='Pre-Release of "{}"'.format(pushed_name) | ||
release_tag = "releasetag/"+pushed_name #better not use pure pushed name, creates a tag with the name of the branch, leads to ambiguous references => can not push to this branch easily | ||
else: | ||
print('unknown git-reference type: ' + fullref) | ||
publish_to_release = False | ||
is_prerelease = True | ||
release_title='Pre-Release of "{}"'.format(pushed_name) | ||
release_tag = "releasetag/"+pushed_name #avoid ambiguity in references in all cases | ||
|
||
#helper function: set github variable and print it to console | ||
return subprocess.check_output([ | ||
'git', | ||
'describe', | ||
'--match=xxxxxxxxxxxxxxxxxxxx', | ||
'--always', | ||
'--abbrev', | ||
'--dirty' | ||
]).decode('ascii').strip() | ||
|
||
|
||
def write_changelog(version): | ||
changelog = subprocess.check_output([ | ||
'perl', | ||
f'{REPO_PATH}/.github/actions_scripts/getChangelog.pl', | ||
f'{REPO_PATH}/ChangeLog', | ||
version, | ||
]) | ||
with open(f'{REPO_PATH}/autoLatestChangelog.md', 'wb') as f: | ||
f.write(changelog) | ||
|
||
|
||
def get_release_version_name(jamulus_pro_version): | ||
if "dev" in jamulus_pro_version: | ||
name = "{}-{}".format(jamulus_pro_version, get_git_hash()) | ||
print("building an intermediate version: ", name) | ||
return name | ||
|
||
name = jamulus_pro_version | ||
print("building a release version: ", name) | ||
return name | ||
|
||
|
||
def set_github_variable(varname, varval): | ||
print("{}='{}'".format(varname, varval)) #console output | ||
print("{}='{}'".format(varname, varval)) # console output | ||
print("::set-output name={}::{}".format(varname, varval)) | ||
|
||
#set github-available variables | ||
set_github_variable("PUBLISH_TO_RELEASE", str(publish_to_release).lower()) | ||
set_github_variable("IS_PRERELEASE", str(is_prerelease).lower()) | ||
set_github_variable("RELEASE_TITLE", release_title) | ||
set_github_variable("RELEASE_TAG", release_tag) | ||
set_github_variable("PUSHED_NAME", pushed_name) | ||
set_github_variable("JAMULUS_VERSION", release_version_name) | ||
|
||
jamulus_pro_version = get_version_from_jamulus_pro() | ||
write_changelog(jamulus_pro_version) | ||
release_version_name = get_release_version_name(jamulus_pro_version) | ||
|
||
fullref = os.environ['GITHUB_REF'] | ||
publish_to_release = bool(re.match(r'^refs/tags/r\d+_\d+_\d+\S*$', fullref)) | ||
|
||
# RELEASE_VERSION_NAME is required for all builds including branch pushes | ||
# and PRs: | ||
set_github_variable("RELEASE_VERSION_NAME", release_version_name) | ||
set_github_variable("X_GITHUB_WORKSPACE", os.environ['GITHUB_WORKSPACE']) | ||
|
||
# PUBLISH_TO_RELEASE is always required as the workflow decides about further | ||
# steps based on this. It will only be true for tag pushes with a tag | ||
# starting with "r". | ||
set_github_variable("PUBLISH_TO_RELEASE", str(publish_to_release).lower()) | ||
|
||
if publish_to_release: | ||
reflist = fullref.split("/", 2) | ||
release_tag = reflist[2] | ||
release_title = f"Release {release_version_name} ({release_tag})" | ||
is_prerelease = not re.match(r'^r\d+_\d+_\d+$', release_tag) | ||
if not is_prerelease and release_version_name != release_tag[1:].replace('_', '.'): | ||
raise Exception(f"non-pre-release tag {release_tag} doesn't match Jamulus.pro VERSION = {release_version_name}") | ||
|
||
# Those variables are only used when a release is created at all: | ||
set_github_variable("IS_PRERELEASE", str(is_prerelease).lower()) | ||
set_github_variable("RELEASE_TITLE", release_title) | ||
set_github_variable("RELEASE_TAG", release_tag) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we need to remember using _ instead of . everywhere on git.
(I'm not a fan of two different versions of describing the same version, but as far as I remember it is needed somehow).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
Agreed. I'd be more happy if we were following the more common vA.B.C scheme. If we decide to go that route we should do that properly and adjust all places though. :)
It's just that I think it will cause some breakage (e.g. people checking out git tags have to adjust, scripts may have to be updated, etc.).
If you want to make it happen I suggest opening an issue... :)