|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import re |
| 4 | +import requests |
| 5 | +import json |
| 6 | + |
| 7 | +def run_command(command, shell=False, capture_output=False): |
| 8 | + return subprocess.run(command, shell=shell, check=True, capture_output=capture_output, text=True).stdout.strip() |
| 9 | + |
| 10 | +def get_git_status(): |
| 11 | + return run_command("git status | grep 'nothing to commit, working tree clean' | wc -l", shell=True, capture_output=True) |
| 12 | + |
| 13 | +def get_commit_count(): |
| 14 | + return int(run_command("git log --oneline | wc -l", shell=True, capture_output=True)) |
| 15 | + |
| 16 | +def get_package_version(package_name, project_name): |
| 17 | + output = run_command(f"dotnet list {project_name} package | grep '{package_name} '", shell=True) |
| 18 | + match = re.search(r'(\d+\.\d+\.\d+)', output) |
| 19 | + return match.group(0) if match else None |
| 20 | + |
| 21 | +def bump_package_version(project_name, package_name, old_version, new_version, repository): |
| 22 | + issue_data = { |
| 23 | + "title": f"Bump {package_name} from {old_version} to {new_version}", |
| 24 | + "body": "TBD", |
| 25 | + "assignees": ["renemadsen"], |
| 26 | + "labels": [".NET", "backend", "enhancement"] |
| 27 | + } |
| 28 | + |
| 29 | + headers = { |
| 30 | + "Authorization": f"token {os.getenv('CHANGELOG_GITHUB_TOKEN')}", |
| 31 | + "Content-Type": "application/json" |
| 32 | + } |
| 33 | + |
| 34 | + response = requests.post( |
| 35 | + f"https://api.github.com/repos/microting/{repository}/issues", |
| 36 | + headers=headers, |
| 37 | + data=json.dumps(issue_data) |
| 38 | + ) |
| 39 | + |
| 40 | + issue_number = response.json().get("number") |
| 41 | + run_command(["git", "add", "."]) |
| 42 | + run_command(["git", "commit", "-a", "-m", f"closes #{issue_number}"]) |
| 43 | + |
| 44 | +def get_git_version(): |
| 45 | + return run_command("git tag --sort=-creatordate | head -n 1", shell=True).replace('v', '') |
| 46 | + |
| 47 | +def update_git_version(): |
| 48 | + current_version = get_git_version() |
| 49 | + major, minor, build = map(int, current_version.split('.')) |
| 50 | + new_version = f"v{major}.{minor}.{build + 1}" |
| 51 | + run_command(["git", "tag", new_version]) |
| 52 | + run_command(["git", "push", "--tags"]) |
| 53 | + run_command(["git", "push"]) |
| 54 | + return new_version |
| 55 | + |
| 56 | +def process_repository(project_name, packages, repository): |
| 57 | + for package_name in packages: |
| 58 | + old_version = get_package_version(package_name, project_name) |
| 59 | + run_command(["dotnet", "add", project_name, "package", package_name]) |
| 60 | + new_version = get_package_version(package_name, project_name) |
| 61 | + |
| 62 | + if new_version and new_version != old_version: |
| 63 | + bump_package_version(project_name, package_name, old_version, new_version, repository) |
| 64 | + |
| 65 | +def main(): |
| 66 | + os.chdir(os.path.expanduser("~")) |
| 67 | + |
| 68 | + if int(get_git_status()) > 0: |
| 69 | + run_command("git pull", shell=True) |
| 70 | + current_number_of_commits = get_commit_count() |
| 71 | + |
| 72 | + os.chdir(os.path.join("eFormAPI", "Plugins", "OuterInnerResource.Pn", "OuterInnerResource.Pn")) |
| 73 | + packages = [ |
| 74 | + 'Microting.eForm', 'Microting.eFormApi.BasePn', 'Microting.eFormOuterInnerResourceBase' |
| 75 | + ] |
| 76 | + process_repository('OuterInnerResource.Pn.csproj', packages, 'eform-angular-outer-inner-resource-plugin') |
| 77 | + |
| 78 | + os.chdir("..") |
| 79 | + os.chdir("OuterInnerResource.Pn.Test") |
| 80 | + packages = [ |
| 81 | + 'Microsoft.NET.Test.Sdk', 'NSubstitute', 'NUnit', 'NUnit3TestAdapter', |
| 82 | + 'NUnit.Analyzers', 'Testcontainers', 'Testcontainers.Mariadb' |
| 83 | + ] |
| 84 | + process_repository('OuterInnerResource.Pn.Test.csproj', packages, 'eform-angular-outer-inner-resource-plugin') |
| 85 | + |
| 86 | + new_number_of_commits = get_commit_count() |
| 87 | + if new_number_of_commits > current_number_of_commits: |
| 88 | + new_version = update_git_version() |
| 89 | + print(f"Updated Microting eForm and pushed new version {new_version}") |
| 90 | + os.chdir(os.path.join("..", "..", "..", "..")) |
| 91 | + run_command(f"github_changelog_generator -u microting -p eform-angular-outer-inner-resource-plugin -t {os.getenv('CHANGELOG_GITHUB_TOKEN')}", shell=True) |
| 92 | + run_command(["git", "add", "CHANGELOG.md"]) |
| 93 | + run_command(["git", "commit", "-m", "Updating changelog"]) |
| 94 | + run_command(["git", "push"]) |
| 95 | + else: |
| 96 | + print("Nothing to do, everything is up to date.") |
| 97 | + else: |
| 98 | + print("Working tree is not clean, so we are not going to upgrade. Clean before doing upgrade!") |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + main() |
0 commit comments