From 4ea0392c0de5a126de0c9e310d805d4c03728230 Mon Sep 17 00:00:00 2001 From: AllyW Date: Tue, 18 Jun 2024 15:47:19 +0800 Subject: [PATCH] {CI} Add codegen survey bot (#7721) * add codegen survey * add yml * remove unused --- .github/workflows/ProcessCodeReview.yml | 115 ++++++++++++++++++++++++ scripts/ci/codegen_cal.py | 96 ++++++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100644 .github/workflows/ProcessCodeReview.yml create mode 100644 scripts/ci/codegen_cal.py diff --git a/.github/workflows/ProcessCodeReview.yml b/.github/workflows/ProcessCodeReview.yml new file mode 100644 index 00000000000..3bea6b66128 --- /dev/null +++ b/.github/workflows/ProcessCodeReview.yml @@ -0,0 +1,115 @@ +name: Auto Review PR + +on: + pull_request_target: + types: [opened, synchronize] + branches: + - main + +permissions: {} + +jobs: + pr-code-review: + if: ${{ !contains(github.event.pull_request.labels.*.name, 'skip-code-review') }} + runs-on: ubuntu-latest + permissions: + pull-requests: read + contents: read + steps: + - name: Check Init Event + env: + action: ${{ toJSON(github.event.action) }} + label: ${{ toJSON(github.event.label) }} + run: | + echo start review module changed + - name: Set up Python 3.11 + uses: actions/setup-python@v3 + with: + python-version: "3.11" + - name: Checkout CLI extension repo + uses: actions/checkout@master + with: + fetch-depth: 0 # checkout all branches + ref: ${{ github.event.pull_request.head.ref }} + repository: ${{ github.event.pull_request.head.repo.full_name }} # checkout pull request branch + - name: Show workdirectory after site cloned + run: | + pwd + ls + - name: Get Diff Modules + env: + bash_sha: ${{ github.event.pull_request.base.sha }} + base_branch: ${{ github.event.pull_request.base.ref }} + base_branch_pre: "upstream" + diff_sha: ${{ github.event.pull_request.head.sha }} + diff_branch: ${{ github.event.pull_request.head.ref }} + repo_full_name: ${{ github.event.pull_request.head.repo.full_name }} + run: | + set -x + git --version + git log --oneline | head -n 30 + git branch -a + git fetch https://github.com/Azure/azure-cli-extensions.git "$base_branch":"$base_branch_pre"/"$base_branch" + git checkout "$base_branch_pre"/"$base_branch" + git log --oneline | head -n 30 + git checkout "$diff_branch" + git log --oneline | head -n 30 + git --no-pager diff --name-only --diff-filter=ACMRT "$base_branch_pre"/"$base_branch"..."$diff_branch" > changed_files + cat changed_files + 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 "$changed_module_list" + do + echo changed module: "${mod}" + done + - name: Check whether aaz-related Module + id: check_if_aaz_used + env: + pr_label_list: ${{ toJson(github.event.pull_request.labels.*.name) }} + result_path: "./review_result" + output_file: "is_aaz_check.txt" + survey_comment_file: "aaz_survey_comment.txt" + base_branch: ${{ github.event.pull_request.base.ref }} + base_branch_pre: "upstream" + run: | + set -ev + mkdir ${result_path} + git checkout "$base_branch_pre"/"$base_branch" -- scripts/ci/codegen_cal.py + python scripts/ci/codegen_cal.py --job check + ls ${result_path} + - name: Archive production artifacts + uses: actions/upload-artifact@v4 + with: + name: review-code-output + path: | + review_result + retention-days: 1 + action-on-output: + needs: pr-code-review + runs-on: ubuntu-latest + permissions: + pull-requests: write + contents: read + steps: + - name: Download code review result + uses: actions/download-artifact@v4 + with: + name: review-code-output + - name: Show workdirectory after result downloaded + run: | + pwd + ls + - name: Check comment file existence + id: check_survey_comment_file + uses: andstor/file-existence-action@v3 + with: + files: "./aaz_survey_comment.txt" + - name: Comment survey on the pull request + if: steps.check_survey_comment_file.outputs.files_exists == 'true' + uses: mshick/add-pr-comment@v2 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + message-id: aazSurveyCommentBot + message-path: | + aaz_survey_comment.txt \ No newline at end of file diff --git a/scripts/ci/codegen_cal.py b/scripts/ci/codegen_cal.py new file mode 100644 index 00000000000..92129befa6e --- /dev/null +++ b/scripts/ci/codegen_cal.py @@ -0,0 +1,96 @@ +#!/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. +# -------------------------------------------------------------------------------------------- +# pylint: disable=line-too-long +import os +import re +import argparse +from util import get_repo_root + +output_file = os.environ.get('output_file', None) +result_path = os.environ.get('result_path', None) + +cli_ext_path = get_repo_root() +cli_ext_src_path = os.path.join(cli_ext_path, "src") +print("cli_ext_path: ", cli_ext_path) +print("cli_ext_src_path: ", cli_ext_src_path) + +DEFAULT_SURVEY_MESSAGE = "Thank you for using our CodeGen tool. We value your feedback, and we would like to know how we can improve our product. Please take a few minutes to fill our [codegen survey](https://forms.office.com/r/j6rQuFUqUf?origin=lprLink) " + +def check_is_module_aaz_related(mod): + codegen_aaz_folder_pattern = re.compile(r"src/%s/azext_.*?/aaz/" % mod) + module_path = os.path.join(cli_ext_src_path, mod) + print("module_path: ", module_path) + for root, subdir, files in os.walk(module_path): + codegen_aaz_match = re.findall(codegen_aaz_folder_pattern, root) + if codegen_aaz_match: + print(codegen_aaz_match) + return True + return False + +def save_comment_pr_survey(comment_pr_survey): + print("check comment_pr_survey: ", comment_pr_survey) + with open(os.path.join(cli_ext_path, result_path, output_file), "w") as f: + f.write(str(comment_pr_survey) + "\n") + +def check_aaz_module(): + comment_pr_survey = 0 + changed_module_list = os.environ.get('changed_module_list', "").split() + for mod in changed_module_list: + if check_is_module_aaz_related(mod): + comment_pr_survey = 1 + break + save_comment_pr_survey(comment_pr_survey) + if comment_pr_survey == 1: + comment_message = [] + add_survey_hint_message(comment_message) + save_comment_message(comment_message) + +def add_survey_hint_message(comment_message): + comment_message.append("## CodeGen Tools Feedback Collection") + comment_message.append(DEFAULT_SURVEY_MESSAGE) + +def save_comment_message(comment_message): + print("comment_message:") + print(comment_message) + survey_comment_file = os.environ.get('survey_comment_file', "") + with open(os.path.join(cli_ext_path, result_path, survey_comment_file), "w") as f: + for line in comment_message: + f.write(line + "\n") + +def save_gh_output(): + with open(os.environ['GITHUB_OUTPUT'], 'a') as fh: + print(f'CommentAAZSurvey={comment_pr_survey}', file=fh) + +def set_aaz_comment(): + if not os.path.exists(os.path.join(cli_ext_path, result_path, output_file)): + print("error in file dowdload") + return + comment_pr_survey = 0 + with open(os.path.join(cli_ext_path, result_path, output_file), "r") as f: + for line in f: + comment_pr_survey = int(line.strip()) + print("comment_pr_survey: ", comment_pr_survey) + save_gh_output() + if comment_pr_survey: + comment_message = [] + add_survey_hint_message(comment_message) + save_comment_message(comment_message) + +def main(job): + if job == "check": + check_aaz_module() + elif job == "set": + set_aaz_comment() + else: + print("unsupported job type") + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument("--job", choices=["check", "set"], required=True, help="job type") + args = parser.parse_args() + print(vars(args)) + main(args.job) \ No newline at end of file