Skip to content

Commit c222a1e

Browse files
committed
backup
1 parent fc212b8 commit c222a1e

File tree

5 files changed

+652
-0
lines changed

5 files changed

+652
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# This workflow handles "/approve" comments on PRs with "lake-gate" label
2+
# It performs a fast-forward merge of the stable branch to point to the same commit as the temporary branch
3+
#
4+
# This workflow uses the built-in GITHUB_TOKEN with the required permissions set below.
5+
6+
name: Approve Lake Gate PR
7+
8+
on:
9+
issue_comment:
10+
types: [created]
11+
12+
permissions:
13+
contents: write
14+
pull-requests: write
15+
16+
jobs:
17+
approve-lake-gate:
18+
runs-on: ubuntu-latest
19+
if: github.event.issue.pull_request && contains(github.event.comment.body, '/approve') && contains(github.event.issue.labels.*.name, 'lake-gate')
20+
21+
steps:
22+
- name: Checkout repository
23+
uses: actions/checkout@v4
24+
with:
25+
fetch-depth: 0
26+
token: ${{ secrets.GITHUB_TOKEN }}
27+
28+
- name: Disallow forks
29+
run: |
30+
set -euo pipefail
31+
PR_NUMBER="${{ github.event.issue.number }}"
32+
IS_CROSS=$(gh pr view "$PR_NUMBER" --json isCrossRepository --jq '.isCrossRepository')
33+
if [ "$IS_CROSS" = "true" ]; then
34+
gh pr comment "$PR_NUMBER" --body "❌ Cannot approve: fork-based PRs are not supported for lake-gate. Please open the PR from a branch in the main repository."
35+
exit 1
36+
fi
37+
env:
38+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39+
40+
- name: Check if user is authorized to approve
41+
run: |
42+
set -euo pipefail
43+
COMMENT_USER="${{ github.event.comment.user.login }}"
44+
echo "Checking authorization for user: $COMMENT_USER"
45+
46+
# Check if the user is in the approve-lake-gate alias in OWNERS_ALIASES file
47+
if yq eval '.aliases.approve-lake-gate[] | select(. == "'${COMMENT_USER}'")' OWNERS_ALIASES | grep -q "${COMMENT_USER}"; then
48+
echo "✅ User ${COMMENT_USER} is authorized to approve lake-gate PRs"
49+
else
50+
echo "❌ User ${COMMENT_USER} is not authorized to approve lake-gate PRs"
51+
52+
# Show available approvers for debugging
53+
echo "Available approve-lake-gate users:"
54+
yq eval '.aliases.approve-lake-gate[]' OWNERS_ALIASES || echo "No approve-lake-gate alias found"
55+
56+
gh pr comment "${{ github.event.issue.number }}" --body "❌ @${COMMENT_USER} is not authorized to approve lake-gate PRs. Only users listed in the approve-lake-gate alias in OWNERS_ALIASES file can approve."
57+
exit 1
58+
fi
59+
env:
60+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
61+
62+
- name: Configure Git
63+
run: |
64+
git config --global user.name "github-actions[bot]"
65+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
66+
67+
- name: Get PR details and perform fast-forward merge
68+
run: |
69+
set -euo pipefail
70+
PR_NUMBER="${{ github.event.issue.number }}"
71+
72+
# Get PR details
73+
PR_DATA=$(gh pr view "$PR_NUMBER" --json headRefName,baseRefName)
74+
HEAD_BRANCH=$(echo "$PR_DATA" | jq -r '.headRefName')
75+
BASE_BRANCH=$(echo "$PR_DATA" | jq -r '.baseRefName')
76+
77+
echo "PR #$PR_NUMBER details:"
78+
echo " Head branch: $HEAD_BRANCH"
79+
echo " Base branch: $BASE_BRANCH"
80+
81+
# Ensure we're working with the stable branch as base
82+
if [ "$BASE_BRANCH" != "stable" ]; then
83+
echo "Error: PR base branch is not 'stable'. Expected 'stable', got '$BASE_BRANCH'"
84+
exit 1
85+
fi
86+
87+
# Fetch all refs
88+
git fetch origin
89+
90+
# Switch to and update stable branch
91+
git switch stable
92+
git reset --hard origin/stable
93+
94+
# Switch to the temporary branch from the PR
95+
git switch "$HEAD_BRANCH"
96+
git reset --hard origin/"$HEAD_BRANCH"
97+
98+
# Get the commit that the temporary branch points to
99+
TEMP_BRANCH_COMMIT=$(git rev-parse HEAD)
100+
echo "Temporary branch commit: $TEMP_BRANCH_COMMIT"
101+
102+
# Switch back to stable and perform fast-forward merge
103+
git switch stable
104+
105+
# Check if we can fast-forward merge
106+
if git merge-base --is-ancestor stable "$TEMP_BRANCH_COMMIT"; then
107+
echo "Performing fast-forward merge of stable to $TEMP_BRANCH_COMMIT"
108+
git reset --hard "$TEMP_BRANCH_COMMIT"
109+
110+
# Push the updated stable branch
111+
git push origin stable
112+
113+
echo "✅ Successfully fast-forwarded stable branch to commit $TEMP_BRANCH_COMMIT"
114+
115+
# Wait a moment for GitHub to process the push
116+
sleep 2
117+
118+
# Check PR status and close if still open
119+
PR_STATE=$(gh pr view "$PR_NUMBER" --json state --jq '.state')
120+
if [ "$PR_STATE" = "OPEN" ]; then
121+
echo "PR is still open, closing it manually..."
122+
gh pr close "$PR_NUMBER" --comment "✅ Approved and merged! The stable branch has been fast-forwarded to point to the same commit as this temporary branch."
123+
else
124+
echo "PR was automatically closed by GitHub (state: $PR_STATE)"
125+
# Add a comment to the already-closed PR
126+
gh pr comment "$PR_NUMBER" --body "✅ Approved and merged! The stable branch has been fast-forwarded to point to the same commit as this temporary branch."
127+
fi
128+
129+
# Clean up the temporary branch after some delay
130+
sleep 5 # Brief pause to ensure PR operations complete
131+
echo "Cleaning up temporary branch: $HEAD_BRANCH"
132+
133+
# Guard: Only delete branches that match the expected lake-gate pattern
134+
if [[ "$HEAD_BRANCH" =~ ^lake-gate- ]]; then
135+
echo "Branch matches lake-gate pattern, proceeding with deletion..."
136+
git push origin --delete "$HEAD_BRANCH" || {
137+
echo "Warning: Failed to delete branch $HEAD_BRANCH (it may have already been deleted)"
138+
}
139+
else
140+
echo "Warning: Branch '$HEAD_BRANCH' does not match expected lake-gate pattern (lake-gate-*). Skipping deletion to prevent accidental removal of non-lake-gate branches."
141+
fi
142+
143+
else
144+
echo "Error: Cannot fast-forward merge. The stable branch is not an ancestor of the temporary branch commit."
145+
gh pr comment "$PR_NUMBER" --body "❌ Cannot approve: Fast-forward merge is not possible. The stable branch is not an ancestor of the temporary branch. Please rebase or recreate the PR."
146+
exit 1
147+
fi
148+
env:
149+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
150+
151+
- name: Summary
152+
run: |
153+
echo "✅ Lake-gate PR approved and merged successfully via fast-forward merge."
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
# This workflow automatically creates a temporary branch from main to sync with stable branch, for image related changes creates a PR with "lake-gate" label for verification
2+
# It runs every 4 hours and can be triggered manually
3+
#
4+
# This workflow uses the built-in GITHUB_TOKEN with the required permissions set below.
5+
6+
name: Sync Main to Stable
7+
8+
on:
9+
schedule:
10+
# Run every 4 hours
11+
- cron: '0 */4 * * *'
12+
workflow_dispatch:
13+
14+
permissions:
15+
contents: write
16+
pull-requests: write
17+
18+
# Configuration: PR Reviewers
19+
# To modify who gets requested to review sync PRs, update the list below with comma-separated or space-separated GitHub usernames
20+
env:
21+
PR_REVIEWERS: "sutaakar"
22+
23+
jobs:
24+
lake-gate:
25+
runs-on: ubuntu-latest
26+
27+
steps:
28+
- name: Checkout repository
29+
uses: actions/checkout@v4
30+
with:
31+
fetch-depth: 0
32+
token: ${{ secrets.GITHUB_TOKEN }}
33+
34+
- name: Configure Git
35+
run: |
36+
git config --global user.name "github-actions[bot]"
37+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
38+
39+
- name: Check for differences between main and stable
40+
id: check-diff
41+
run: |
42+
set -euo pipefail
43+
# Ensure we have the latest refs
44+
git fetch origin main
45+
git fetch origin stable || {
46+
echo "Error: stable branch doesn't exist. Please create the stable branch first."
47+
exit 1
48+
}
49+
50+
# Update local refs to match remote
51+
git checkout main
52+
git reset --hard origin/main
53+
git checkout -B stable origin/stable
54+
55+
# Get commits that are in main but not in stable
56+
NEW_COMMITS=$(git rev-list stable..main --reverse)
57+
58+
if [ -z "$NEW_COMMITS" ]; then
59+
echo "No new commits to cherry-pick"
60+
echo "new_commits=" >> $GITHUB_OUTPUT
61+
else
62+
echo "Found new commits to cherry-pick:"
63+
echo "$NEW_COMMITS"
64+
# Store commits as a single line with spaces
65+
COMMITS_LINE=$(echo "$NEW_COMMITS" | tr '\n' ' ' | sed 's/ $//')
66+
echo "new_commits=$COMMITS_LINE" >> $GITHUB_OUTPUT
67+
68+
# Get the latest commit hash for PR title
69+
LATEST_COMMIT=$(echo "$NEW_COMMITS" | tail -n1)
70+
LATEST_COMMIT_SHORT=$(git rev-parse --short "$LATEST_COMMIT")
71+
LATEST_COMMIT_MSG=$(git log --format=%s -n 1 "$LATEST_COMMIT")
72+
echo "latest_commit=$LATEST_COMMIT" >> $GITHUB_OUTPUT
73+
echo "latest_commit_short=$LATEST_COMMIT_SHORT" >> $GITHUB_OUTPUT
74+
echo "latest_commit_msg=$LATEST_COMMIT_MSG" >> $GITHUB_OUTPUT
75+
fi
76+
77+
- name: Check for existing sync PRs
78+
id: check-existing-pr
79+
if: steps.check-diff.outputs.new_commits != ''
80+
run: |
81+
set -euo pipefail
82+
# Look for existing PRs with the lake-gate label
83+
EXISTING_PR_DATA=$(gh pr list --base stable --label "lake-gate" --state open --json number,headRefName,title | jq -r '.[0] // empty')
84+
85+
if [ -n "$EXISTING_PR_DATA" ] && [ "$EXISTING_PR_DATA" != "null" ]; then
86+
EXISTING_PR_NUMBER=$(echo "$EXISTING_PR_DATA" | jq -r '.number')
87+
EXISTING_PR_BRANCH=$(echo "$EXISTING_PR_DATA" | jq -r '.headRefName')
88+
echo "Found existing PR: #$EXISTING_PR_NUMBER (branch: $EXISTING_PR_BRANCH)"
89+
echo "existing_pr_number=$EXISTING_PR_NUMBER" >> $GITHUB_OUTPUT
90+
echo "existing_pr_branch=$EXISTING_PR_BRANCH" >> $GITHUB_OUTPUT
91+
92+
# Check if the existing PR already contains the latest commit
93+
PR_TITLE=$(echo "$EXISTING_PR_DATA" | jq -r '.title')
94+
LATEST_COMMIT_SHORT="${{ steps.check-diff.outputs.latest_commit_short }}"
95+
96+
if echo "$PR_TITLE" | grep -q "$LATEST_COMMIT_SHORT"; then
97+
echo "Existing PR already contains the latest commit"
98+
echo "pr_up_to_date=true" >> $GITHUB_OUTPUT
99+
else
100+
echo "Existing PR is outdated"
101+
echo "pr_up_to_date=false" >> $GITHUB_OUTPUT
102+
fi
103+
else
104+
echo "No existing cherry-pick PR found"
105+
echo "existing_pr_number=" >> $GITHUB_OUTPUT
106+
echo "existing_pr_branch=" >> $GITHUB_OUTPUT
107+
echo "pr_up_to_date=false" >> $GITHUB_OUTPUT
108+
fi
109+
env:
110+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
111+
112+
- name: Close outdated PR and delete branch
113+
if: steps.check-diff.outputs.new_commits != '' && steps.check-existing-pr.outputs.existing_pr_number != '' && steps.check-existing-pr.outputs.pr_up_to_date == 'false'
114+
run: |
115+
set -euo pipefail
116+
PR_NUMBER="${{ steps.check-existing-pr.outputs.existing_pr_number }}"
117+
BRANCH_NAME="${{ steps.check-existing-pr.outputs.existing_pr_branch }}"
118+
119+
# Safety check: Verify the branch matches automation pattern before closing/deleting
120+
if [[ "$BRANCH_NAME" =~ ^lake-gate- ]]; then
121+
echo "Branch matches automation pattern (lake-gate-*), proceeding with closure and deletion..."
122+
echo "Closing outdated PR #$PR_NUMBER (branch: $BRANCH_NAME)"
123+
gh pr close "$PR_NUMBER" --comment "Closing this PR as newer commits are available. A new PR will be created automatically."
124+
125+
sleep 5 # Brief pause to ensure PR operations complete
126+
127+
# Delete the temporary branch
128+
if [ -n "$BRANCH_NAME" ]; then
129+
echo "Deleting temporary branch: $BRANCH_NAME"
130+
git push origin --delete "$BRANCH_NAME" || {
131+
echo "Warning: Failed to delete branch $BRANCH_NAME (it may have already been deleted)"
132+
}
133+
else
134+
echo "Warning: No branch name found for PR #$PR_NUMBER"
135+
fi
136+
else
137+
echo "Warning: Branch '$BRANCH_NAME' does not match expected automation pattern (lake-gate-*). Skipping closure and deletion to prevent impacting user branches."
138+
echo "PR #$PR_NUMBER will remain open. Manual intervention may be required."
139+
fi
140+
env:
141+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
142+
143+
- name: Create sync branch and PR
144+
if: steps.check-diff.outputs.new_commits != '' && steps.check-existing-pr.outputs.pr_up_to_date == 'false'
145+
run: |
146+
set -euo pipefail
147+
# Create a unique branch name with timestamp
148+
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
149+
BRANCH_NAME="lake-gate-$TIMESTAMP"
150+
151+
echo "Creating branch: $BRANCH_NAME from main"
152+
git checkout main
153+
git checkout -b "$BRANCH_NAME"
154+
155+
# Push the branch
156+
git push origin "$BRANCH_NAME"
157+
158+
# Prepare PR body
159+
COMMITS="${{ steps.check-diff.outputs.new_commits }}"
160+
LATEST_COMMIT="${{ steps.check-diff.outputs.latest_commit }}"
161+
LATEST_COMMIT_SHORT="${{ steps.check-diff.outputs.latest_commit_short }}"
162+
COMMIT_COUNT=$(echo "$COMMITS" | wc -w)
163+
164+
# Build PR body using here document
165+
PR_BODY=$(cat << EOF
166+
## Automated Sync from main to stable
167+
168+
This PR automatically syncs the \`main\` branch to the \`stable\` branch by creating a temporary branch directly from main.
169+
170+
**Latest commit:** $LATEST_COMMIT_SHORT - ${{ steps.check-diff.outputs.latest_commit_msg }}
171+
**Total commits to sync:** $COMMIT_COUNT
172+
173+
## ⚠️ IMPORTANT: How to Merge
174+
175+
**🚫 DO NOT use the GitHub merge button!**
176+
177+
Once all checks are complete, comment **\`/approve\`** on this PR to automatically fast-forward merge the stable branch to point to the same commit as this temporary branch.
178+
179+
**Only use the \`/approve\` command - the GitHub merge button will not work correctly for this automated sync process.**
180+
181+
## Pre-merge Checklist
182+
183+
Before approving this PR, please ensure the following tasks are completed:
184+
185+
- [ ] **PR checks**: PR checks passed
186+
- [ ] **E2E tests**: Smoke and sanity tests passed against cluster with training operator image built from this PR
187+
188+
### Commits to be synced:
189+
EOF
190+
)
191+
192+
for commit in $COMMITS; do
193+
COMMIT_SHORT=$(git rev-parse --short "$commit")
194+
COMMIT_MSG=$(git log --format=%s -n 1 "$commit")
195+
PR_BODY="$PR_BODY"$'\n'"- $COMMIT_SHORT: $COMMIT_MSG"
196+
done
197+
198+
PR_BODY="$PR_BODY"$'\n\n'"---"$'\n'"*This PR was created automatically by the sync workflow.*"
199+
200+
# Create the PR
201+
PR_TITLE="Auto sync main to stable (up to $LATEST_COMMIT_SHORT)"
202+
203+
# Normalize PR_REVIEWERS by replacing commas with spaces and create reviewer flags
204+
REVIEWER_FLAGS=""
205+
NORMALIZED_REVIEWERS=$(echo "$PR_REVIEWERS" | tr ',' ' ')
206+
for reviewer in $NORMALIZED_REVIEWERS; do
207+
if [ -n "$reviewer" ]; then
208+
REVIEWER_FLAGS="$REVIEWER_FLAGS --reviewer $reviewer"
209+
fi
210+
done
211+
212+
gh pr create \
213+
--title "$PR_TITLE" \
214+
--body "$PR_BODY" \
215+
--base stable \
216+
--head "$BRANCH_NAME" \
217+
--label "lake-gate" \
218+
$REVIEWER_FLAGS \
219+
--draft
220+
221+
echo "Created PR: $PR_TITLE"
222+
env:
223+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
224+
225+
- name: Summary
226+
run: |
227+
if [ -z "${{ steps.check-diff.outputs.new_commits }}" ]; then
228+
echo "✅ No new commits to sync. Stable branch is up to date."
229+
elif [ "${{ steps.check-existing-pr.outputs.pr_up_to_date }}" == "true" ]; then
230+
echo "✅ Existing PR already contains the latest commits. No action needed."
231+
else
232+
echo "✅ Sync process completed. New PR created or existing PR updated."
233+
fi

OWNERS_ALIASES

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
aliases:
2+
approve-lake-gate:
3+
- abhijeet-dhumal
4+
- ChughShilpa
5+
- efazal
6+
- kapil27
7+
- sutaakar

0 commit comments

Comments
 (0)