forked from Azure/azure-cli-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
{CI} Add module release version calulation workflow (Azure#7208)
* add version cal workflow Co-authored-by: ZelinWang <zelinwang@microsoft.com>
- Loading branch information
Showing
2 changed files
with
267 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,134 @@ | ||
name: Generate Release Version and Comment PR | ||
|
||
on: | ||
workflow_dispatch: | ||
pull_request_target: | ||
types: [opened, labeled, unlabeled, synchronize] | ||
paths: | ||
- '**.py' | ||
|
||
permissions: | ||
pull-requests: write | ||
|
||
jobs: | ||
version-cal: | ||
if: startsWith(github.head_ref, 'auto-version') | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Set Init Version Message | ||
run: | | ||
echo "message=$(echo 'Suggested init version: 1.0.0b1 for preview release and 1.0.0 for stable release')" >> $GITHUB_ENV | ||
- name: Set up Python 3.10 | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: "3.10" | ||
- name: Checkout CLI extension repo | ||
uses: actions/checkout@master | ||
with: | ||
fetch-depth: 0 # checkout all branches | ||
- name: Show workdirectory after site cloned | ||
run: | | ||
pwd | ||
ls | ||
- name: Get Diff Files | ||
run: | | ||
echo github.event.pull_request.base.sha: ${{github.event.pull_request.base.sha}} | ||
echo github.event.pull_request.base.ref: ${{ github.event.pull_request.base.ref }} | ||
echo github.sha: ${{github.sha}} | ||
echo GITHUB_BASE_REF: ${GITHUB_BASE_REF} | ||
echo GITHUB_HEAD_REF: ${GITHUB_HEAD_REF} | ||
echo GITHUB_SHA: ${GITHUB_SHA} | ||
echo GITHUB_REF: ${GITHUB_REF} | ||
echo "base_branch=${GITHUB_BASE_REF}" >> $GITHUB_ENV | ||
echo "diff_branch=${GITHUB_HEAD_REF}" >> $GITHUB_ENV | ||
echo "base_commit=${GITHUB_SHA}" >> $GITHUB_ENV | ||
echo "diff_commit=${GITHUB_SHA}" >> $GITHUB_ENV | ||
git --version | ||
git log --oneline | head -n 30 | ||
echo git branch -a | ||
git branch -a | ||
git diff --name-only --diff-filter=ACMRT ${{ github.event.pull_request.base.sha }} ${{ github.sha }} > changed_files | ||
cat changed_files | ||
cat changed_files | grep azext_ | awk -F"azext_" '{print $1}'| awk -F"/" '{print $2}' | sort | uniq > changed_modules | ||
echo "changed_module_list=$(cat changed_files | grep azext_ | awk -F"azext_" '{print $1}'| awk -F"/" '{print $2}' | sort | uniq | xargs)" >> $GITHUB_ENV | ||
- name: Display Diff Modules | ||
run: | | ||
for mod in `cat changed_modules` | ||
do | ||
echo changed module: ${mod} | ||
done | ||
- name: Checkout CLI main repo | ||
uses: actions/checkout@master | ||
with: | ||
repository: Azure/azure-cli | ||
path: ./azure-cli | ||
- name: Show workdirectory after cli cloned | ||
run: | | ||
pwd | ||
ls | ||
- name: Move the main repo to the same level as the extension repo | ||
run: | | ||
mv azure-cli ../ | ||
cd ../ | ||
pwd | ||
ls | ||
- name: Install azdev | ||
run: | | ||
python -m pip install --upgrade pip | ||
set -ev | ||
python -m venv env | ||
chmod +x env/bin/activate | ||
source ./env/bin/activate | ||
pip install azdev | ||
azdev --version | ||
cd ../ | ||
azdev setup -c azure-cli -r azure-cli-extensions --debug | ||
az --version | ||
pip list -v | ||
- name: Gen Base and Diff Metadata | ||
id: get_comment_message | ||
env: | ||
pr_label_list: ${{ toJson(github.event.pull_request.labels.*.name) }} | ||
base_meta_path: "./base_meta/" | ||
diff_meta_path: "./diff_meta/" | ||
output_file: "version_update.txt" | ||
run: | | ||
chmod +x env/bin/activate | ||
source ./env/bin/activate | ||
echo GITHUB_BASE_REF: ${GITHUB_BASE_REF} | ||
git checkout ${GITHUB_BASE_REF} | ||
for mod in `cat changed_modules` | ||
do | ||
echo ************************** | ||
echo changed module: ${mod} | ||
azdev extension add ${mod} | ||
azdev command-change meta-export ${mod} --meta-output-path ./${base_meta_path}/ | ||
azdev extension remove ${mod} | ||
echo ************************** | ||
echo -e "\n\n\n\n\n" | ||
done | ||
echo GITHUB_HEAD_REF: ${GITHUB_HEAD_REF} | ||
git checkout ${GITHUB_HEAD_REF} | ||
for mod in `cat changed_modules` | ||
do | ||
echo ************************** | ||
echo changed module: ${mod} | ||
azdev extension add ${mod} | ||
azdev command-change meta-export ${mod} --meta-output-path ./${diff_meta_path}/ | ||
azdev extension remove ${mod} | ||
echo ************************** | ||
echo -e "\n\n\n\n\n" | ||
done | ||
echo ls base_meta_path: | ||
ls ./${base_meta_path}/ | ||
echo ls diff_meta_path | ||
ls ./${diff_meta_path}/ | ||
python scripts/ci/release_version_cal.py | ||
# echo "commit_message=$(cat ${output_file})" >> $GITHUB_ENV | ||
- name: Comment on the pull request | ||
uses: mshick/add-pr-comment@v2 | ||
with: | ||
message-path: | | ||
version_update.txt | ||
message-failure: | | ||
Please refer to [Extension version schema](https://github.com/Azure/azure-cli/blob/release/doc/extensions/versioning_guidelines.md) to update release versions. |
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 |
---|---|---|
@@ -0,0 +1,133 @@ | ||
#!/usr/bin/env python | ||
|
||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
|
||
import os | ||
from packaging.version import parse | ||
|
||
from azdev.utilities.path import get_cli_repo_path, get_ext_repo_paths | ||
from azdev.operations.extensions import cal_next_version | ||
from azdev.operations.constant import (PREVIEW_INIT_SUFFIX, VERSION_MAJOR_TAG, VERSION_MINOR_TAG, | ||
VERSION_PATCH_TAG, VERSION_STABLE_TAG, VERSION_PREVIEW_TAG, VERSION_PRE_TAG) | ||
from util import get_index_data | ||
|
||
|
||
base_branch = os.environ.get('base_branch', None) | ||
diff_branch = os.environ.get('diff_branch', None) | ||
base_meta_path = os.environ.get('base_meta_path', None) | ||
diff_meta_path = os.environ.get('diff_meta_path', None) | ||
output_file = os.environ.get('output_file', None) | ||
changed_module_list = os.environ.get('changed_module_list', "").split() | ||
pr_label_list = os.environ.get('pr_label_list', "").split() | ||
pr_label_list = [name.lower() for name in pr_label_list] | ||
|
||
DEFAULT_VERSION = "0.0.0" | ||
|
||
|
||
def get_module_metadata_of_max_version(mod): | ||
if mod not in get_index_data()['extensions']: | ||
print("No previous release for {0}".format(mod)) | ||
return None | ||
pre_releases = get_index_data()['extensions'][mod] | ||
candidates_sorted = sorted(pre_releases, key=lambda c: parse(c['metadata']['version']), reverse=True) | ||
chosen = candidates_sorted[0] | ||
return chosen | ||
|
||
|
||
def get_next_version_pre_tag(): | ||
if VERSION_STABLE_TAG in pr_label_list: | ||
return VERSION_STABLE_TAG | ||
elif VERSION_PREVIEW_TAG in pr_label_list: | ||
return VERSION_PREVIEW_TAG | ||
else: | ||
return None | ||
|
||
|
||
def get_next_version_segment_tag(): | ||
""" | ||
manual label order: | ||
major > minor > patch > pre | ||
""" | ||
if VERSION_MAJOR_TAG in pr_label_list: | ||
return VERSION_MAJOR_TAG | ||
elif VERSION_MINOR_TAG in pr_label_list: | ||
return pr_label_list | ||
elif VERSION_PATCH_TAG in pr_label_list: | ||
return VERSION_PATCH_TAG | ||
elif VERSION_PRE_TAG in pr_label_list: | ||
return VERSION_PRE_TAG | ||
else: | ||
return None | ||
|
||
|
||
def gen_comment_message(mod, next_version, comment_message): | ||
comment_message.append("### module: {0}".format(mod)) | ||
comment_message.append(" - suggested next version number in setup.py: {0}".format(next_version.get("version", "-"))) | ||
if next_version.get("has_preview_tag", False): | ||
comment_message.append(' - azext_{0}/azext_metadata.json: "azext.isPreview": true,'.format(mod)) | ||
|
||
|
||
def add_label_hint_message(comment_message): | ||
comment_message.append("#### Notes") | ||
comment_message.append(" - Stable/preview tag is inherited from last release. " | ||
"If needed, please add `stable`/`preview` label to modify it.") | ||
comment_message.append(" - Major/minor/patch/pre increment of version number is calculated by pull request " | ||
"code changes automatically. " | ||
"If needed, please add `major`/`minor`/`patch`/`pre` label to adjust it.") | ||
|
||
|
||
def main(): | ||
print("Start calculate release version ...\n") | ||
cli_ext_path = get_ext_repo_paths()[0] | ||
print("get_cli_repo_path: ", get_cli_repo_path()) | ||
print("get_ext_repo_paths: ", cli_ext_path) | ||
print("base_branch: ", base_branch) | ||
print("diff_branch: ", diff_branch) | ||
print("base_meta_path: ", base_meta_path) | ||
print("diff_meta_path: ", diff_meta_path) | ||
print("output_file: ", output_file) | ||
print("changed_module_list: ", changed_module_list) | ||
print("pr_label_list: ", pr_label_list) | ||
next_version_pre_tag = get_next_version_pre_tag() | ||
next_version_segment_tag = get_next_version_segment_tag() | ||
print("next_version_pre_tag: ", next_version_pre_tag) | ||
print("next_version_segment_tag: ", next_version_segment_tag) | ||
comment_message = [] | ||
for mod in changed_module_list: | ||
base_meta_file = os.path.join(cli_ext_path, base_meta_path, "az_" + mod + "_meta.json") | ||
diff_meta_file = os.path.join(cli_ext_path, diff_meta_path, "az_" + mod + "_meta.json") | ||
if not os.path.exists(base_meta_file): | ||
print("no base meta file found for {0}".format(mod)) | ||
continue | ||
if not os.path.exists(diff_meta_file): | ||
print("no diff meta file found for {0}".format(mod)) | ||
continue | ||
|
||
pre_release = get_module_metadata_of_max_version(mod) | ||
if pre_release is None: | ||
next_version = cal_next_version(base_meta_file=base_meta_file, diff_meta_file=diff_meta_file, | ||
current_version=DEFAULT_VERSION, | ||
next_version_pre_tag=next_version_pre_tag, | ||
next_version_segment_tag=next_version_segment_tag | ||
) | ||
else: | ||
next_version = cal_next_version(base_meta_file=base_meta_file, diff_meta_file=diff_meta_file, | ||
current_version=pre_release['metadata']['version'], | ||
is_preview=pre_release['metadata'].get("azext.isPreview", None), | ||
is_experimental=pre_release['metadata'].get("azext.isExperimental", None), | ||
next_version_pre_tag=next_version_pre_tag, | ||
next_version_segment_tag=next_version_segment_tag | ||
) | ||
gen_comment_message(mod, next_version, comment_message) | ||
add_label_hint_message(comment_message) | ||
print(comment_message) | ||
with open(os.path.join(cli_ext_path, output_file), "w") as f: | ||
for line in comment_message: | ||
f.write(line + "\n") | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |