Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{CI} Add codegen survey bot #7721

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions .github/workflows/ProcessCodeReview.yml
Original file line number Diff line number Diff line change
@@ -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
96 changes: 96 additions & 0 deletions scripts/ci/codegen_cal.py
Original file line number Diff line number Diff line change
@@ -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)
Loading