Last active
July 25, 2019 14:02
-
-
Save willianantunes/ea99e9542a86ba7c6553dbbe9e2bec70 to your computer and use it in GitHub Desktop.
Python Script following SemVer (Semantic Versioning) integrated with GitHub to use in Azure DevOps
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
import json | |
import sys | |
import urllib.request | |
def set_value(key, value): | |
print(f"Set key {key} as {value}") | |
print(f"##vso[task.setvariable variable={key};]{value}") | |
def pull_request_setup(list_of_prs, commit_sha): | |
print(f"Length of PRs: {len(list_of_prs)}") | |
print(f"Commit SHA to search for: {commit_sha}") | |
if len(list_of_prs) > 0: | |
for pr in list_of_prs: | |
if pr.get("merge_commit_sha") and pr["merge_commit_sha"] == commit_sha: | |
pr_details = {"compare": pr["head"]["ref"], "base": pr["base"]["ref"]} | |
print(f"Built PR details: {pr_details}") | |
return pr_details | |
def is_for_major_version(value: str) -> bool: | |
return "major" in value.lower() | |
def is_for_hotfix_version(value: str) -> bool: | |
return "hotfix" in value.lower() | |
def do_request_and_convert_to_json(address, headers): | |
body = urllib.request.urlopen(urllib.request.Request(address, headers=headers)).read() | |
return json.loads(body) | |
env_tag_version = "ENV_TAG_VERSION" | |
owner, repository = sys.argv[1].split("/") | |
provided_commit_sha = sys.argv[2] | |
api_token = sys.argv[3] | |
base_headers = {"Authorization": f"token {api_token}"} | |
base_address = f"https://api.github.com/repos/{owner}/{repository}" | |
address_tags = f"{base_address}/tags" | |
address_pulls = f"{base_address}/pulls?state=closed" | |
tag_result = do_request_and_convert_to_json(address_tags, base_headers) | |
# More or less based on https://semver.org/, which is MAJOR.MINOR.PATCH | |
if len(tag_result) == 0: | |
print("First release of the repository") | |
set_value(env_tag_version, "0.1.0") | |
else: | |
latest_tag_version = tag_result[0]["name"] | |
latest_tag_commit_sha = tag_result[0]["commit"]["sha"] | |
if latest_tag_commit_sha == provided_commit_sha: | |
print(f"Provided commit sha is the same as the tag, no need to generate tag/release") | |
set_value(env_tag_version, latest_tag_version) | |
else: | |
print(f"Latest tag version and commit sha: {latest_tag_version} / {latest_tag_commit_sha}") | |
major, minor, patch = [int(x) for x in latest_tag_version.split(".")] | |
pr_result = do_request_and_convert_to_json(address_pulls, base_headers) | |
evaluated_pr = pull_request_setup(pr_result, provided_commit_sha) | |
if evaluated_pr: | |
is_major = is_for_major_version(evaluated_pr["compare"]) | |
if is_major: | |
print("Major release!") | |
major += 1 | |
minor, patch = 0, 0 | |
set_value(env_tag_version, f"{major}.{minor}.{patch}") | |
else: | |
is_hotfix = is_for_hotfix_version(evaluated_pr["compare"]) | |
if is_hotfix: | |
print("Hotfix!") | |
patch += 1 | |
set_value(env_tag_version, f"{major}.{minor}.{patch}") | |
else: | |
print("Simple release with minor update!") | |
minor += 1 | |
set_value(env_tag_version, f"{major}.{minor}.{patch}") | |
else: | |
print("There is no PR. Thus only minor update...") | |
minor += 1 | |
set_value(env_tag_version, f"{major}.{minor}.{patch}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment