1+ import sys
2+ import json
3+ import os
4+ import requests
5+ from packaging import version
6+ import re
7+
8+ mendix_version = sys .argv [1 ]
9+ mendix_version_base = re .match (r'(\d+\.\d+\.\d+)' , mendix_version ).group (1 )
10+
11+ print (f"Mendix version: { mendix_version } " )
12+ print (f"Mendix base version: { mendix_version_base } " )
13+
14+ # Parse the JSON file
15+ try :
16+ with open ('mendix_version.json' , 'r' ) as file :
17+ version_data = json .load (file )
18+ except FileNotFoundError :
19+ print ("mendix_version.json not found" )
20+ sys .exit (1 )
21+
22+ # Find the best matching version range
23+ best_match = None
24+ highest_min_version = version .parse ("0.0.0" )
25+ mendix_version_obj = version .parse (mendix_version_base )
26+
27+ for range_str , data in version_data .items ():
28+ if range_str .startswith (">=" ):
29+ min_ver_str = range_str [2 :]
30+ min_ver = version .parse (min_ver_str )
31+
32+ if mendix_version_obj >= min_ver and min_ver > highest_min_version :
33+ best_match = range_str
34+ highest_min_version = min_ver
35+ print (f"Found matching range: { range_str } " )
36+
37+ if best_match :
38+ print (f"Best matching range: { best_match } " )
39+
40+ # Get the major version to look for in tags
41+ max_pattern = version_data [best_match ].get ("max" , "" )
42+ major_version = max_pattern .split ('.' )[0 ]
43+
44+ print (f"Looking for latest version with major version: { major_version } " )
45+
46+ # Get available tags from native-template repository
47+ response = requests .get ('https://api.github.com/repos/mendix/native-template/tags' )
48+ if response .status_code == 200 :
49+ all_tags = response .json ()
50+ tag_names = [tag ['name' ] for tag in all_tags ]
51+ print (f"Available tags: { tag_names } " )
52+
53+ # Find the latest version matching the major version
54+ matching_tags = []
55+
56+ for tag in tag_names :
57+ # Remove 'v' prefix if present for comparison
58+ clean_tag = tag [1 :] if tag .startswith ('v' ) else tag
59+
60+ # Check if the tag starts with the major version
61+ if clean_tag .startswith (f"{ major_version } ." ):
62+ try :
63+ # Try to parse as a version (this handles proper version numbers)
64+ tag_version = version .parse (clean_tag )
65+ matching_tags .append ((tag , tag_version ))
66+ except :
67+ # Skip tags that don't parse as proper versions
68+ pass
69+
70+ if matching_tags :
71+ # Sort by version (highest last) and get the last one
72+ matching_tags .sort (key = lambda x : x [1 ])
73+ latest_version = matching_tags [- 1 ][0 ]
74+ print (f"Selected Native Template version: { latest_version } " )
75+ with open (os .environ ['GITHUB_OUTPUT' ], 'a' ) as f :
76+ f .write (f"nt_branch={ latest_version } \n " )
77+ else :
78+ # Fallback to min version if no matching tag found
79+ min_version = version_data [best_match ].get ("min" , "" )
80+ print (f"No matching tag found, using minimum version: { min_version } " )
81+ with open (os .environ ['GITHUB_OUTPUT' ], 'a' ) as f :
82+ f .write (f"nt_branch={ min_version } \n " )
83+ else :
84+ print (f"Failed to get tags: { response .status_code } " )
85+ print ("Using master as fallback" )
86+ with open (os .environ ['GITHUB_OUTPUT' ], 'a' ) as f :
87+ f .write ("nt_branch=master\n " )
88+ else :
89+ print ("No matching version range found, using master" )
90+ with open (os .environ ['GITHUB_OUTPUT' ], 'a' ) as f :
91+ f .write ("nt_branch=master\n " )
0 commit comments