Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added action to create PR #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions .github/actions/create_pull_request_to_another_repo/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
name: "Create pull Request to another repository"
description: "Used for pull request a copy of a folder to another repository"

inputs:
source_folder:
description: "Source folder from origin"
required: true
destination_repo:
description: "Destination repository"
required: true
destination_folder:
description: "Destination folder to push the origin folder"
required: false
user_email:
description: "Email for the git commit"
required: true
user_name:
description: "GitHub username for the commit"
required: true
destination_head_branch:
description: "The branch to create to push the changes. Cannot be master or main"
required: true
destination_base_branch:
description: "The branch into which you want your code merged."
required: false
default: "main"
pull_request_reviewers:
description: "Pull request reviewers users"
required: false
commit_message_title:
description: "Pull request commit message"
required: false
api_token_github:
description: "Github Token for making the PR"
required: true

runs:
using: composite
steps:
- name: Create Pull Request to Provided repository
env:
INPUT_SOURCE_FOLDER : ${{ inputs.source-folder }}
INPUT_DESTINATION_REPO : ${{ inputs.destination-repo }}
INPUT_DESTINATION_FOLDER : ${{ inputs.destination-folder }}
INPUT_USER_EMAIL : ${{ inputs.user-email }}
INPUT_USER_NAME : ${{ inputs.user-name }}
INPUT_DESTINATION_HEAD_BRANCH : ${{ inputs.destination-head-branch }}
INPUT_DESTINATION_BASE_BRANCH : ${{ inputs.destination-base-branch }}
INPUT_PULL_REQUEST_REVIEWERS : ${{ inputs.pull-request-reviewers }}
INPUT_COMMIT_MESSAGE_TITLE : ${{ inputs.commit-message-title }}
API_TOKEN_GITHUB: ${{ inputs.api_token_github }}
shell: bash
run: |
set -e
set -x

if [ -z "$INPUT_SOURCE_FOLDER" ]
then
echo "Source folder must be defined"
return -1
fi

if [ $INPUT_DESTINATION_HEAD_BRANCH == "main" ] || [ $INPUT_DESTINATION_HEAD_BRANCH == "master"]
then
echo "Destination head branch cannot be 'main' nor 'master'"
return -1
fi

if [ -z "$INPUT_PULL_REQUEST_REVIEWERS" ]
then
PULL_REQUEST_REVIEWERS=$INPUT_PULL_REQUEST_REVIEWERS
else
PULL_REQUEST_REVIEWERS='-r '$INPUT_PULL_REQUEST_REVIEWERS
fi

if [ -z "$INPUT_COMMIT_MESSAGE_TITLE" ]
then
INPUT_COMMIT_MESSAGE="Update from https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
else
INPUT_COMMIT_MESSAGE="$INPUT_COMMIT_MESSAGE_TITLE: Update from https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
fi

CLONE_DIR=$(mktemp -d)

echo "Setting git variables"
export GITHUB_TOKEN=$API_TOKEN_GITHUB
git config --global user.email "$INPUT_USER_EMAIL"
git config --global user.name "$INPUT_USER_NAME"

echo "Cloning destination git repository"
git clone "https://[email protected]/$INPUT_DESTINATION_REPO.git" "$CLONE_DIR"

echo "Copying contents to git repo"
mkdir -p $CLONE_DIR/$INPUT_DESTINATION_FOLDER/
cp -R $INPUT_SOURCE_FOLDER "$CLONE_DIR/$INPUT_DESTINATION_FOLDER/"
cd "$CLONE_DIR"
git checkout -b "$INPUT_DESTINATION_HEAD_BRANCH"

echo "Adding git commit"
git add .
if git status | grep -q "Changes to be committed"
then
git commit --message "$INPUT_COMMIT_MESSAGE"
echo "Pushing git commit"
git push -u origin HEAD:$INPUT_DESTINATION_HEAD_BRANCH
echo "Creating a pull request"
gh pr create -t $INPUT_DESTINATION_HEAD_BRANCH \
-b $INPUT_DESTINATION_HEAD_BRANCH \
-B $INPUT_DESTINATION_BASE_BRANCH \
-H $INPUT_DESTINATION_HEAD_BRANCH \
$PULL_REQUEST_REVIEWERS
else
echo "No changes detected"
fi

branding:
icon: "git-commit"
color: "green"