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 8 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
81 changes: 81 additions & 0 deletions .github/workflows/api_report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: API Diff Report

on: [pull_request]

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

env:
stageProgress: "progress"
stageEnd: "end"
prApiOutput: "~/pr_branch"
baseApiOutput: "~/base_branch"
diffReportOutput: "~/diff_report"
sunmou99 marked this conversation as resolved.
Show resolved Hide resolved
sunmou99 marked this conversation as resolved.
Show resolved Hide resolved

jobs:
diff_report:
if: github.event.pull_request.base.ref == 'master'
sunmou99 marked this conversation as resolved.
Show resolved Hide resolved
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

- 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.stageProgress }} \
--token ${{github.token}} \
--pr_number ${{github.event.pull_request.number}} \
--commit $GITHUB_SHA \
--run_id ${{github.run_id}}

- 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: Generate API files for PR branch
run: python ~/api_diff_report/api_info.py -f ${{ steps.get_changed_files.outputs.file_list }} -o ${{ env.prApiOutput }} -t "~/api_diff_report/theme"

- name: Checkout Base branch
uses: actions/checkout@v3
with:
ref: ${{ github.base_ref }}
sunmou99 marked this conversation as resolved.
Show resolved Hide resolved
sunmou99 marked this conversation as resolved.
Show resolved Hide resolved

- name: Generate API files for Base branch
run: python ~/api_diff_report/api_info.py -f ${{ steps.get_changed_files.outputs.file_list }} -o ${{ env.baseApiOutput }} -t "~/api_diff_report/theme"

- name: Generate API Diff Report
run: python ~/api_diff_report/api_diff_report.py -p ${{ env.prApiOutput }} -b ${{ env.baseApiOutput }} -o ${{ env.diffReportOutput }}

- name: Update Diff Report Comment in PR
run: |
python ~/api_diff_report/pr_commenter.py --stage ${{ env.stageEnd }} \
--report ${{ env.diffReportOutput }} \
--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
path: |
${{ env.prApiOutput }}
${{ env.baseApiOutput }}
${{ env.diffReportOutput }}
retention-days: 1
47 changes: 25 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,49 @@
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):
new_api_json = json.load(open(new_api_file))
sunmou99 marked this conversation as resolved.
Show resolved Hide resolved
else:
new_api_json = {}
if os.path.exists(old_api_file):
old_api_json = json.load(open(old_api_file))
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 +165,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 +241,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
214 changes: 214 additions & 0 deletions scripts/api_diff_report/pr_commenter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
# -*- coding: utf-8 -*-
sunmou99 marked this conversation as resolved.
Show resolved Hide resolved
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import json
import logging
import requests
import argparse
import api_diff_report
import datetime
import pytz
sunmou99 marked this conversation as resolved.
Show resolved Hide resolved

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

STAGES_PROGRESS = "progress"
STAGES_END = "end"

TITLE_PROGESS = "## ⏳  Detecting API diff in progress...\n"
sunmou99 marked this conversation as resolved.
Show resolved Hide resolved
TITLE_END_DIFF = '## Apple API Diff Report\n'
TITLE_END_NO_DIFF = "## ✅  No API diff detected\n"

COMMENT_HIDDEN_IDENTIFIER = '\r\n<hidden value="diff-report"></hidden>\r\n'
GITHUB_API_URL = 'https://api.github.com/repos/firebase/firebase-ios-sdk'
PR_LABLE = "public-api-change"
sunmou99 marked this conversation as resolved.
Show resolved Hide resolved


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

# Parse command-line arguments
args = parse_cmdline_args()

stage = args.stage
token = args.token
pr_number = args.pr_number
commit = args.commit
run_id = args.run_id

report = ""
if stage == STAGES_PROGRESS:
report = COMMENT_HIDDEN_IDENTIFIER
report += generate_markdown_title(TITLE_PROGESS, commit, run_id)
delete_label(token, pr_number, PR_LABLE)
elif stage == STAGES_END:
diff_report_file = os.path.join(os.path.expanduser(args.report),
api_diff_report.API_DIFF_FILE_NAME)
with open(diff_report_file, 'r') as file:
report_content = file.read()
if report_content:
report = COMMENT_HIDDEN_IDENTIFIER + generate_markdown_title(
TITLE_END_DIFF, commit, run_id) + report_content
add_label(token, pr_number, PR_LABLE)
else:
report = COMMENT_HIDDEN_IDENTIFIER + generate_markdown_title(
TITLE_END_NO_DIFF, commit, run_id)
delete_label(token, pr_number, PR_LABLE)

