#!/bin/bash
# Set the origin and UPSTREAM repository URLs
REPO_OWNER=$1
SFOPS_REPO=$2
UPSTREAM_REPO_URL=$3
REPO_URL=$REPO_OWNER/$SFOPS_REPO
MAIN_BRANCH="main"
SPECIFIC_TAG=$4
# Temporary directory for the clone
TEMP_DIR=$(mktemp -d)
echo "Starting the PR creation job..."
echo "Repository URL: $REPO_URL"
echo "sfops Repo Owner: $REPO_OWNER"
echo "Temporary Directory: $TEMP_DIR"
# Clone the repository into the temp directory
echo "Cloning the repository..."
gh auth setup-git
gh repo clone $REPO_URL $TEMP_DIR
# Navigate to the temp directory
cd $TEMP_DIR
# Set up the GitHub bot user
git config user.email "sfops-bot+bot@users.noreply.github.com"
git config user.name "sfops-bot"
# Add the UPSTREAM repository as a remote
git remote add UPSTREAM "$UPSTREAM_REPO_URL"
# Fetch tags from the UPSTREAM repository
git fetch UPSTREAM --tags
echo "Specific tag in the UPSTREAM repository is $SPECIFIC_TAG"
echo "Specific tag in the UPSTREAM repository is $SPECIFIC_TAG"
# Check if the specific tag exists in the UPSTREAM repository
if git ls-remote --tags UPSTREAM "$SPECIFIC_TAG" | grep -q "$SPECIFIC_TAG"; then
# Fetch the specific tag from the UPSTREAM repository
git fetch UPSTREAM refs/tags/"$SPECIFIC_TAG":refs/tags/"$SPECIFIC_TAG"
# Create a new branch from the tag and switch to it
git checkout -b pr-"$SPECIFIC_TAG" refs/tags/"$SPECIFIC_TAG"
# Replace version.txt and CHANGELOG.md files from the main branch
git checkout origin/$MAIN_BRANCH -- version.txt CHANGELOG.md
# Check if there are any changes to commit
if git diff --quiet HEAD; then
echo "No changes detected in version.txt and CHANGELOG.md compared to the main branch."
else
# Replace version.txt and CHANGELOG.md files from the main branch
git checkout origin/$MAIN_BRANCH -- version.txt CHANGELOG.md
# Commit the changes
git commit -m "Update version.txt and CHANGELOG.md from main branch"
fi
# Merge the main branch into the PR branch
if git merge -X ours origin/$MAIN_BRANCH; then
echo "Merge successful. No conflicts detected."
else
echo "Merge failed. Conflicts detected."
# Overwrite conflicting files with the versions from the tag branch
git checkout --ours -- .
git add .
git commit -m "Resolve conflicts by overwriting with tag branch"
fi
# Push the new branch to the origin
git push origin pr-"$SPECIFIC_TAG"
# Create a PR using GitHub CLI
gh pr create --title "feat(upstream): update to upstream tag $SPECIFIC_TAG" --body "This PR updates the repository to the latest tag $SPECIFIC_TAG, favoring files from the source.flxbl.io repository and including version.txt and CHANGELOG.md from the main branch." --head pr-"$SPECIFIC_TAG"
else
echo "Tag $SPECIFIC_TAG does not exist in the UPSTREAM repository. Skipping branch creation and PR creation."
fi
# Clean up: change out of the temp directory and remove it
cd ..
rm -rf "$TEMP_DIR"