Pull requests to pull branches from develop branch in the main branch #45
This file contains hidden or 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: Pull requests to pull branches from develop branch in the main branch | |
| on: | |
| workflow_dispatch: | |
| schedule: | |
| - cron: '0 9 * * *' | |
| push: | |
| branches: | |
| - ci/pr | |
| jobs: | |
| build: | |
| runs-on: self-hosted | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| repo: | |
| - ONLYOFFICE/web-apps | |
| - ONLYOFFICE/core-fonts | |
| - ONLYOFFICE/core | |
| - ONLYOFFICE/server | |
| - ONLYOFFICE/sdkjs | |
| steps: | |
| - name: Get repo name as output by splitting matrix.repo | |
| id: repo_name | |
| run: | | |
| IFS='/' read -r owner repo <<< "${{ matrix.repo }}" | |
| echo "repo=${repo}" >> $GITHUB_OUTPUT | |
| echo "upstream_branch=master" >> $GITHUB_OUTPUT | |
| - name: Checkout runner workspace | |
| uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 | |
| with: | |
| repository: "Euro-Office/${{ steps.repo_name.outputs.repo }}" | |
| token: ${{ secrets.EURO_OFFICE_MIRROR_TOKEN }} | |
| ref: main | |
| - name: Merge commits and create PR from ${{ steps.repo_name.outputs.upstream_branch }} branch | |
| run: | | |
| git config --global user.name "Euro-Office via GitHub Actions" | |
| git config --global user.email "noreply@github.com" | |
| git remote add upstream https://github.com/${{ matrix.repo }}.git | |
| git fetch upstream ${{ steps.repo_name.outputs.upstream_branch }} | |
| git fetch origin update-from-${{ steps.repo_name.outputs.upstream_branch }} || true | |
| git checkout -B update-from-${{ steps.repo_name.outputs.upstream_branch }} origin/update-from-${{ steps.repo_name.outputs.upstream_branch }} || git checkout -B update-from-${{ steps.repo_name.outputs.upstream_branch }} | |
| # Check if there are new commits to merge | |
| commits=$(git log HEAD..upstream/${{ steps.repo_name.outputs.upstream_branch }} --format="%H" --reverse) | |
| if [ -z "$commits" ]; then | |
| echo "Branch is already up to date, nothing to do." | |
| exit 0 | |
| fi | |
| # Attempt to merge upstream branch | |
| merge_success=true | |
| conflicted_files="" | |
| if git merge upstream/${{ steps.repo_name.outputs.upstream_branch }} --no-edit; then | |
| echo "Merge completed successfully without conflicts" | |
| git push origin update-from-${{ steps.repo_name.outputs.upstream_branch }} --force-with-lease | |
| else | |
| echo "Merge has conflicts" | |
| merge_success=false | |
| # Collect conflicted files | |
| conflicted_files=$(git diff --name-only --diff-filter=U | sed 's/^/- `/' | sed 's/$/`/') | |
| # Abort the merge | |
| git merge --abort | |
| fi | |
| # Build PR body | |
| pr_body="This pull request updates the \`main\` branch with changes from the \`${{ steps.repo_name.outputs.upstream_branch }}\` branch in the upstream repository." | |
| if [ "$merge_success" = false ]; then | |
| pr_body="${pr_body}"$'\n\n## ⚠️ Merge Conflicts Detected\n\nThe automatic merge from upstream encountered conflicts in the following files:\n\n'"${conflicted_files}"$'\n\n### Action Required\n\n- [ ] Manually merge the upstream branch to inspect and resolve the conflicts:\n ```bash\n git fetch upstream '"${{ steps.repo_name.outputs.upstream_branch }}"$'\n git checkout update-from-'"${{ steps.repo_name.outputs.upstream_branch }}"$'\n git merge upstream/'"${{ steps.repo_name.outputs.upstream_branch }}"$'\n # Resolve conflicts, then:\n git add .\n git commit\n git push origin update-from-'"${{ steps.repo_name.outputs.upstream_branch }}"$'\n ```' | |
| fi | |
| # Write PR body to temp file to avoid "Argument list too long" error | |
| pr_body_file=$(mktemp) | |
| echo "$pr_body" > "$pr_body_file" | |
| printf "PR body:\n%s\n" "$pr_body" | |
| # Create PR or update body of existing one | |
| pr_data=$(gh pr view \ | |
| --repo "Euro-Office/${{ steps.repo_name.outputs.repo }}" \ | |
| "update-from-${{ steps.repo_name.outputs.upstream_branch }}" \ | |
| --json url,state 2>/dev/null || true) | |
| pr_url=$(echo "$pr_data" | jq -r '.url // empty') | |
| pr_state=$(echo "$pr_data" | jq -r '.state // empty') | |
| if [ -n "$pr_url" ] && [ "$pr_state" = "OPEN" ]; then | |
| echo "Updating existing open PR: $pr_url" | |
| gh pr edit "$pr_url" \ | |
| --title "Update from ${{ steps.repo_name.outputs.upstream_branch }} branch in upstream repository" \ | |
| --body-file "$pr_body_file" || true | |
| elif [ "$merge_success" = true ] || [ -n "$conflicted_files" ]; then | |
| echo "Creating new PR (previous PR state: $pr_state)" | |
| gh pr create \ | |
| --repo "Euro-Office/${{ steps.repo_name.outputs.repo }}" \ | |
| --head update-from-${{ steps.repo_name.outputs.upstream_branch }} \ | |
| --base main \ | |
| --title "Update from ${{ steps.repo_name.outputs.upstream_branch }} branch in upstream repository" \ | |
| --body-file "$pr_body_file" || true | |
| fi | |
| # Clean up temp file | |
| rm -f "$pr_body_file" | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.EURO_OFFICE_MIRROR_TOKEN }} | |