forked from wylab/wylab-station-14
67 lines
2.5 KiB
YAML
67 lines
2.5 KiB
YAML
name: Check Merge Conflicts
|
|
|
|
on:
|
|
pull_request_target:
|
|
types:
|
|
- opened
|
|
- synchronize
|
|
- reopened
|
|
- ready_for_review
|
|
|
|
jobs:
|
|
check-conflicts:
|
|
if: github.event.pull_request.draft == false && github.actor != 'IanComradeBot'
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Check mergeable status and label
|
|
env:
|
|
API_TOKEN: ${{ secrets.API_TOKEN }}
|
|
run: |
|
|
PR_INDEX=${{ github.event.pull_request.number }}
|
|
REPO_OWNER=${{ github.repository_owner }}
|
|
REPO_NAME=${{ github.event.repository.name }}
|
|
API_URL="${{ github.server_url }}/api/v1"
|
|
|
|
# Get PR mergeable status
|
|
PR_DATA=$(curl -s -H "Authorization: token $API_TOKEN" \
|
|
"$API_URL/repos/$REPO_OWNER/$REPO_NAME/pulls/$PR_INDEX")
|
|
MERGEABLE=$(echo "$PR_DATA" | jq -r '.mergeable')
|
|
|
|
echo "PR #$PR_INDEX mergeable status: $MERGEABLE"
|
|
|
|
LABEL_NAME="S: Merge Conflict"
|
|
|
|
if [ "$MERGEABLE" = "false" ]; then
|
|
echo "PR has merge conflicts, adding label and comment..."
|
|
|
|
# Add label
|
|
curl -s -X POST -H "Authorization: token $API_TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
"$API_URL/repos/$REPO_OWNER/$REPO_NAME/issues/$PR_INDEX/labels" \
|
|
-d "{\"labels\":[\"$LABEL_NAME\"]}"
|
|
|
|
# Add comment
|
|
curl -s -X POST -H "Authorization: token $API_TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
"$API_URL/repos/$REPO_OWNER/$REPO_NAME/issues/$PR_INDEX/comments" \
|
|
-d '{"body":"This pull request has conflicts, please resolve those before we can evaluate the pull request."}'
|
|
|
|
echo "Label and comment added."
|
|
else
|
|
echo "PR is mergeable, no conflicts detected."
|
|
|
|
# Check if label exists and remove it
|
|
LABELS=$(curl -s -H "Authorization: token $API_TOKEN" \
|
|
"$API_URL/repos/$REPO_OWNER/$REPO_NAME/issues/$PR_INDEX/labels")
|
|
HAS_LABEL=$(echo "$LABELS" | jq -r ".[] | select(.name == \"$LABEL_NAME\") | .id")
|
|
|
|
if [ -n "$HAS_LABEL" ]; then
|
|
echo "Removing stale conflict label..."
|
|
# URL-encode the label name (handles spaces, colons, etc.)
|
|
LABEL_NAME_ENCODED=$(echo "$LABEL_NAME" | jq -rR @uri)
|
|
curl -s -X DELETE -H "Authorization: token $API_TOKEN" \
|
|
"$API_URL/repos/$REPO_OWNER/$REPO_NAME/issues/$PR_INDEX/labels/$LABEL_NAME_ENCODED"
|
|
echo "Conflict label removed."
|
|
fi
|
|
fi
|