if report:
comment_id = get_comment_id(token, pr_number, COMMENT_HIDDEN_IDENTIFIER)
if not comment_id:
add_comment(token, pr_number, report)
else:
update_comment(token, comment_id, report)


def generate_markdown_title(title, commit, run_id):
pst_now = datetime.datetime.utcnow().astimezone(
pytz.timezone('America/Los_Angeles'))
return (
title + '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')


RETRIES = 3
BACKOFF = 5
RETRY_STATUS = (403, 500, 502, 504)
TIMEOUT = 5


def requests_retry_session(retries=RETRIES,
backoff_factor=BACKOFF,
status_forcelist=RETRY_STATUS):
session = requests.Session()
retry = Retry(total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session


def get_comment_id(token, issue_number, comment_identifier):
comments = list_comments(token, issue_number)
for comment in comments:
if comment_identifier in comment['body']:
return comment['id']
return None


def list_comments(token, issue_number):
"""https://docs.github.com/en/rest/reference/issues#list-issue-comments"""
url = f'{GITHUB_API_URL}/issues/{issue_number}/comments'
headers = {
'Accept': 'application/vnd.github.v3+json',
'Authorization': f'token {token}'
}
with requests_retry_session().get(url, headers=headers,
timeout=TIMEOUT) as response:
logging.info("list_comments: %s response: %s", url, response)
return response.json()


def add_comment(token, issue_number, comment):
"""https://docs.github.com/en/rest/reference/issues#create-an-issue-comment"""
url = f'{GITHUB_API_URL}/issues/{issue_number}/comments'
headers = {
'Accept': 'application/vnd.github.v3+json',
'Authorization': f'token {token}'
}
data = {'body': comment}
with requests.post(url,
headers=headers,
data=json.dumps(data),
timeout=TIMEOUT) as response:
logging.info("add_comment: %s response: %s", url, response)


def update_comment(token, comment_id, comment):
"""https://docs.github.com/en/rest/reference/issues#update-an-issue-comment"""
url = f'{GITHUB_API_URL}/issues/comments/{comment_id}'
headers = {
'Accept': 'application/vnd.github.v3+json',
'Authorization': f'token {token}'
}
data = {'body': comment}
with requests_retry_session().patch(url,
headers=headers,
data=json.dumps(data),
timeout=TIMEOUT) as response:
logging.info("update_comment: %s response: %s", url, response)


def delete_comment(token, comment_id):
"""https://docs.github.com/en/rest/reference/issues#delete-an-issue-comment"""
url = f'{GITHUB_API_URL}/issues/comments/{comment_id}'
headers = {
'Accept': 'application/vnd.github.v3+json',
'Authorization': f'token {token}'
}
with requests.delete(url, headers=headers, timeout=TIMEOUT) as response:
logging.info("delete_comment: %s response: %s", url, response)


def add_label(token, issue_number, label):
"""https://docs.github.com/en/rest/reference/issues#add-labels-to-an-issue"""
url = f'{GITHUB_API_URL}/issues/{issue_number}/labels'
headers = {}
sunmou99 marked this conversation as resolved.
Show resolved Hide resolved
headers = {
'Accept': 'application/vnd.github.v3+json',
'Authorization': f'token {token}'
}
data = [label]
with requests.post(url,
headers=headers,
data=json.dumps(data),
timeout=TIMEOUT) as response:
logging.info("add_label: %s response: %s", url, response)


def delete_label(token, issue_number, label):
"""https://docs.github.com/en/rest/reference/issues#delete-a-label"""
url = f'{GITHUB_API_URL}/issues/{issue_number}/labels/{label}'
headers = {
'Accept': 'application/vnd.github.v3+json',
'Authorization': f'token {token}'
}
with requests.delete(url, headers=headers, timeout=TIMEOUT) as response:
logging.info("delete_label: %s response: %s", url, response)


def parse_cmdline_args():
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--stage')
parser.add_argument('-r', '--report')
parser.add_argument('-t', '--token')
parser.add_argument('-n', '--pr_number')
parser.add_argument('-c', '--commit')
parser.add_argument('-i', '--run_id')

args = parser.parse_args()
return args


if __name__ == '__main__':
main()
Loading