Skip to content

Update from template (auto-merged) #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 50 additions & 16 deletions .github/workflows/update-from-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ permissions:
jobs:
update:
runs-on: ubuntu-latest

outputs:
update_branch_merged_commit: ${{ steps.manage-branches.outputs.update_branch_merged_commit }}
create_update_branch_merged_pr: ${{ steps.manage-branches.outputs.create_update_branch_merged_pr }}
steps:
- uses: actions/checkout@v4
with:
Expand Down Expand Up @@ -169,22 +171,50 @@ jobs:
gh_pr_up -B "${{ steps.manage-branches.outputs.base_branch }}" \
--title "Update from template (auto-merged)" \
--body "An automated PR to sync changes from the template into this repo"

# Wait a moment so that checks of PR have higher prio than following job
sleep 3

# Split into two jobs to help with executor starvation
auto-merge:
needs: [update]
if: needs.update.outputs.create_update_branch_merged_pr == 1
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
# Required because otherwise there are always changes detected when executing diff/rev-list
fetch-depth: 0
# If no PAT is used the following error occurs on a push:
# refusing to allow a GitHub App to create or update workflow `.github/workflows/xxx.yml` without `workflows` permission
token: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }}

- name: Init Git
run: |
git config --global user.email "111048771+xdev-gh-bot@users.noreply.github.com"
git config --global user.name "XDEV Bot"

- name: Checking if auto-merge for PR update_branch_merged can be done
id: auto-merge-check
if: steps.manage-branches.outputs.create_update_branch_merged_pr == 1
env:
GH_TOKEN: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }}
run: |
not_failed_conclusion="skipped|neutral|success"
not_relevant_app_slug="dependabot|github-pages"
not_relevant_app_slug="dependabot|github-pages|sonarcloud"

echo "Waiting for workflows to start..."
sleep 60s
echo "Waiting for checks to start..."
sleep 40s

for i in {1..15}; do
for i in {1..20}; do
echo "Checking if PR can be auto-merged. Try: $i"

echo "Checking if update-branch-merged exists"
git fetch
if [[ $(git rev-parse origin/${{ env.UPDATE_BRANCH_MERGED }}) ]]; then
echo "Branch origin/${{ env.UPDATE_BRANCH_MERGED }} is missing"
exit 0
fi

echo "Fetching checks"
cs_response=$(curl -sL \
--fail-with-body \
Expand All @@ -193,7 +223,7 @@ jobs:
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GH_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository }}/commits/${{ steps.manage-branches.outputs.update_branch_merged_commit }}/check-suites)
https://api.github.com/repos/${{ github.repository }}/commits/${{ needs.update.outputs.update_branch_merged_commit }}/check-suites)

cs_data=$(echo $cs_response | jq '.check_suites[] | { conclusion: .conclusion, slug: .app.slug, check_runs_url: .check_runs_url }')
echo $cs_data
Expand Down Expand Up @@ -228,23 +258,27 @@ jobs:
fi

echo "Waiting before next run..."
sleep 60s
sleep 30s
done

echo "Timed out"
echo "perform=0" >> $GITHUB_OUTPUT
echo "Timed out - Assuming executor starvation - Forcing merge"
echo "perform=1" >> $GITHUB_OUTPUT

- name: Auto-merge update_branch_merged
if: steps.auto-merge-check.outputs.perform == 1
run: |
base_branch="${{ steps.manage-branches.outputs.base_branch }}"
echo "Restoring base branch $base_branch"
git checkout $base_branch
echo "Getting base branch"
base_branch=$(git branch --show-current)
echo "Base branch is $base_branch"

echo "Fetching..."
git fetch
if [[ $(git rev-parse origin/${{ env.UPDATE_BRANCH_MERGED }}) ]]; then
echo "Branch origin/${{ env.UPDATE_BRANCH_MERGED }} is missing"
exit 0
fi

expected_commit="${{ steps.manage-branches.outputs.update_branch_merged_commit }}"
expected_commit="${{ needs.update.outputs.update_branch_merged_commit }}"
actual_commit=$(git rev-parse origin/${{ env.UPDATE_BRANCH_MERGED }})
if [[ "$expected_commit" != "$actual_commit" ]]; then
echo "Branch ${{ env.UPDATE_BRANCH_MERGED }} contains unexpected commit $actual_commit"
Expand All @@ -256,8 +290,8 @@ jobs:
echo "Ensuring that current branch $base_branch is up-to-date"
git pull

echo "Merging ${{ env.UPDATE_BRANCH_MERGED }} into $base_branch"
git merge ${{ env.UPDATE_BRANCH_MERGED }} && merge_exit_code=$? || merge_exit_code=$?
echo "Merging origin/${{ env.UPDATE_BRANCH_MERGED }} into $base_branch"
git merge origin/${{ env.UPDATE_BRANCH_MERGED }} && merge_exit_code=$? || merge_exit_code=$?
if [ $merge_exit_code -ne 0 ]; then
echo "Unexpected merge failure $merge_exit_code - Requires manual resolution"

Expand Down