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

Api Diff Report CI #11236

Merged
merged 27 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 26 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
90 changes: 90 additions & 0 deletions .github/workflows/api_diff_report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: API Diff Report

on: [pull_request]

concurrency:
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.head_ref || github.ref }}
cancel-in-progress: true

env:
STAGE_PROGRESS: progress
STAGE_END: end
PR_API_OUTPUT: ci_outputs/pr_branch_api
BASE_API_OUTPUT: ci_outputs/base_branch_api
DIFF_REPORT_OUTPUT: ci_outputs/diff_report

jobs:
diff_report:
runs-on: macos-latest

steps:
- name: Checkout PR branch
uses: actions/checkout@v3
with:
fetch-depth: 2

- name: Copy diff report tools
run: cp -a scripts/api_diff_report/. ~/api_diff_report
sunmou99 marked this conversation as resolved.
Show resolved Hide resolved

- id: get_changed_files
name: Get changed file list
run: |
echo "file_list=$(git diff --name-only -r HEAD^1 HEAD | tr '\n' ' ')" >> $GITHUB_OUTPUT

- name: Setup python
uses: actions/setup-python@v4
with:
python-version: 3.7

- name: Install Prerequisites
run: ~/api_diff_report/prerequisite.sh

- name: Clean Diff Report Comment in PR
run: |
python ~/api_diff_report/pr_commenter.py \
--stage ${{ env.STAGE_PROGRESS }} \
--token ${{github.token}} \
--pr_number ${{github.event.pull_request.number}} \
--commit $GITHUB_SHA \
--run_id ${{github.run_id}}

- name: Generate API files for PR branch
run: |
python ~/api_diff_report/api_info.py \
--file_list ${{ steps.get_changed_files.outputs.file_list }} \
--output_dir ${{ env.PR_API_OUTPUT }} \
--api_theme_dir ~/api_diff_report/theme
sunmou99 marked this conversation as resolved.
Show resolved Hide resolved

- name: Checkout Base branch
run: git checkout HEAD^

- name: Generate API files for Base branch
run: |
python ~/api_diff_report/api_info.py \
--file_list ${{ steps.get_changed_files.outputs.file_list }} \
--output_dir ${{ env.BASE_API_OUTPUT }} \
--api_theme_dir ~/api_diff_report/theme

- name: Generate API Diff Report
run: |
python ~/api_diff_report/api_diff_report.py \
--pr_branch ${{ env.PR_API_OUTPUT }} \
--base_branch ${{ env.BASE_API_OUTPUT }} \
--output_dir ${{ env.DIFF_REPORT_OUTPUT }}

- name: Update Diff Report Comment in PR
run: |
python ~/api_diff_report/pr_commenter.py \
--stage ${{ env.STAGE_END }} \
--report ${{ env.DIFF_REPORT_OUTPUT }} \
--token ${{github.token}} \
--pr_number ${{github.event.pull_request.number}} \
--commit $GITHUB_SHA \
--run_id ${{github.run_id}}

- uses: actions/upload-artifact@v3
if: ${{ !cancelled() }}
with:
name: api_info_and_report
path: ci_outputs
retention-days: 1
49 changes: 27 additions & 22 deletions scripts/api_diff_report/api_diff_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,51 @@
import logging
import os
import api_info
sunmou99 marked this conversation as resolved.
Show resolved Hide resolved
import datetime
import pytz

STATUS_ADD = 'ADDED'
STATUS_REMOVED = 'REMOVED'
STATUS_MODIFIED = 'MODIFIED'
STATUS_ERROR = 'BUILD ERROR'
API_DIFF_FILE_NAME = 'api_diff_report.markdown'


def main():
logging.getLogger().setLevel(logging.INFO)

args = parse_cmdline_args()

pr_branch = os.path.expanduser(args.pr_branch)
base_branch = os.path.expanduser(args.base_branch)
new_api_json = json.load(
open(os.path.join(pr_branch, api_info.API_INFO_FILE_NAME)))
old_api_json = json.load(
open(os.path.join(base_branch, api_info.API_INFO_FILE_NAME)))
new_api_file = os.path.join(os.path.expanduser(args.pr_branch),
api_info.API_INFO_FILE_NAME)
old_api_file = os.path.join(os.path.expanduser(args.base_branch),
api_info.API_INFO_FILE_NAME)
if os.path.exists(new_api_file):
with open(new_api_file) as f:
new_api_json = json.load(f)
else:
new_api_json = {}
if os.path.exists(old_api_file):
with open(old_api_file) as f:
old_api_json = json.load(f)
else:
old_api_json = {}

diff = generate_diff_json(new_api_json, old_api_json)
if diff:
logging.info(f'json diff: \n{json.dumps(diff, indent=2)}')
logging.info(f'plain text diff report: \n{generate_text_report(diff)}')
logging.info(f'markdown diff report: \n{generate_markdown_report(diff)}')
report = generate_markdown_report(diff)
logging.info(f'markdown diff report: \n{report}')
else:
logging.info('No API Diff Detected.')
report = ""

output_dir = os.path.expanduser(args.output_dir)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
api_report_path = os.path.join(output_dir, API_DIFF_FILE_NAME)
logging.info(f'Writing API diff report to {api_report_path}')
with open(api_report_path, 'w') as f:
f.write(report)


def generate_diff_json(new_api, old_api, level='module'):
Expand Down Expand Up @@ -150,17 +167,6 @@ def generate_text_report(diff, level=0, print_key=True):
return report


def generate_markdown_title(commit, run_id):
pst_now = datetime.datetime.utcnow().astimezone(
pytz.timezone('America/Los_Angeles'))
return (
'## Apple API Diff Report\n' + 'Commit: %s\n' % commit
+ 'Last updated: %s \n' % pst_now.strftime('%a %b %e %H:%M %Z %G')
+ '**[View workflow logs & download artifacts]'
+ '(https://github.com/firebase/firebase-ios-sdk/actions/runs/%s)**\n\n'
% run_id + '-----\n')


def generate_markdown_report(diff, level=0):
report = ''
header_str = '#' * (level + 3)
Expand Down Expand Up @@ -237,8 +243,7 @@ def parse_cmdline_args():
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--pr_branch')
parser.add_argument('-b', '--base_branch')
parser.add_argument('-c', '--commit')
parser.add_argument('-i', '--run_id')
parser.add_argument('-o', '--output_dir', default='output_dir')

args = parser.parse_args()
return args
Expand Down
4 changes: 2 additions & 2 deletions scripts/api_diff_report/api_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ def main():
changed_api_files = get_api_files(args.file_list)
if not changed_api_files:
logging.info('No Changed API File Detected')
exit(1)
exit(0)
changed_modules = icore_module.detect_changed_modules(changed_api_files)
if not changed_modules:
logging.info('No Changed Module Detected')
exit(1)
exit(0)

# Generate API documentation and parse API declarations
# for each changed module
Expand Down
Loading