Skip to content

Workflow file for this run

name: Detect Changed Files on PR Merge or Direct Push to Main
on:
pull_request:
types: [closed]
branches:
- gh-monitor
push:
branches:
- gh-monitor
jobs:
find-changed-files:
if: |
(github.event_name == 'pull_request' && github.event.pull_request.merged == true) ||
(github.event_name == 'push')
runs-on: ubuntu-latest
outputs:
changed_files: ${{ steps.list_changed_files.outputs.changed_files }}
authors: ${{ steps.get_authors.outputs.authors }}
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Determine if PR or Direct Push
id: check_event
run: |
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
echo "IS_PR=true" >> $GITHUB_ENV
else
echo "IS_PR=false" >> $GITHUB_ENV
fi
- name: Fetch changes
run: |
git fetch origin main
if [[ "${{ env.IS_PR }}" == "true" ]]; then
git fetch origin pull/${{ env.PR_NUMBER }}/head:pr-${{ env.PR_NUMBER }}
else
git fetch origin ${GITHUB_SHA}^:previous_commit
fi
- name: List changed files
id: list_changed_files
run: |
if [[ "${{ env.IS_PR }}" == "true" ]]; then
CHANGED_FILES=$(git diff --name-only main...pr-${{ env.PR_NUMBER }})
DIFF=$(git diff main...pr-${{ env.PR_NUMBER }})
else
CHANGED_FILES=$(git diff --name-only previous_commit...${GITHUB_SHA})
DIFF=$(git diff previous_commit...${GITHUB_SHA})
fi
echo "CHANGED_FILES=$CHANGED_FILES" >> $GITHUB_ENV
echo "::set-output name=changed_files::$CHANGED_FILES"
echo "$DIFF" > report.txt
- name: Get commit authors
id: get_authors
run: |
if [[ "${{ env.IS_PR }}" == "true" ]]; then
AUTHORS=$(git log --format='%ae' main...pr-${{ env.PR_NUMBER }} | sort | uniq)
else
AUTHORS=$(git log --format='%ae' previous_commit...${GITHUB_SHA} | sort | uniq)
fi
echo "AUTHORS=$AUTHORS" >> $GITHUB_ENV
echo "::set-output name=authors::$AUTHORS"
- name: Output changed files and authors
run: |
echo "Changed files: ${{ env.CHANGED_FILES }}"
echo "Authors: ${{ env.AUTHORS }}"
- name: Save changed files and authors to a file
run: |
echo "Changed files: ${{ env.CHANGED_FILES }}" > changed_files.txt
echo "Authors: ${{ env.AUTHORS }}" >> changed_files.txt
cat report.txt >> changed_files.txt
- name: Upload changed files artifact
uses: actions/upload-artifact@v2
with:
name: changed-files
path: changed_files.txt
- name: Upload diff report artifact
uses: actions/upload-artifact@v2
with:
name: diff-report
path: report.txt
notify-update:
runs-on: ubuntu-latest
needs: [find-changed-files]
if: success()
steps:
- name: Download diff report
uses: actions/download-artifact@v2
with:
name: diff-report
path: .
- name: Notify Discord with update
run: |
DIFF_CONTENT=$(cat report.txt)
curl -X POST -H "Content-Type: application/json" \
-d "{\"content\": \"<@762664509813817345> Update on GH. Authors: ${{ needs.find-changed-files.outputs.authors }}. Changed files: ${{ needs.find-changed-files.outputs.changed_files }}. Diff report: \n\`\`\`${DIFF_CONTENT}\`\`\`\"}" \
${{ secrets.GH_MONITOR_WEBHOOK }}