Skip to content

Commit

Permalink
use github api to get diff file and changed files
Browse files Browse the repository at this point in the history
  • Loading branch information
yin1999 committed Nov 30, 2022
1 parent 0cbb259 commit c9dfc12
Showing 1 changed file with 27 additions and 31 deletions.
58 changes: 27 additions & 31 deletions .github/workflows/pr-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,52 @@ on:
jobs:
tests:
runs-on: ubuntu-latest
env:
CHANGED_FILES: /tmp/changed-files.txt
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}

steps:
- name: Diffset
id: diffset
uses: softprops/diffset@v1
with:
base: ${{ github.base_ref }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Check diffset
run: |
# document changes
GIT_DIFF_CONTENT=$(echo "${{ steps.diffset.outputs.diffset }}" | tr -s " " "\n" | egrep -i "^files/.*/.*\.(html|md)$" | tr "\n" " ")
echo "GIT_DIFF_CONTENT=$GIT_DIFF" >> $GITHUB_ENV
echo "changed documents: $GIT_DIFF_CONTENT"
# image changes
GIT_DIFF_IMAGE=$(echo "${{ steps.diffset.outputs.diffset }}" | tr -s " " "\n" | egrep -i "^files/.*/.*\.(png|jpeg|jpg|gif|svg|webp)$" | tr "\n" " ")
echo "GIT_DIFF_IMAGE=$GIT_DIFF_IMAGE" >> $GITHUB_ENV
echo "changed images: $GIT_DIFF_IMAGE"
# set global flag if there are any changes
if [ ! -z "$GIT_DIFF_CONTENT" ] || [ ! -z "$GIT_DIFF_IMAGE" ]; then
echo "FILE_CHANGE=1" >> $GITHUB_ENV
fi
- uses: actions/checkout@v3
if: env.FILE_CHANGE
# Needed otherswise it will check out a merge commit which means
# you can't get a `git diff` for this PR compared to its base.
# with:
# ref: ${{ github.event.pull_request.head.sha }}

- uses: actions/checkout@v3
if: env.FILE_CHANGE
with:
repository: mdn/content
path: mdn/content

- name: Setup Node.js environment
if: env.FILE_CHANGE
uses: actions/setup-node@v3
with:
node-version: "16"
cache: "yarn"
cache-dependency-path: mdn/content/yarn.lock

- name: Install all yarn packages
if: env.FILE_CHANGE
working-directory: ${{ github.workspace }}/mdn/content
run: |
yarn --frozen-lockfile
- name: Get Changed files
run: |
# Use the GitHub API to get the list of changed files
# documenation: https://docs.github.com/rest/commits/commits#compare-two-commits
gh api repos/{owner}/{repo}/compare/${{ env.BASE_SHA }}...${{ env.HEAD_SHA }} \
--paginate \
--jq '.files | .[] | select(.status|IN("added", "modified", "renamed", "copied", "changed")) | .filename' > ${{ env.CHANGED_FILES }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Get changed documents
run: |
DIFF_DOCUMENTS=$(cat ${{ env.CHANGED_FILES }} | egrep -i "^files/.*/.*\.(html|md)$" | tr "\n" " ")
echo "GIT_DIFF_CONTENT=$DIFF_DOCUMENTS" >> $GITHUB_ENV
- name: Build changed content
if: env.GIT_DIFF_CONTENT
if: ${{ env.GIT_DIFF_CONTENT }}
env:
CONTENT_ROOT: ${{ github.workspace }}/mdn/content/files
CONTENT_TRANSLATED_ROOT: ${{ github.workspace }}/files
Expand Down Expand Up @@ -128,7 +124,7 @@ jobs:
# directory.
# The purpose of this is for the PR Review Companion to later
# be able to use this raw diff file for the benefit of analyzing.
gh pr diff ${{ github.event.number }} > $BUILD_OUT_ROOT/DIFF
wget https://github.com/${{ github.action_repository }}/compare/${{ env.BASE_SHA }}...${{ env.HEAD_SHA }}.diff -O $BUILD_OUT_ROOT/DIFF
- name: Merge static assets with built documents
if: ${{ env.GIT_DIFF_CONTENT }}
Expand Down Expand Up @@ -159,9 +155,9 @@ jobs:

- name: Get changed images
if: ${{ env.GIT_DIFF_CONTENT }}
# note that we should get the absolute path of the changed images
run: |
DIFF_IMAGES=$(echo ${{ env.GIT_DIFF_IMAGES }} | | tr -s " " "\n" | xargs readlink -e | tr "\n" " ")
# note that we should get the absolute path of the changed images
DIFF_IMAGES=$(cat ${{ env.CHANGED_FILES }} | egrep -i "^files/.*/.*\.(png|jpeg|jpg|gif|svg|webp)$" | xargs readlink -e | tr "\n" " ")
echo "GIT_DIFF_IMAGES=$DIFF_IMAGES" >> $GITHUB_ENV
- name: Check changed files
Expand Down

0 comments on commit c9dfc12

Please sign in to comment.