Skip to content

Instantly share code, notes, and snippets.

@Trung-DV
Last active August 1, 2023 15:06
Show Gist options
  • Save Trung-DV/4e6a8f67c03c7236833c9860da725eca to your computer and use it in GitHub Desktop.
Save Trung-DV/4e6a8f67c03c7236833c9860da725eca to your computer and use it in GitHub Desktop.
Push empty commit to a PR
FROM alpine
LABEL authors="Trung-DV"
WORKDIR /pr
RUN apk add --no-cache git github-cli
COPY ./entrypoint.sh entrypoint.sh
ENTRYPOINT ["./entrypoint.sh"]
#!/bin/sh
# Check whether GITHUB_TOKEN is set via environment
if [ -n "$GITHUB_TOKEN" ]; then
# Check if the command line argument for PR_URL is provided
if [ -z "$1" ]; then
echo "Error: PR_URL is not set. Please provide a pull request URL as the first argument."
exit 1
fi
# Extract repository and PR number from PR_URL
REPO=$(echo "$1" | cut -d'/' -f4,5)
PR_URL=$1
else
# Check whether the necessary arguments are provided to the program
if [ -z "$1" ]; then
echo "Error: GITHUB_TOKEN is not set. Please provide a GitHub token as the first argument."
exit 1
elif [ -z "$2" ]; then
echo "Error: PR_URL is not set. Please provide a pull request URL as the second argument."
exit 1
fi
# Use the first argument as the GITHUB_TOKEN
GITHUB_TOKEN=$1
# Use the second argument as the PR_URL
PR_URL=$2
# Extract repository and PR number from PR_URL
REPO=$(echo "$2" | cut -d'/' -f4,5)
fi
# Authenticate gh
echo "$GITHUB_TOKEN" | gh auth login --with-token > /dev/null 2>&1
# Check if 'gh auth login' was successful by checking the authentication status
gh auth status
# Get GitHub username
GIT_USER=$(gh api user --jq .login)
# Get GitHub noreply email
GIT_EMAIL=$(gh api user/public_emails --jq '.[].email | select(endswith("@users.noreply.github.com"))')
BRANCH=$(gh pr view $PR_URL --json headRefName -q .headRefName)
# Clone the repo using 'gh repo clone'
gh repo clone $REPO -- --single-branch --branch $BRANCH
cd $(echo $REPO | cut -d'/' -f2)
echo "Current directory: $(pwd), branch: $(git branch --show-current)"
# Configure git's user for the local repo
git config user.name "$GIT_USER"
git config user.email "$GIT_EMAIL"
# Set Git to use the 'gh' cli for authentication with GitHub
git config --global credential.helper '!gh auth git-credential'
# Step 1: Create an empty commit
git commit --allow-empty -m "Trigger"
# Step 2: Push the commit to the remote repository
git push origin $BRANCH
gh pr review --approve
## Build and run docker image
docker build . -t trigger-pr
docker run --rm -e GITHUB_TOKEN=$GITHUB_TOKEN trigger-pr $PR_URL
## Run from docker image
docker run --rm -e GITHUB_TOKEN=$GITHUB_TOKEN_TS ghcr.io/trung-dv/trigger-pr $1
## Wrap as a function
trigger-pr() {
docker run --rm -e GITHUB_TOKEN=$GITHUB_TOKEN_TS ghcr.io/trung-dv/trigger-pr $1
}
trigger-pr https://github.com/redis/redis/pull/12453
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment