Skip to content

Instantly share code, notes, and snippets.

@enisozgen
Created January 26, 2020 16:21
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save enisozgen/52de748cd72c9686fab44709728fa06f to your computer and use it in GitHub Desktop.
# Create pull request from your branch to target branch
# USAGE
# Switch for branch you want to make Pull request
# bash create_pr_from_cli.sh <target_branch> <OPTIONAL_pr_message>
# Limitation one script allows you to make PR for one repository.
# You can change this easily.
# TODO
PROJECTNAME="ProjectName"
# Also There is way to get this variable automatically
# but it's not necessary since that not going to change so much
# TODO
USERNAME="~Yourusername"
# TODO Write your domain here
DOMAIN="https://example.net"
TARGETBRANCH=$1
COMMITDESCRIPTION=$2
# Get current branch name
CURRENTBRANCH=$(git branch | grep \* | cut -d ' ' -f2)
# Get the repository name
# NOTE I assume you use same folder name as main project another case you need to also fix here.
REPOSITORYNAME="$( basename `git rev-parse --show-toplevel` )"
# If there is not any pr_message Branch name will be description
if [ -z "$COMMITDESCRIPTION"];
then
COMMITDESCRIPTION=$CURRENTBRANCH
echo $COMMITDESCRIPTION
fi
# TODO Change with your reviewers
# NOTE may some parts of the json is not necessary(like active, approved, status)
# I didn't test what it can be without them so I leave it how I use.
# Maybe you can make test and inform me about it :)
generate_post_data()
{
cat <<EOF
{
"title": "$COMMITDESCRIPTION",
"description": "$COMMITDESCRIPTION",
"state": "OPEN",
"open": true,
"closed": false,
"reviewers": [
{
"user": {
"name": "user1",
"emailAddress": "example@example.com",
"id": 1984,
"displayName": "User1 LastName1",
"active": true,
"slug": "example",
"type": "NORMAL",
"links": {
"self": [
{
"href": "https://example.net/git/users/example"
}
]
}
},
"role": "REVIEWER",
"approved": false,
"status": "UNAPPROVED"
},
{
"user": {
"name": "user2",
"emailAddress": "example@example.com",
"id": 1984,
"displayName": "User2 LastName2",
"active": true,
"slug": "example2",
"type": "NORMAL",
"links": {
"self": [
{
"href": "https://example.net/git/users/example2"
}
]
}
},
"role": "REVIEWER",
"approved": false,
"status": "UNAPPROVED"
}
],
"fromRef": {
"id": "refs/heads/$CURRENTBRANCH",
"repository": {
"slug": "$REPOSITORYNAME",
"name": null,
"project": {
"key": "$USERNAME"
}
}
},
"toRef": {
"id": "refs/heads/$TARGETBRANCH",
"repository": {
"slug": "$REPOSITORYNAME",
"name": null,
"project": {
"key": "$PROJECTNAME"
}
}
},
"close_source_branch": false
}
EOF
}
echo "Base Branch >>>> $CURRENTBRANCH"
echo "Target Branch >>>> $TARGETBRANCH"
echo "Repository name >>>> $REPOSITORYNAME"
echo "Commit Message will be >>>> $COMMITDESCRIPTION"
read -p "Press Enter to continue" </dev/tty
# TODO
curl -u YourAweomeUsername:YourAwesomePassword $DOMAIN/git/rest/api/1.0/projects/$PROJECTNAME/repos/$REPOSITORYNAME/pull-requests \
--request POST \
--header 'Content-Type: application/json' \
--data "$(generate_post_data)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment