Skip to content

Commit

Permalink
feat(ci): create PR suggestion by comment
Browse files Browse the repository at this point in the history
  • Loading branch information
yin1999 committed Jul 23, 2023
1 parent 0933b15 commit e04cf84
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions .github/workflows/pr-format-suggestion.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: PR Format

on:
issue_comment:
types: [created]

jobs:
tests:
if: |
github.event.issue.pull_request && # only run on PRs
!github.event.issue.pull_request.merged_at && # ignore merged PRs
startsWith(github.event.comment.body, '@mdn-bot /format') # only run when asked
runs-on: ubuntu-latest
steps:
- name: Check push permission
run: |
# Check if the sender of the comment is collaborator or the author of the PR
gh api repos/{owner}/{repo}/collaborators/${{ github.event.sender.login }}
# check if the exit code is 0
if [ $? -ne 0 ]; then
# if not, check if the sender of the comment is the author of the PR
PR_AUTHER=$(gh api repos/${{ github.repository }}/pulls/${{ github.event.issue.number }} \
--jq '.user.login')
if [ "${PR_AUTHER}" != "${{ github.event.sender.login }}" ]; then
# if not, send a comment and exit
gh pr comment ${{ github.event.issue.number }} \
--body "Sorry, only collaborators or the author of the PR can run this command."
--repo ${{ github.repository }}
exit 1
fi
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Get PR information
run: |
# Get the base and head SHA
SHA=$(gh api repos/${{ github.repository }}/pulls/${{ github.event.issue.number }} \
--jq '.base.sha, .head.sha')
# Set the environment variables
echo "BASE_SHA=$(echo $SHA | cut -d ' ' -f 1)" >> $GITHUB_ENV
echo "HEAD_SHA=$(echo $SHA | cut -d ' ' -f 2)" >> $GITHUB_ENV
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- uses: actions/checkout@v3
with:
ref: ${{ env.BASE_SHA }}

- name: Setup Node.js environment
uses: actions/setup-node@v3
with:
node-version-file: ".nvmrc"
cache: "yarn"
cache-dependency-path: yarn.lock

- name: Install all yarn packages
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
DIFF_DOCUMENTS=$(gh api repos/{owner}/{repo}/compare/${{ env.BASE_SHA }}...${{ env.HEAD_SHA }} \
--jq '.files | .[] | select(.status|IN("added", "modified", "renamed", "copied", "changed")) | .filename')
# filter out files that are not interesting to us
GIT_DIFF_CONTENT=$(echo "${DIFF_DOCUMENTS}" | egrep -i ".*\.(md|yml)$" | xargs)
echo "GIT_DIFF_CONTENT=${GIT_DIFF_CONTENT}" >> $GITHUB_ENV
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Format changed content
if: ${{ env.GIT_DIFF_CONTENT }}
run: |
yarn prettier --write ${{ env.GIT_DIFF_CONTENT }}
- name: Submit suggestion
uses: reviewdog/action-suggester@v1
with:
tool_name: prettier

0 comments on commit e04cf84

Please sign in to comment.