|
| 1 | +name: Sync Content to Next Branch |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - 'docs/cms/**' |
| 9 | + - 'docs/cloud/**' |
| 10 | + - 'static/img/assets/**' |
| 11 | + pull_request: |
| 12 | + types: [labeled, closed] |
| 13 | + |
| 14 | +jobs: |
| 15 | + # Job 1: Automatic replication of commits pushed to main |
| 16 | + cherry-pick-from-push: |
| 17 | + if: github.event_name == 'push' |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - name: Checkout repository |
| 21 | + uses: actions/checkout@v3 |
| 22 | + with: |
| 23 | + fetch-depth: 0 |
| 24 | + token: ${{ secrets.CONTENT_SYNC_TOKEN }} |
| 25 | + |
| 26 | + - name: Set up Git |
| 27 | + run: | |
| 28 | + git config user.name "GitHub Actions Bot" |
| 29 | + git config user.email "[email protected]" |
| 30 | +
|
| 31 | + - name: Get latest commit that modified content |
| 32 | + id: get-commit |
| 33 | + run: | |
| 34 | + COMMIT_HASH=$(git log -1 --format="%H" -- docs/cms docs/cloud static/img/assets) |
| 35 | + COMMIT_MSG=$(git log -1 --format="%s" $COMMIT_HASH) |
| 36 | + echo "commit_hash=$COMMIT_HASH" >> $GITHUB_OUTPUT |
| 37 | + echo "commit_msg=$COMMIT_MSG" >> $GITHUB_OUTPUT |
| 38 | +
|
| 39 | + - name: Check if commit modifies only content files |
| 40 | + id: check-content-only |
| 41 | + run: | |
| 42 | + # Check if the commit touches only allowed folders |
| 43 | + MODIFIED_FILES=$(git diff-tree --no-commit-id --name-only -r ${{ steps.get-commit.outputs.commit_hash }}) |
| 44 | + INVALID_FILES=$(echo "$MODIFIED_FILES" | grep -v -E '^(docs/cms/|docs/cloud/|static/img/assets/)') |
| 45 | + |
| 46 | + if [ -z "$INVALID_FILES" ]; then |
| 47 | + echo "content_only=true" >> $GITHUB_OUTPUT |
| 48 | + else |
| 49 | + echo "content_only=false" >> $GITHUB_OUTPUT |
| 50 | + fi |
| 51 | +
|
| 52 | + - name: Create PR to next branch |
| 53 | + if: steps.check-content-only.outputs.content_only == 'true' |
| 54 | + run: | |
| 55 | + BRANCH_NAME="sync-content-$(date +%Y%m%d-%H%M%S)" |
| 56 | + git fetch origin next |
| 57 | + git checkout -b $BRANCH_NAME origin/next |
| 58 | + |
| 59 | + git cherry-pick ${{ steps.get-commit.outputs.commit_hash }} || { |
| 60 | + if git diff --name-only --diff-filter=U | grep -q -E '^(docs/cms/|docs/cloud/|static/img/assets/)'; then |
| 61 | + # Try to resolve simple conflicts in content files |
| 62 | + git add $(git diff --name-only --diff-filter=U | grep -E '^(docs/cms/|docs/cloud/|static/img/assets/)') |
| 63 | + git cherry-pick --continue |
| 64 | + else |
| 65 | + # If conflict involves other files, abort |
| 66 | + git cherry-pick --abort |
| 67 | + echo "Conflict detected in non-content files, cherry-pick aborted" |
| 68 | + exit 1 |
| 69 | + fi |
| 70 | + } |
| 71 | + |
| 72 | + git push origin $BRANCH_NAME |
| 73 | + |
| 74 | + # Create a PR with a title that includes reference to the original commit |
| 75 | + gh pr create --base next --head $BRANCH_NAME \ |
| 76 | + --title "[Auto-sync] ${{ steps.get-commit.outputs.commit_msg }}" \ |
| 77 | + --body "Automatic synchronization of commit from main: ${{ steps.get-commit.outputs.commit_hash }}\n\nChanges applied automatically by GitHub Actions." |
| 78 | + env: |
| 79 | + GITHUB_TOKEN: ${{ secrets.CONTENT_SYNC_TOKEN }} |
| 80 | + |
| 81 | + # Job 2: Create PR to next when a PR is labeled |
| 82 | + create-pr-for-labeled: |
| 83 | + if: github.event_name == 'pull_request' && github.event.action == 'labeled' && github.event.label.name == 'temp: port to docs-next' |
| 84 | + runs-on: ubuntu-latest |
| 85 | + steps: |
| 86 | + - name: Checkout repository |
| 87 | + uses: actions/checkout@v3 |
| 88 | + with: |
| 89 | + fetch-depth: 0 |
| 90 | + token: ${{ secrets.CONTENT_SYNC_TOKEN }} |
| 91 | + |
| 92 | + - name: Set up Git |
| 93 | + run: | |
| 94 | + git config user.name "GitHub Actions Bot" |
| 95 | + git config user.email "[email protected]" |
| 96 | +
|
| 97 | + - name: Check if PR contains only content changes |
| 98 | + id: check-pr-files |
| 99 | + run: | |
| 100 | + # Get list of modified files in the PR |
| 101 | + PR_FILES=$(gh pr view ${{ github.event.pull_request.number }} --json files --jq '.files[].path') |
| 102 | + |
| 103 | + # Check if all files are in allowed directories |
| 104 | + INVALID_FILES=$(echo "$PR_FILES" | grep -v -E '^(docs/cms/|docs/cloud/|static/img/assets/)') |
| 105 | + |
| 106 | + if [ -z "$INVALID_FILES" ]; then |
| 107 | + echo "content_only=true" >> $GITHUB_OUTPUT |
| 108 | + else |
| 109 | + echo "content_only=false" >> $GITHUB_OUTPUT |
| 110 | + echo "Non-content files found: $INVALID_FILES" |
| 111 | + fi |
| 112 | + env: |
| 113 | + GITHUB_TOKEN: ${{ secrets.CONTENT_SYNC_TOKEN }} |
| 114 | + |
| 115 | + - name: Create PR to next branch |
| 116 | + if: steps.check-pr-files.outputs.content_only == 'true' |
| 117 | + run: | |
| 118 | + # Get source branch of the original PR |
| 119 | + SOURCE_BRANCH="${{ github.event.pull_request.head.ref }}" |
| 120 | + SOURCE_REPO="${{ github.event.pull_request.head.repo.full_name }}" |
| 121 | + PR_TITLE="${{ github.event.pull_request.title }}" |
| 122 | + |
| 123 | + # Create a new branch based on next |
| 124 | + TARGET_BRANCH="next-port-pr${{ github.event.pull_request.number }}" |
| 125 | + git fetch origin next |
| 126 | + git checkout -b $TARGET_BRANCH origin/next |
| 127 | + |
| 128 | + # If PR comes from a fork, fetch its changes |
| 129 | + if [ "$SOURCE_REPO" != "${{ github.repository }}" ]; then |
| 130 | + git fetch "https://github.com/$SOURCE_REPO.git" $SOURCE_BRANCH |
| 131 | + fi |
| 132 | + |
| 133 | + # Get each modified file that matches our criteria |
| 134 | + for FILE in $(echo "$PR_FILES" | grep -E '^(docs/cms/|docs/cloud/|static/img/assets/)'); do |
| 135 | + mkdir -p $(dirname "$FILE") |
| 136 | + git checkout FETCH_HEAD -- "$FILE" |
| 137 | + done |
| 138 | + |
| 139 | + # Commit and push |
| 140 | + git add . |
| 141 | + git commit -m "Port PR #${{ github.event.pull_request.number }}: $PR_TITLE to next branch" |
| 142 | + git push origin $TARGET_BRANCH |
| 143 | + |
| 144 | + # Create PR to next |
| 145 | + gh pr create --base next --head $TARGET_BRANCH \ |
| 146 | + --title "[Port to next] $PR_TITLE" \ |
| 147 | + --body "Automatic port of PR #${{ github.event.pull_request.number }} to next branch.\n\nOriginal PR: #${{ github.event.pull_request.number }}\nCreated automatically after adding the 'temp: port to docs-next' label." |
| 148 | + env: |
| 149 | + GITHUB_TOKEN: ${{ secrets.CONTENT_SYNC_TOKEN }} |
| 150 | + |
| 151 | + # Job 3: Sync changes when a labeled PR is merged to main |
| 152 | + sync-merged-pr: |
| 153 | + if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'temp: port to docs-next') |
| 154 | + runs-on: ubuntu-latest |
| 155 | + steps: |
| 156 | + - name: Checkout repository |
| 157 | + uses: actions/checkout@v3 |
| 158 | + with: |
| 159 | + fetch-depth: 0 |
| 160 | + token: ${{ secrets.CONTENT_SYNC_TOKEN }} |
| 161 | + |
| 162 | + - name: Set up Git |
| 163 | + run: | |
| 164 | + git config user.name "GitHub Actions Bot" |
| 165 | + git config user.email "[email protected]" |
| 166 | +
|
| 167 | + - name: Get merge commit |
| 168 | + id: get-merge-commit |
| 169 | + run: | |
| 170 | + MERGE_COMMIT=$(git log -1 --format="%H" ${{ github.event.pull_request.merge_commit_sha }}) |
| 171 | + echo "merge_commit=$MERGE_COMMIT" >> $GITHUB_OUTPUT |
| 172 | +
|
| 173 | + - name: Cherry-pick merge commit to next |
| 174 | + run: | |
| 175 | + BRANCH_NAME="sync-merged-pr${{ github.event.pull_request.number }}" |
| 176 | + git fetch origin next |
| 177 | + git checkout -b $BRANCH_NAME origin/next |
| 178 | + |
| 179 | + # For a merge commit, we need to use -m 1 to cherry-pick the parent changes |
| 180 | + git cherry-pick -m 1 ${{ steps.get-merge-commit.outputs.merge_commit }} || { |
| 181 | + if git diff --name-only --diff-filter=U | grep -q -E '^(docs/cms/|docs/cloud/|static/img/assets/)'; then |
| 182 | + # Try to resolve simple conflicts in content files |
| 183 | + git add $(git diff --name-only --diff-filter=U | grep -E '^(docs/cms/|docs/cloud/|static/img/assets/)') |
| 184 | + git cherry-pick --continue |
| 185 | + else |
| 186 | + # If conflict involves other files, abort |
| 187 | + git cherry-pick --abort |
| 188 | + echo "Conflict detected in non-content files, cherry-pick aborted" |
| 189 | + exit 1 |
| 190 | + fi |
| 191 | + } |
| 192 | + |
| 193 | + git push origin $BRANCH_NAME |
| 194 | + |
| 195 | + # Find if there's an existing PR for this original PR |
| 196 | + EXISTING_PORT_PR=$(gh pr list --base next --head "next-port-pr${{ github.event.pull_request.number }}" --json number --jq '.[0].number') |
| 197 | + |
| 198 | + if [ -n "$EXISTING_PORT_PR" ]; then |
| 199 | + # If a PR already exists, close it and add a comment |
| 200 | + gh pr close $EXISTING_PORT_PR --comment "This PR is replaced by the direct synchronization of the merge commit: #INSERT_NEW_PR_NUMBER_HERE" |
| 201 | + fi |
| 202 | + |
| 203 | + # Create PR to next |
| 204 | + gh pr create --base next --head $BRANCH_NAME \ |
| 205 | + --title "[Merged-sync] ${{ github.event.pull_request.title }}" \ |
| 206 | + --body "Synchronization of the merge commit from PR #${{ github.event.pull_request.number }} to next.\n\nThis PR replaces any previous port PR created for #${{ github.event.pull_request.number }}." |
| 207 | + env: |
| 208 | + GITHUB_TOKEN: ${{ secrets.CONTENT_SYNC_TOKEN }} |
0 commit comments