Created
January 26, 2024 08:17
-
-
Save kkarpieszuk/ab80774b851bcb113202526cbb0ce435 to your computer and use it in GitHub Desktop.
Husky script to check if the branch has conflicts with develop branch, only in gulp generated files.
This file contains 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
#!/bin/bash | |
# Generated by ChatGPT, must be reviewed | |
# Configuration | |
GITHUB_TOKEN="your_access_token" | |
REPO_OWNER="username_or_organization" | |
REPO_NAME="repository_name" | |
LABEL="has-conflicts" | |
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) | |
# Fetch the latest changes from the develop branch | |
git fetch origin develop | |
# Check for conflicts with the develop branch | |
if git merge --no-commit --no-ff origin/develop; then | |
echo "No conflicts with the develop branch." | |
git merge --abort | |
exit 0 | |
fi | |
# Get the list of files in conflict | |
CONFLICT_FILES=$(git diff --name-only --diff-filter=U) | |
# Check if all conflicts are only in .css and .min.js files | |
if echo "$CONFLICT_FILES" | grep -E -vq '\.(css|min\.js)$'; then | |
echo "Conflicts also in files other than .css and .min.js." | |
git merge --abort | |
exit 0 | |
fi | |
# Find the Pull Request associated with the current branch | |
PR_NUMBER=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ | |
"https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/pulls?state=open&head=$REPO_OWNER:$CURRENT_BRANCH" \ | |
| jq -r '.[0].number') | |
# Check if PR is found | |
if [ -z "$PR_NUMBER" ] || [ "$PR_NUMBER" = "null" ]; then | |
echo "No PR found for branch $CURRENT_BRANCH" | |
git merge --abort | |
exit 1 | |
fi | |
# Add label to the PR | |
curl -s -X POST -H "Authorization: token $GITHUB_TOKEN" \ | |
-H "Content-Type: application/json" \ | |
-d "{\"labels\": [\"$LABEL\"]}" \ | |
"https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/issues/$PR_NUMBER/labels" | |
# Revert to the state before the merge attempt | |
git merge --abort |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment