forked from car-on-sale/action-pull-request-another-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·95 lines (75 loc) · 2.35 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/sh
set -e
set -x
if [ -z "$INPUT_SOURCE_FOLDER" ]
then
echo "Source folder must be defined"
return -1
fi
if [ -z $INPUT_PR_TITLE ]
then
echo "pr_title must be defined"
return -1
fi
if [ -z $INPUT_COMMIT_MSG ]
then
echo "commit_msg 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
HOME_DIR=$PWD
CLONE_DIR=$(mktemp -d)
echo "Setting git variables"
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 "Creating folder"
mkdir -p $CLONE_DIR/$INPUT_DESTINATION_FOLDER/
cd "$CLONE_DIR"
BRANCH_EXISTS=$(git show-ref "$INPUT_DESTINATION_HEAD_BRANCH" | wc -l)
echo "Checking if branch already exists"
git fetch -a
if [ $BRANCH_EXISTS == 1 ];
then
git checkout "$INPUT_DESTINATION_HEAD_BRANCH"
else
git checkout -b "$INPUT_DESTINATION_HEAD_BRANCH"
fi
echo "Copying files"
rsync -a --delete "$HOME_DIR/$INPUT_SOURCE_FOLDER" "$CLONE_DIR/$INPUT_DESTINATION_FOLDER/"
git add .
if git status | grep -q "Changes to be committed"
then
git commit --message "$INPUT_COMMIT_MSG"
if [ $BRANCH_EXISTS == 1 ];
then
echo "Pushing git commit"
git push -u origin HEAD:$INPUT_DESTINATION_HEAD_BRANCH
echo "Updating pull request"
CURRENT_BODY=$(gh pr view $INPUT_DESTINATION_HEAD_BRANCH --json body | jq '.body')
CURRENT_BODY=${CURRENT_BODY:1:${#CURRENT_BODY} - 2}
gh pr edit $INPUT_DESTINATION_HEAD_BRANCH -b "$CURRENT_BODY & https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
else
echo "Pushing git commit"
git push -u origin HEAD:$INPUT_DESTINATION_HEAD_BRANCH
echo "Creating a pull request"
gh pr create -t "$INPUT_PR_TITLE" \
-b "https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA" \
-B $INPUT_DESTINATION_BASE_BRANCH \
-H $INPUT_DESTINATION_HEAD_BRANCH \
$PULL_REQUEST_REVIEWERS
fi
else
echo "No changes detected"
fi