name: "Sync with Upstream and Create PR" on: workflow_dispatch: workflow_call: jobs: sync-upstream: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 with: token: ${{ secrets.GITHUB_TOKEN }} fetch-depth: 0 # Retrieve full history to allow merging - name: Generate a token for Rekku if: ${{ github.repository == 'RetroDECK/RetroDECK' }} id: generate-rekku-token uses: actions/create-github-app-token@v1 with: app-id: ${{ vars.REKKU_APP_ID }} private-key: ${{ secrets.REKKU_PRIVATE_KEY }} repositories: "RetroDECK,Cooker" owner: "RetroDECK" - name: Configuring Git run: | git config --global protocol.file.allow always git config --global user.name "Rekku" git config --global user.email "rekku@retrodeck.net" - name: Set default values for UPSTREAM_REPO and TARGET_BRANCH if not set run: | # Use the current repository name if UPSTREAM_REPO is not set : "${UPSTREAM_REPO:=${{ github.event.repository.name }}}" : "${TARGET_BRANCH:=master}" # Format the repository name for flathub, ensure no trailing spaces UPSTREAM_REPO="https://github.com/flathub/${UPSTREAM_REPO}" echo "UPSTREAM_REPO=${UPSTREAM_REPO}" >> $GITHUB_ENV echo "TARGET_BRANCH=${TARGET_BRANCH}" >> $GITHUB_ENV - name: Sync from Upstream Repository env: BRANCH_NAME: "sync-${{ github.run_id }}" # Temporary branch name for the PR run: | echo "Syncing from upstream repository ${{ env.UPSTREAM_REPO }} on branch ${{ env.TARGET_BRANCH }}" # Fetch and delete the branch if it exists locally git fetch origin if git rev-parse --verify "$BRANCH_NAME" >/dev/null 2>&1; then echo "Branch $BRANCH_NAME exists locally. Deleting it." git branch -D "$BRANCH_NAME" fi # Check if the branch exists on the remote and delete it if it does if git ls-remote --heads origin "$BRANCH_NAME" | grep "$BRANCH_NAME"; then echo "Branch $BRANCH_NAME exists on the remote. Deleting it." git push origin --delete "$BRANCH_NAME" fi # Perform fetch and merge, attempting to resolve conflicts by preferring the upstream changes git fetch "${{ env.UPSTREAM_REPO }}" "${{ env.TARGET_BRANCH }}" git checkout -b "$BRANCH_NAME" # Attempt to merge, allowing conflicts if ! git merge FETCH_HEAD -m "Merge changes from upstream ${{ env.UPSTREAM_REPO }}/${{ env.TARGET_BRANCH }}"; then echo "Conflicts detected. Staging conflicted files for the pull request." # Stage all changes, including conflicts git add -A # Commit the merge with conflicts git commit -m "Merge changes from upstream ${{ env.UPSTREAM_REPO }}/${{ env.TARGET_BRANCH }} with conflicts" fi echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV - name: Push changes to new branch if: success() run: | git push origin "${{ env.BRANCH_NAME }}" - name: Create Pull Request if: success() uses: peter-evans/create-pull-request@v7.0.5 with: token: ${{ steps.generate-rekku-token.outputs.token || secrets.GITHUB_TOKEN }} commit-message: "Sync with upstream changes from ${{ env.UPSTREAM_REPO }}" branch: ${{ env.BRANCH_NAME }} title: "Sync with upstream ${{ env.UPSTREAM_REPO }}" body: | This PR merges the latest changes from the upstream repository: - **Repository**: ${{ env.UPSTREAM_REPO }} - **Branch**: ${{ env.TARGET_BRANCH }} ## Conflict Resolution Instructions If there are conflicts in this PR, you can resolve them locally by following these steps: 1. **Fetch the remote branch**: ```bash git fetch origin ${{ env.BRANCH_NAME }} ``` 2. **Checkout the branch**: ```bash git checkout ${{ env.BRANCH_NAME }} ``` 3. **Merge the upstream branch manually**: ```bash git fetch https://github.com/flathub/${{ env.UPSTREAM_REPO }} ${{ env.TARGET_BRANCH }} git merge FETCH_HEAD ``` 4. **Resolve any conflicts**: Open each conflicted file and manually resolve the conflicts. Then mark each file as resolved: ```bash git add ``` 5. **Complete the merge**: ```bash git commit ``` 6. **Push the resolved branch**: ```bash git push origin ${{ env.BRANCH_NAME }} ``` base: ${{ env.TARGET_BRANCH }}