Skip to content

Commit 5c95b06

Browse files
committed
fix: target branch detection in database-schema action
1 parent d515949 commit 5c95b06

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

.github/workflows/database-schema.yml

+29-4
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,29 @@ jobs:
102102
cp database/schema/mysql-schema-new.sql database/schema/mysql-schema.sql
103103
fi
104104
105+
- name: Determine target branch
106+
id: target-branch
107+
run: |
108+
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
109+
# If this is a PR, use the base branch of the PR
110+
echo "branch=${{ github.base_ref }}" >> $GITHUB_OUTPUT
111+
else
112+
# For push events, default to development
113+
echo "branch=development" >> $GITHUB_OUTPUT
114+
fi
115+
105116
- name: Check for existing PRs
106117
id: check-pr
107118
if: steps.diff.outputs.has_changes == 'true'
108119
run: |
109-
PR_EXISTS=$(gh pr list --base development --head "update-schema-dump*" --json number --jq 'length')
120+
# First check if there's an existing open PR with any update-schema-dump branch
121+
PR_EXISTS=$(gh pr list --state open --head "update-schema-dump" --json number --jq 'length')
110122
if [ "$PR_EXISTS" -gt "0" ]; then
111123
echo "existing_pr=true" >> $GITHUB_OUTPUT
112-
PR_NUMBER=$(gh pr list --base development --head "update-schema-dump*" --json number --jq '.[0].number')
113-
BRANCH_NAME=$(gh pr view $PR_NUMBER --json headRefName --jq '.headRefName')
124+
PR_NUMBER=$(gh pr list --state open --head "update-schema-dump" --json number,headRefName --jq '.[0].number')
125+
BRANCH_NAME=$(gh pr list --state open --head "update-schema-dump" --json headRefName --jq '.[0].headRefName')
114126
echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT
127+
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
115128
else
116129
echo "existing_pr=false" >> $GITHUB_OUTPUT
117130
fi
@@ -131,6 +144,18 @@ jobs:
131144
env:
132145
GH_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
133146

147+
- name: Update existing PR description
148+
if: steps.diff.outputs.has_changes == 'true' && steps.check-pr.outputs.existing_pr == 'true'
149+
run: |
150+
gh pr edit ${{ steps.check-pr.outputs.pr_number }} --body "This PR updates the MySQL schema dump with the latest migration changes.
151+
152+
Related to commit: ${{ steps.find-commit.outputs.commit_id }}
153+
Commit message: ${{ steps.find-commit.outputs.commit_msg }}
154+
155+
[View commit](https://github.com/HDInnovations/UNIT3D/commit/${{ steps.find-commit.outputs.commit_id }})"
156+
env:
157+
GH_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
158+
134159
- name: Create new PR with schema changes
135160
if: steps.diff.outputs.has_changes == 'true' && steps.check-pr.outputs.existing_pr == 'false'
136161
uses: peter-evans/create-pull-request@v5
@@ -148,7 +173,7 @@ jobs:
148173
branch: update-schema-dump
149174
branch-suffix: timestamp
150175
delete-branch: true
151-
base: development
176+
base: ${{ steps.target-branch.outputs.branch }}
152177
path: .
153178
add-paths: |
154179
database/schema/mysql-schema.sql

0 commit comments

Comments
 (0)