Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions .github/workflows/check-source-branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: Check PR Branch Origin

on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
check-branch-origin:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Fetch all branches
run: |
git fetch origin
git fetch --all
git branch -r

- name: Check if branch was created from starlight-dev
run: |
PR_BRANCH="${{ github.head_ref }}"
HEAD_REPO="${{ github.event.pull_request.head.repo.full_name }}"
BASE_REPO="${{ github.event.pull_request.base.repo.full_name }}"

echo "PR Branch: $PR_BRANCH"
echo "Head repository: $HEAD_REPO"
echo "Base repository: $BASE_REPO"

if [ "$HEAD_REPO" != "$BASE_REPO" ]; then
echo "Adding fork as remote..."
git remote add fork https://github.com/$HEAD_REPO.git
git fetch fork
FORK_BRANCH="fork/$PR_BRANCH"
else
FORK_BRANCH="origin/$PR_BRANCH"
fi

if ! git show-ref --verify --quiet refs/remotes/origin/starlight-dev; then
echo "Branch 'starlight-dev' not found in origin"
exit 1
fi

if ! git show-ref --verify --quiet refs/remotes/$FORK_BRANCH 2>/dev/null && ! git show-ref --verify --quiet refs/remotes/origin/$PR_BRANCH 2>/dev/null; then
echo "Branch '$PR_BRANCH' not found"
echo "Available branches:"
git branch -r
exit 1
fi

if git show-ref --verify --quiet refs/remotes/$FORK_BRANCH 2>/dev/null; then
BRANCH_REF=$FORK_BRANCH
else
BRANCH_REF="origin/$PR_BRANCH"
fi

echo "Using branch reference: $BRANCH_REF"

if ! BASE_COMMIT=$(git merge-base origin/starlight-dev $BRANCH_REF 2>/dev/null); then
echo "Could not find merge base between 'starlight-dev' and '$PR_BRANCH'"
exit 1
fi

echo "Base commit: $BASE_COMMIT"

if git merge-base --is-ancestor $BASE_COMMIT origin/starlight-dev; then
echo "Branch '$PR_BRANCH' was correctly created from 'starlight-dev'"
else
echo "Branch '$PR_BRANCH' was NOT created from 'starlight-dev'"
exit 1
fi

- name: Add comment on failure
if: failure()
uses: actions/github-script@v7
with:
script: |
const body = `**Branch Origin Check Failed**

This pull request branch was not created from the \`starlight-dev\` branch. Please fix this before the PR can be merged.

**How to fix:**

**Rebase current branch:**
\`\`\`bash
git checkout your-branch-name
git rebase origin/starlight-dev
git push --force-with-lease
\`\`\`
`;

Loading