-
Notifications
You must be signed in to change notification settings - Fork 2k
CRE-1827: detect potential merge conflicts #21255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2a5855c
cre-1827: detect potential merge conflicts
mchain0 ea13cd7
cre-1827: use gh api for comment
mchain0 1d97ae5
cre-1827: another comment attempt
mchain0 7dce7fd
cre-1827: logic improved
mchain0 0fd815a
cre-1827: optimized for overlapping files only
mchain0 662691a
cre-1827: review improvements
mchain0 36b9e1e
cre-1827: review improvement
mchain0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| name: Detect conflicting PRs | ||
| description: Detects PRs that would conflict with the current PR and posts a comment | ||
|
|
||
| inputs: | ||
| github-token: | ||
| description: GitHub token with pull-requests write and contents read permissions | ||
| required: true | ||
| repository: | ||
| description: Repository in owner/repo format | ||
| required: true | ||
| pr-number: | ||
| description: Current PR number | ||
| required: true | ||
| head-ref: | ||
| description: Head branch of the current PR | ||
| required: true | ||
| base-ref: | ||
| description: Base branch of the current PR | ||
| required: true | ||
|
|
||
| runs: | ||
| using: composite | ||
| steps: | ||
| - name: Fetch base and current branch | ||
| shell: bash | ||
| run: | | ||
| git fetch origin ${{ inputs.base-ref }} ${{ inputs.head-ref }} | ||
|
|
||
| - name: Detect conflicts and update comment | ||
| shell: bash | ||
| env: | ||
| GH_TOKEN: ${{ inputs.github-token }} | ||
| REPO: ${{ inputs.repository }} | ||
| CURRENT_PR: ${{ inputs.pr-number }} | ||
| CURRENT_BRANCH: ${{ inputs.head-ref }} | ||
| BASE_BRANCH: ${{ inputs.base-ref }} | ||
| run: | | ||
| set -e | ||
|
|
||
| MARKER="<!-- conflict-check -->" | ||
|
|
||
| # Get files changed in current PR | ||
| current_files=$(gh api --method GET --paginate "repos/${REPO}/pulls/${CURRENT_PR}/files" --jq '.[].filename' | sort -u) | ||
| current_files_count=$(echo "$current_files" | wc -l) | ||
|
mchain0 marked this conversation as resolved.
Outdated
|
||
| echo "Current PR touches $current_files_count files" | ||
|
|
||
| # Get all open PRs targeting same base (paginated) | ||
| prs=$(gh api --method GET --paginate "repos/${REPO}/pulls?state=open&base=${BASE_BRANCH}" --jq '.[].number') | ||
|
mchain0 marked this conversation as resolved.
Outdated
|
||
| total_prs=$(echo "$prs" | wc -w) | ||
|
mchain0 marked this conversation as resolved.
Outdated
|
||
| echo "Found $total_prs open PRs targeting ${BASE_BRANCH}" | ||
|
|
||
| # Find PRs with overlapping files (fast API check) | ||
| candidates=() | ||
| for pr in $prs; do | ||
| [ "$pr" = "$CURRENT_PR" ] && continue | ||
|
|
||
| other_files=$(gh api --method GET --paginate "repos/${REPO}/pulls/${pr}/files" --jq '.[].filename' | sort -u) | ||
|
|
||
| # Check for file intersection | ||
| if comm -12 <(echo "$current_files") <(echo "$other_files") | grep -q .; then | ||
| candidates+=("$pr") | ||
| fi | ||
| done | ||
|
|
||
| echo "Found ${#candidates[@]} PRs with overlapping files, checking for conflicts..." | ||
|
|
||
| if [ ${#candidates[@]} -eq 0 ]; then | ||
| conflicts=() | ||
| else | ||
| # Fetch only the PR refs we need | ||
| fetch_refs="" | ||
| for pr in "${candidates[@]}"; do | ||
| fetch_refs="$fetch_refs +refs/pull/${pr}/head:refs/remotes/origin/pr/${pr}" | ||
| done | ||
| git fetch origin $fetch_refs | ||
|
|
||
| mkdir -p worktrees | ||
| conflicts=() | ||
|
|
||
| for pr in "${candidates[@]}"; do | ||
| other_branch="origin/pr/$pr" | ||
| dir="worktrees/$pr" | ||
|
|
||
| git worktree add -q --detach "$dir" "origin/${BASE_BRANCH}" | ||
|
|
||
| if git -C "$dir" merge --no-commit --no-ff "$other_branch" >/dev/null 2>&1; then | ||
| git -C "$dir" -c user.email="ci@local" -c user.name="CI" commit --no-edit -m "temp" >/dev/null 2>&1 | ||
|
|
||
| if ! git -C "$dir" merge --no-commit --no-ff "origin/${CURRENT_BRANCH}" >/dev/null 2>&1; then | ||
| conflicts+=("#$pr") | ||
| fi | ||
| fi | ||
|
|
||
| git worktree remove -f "$dir" | ||
| done | ||
| fi | ||
|
|
||
| echo "Found ${#conflicts[@]} conflicting PRs" | ||
|
|
||
| if [ ${#conflicts[@]} -eq 0 ]; then | ||
| BODY="${MARKER} | ||
| ✅ No conflicts with other open PRs targeting \`${BASE_BRANCH}\`" | ||
| else | ||
| BODY="${MARKER} | ||
| ❌ Conflicts with: | ||
|
|
||
| $(printf '%s\n' "${conflicts[@]}")" | ||
| fi | ||
|
|
||
| COMMENT_ID=$(gh api --method GET "repos/${REPO}/issues/${CURRENT_PR}/comments" \ | ||
| --jq ".[] | select(.body | contains(\"${MARKER}\")) | .id" \ | ||
| | head -n1) | ||
|
|
||
| if [ -n "$COMMENT_ID" ]; then | ||
| gh api --method PATCH "repos/${REPO}/issues/comments/${COMMENT_ID}" \ | ||
| -f body="$BODY" | ||
| else | ||
| gh api --method POST "repos/${REPO}/issues/${CURRENT_PR}/comments" \ | ||
| -f body="$BODY" | ||
| fi | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| name: Detect PR Conflicts | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened, synchronize, reopened] | ||
|
|
||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.event.pull_request.number }} | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| detect-conflicts: | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| pull-requests: write | ||
| contents: read | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
|
mchain0 marked this conversation as resolved.
Outdated
|
||
|
|
||
| - uses: ./.github/actions/prconflicts | ||
| with: | ||
| github-token: ${{ github.token }} | ||
| repository: ${{ github.repository }} | ||
| pr-number: ${{ github.event.pull_request.number }} | ||
| head-ref: ${{ github.head_ref }} | ||
| base-ref: ${{ github.base_ref }} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.