Skip to content

Commit 0f5e9b6

Browse files
authored
{CI} Adjust version calculation to use whl for base metadata (#8610)
* adjust version cal use whl for base metadata generation * reset scripts * remove azdev mod source before loading module from whl
1 parent 6cee104 commit 0f5e9b6

File tree

2 files changed

+136
-27
lines changed

2 files changed

+136
-27
lines changed

.github/workflows/VersionCalPRComment.yml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,28 +140,26 @@ jobs:
140140
chmod +x env/bin/activate
141141
source ./env/bin/activate
142142
set -ev
143-
git checkout "$base_branch_pre"/"$base_branch"
144143
mkdir "$base_meta_path"
145-
for mod in ${changed_module_list[@]}
146-
do
147-
echo changed module: "${mod}"
148-
azdev extension add "${mod}" && azdev command-change meta-export "${mod}" --meta-output-path ./"$base_meta_path"/ && azdev extension remove "${mod}"
149-
done
144+
mkdir "$diff_meta_path"
145+
150146
# some module, like confcom, will change local data files, like data/rules.rego, during installation, need to reset when checkout different branch
151147
git checkout -f "$diff_branch"
152-
mkdir "$diff_meta_path"
148+
# always use the latest index.json and scripts from base branch
149+
git checkout "$base_branch_pre"/"$base_branch" -- scripts
150+
git checkout "$base_branch_pre"/"$base_branch" -- src/index.json
151+
153152
for mod in ${changed_module_list[@]}
154153
do
155154
echo changed module: "${mod}"
156155
azdev extension add "${mod}" && azdev command-change meta-export "${mod}" --meta-output-path ./"$diff_meta_path"/
157156
done
158-
ls ./"$base_meta_path"/
159157
ls ./"$diff_meta_path"/
158+
ls ./"$base_meta_path"/
160159
ls "$result_path"
161-
git checkout "$base_branch_pre"/"$base_branch" -- scripts
162-
# use the latest index.json from base branch
163-
git checkout "$base_branch_pre"/"$base_branch" -- src/index.json
164160
python scripts/ci/release_version_cal.py
161+
ls ./"$diff_meta_path"/
162+
ls ./"$base_meta_path"/
165163
ls "$result_path"
166164
cat "$result_path"/"$remove_labels_file"
167165
cat "$result_path"/"$output_file"

scripts/ci/release_version_cal.py

Lines changed: 127 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,15 @@ def extract_module_version_update_info(mod_update_info, mod):
8686
diff_file_started = True
8787

8888

89-
def extract_module_metadata_update_info(mod_update_info, mod):
89+
def extract_module_metadata_update_info_pre(mod_update_info, mod):
9090
"""
9191
re pattern:
9292
--- a/src/monitor-control-service/azext_amcs/azext_metadata.json
9393
+++ b/src/monitor-control-service/azext_amcs/azext_metadata.json
9494
- "azext.isPreview": true
9595
+ "azext.isPreview": true
9696
--- a/src/monitor-control-service/HISTORY.RST
97+
not robust, use previous version's metatata and current metadata to compare
9798
"""
9899
mod_update_info["meta_updated"] = False
99100
module_meta_update_pattern = re.compile(r"\+\+\+.*?src/%s/azext_.*?/azext_metadata.json" % mod)
@@ -123,21 +124,142 @@ def extract_module_metadata_update_info(mod_update_info, mod):
123124
if module_meta_update_match:
124125
mod_update_info["meta_updated"] = True
125126

127+
def check_extension_list():
128+
cmd = ["az", "extension", "list", "-o", "table"]
129+
print("cmd: ", cmd)
130+
res = subprocess.run(cmd, stdout=subprocess.PIPE)
131+
print(res.stdout.decode("utf8"))
126132

127-
def find_module_metadata_of_latest_version(mod):
133+
def clean_mod_of_azdev(mod):
134+
"""
135+
be sure to get all the required info before removing mod in azdev
136+
"""
137+
print("removing azdev extensions: ", mod)
138+
cmd = ["azdev", "extension", "remove", mod]
139+
result = subprocess.run(cmd, stdout=subprocess.PIPE)
140+
if result.returncode:
141+
raise Exception(f'Error when removing {mod} from azdev')
142+
# remove extension source code
143+
cmd = ["rm", "-rf", "./src/" + mod]
144+
result = subprocess.run(cmd, stdout=subprocess.PIPE)
145+
if result.returncode:
146+
raise Exception(f'Error when removing {mod} from src')
147+
148+
149+
def install_mod_of_last_version(pkg_name, pre_release):
150+
whl_file_url = pre_release['downloadUrl']
151+
cmd = ["az", "extension", "add", "-s", whl_file_url, "-y"]
152+
print("cmd: ", cmd)
153+
result = subprocess.run(cmd, stdout=subprocess.PIPE)
154+
if result.returncode:
155+
raise Exception(f'Error when adding {pkg_name} from source {whl_file_url}')
156+
check_extension_list()
157+
158+
159+
def remove_mod_of_last_version(pkg_name):
160+
cmd = ['az', 'extension', 'remove', '-n', pkg_name]
161+
result = subprocess.run(cmd, stdout=subprocess.PIPE)
162+
if result.returncode:
163+
raise Exception(f'Error when removing {pkg_name}')
164+
check_extension_list()
165+
166+
def gen_metadata_from_whl(pkg_name, target_folder):
167+
cmd = ['azdev', 'command-change', 'meta-export', pkg_name, '--include-whl-extensions', '--meta-output-path', target_folder, "--debug"]
168+
print("cmd: ", cmd)
169+
result = subprocess.run(cmd, stdout=subprocess.PIPE)
170+
if result.returncode:
171+
raise Exception(f'Error when generating metadata from whl for {pkg_name}')
172+
173+
174+
def get_mod_package_name(mod):
175+
"""
176+
the preliminary step for running following cmd is installing extension using azdev
177+
"""
128178
cmd = ["azdev", "extension", "show", "--mod-name", mod, "--query", "pkg_name", "-o", "tsv"]
129179
result = subprocess.run(cmd, stdout=subprocess.PIPE)
130180
if result.returncode == 0:
131181
mod = result.stdout.decode("utf8").strip()
132-
return get_module_metadata_of_max_version(mod)
182+
return mod
183+
184+
185+
def get_module_metadata_of_max_version(mod):
186+
if mod not in get_index_data()['extensions']:
187+
print("No previous release for {0}".format(mod))
188+
return None
189+
pre_releases = get_index_data()['extensions'][mod]
190+
candidates_sorted = sorted(pre_releases, key=lambda c: parse(c['metadata']['version']), reverse=True)
191+
chosen = candidates_sorted[0]
192+
return chosen
193+
194+
195+
def find_module_metadata_of_latest_version(mod):
196+
pkg_name = get_mod_package_name(mod)
197+
return get_module_metadata_of_max_version(pkg_name)
198+
199+
200+
def find_module_metadata_of_current_version(mod):
201+
mod_directory = cli_ext_path + "/src/" + mod
202+
for root, dirs, files in os.walk(mod_directory):
203+
if 'azext_metadata.json' in files:
204+
file_path = os.path.join(root, 'azext_metadata.json')
205+
try:
206+
with open(file_path, 'r', encoding='utf-8') as f:
207+
data = json.load(f)
208+
print(f"Found azext_metadata.json at: {file_path}")
209+
return data
210+
except Exception as e:
211+
print(f"Error reading {file_path}: {e}")
212+
return None
213+
print(f"Mod: {mod} does not have azext_metadata.json, please check")
214+
return None
215+
216+
def extract_module_metadata_update_info(mod_update_info, mod):
217+
"""
218+
use previous version's metatata and current metadata to compare
219+
relevent mod files should be in diff branch
220+
"""
221+
mod_update_info["meta_updated"] = False
222+
# metadata is required for this task, (and also az extension *)
223+
last_meta_data = find_module_metadata_of_latest_version(mod).get("metadata", {})
224+
current_meta_data = find_module_metadata_of_current_version(mod)
225+
if not current_meta_data:
226+
raise Exception(f"Please check {mod}: azext_metadata.json file")
227+
if not last_meta_data:
228+
if current_meta_data.get("azext.isPreview", None):
229+
mod_update_info["meta_updated"] = True
230+
mod_update_info["preview_tag_diff"] = "add"
231+
return
232+
if last_meta_data.get("azext.isExperimental", False) and not current_meta_data.get("azext.isExperimental", False):
233+
mod_update_info["exp_tag_diff"] = "remove"
234+
mod_update_info["meta_updated"] = True
235+
if not last_meta_data.get("azext.isExperimental", False) and current_meta_data.get("azext.isExperimental", False):
236+
mod_update_info["exp_tag_diff"] = "add"
237+
mod_update_info["meta_updated"] = True
238+
if last_meta_data.get("azext.isPreview", False) and not current_meta_data.get("azext.isPreview", False):
239+
mod_update_info["preview_tag_diff"] = "remove"
240+
mod_update_info["meta_updated"] = True
241+
if not last_meta_data.get("azext.isPreview", False) and current_meta_data.get("azext.isPreview", False):
242+
mod_update_info["preview_tag_diff"] = "add"
243+
mod_update_info["meta_updated"] = True
133244

134245

135246
def extract_module_version_info(mod_update_info, mod):
136247
next_version_pre_tag = get_next_version_pre_tag()
137248
next_version_segment_tag = get_next_version_segment_tag()
138249
print("next_version_pre_tag: ", next_version_pre_tag)
139250
print("next_version_segment_tag: ", next_version_segment_tag)
140-
base_meta_file = os.path.join(cli_ext_path, base_meta_path, "az_" + mod + "_meta.json")
251+
pkg_name = get_mod_package_name(mod)
252+
pre_release = get_module_metadata_of_max_version(pkg_name)
253+
print(f"Get prerelease info for mod: {mod} as below:")
254+
print(json.dumps(pre_release))
255+
clean_mod_of_azdev(mod)
256+
print("Start generating base metadata")
257+
install_mod_of_last_version(pkg_name, pre_release)
258+
base_meta_folder = os.path.join(cli_ext_path, base_meta_path)
259+
gen_metadata_from_whl(pkg_name, base_meta_folder)
260+
remove_mod_of_last_version(pkg_name)
261+
print("End generating base metadata")
262+
base_meta_file = os.path.join(cli_ext_path, base_meta_path, "az_" + pkg_name + "_meta.json")
141263
diff_meta_file = os.path.join(cli_ext_path, diff_meta_path, "az_" + mod + "_meta.json")
142264
if not os.path.exists(base_meta_file) and not os.path.exists(diff_meta_file):
143265
print("no base and diff meta file found for {0}".format(mod))
@@ -149,7 +271,6 @@ def extract_module_version_info(mod_update_info, mod):
149271
elif not os.path.exists(diff_meta_file):
150272
print("no diff meta file found for {0}".format(mod))
151273
return
152-
pre_release = find_module_metadata_of_latest_version(mod)
153274
if pre_release is None:
154275
next_version = cal_next_version(base_meta_file=base_meta_file, diff_meta_file=diff_meta_file,
155276
current_version=DEFAULT_VERSION,
@@ -177,16 +298,6 @@ def fill_module_update_info(mods_update_info):
177298
print(mods_update_info)
178299

179300

180-
def get_module_metadata_of_max_version(mod):
181-
if mod not in get_index_data()['extensions']:
182-
print("No previous release for {0}".format(mod))
183-
return None
184-
pre_releases = get_index_data()['extensions'][mod]
185-
candidates_sorted = sorted(pre_releases, key=lambda c: parse(c['metadata']['version']), reverse=True)
186-
chosen = candidates_sorted[0]
187-
return chosen
188-
189-
190301
def get_next_version_pre_tag():
191302
if VERSION_STABLE_TAG in pr_label_list:
192303
return VERSION_STABLE_TAG
@@ -375,4 +486,4 @@ def main():
375486

376487

377488
if __name__ == '__main__':
378-
main()
489+
main()

0 commit comments

Comments
 (0)