pixell45 #1721
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Validate/Merge | |
on: | |
pull_request_target: | |
types: | |
- opened | |
- edited | |
- synchronize | |
permissions: | |
pull-requests: write | |
contents: write | |
jobs: | |
audit: | |
runs-on: ubuntu-latest | |
outputs: | |
PR_AUTHOR: ${{ steps.pr_user.outputs.PR_AUTHOR }} | |
steps: | |
- name: Check if PR is from a fork | |
id: pr_source | |
run: | | |
if [[ "${{ github.event.pull_request.head.repo.full_name }}" != "${{ github.repository }}" ]]; then | |
echo "PR is from a fork." | |
echo "::set-output name=is_fork::true" | |
else | |
echo "PR is not from a fork." | |
echo "::set-output name=is_fork::false" | |
fi | |
- name: Checkout base branch | |
uses: actions/checkout@v4 | |
with: | |
ref: ${{ github.event.pull_request.head.ref }} | |
# Fetch the forked repository where the pull request originates | |
repository: ${{ github.event.pull_request.head.repo.full_name }} | |
fetch-depth: 0 | |
- name: Get the changed files | |
id: changes | |
run: | | |
echo "Fetching changed files from GitHub API..." | |
PR_NUMBER=${{ github.event.pull_request.number }} | |
REPO=${{ github.repository }} | |
echo "Fetching files changed in PR #$PR_NUMBER" | |
# Call the GitHub API to get the list of changed files | |
curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
"https://api.github.com/repos/${REPO}/pulls/${PR_NUMBER}/files" \ | |
| jq -r '.[].filename' > changes.txt | |
echo "Changed files:" | |
cat changes.txt | |
- name: Validate pixel_update.json | |
run: | | |
CHANGED_FILES=$(cat changes.txt) | |
TOTAL_FILES=$(echo "$CHANGED_FILES" | wc -l) | |
# Check if only 'pixel_update.json' was modified | |
if [[ "$TOTAL_FILES" -eq 1 ]] && [[ "$CHANGED_FILES" == "pixel_update.json" ]]; then | |
echo "Only pixel_update.json was modified. Proceeding with validation..." | |
else | |
echo "Error: Changes found in files other than pixel_update.json. Failing the PR." | |
exit 1 | |
fi | |
- name: Save the content of changed file | |
run: | | |
cat pixel_update.json > changed_file_content.txt | |
- name: Get the PR author username | |
id: pr_user | |
run: | | |
branch_name=${{ github.event.pull_request.user.login }} | |
echo "PR_AUTHOR=$branch_name" >> $GITHUB_OUTPUT | |
echo "The PR was created by $branch_name" | |
- name: Pass the diff and file content to Python validation and send request | |
run: | | |
python validate_pixel_update.py changes.txt changed_file_content.txt ${{ github.event.pull_request.user.login }} https://amplace.co/api/update_pixel | |
auto_merge_pr: | |
needs: audit | |
runs-on: ubuntu-latest | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
PR_AUTHOR: ${{ needs.audit.outputs.PR_AUTHOR }} | |
PR_NUMBER: ${{ github.event.number }} | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Create branch if not exists | |
run: | | |
branch_name=$PR_AUTHOR | |
if git ls-remote --heads origin $branch_name | grep -sw $branch_name; then | |
echo "Branch $branch_name already exists." | |
else | |
echo "Branch $branch_name does not exist. Creating new branch..." | |
git checkout -b $branch_name | |
git push --set-upstream origin $branch_name | |
fi | |
- name: Change base branch of PR | |
run: gh pr edit $PR_NUMBER --base $PR_AUTHOR | |
- name: Merge PR | |
run: gh pr merge $PR_NUMBER --merge | |
- name: Close PR if previous steps failed | |
if: failure() | |
run: | | |
echo "Previous steps failed. Closing PR #$PR_NUMBER" | |
gh pr close $PR_NUMBER --delete-branch |