Skip to content

Add workflow to detect backport conflicts before merging #3

Add workflow to detect backport conflicts before merging

Add workflow to detect backport conflicts before merging #3

Workflow file for this run

name: Backport Conflict Check
on:
pull_request:
types: [labeled, synchronize]
jobs:
check-backport:
runs-on: ubuntu-latest
steps:
- name: Extract backport label
id: extract
run: |
echo 'Extracting backport label...'
version=$(jq -r '
.pull_request.labels // []
| map(.name // empty)
| map(select(startswith("backport ")))
| first // ""
| sub("^backport "; "")
' "$GITHUB_EVENT_PATH")
if [[ -z "$version" || "$version" == "null" ]]; then
echo "No backport label found. Skipping."
echo "version=" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "Backport version: '$version'"
echo "version=$version" >> "$GITHUB_OUTPUT"
- name: Checkout backport target branch (from upstream)
if: ${{ steps.extract.outputs.version != '' }}
run: |
git init .
git remote add upstream https://github.com/opensearch-project/OpenSearch.git
git fetch upstream ${{ steps.extract.outputs.version }}
git checkout -b backport-target FETCH_HEAD
git remote add prrepo https://github.com/${{ github.event.pull_request.head.repo.full_name }}.git
git fetch prrepo ${{ github.event.pull_request.head.ref }}
- name: Set git identity
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
- name: Attempt cherry-pick of PR commits
if: ${{ steps.extract.outputs.version != '' }}
run: |
set -o pipefail
{
git cherry-pick ${{ github.event.pull_request.head.sha }}
} &> err.log || {
cat err.log > conflict.txt
exit 1
}
- name: Report conflicts as PR comment
if: ${{ steps.extract.outputs.version != '' && failure() }}
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
let conflictMsg = "⚠️ Backport conflict detected for branch `${{ steps.extract.outputs.version }}`\n";
if (fs.existsSync('conflict.txt')) {
const conflicts = fs.readFileSync('conflict.txt', 'utf8');
conflictMsg += "```\n" + conflicts + "\n```";
}
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: conflictMsg
});