|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# SPDX-License-Identifier: Apache-2.0 |
| 16 | + |
| 17 | +name: Sync Feature Branches |
| 18 | + |
| 19 | +on: |
| 20 | + workflow_dispatch: |
| 21 | + schedule: |
| 22 | + # Run every Monday at 9am PT (5pm UTC) |
| 23 | + - cron: '0 17 * * 1' |
| 24 | + |
| 25 | +jobs: |
| 26 | + sync-feature-branches: |
| 27 | + runs-on: ubuntu-latest |
| 28 | + permissions: |
| 29 | + contents: write |
| 30 | + pull-requests: write |
| 31 | + steps: |
| 32 | + - name: Checkout repository |
| 33 | + uses: actions/checkout@v4 |
| 34 | + with: |
| 35 | + fetch-depth: 0 # Fetch all branches and history |
| 36 | + |
| 37 | + - name: Configure Git |
| 38 | + run: | |
| 39 | + git config user.name "github-actions[bot]" |
| 40 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 41 | +
|
| 42 | + - name: Find and sync feature branches |
| 43 | + env: |
| 44 | + GH_TOKEN: ${{ github.token }} |
| 45 | + run: | |
| 46 | + echo "🔍 Finding feature branches..." |
| 47 | + |
| 48 | + # Get all remote feature branches |
| 49 | + feature_branches=$(git branch -r | grep 'origin/feature/' | sed 's|origin/||' | grep -v HEAD || true) |
| 50 | + |
| 51 | + if [ -z "$feature_branches" ]; then |
| 52 | + echo "ℹ️ No feature branches found matching pattern 'feature/*'" |
| 53 | + exit 0 |
| 54 | + fi |
| 55 | + |
| 56 | + echo "Found feature branches:" |
| 57 | + echo "$feature_branches" |
| 58 | + echo "" |
| 59 | + |
| 60 | + # Process each feature branch |
| 61 | + for branch in $feature_branches; do |
| 62 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 63 | + echo "📋 Processing branch: $branch" |
| 64 | + |
| 65 | + # Check if main has commits not in the feature branch |
| 66 | + git fetch origin main |
| 67 | + git fetch origin "$branch" |
| 68 | + |
| 69 | + commits_behind=$(git rev-list --count origin/$branch..origin/main) |
| 70 | + |
| 71 | + if [ "$commits_behind" -eq 0 ]; then |
| 72 | + echo "✅ Branch $branch is already up to date with main" |
| 73 | + continue |
| 74 | + fi |
| 75 | + |
| 76 | + echo "📊 Branch $branch is $commits_behind commit(s) behind main" |
| 77 | + |
| 78 | + # Extract issue number from branch name (format: feature/PROJ-NNN-short-slug) |
| 79 | + # If no PROJ-NNN pattern is found, use "Issue - None" |
| 80 | + issue_number=$(echo "$branch" | grep -oE 'PROJ-[0-9]+' || echo "") |
| 81 | + if [ -z "$issue_number" ]; then |
| 82 | + issue_ref="Issue - None" |
| 83 | + else |
| 84 | + issue_ref="Issue #$issue_number" |
| 85 | + fi |
| 86 | + |
| 87 | + # Create a temporary branch for the merge |
| 88 | + temp_branch="sync-main-to-${branch//\//-}-$(date +%Y%m%d)" |
| 89 | + |
| 90 | + echo "🌿 Creating temporary branch: $temp_branch" |
| 91 | + git checkout -b "$temp_branch" "origin/$branch" |
| 92 | + |
| 93 | + # Attempt to merge main |
| 94 | + echo "🔀 Merging main into $temp_branch..." |
| 95 | + if git merge origin/main --no-edit -m "Merge main into $branch"; then |
| 96 | + echo "✅ Merge successful (no conflicts)" |
| 97 | + else |
| 98 | + echo "⚠️ Merge has conflicts - will create PR for manual resolution" |
| 99 | + # Abort the merge but keep the branch |
| 100 | + git merge --abort |
| 101 | + # Create a merge commit that preserves conflict markers |
| 102 | + git merge origin/main --no-commit --no-ff || true |
| 103 | + fi |
| 104 | + |
| 105 | + # Push the temporary branch |
| 106 | + echo "⬆️ Pushing $temp_branch to remote..." |
| 107 | + git push origin "$temp_branch" |
| 108 | + |
| 109 | + # Create PR |
| 110 | + echo "📝 Creating pull request..." |
| 111 | + |
| 112 | + gh pr create \ |
| 113 | + --base "$branch" \ |
| 114 | + --head "$temp_branch" \ |
| 115 | + --title "Sync main into $branch" \ |
| 116 | + --body "$(cat <<EOF |
| 117 | + ## Description |
| 118 | + |
| 119 | + This automated PR merges the latest changes from \`main\` into \`$branch\`. |
| 120 | + |
| 121 | + **What to do:** |
| 122 | + Review the changes and merge this PR to keep your feature branch up to date with main. If there are merge conflicts, resolve them before merging. |
| 123 | + |
| 124 | + **Branch Info:** |
| 125 | + - **Target**: \`$branch\` |
| 126 | + - **Source**: \`main\` |
| 127 | + - **Commits behind**: $commits_behind |
| 128 | + |
| 129 | + $issue_ref |
| 130 | + |
| 131 | + ## Checklist |
| 132 | + - [ ] I am familiar with the [Contributing Guidelines](https://github.com/NVIDIA/OSMO/blob/main/CONTRIBUTING.md). |
| 133 | + - [ ] New or existing tests cover these changes. |
| 134 | + - [ ] The documentation is up to date with these changes. |
| 135 | + |
| 136 | + --- |
| 137 | + *This PR was automatically created by the [Sync Feature Branches workflow](.github/workflows/sync-feature-branches.yaml) as part of the [Projects Process](../projects/README.md#stage-in-development).* |
| 138 | + EOF |
| 139 | + )" || echo "⚠️ Failed to create PR for $branch" |
| 140 | + |
| 141 | + echo "✅ Created PR to sync main into $branch" |
| 142 | + |
| 143 | + # Switch back to main for next iteration |
| 144 | + git checkout main |
| 145 | + |
| 146 | + done |
| 147 | + |
| 148 | + echo "" |
| 149 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 150 | + echo "✨ Sync process complete!" |
| 151 | +
|
| 152 | + - name: Summary |
| 153 | + if: always() |
| 154 | + run: | |
| 155 | + echo "### Sync Feature Branches Summary" >> $GITHUB_STEP_SUMMARY |
| 156 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 157 | + echo "Workflow completed. Check the logs above for details on each feature branch." >> $GITHUB_STEP_SUMMARY |
0 commit comments