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