Skip to content

Commit cf5d8ac

Browse files
committed
[CI] Autogenerate PR for generated files when CI fails and regenerates them
1 parent 4fea40f commit cf5d8ac

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

.github/workflows/coq-docker.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ jobs:
345345
generated-files:
346346
needs: build
347347
runs-on: ubuntu-latest
348+
# Required permissions for creating PRs and pushing branches
349+
permissions:
350+
contents: write
351+
pull-requests: write
352+
actions: read
348353
strategy:
349354
fail-fast: false
350355
matrix:
@@ -356,6 +361,10 @@ jobs:
356361
- uses: actions/checkout@v4
357362
with:
358363
submodules: recursive
364+
# fetch-depth: 0 is required to checkout a new branch from the PR's head.
365+
# persist-credentials: true is needed to push to the new branch.
366+
fetch-depth: 0
367+
persist-credentials: true
359368
- name: Download a Build Artifact
360369
uses: actions/download-artifact@v4
361370
with:
@@ -364,6 +373,7 @@ jobs:
364373
- name: make binaries executable
365374
run: git check-ignore src/ExtractionOCaml/* src/ExtractionOCaml/*/* | grep -v '\.' | xargs chmod +x
366375
- name: generated-files
376+
id: generate
367377
run: etc/ci/github-actions-make.sh --warnings -f Makefile.examples -j2 generated-files
368378
if: github.event_name == 'pull_request'
369379
- run: tar -czvf generated-files.tgz fiat-*/
@@ -374,6 +384,72 @@ jobs:
374384
name: generated-files-${{ matrix.coq-version }}
375385
path: generated-files.tgz
376386
if: failure()
387+
- name: Commit files and create PR or comment
388+
if: failure() && github.event_name == 'pull_request'
389+
env:
390+
GH_TOKEN: ${{ github.token }}
391+
PR_NUMBER: ${{ github.event.pull_request.number }}
392+
SOURCE_BRANCH: ${{ github.head_ref }}
393+
BASE_REPO: ${{ github.repository }}
394+
HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
395+
run: |
396+
set -euo pipefail # Exit immediately on error
397+
398+
echo "File generation failed. Committing updated files to a new branch."
399+
400+
# Configure Git to commit as the github-actions bot
401+
git config user.name "github-actions[bot]"
402+
git config user.email "github-actions[bot]@users.noreply.github.com"
403+
404+
# Create a unique, descriptive branch name using slashes for organization
405+
NEW_BRANCH="fix/pr${PR_NUMBER}/generated-files"
406+
git checkout -b "$NEW_BRANCH"
407+
408+
# Add only the relevant generated files to the index
409+
echo "Adding generated files from 'fiat-*/' directory."
410+
git add fiat-*/
411+
412+
# Crucial check: if no files were changed, the failure was for another reason.
413+
# In this case, we exit cleanly to avoid creating an empty commit.
414+
if git diff --staged --quiet; then
415+
echo "No file changes to commit. The failure was likely not due to outdated generated files."
416+
exit 0
417+
fi
418+
419+
git commit -m "chore(generated): Update generated files for #${PR_NUMBER}"
420+
git push --set-upstream origin "$NEW_BRANCH"
421+
422+
# Conditional logic: create a PR for internal branches, or comment for forks.
423+
if [[ "$HEAD_REPO" == "$BASE_REPO" ]]; then
424+
echo "PR is from a branch on the main repository. Creating a fixup PR."
425+
426+
# Create a new PR targeting the original PR's branch
427+
NEW_PR_URL=$(gh pr create \
428+
--title "chore: Update generated files for #${PR_NUMBER}" \
429+
--body "This PR was automatically created to update the generated files for #${PR_NUMBER}. It will be automerged." \
430+
--base "$SOURCE_BRANCH" \
431+
--head "$NEW_BRANCH")
432+
433+
# Enable automerge, so it merges as soon as checks pass.
434+
gh pr merge --auto --squash --delete-branch "$NEW_PR_URL"
435+
436+
echo "Successfully created and set PR ${NEW_PR_URL} to automerge."
437+
438+
else
439+
echo "PR is from a fork. Commenting on original PR with instructions."
440+
441+
# For forks, the contributor must create the PR. We provide a convenient link that
442+
# pre-fills the PR form on their fork, targeting their own branch.
443+
COMPARE_URL="https://github.com/${HEAD_REPO}/compare/${SOURCE_BRANCH}...${BASE_REPO}:${NEW_BRANCH}?expand=1"
444+
445+
COMMENT_BODY="❌ **Generated files are out of date.**
446+
447+
You can review the updates in branch \`${NEW_BRANCH}\` and [open a pull request](${COMPARE_URL}) to merge them into your branch."
448+
449+
gh pr comment "$PR_NUMBER" --body "$COMMENT_BODY"
450+
451+
echo "Successfully commented on PR #${PR_NUMBER} with instructions."
452+
fi
377453
378454
standalone-haskell:
379455
needs: build

0 commit comments

Comments
 (0)