Skip to content

Commit

Permalink
Merge 494fe50 into e73e86a
Browse files Browse the repository at this point in the history
  • Loading branch information
sunmou99 authored May 10, 2023
2 parents e73e86a + 494fe50 commit 565614a
Show file tree
Hide file tree
Showing 13 changed files with 354 additions and 291 deletions.
88 changes: 88 additions & 0 deletions .github/workflows/api_diff_report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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

- 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 }}
- 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 }}
- 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
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
18 changes: 6 additions & 12 deletions scripts/api_diff_report/api_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,25 @@ def main():
# Parse command-line arguments
args = parse_cmdline_args()
output_dir = os.path.expanduser(args.output_dir)
api_theme_dir = os.path.expanduser(args.api_theme_dir)
if not os.path.exists(output_dir):
os.makedirs(output_dir)

# Detect changed modules based on changed files
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
api_container = {}
for _, module in changed_modules.items():
api_doc_dir = os.path.join(output_dir, 'doc', module['name'])
build_api_doc(module, api_doc_dir, api_theme_dir)
build_api_doc(module, api_doc_dir)

if os.path.exists(api_doc_dir):
module_api_container = parse_module(api_doc_dir)
Expand All @@ -74,7 +73,7 @@ def get_api_files(file_list):
]


def build_api_doc(module, output_dir, api_theme_dir):
def build_api_doc(module, output_dir):
"""Use Jazzy to build API documentation for a specific module's source
code."""
if module['language'] == icore_module.SWIFT:
Expand All @@ -84,8 +83,7 @@ def build_api_doc(module, output_dir, api_theme_dir):
+ ' --build-tool-arguments'\
+ f' -scheme,{module["scheme"]}'\
+ ',-destination,generic/platform=iOS,build'\
+ f' --output {output_dir}'\
+ f' --theme {api_theme_dir}'
+ f' --output {output_dir}'
logging.info(cmd)
result = subprocess.Popen(cmd,
universal_newlines=True,
Expand All @@ -97,8 +95,7 @@ def build_api_doc(module, output_dir, api_theme_dir):
cmd = 'jazzy --objc'\
+ f' --framework-root {module["root_dir"]}'\
+ f' --umbrella-header {module["umbrella_header"]}'\
+ f' --output {output_dir}'\
+ f' --theme {api_theme_dir}'
+ f' --output {output_dir}'
logging.info(cmd)
result = subprocess.Popen(cmd,
universal_newlines=True,
Expand Down Expand Up @@ -215,9 +212,6 @@ def parse_cmdline_args():
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file_list', nargs='+', default=[])
parser.add_argument('-o', '--output_dir', default='output_dir')
parser.add_argument('-t',
'--api_theme_dir',
default='scripts/api_diff_report/theme')

args = parser.parse_args()
return args
Expand Down
Loading

0 comments on commit 565614a

Please sign in to comment.