|
| 1 | +--- |
| 2 | +# This GitHub Action aims to sync the downstream branch with the corresponding upstream, resolve known conflicts, and generates a pull request |
| 3 | +name: Sync downstream branch from upstream |
| 4 | +on: # yamllint disable-line rule:truthy |
| 5 | + workflow_dispatch: |
| 6 | + inputs: |
| 7 | + branch_name: |
| 8 | + description: "Provide name of the branch want to sync:" |
| 9 | + required: true |
| 10 | + default: "main" |
| 11 | + type: string |
| 12 | +env: |
| 13 | + RUNTIME_IMAGES_DIR: "jupyter/datascience/ubi9-python-3.11/runtime-images/*.json" |
| 14 | + MANIFEST_FILES: "manifests/base/*.env" |
| 15 | + BRANCH_NAME: ${{ inputs.branch_name }} |
| 16 | + TEMP_BRANCH: sync-${{ inputs.branch_name }}-${{ github.run_id }} |
| 17 | + DOWNSTREAM_OWNER: "red-hat-data-services" |
| 18 | + UPSTREAM_OWNER: "opendatahub-io" |
| 19 | +jobs: |
| 20 | + sync: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + permissions: |
| 23 | + contents: write |
| 24 | + pull-requests: write |
| 25 | + |
| 26 | + steps: |
| 27 | + - name: Set up Git |
| 28 | + run: | |
| 29 | + git config --global user.name "github-actions[bot]" |
| 30 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 31 | +
|
| 32 | + - name: Checkout repository |
| 33 | + uses: actions/checkout@v4 |
| 34 | + with: |
| 35 | + fetch-depth: 0 |
| 36 | + |
| 37 | + - name: Add upstream and downstream remotes |
| 38 | + run: | |
| 39 | + git remote add upstream https://github.com/${{ env.UPSTREAM_OWNER }}/notebooks.git |
| 40 | + git fetch --all |
| 41 | +
|
| 42 | + - name: Determine Downstream Branch |
| 43 | + id: determine_branch |
| 44 | + run: | |
| 45 | + # Check if the branch follows the YYYYx pattern |
| 46 | + if [[ "${{ env.BRANCH_NAME }}" =~ ^[0-9]{4}[a-zA-Z]$ ]]; then |
| 47 | + DOWNSTREAM_BRANCH="release-${{ env.BRANCH_NAME }}" |
| 48 | + else |
| 49 | + # For 'main', keep it as 'main' |
| 50 | + DOWNSTREAM_BRANCH="${{ env.BRANCH_NAME }}" |
| 51 | + fi |
| 52 | + echo "DOWNSTREAM_BRANCH=$DOWNSTREAM_BRANCH" >> $GITHUB_ENV |
| 53 | + echo "Temporary downstream branch will be: $DOWNSTREAM_BRANCH" |
| 54 | +
|
| 55 | + - name: Sync and resolve conflicts |
| 56 | + run: | |
| 57 | + set -e |
| 58 | +
|
| 59 | + echo "Syncing '${{ env.DOWNSTREAM_OWNER }}'/'${{ env.DOWNSTREAM_BRANCH }}' from '${{ env.UPSTREAM_OWNER}}'/'${{ env.BRANCH_NAME }}'" |
| 60 | +
|
| 61 | + # Ensure the downstream branch exists locally |
| 62 | + git checkout -b ${{ env.TEMP_BRANCH }} origin/${{ env.DOWNSTREAM_BRANCH }} || exit 1 |
| 63 | +
|
| 64 | + # Pull changes from the upstream branch |
| 65 | + git pull upstream ${{ env.BRANCH_NAME }} --no-rebase || true |
| 66 | +
|
| 67 | + # Resolve known conflicts |
| 68 | + FILES_TO_RESOLVE=($RUNTIME_IMAGES_DIR $MANIFEST_FILES) |
| 69 | + for FILE in "${FILES_TO_RESOLVE[@]}"; do |
| 70 | + if [[ -f "$FILE" && "$(git status --porcelain=v1 2>/dev/null | grep -c "$FILE")" -gt 0 ]]; then |
| 71 | + echo "Resolving conflict for $FILE by keeping downstream changes." |
| 72 | + git checkout --ours "$FILE" |
| 73 | + git add "$FILE" |
| 74 | + fi |
| 75 | + done |
| 76 | +
|
| 77 | + # Check for unresolved conflicts |
| 78 | + if [[ -n "$(git ls-files -u)" ]]; then |
| 79 | + echo "Unresolved conflicts detected in the following files:" |
| 80 | + git ls-files -u |
| 81 | + echo "Aborting merge due to unexpected conflicts." |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | +
|
| 85 | + # Commit changes if any |
| 86 | + if [[ -n "$(git status --porcelain)" ]]; then |
| 87 | + git commit -m "Merge upstream/${{ env.BRANCH_NAME }} into downstream/${{ env.DOWNSTREAM_BRANCH }} with resolved conflicts" |
| 88 | + else |
| 89 | + echo "No changes to commit. Exiting workflow." |
| 90 | + exit 0 |
| 91 | + fi |
| 92 | +
|
| 93 | + # Push the temporary branch to downstream |
| 94 | + git push origin ${{ env.TEMP_BRANCH }} |
| 95 | +
|
| 96 | + - name: Creates PR |
| 97 | + run: | |
| 98 | + gh pr create --repo https://github.com/${{ env.DOWNSTREAM_OWNER }}/notebooks.git \ |
| 99 | + --title "$pr_title" \ |
| 100 | + --body "$pr_body" \ |
| 101 | + --head ${{ env.DOWNSTREAM_OWNER }}:${{ env.TEMP_BRANCH }} \ |
| 102 | + --base ${{ env.DOWNSTREAM_BRANCH }} |
| 103 | + env: |
| 104 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 105 | + pr_title: "Sync '${{ env.DOWNSTREAM_BRANCH }}' from opendatahub-io:'${{ env.BRANCH_NAME }}'" |
| 106 | + pr_body: | |
| 107 | + :robot: This is an automated Pull Request created by /.github/workflows/sync-downstream.yaml |
0 commit comments