-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·66 lines (56 loc) · 2.33 KB
/
entrypoint.sh
File metadata and controls
executable file
·66 lines (56 loc) · 2.33 KB
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
#!/bin/sh
set -eu
REPOSITORY=$GITHUB_REPOSITORY
CURRENT_BRANCH=${GITHUB_REF##*/}
HEAD_BRANCH=${HEAD_BRANCH:-release}
BASE_BRANCH=${BASE_BRANCH:-main}
CREATE_PR_URL=https://api.github.com/repos/$REPOSITORY/pulls
echo "::info::Configuration"
echo "::info:: CURRENT_BRANCH $CURRENT_BRANCH"
echo "::info:: HEAD_BRANCH $HEAD_BRANCH"
echo "::info:: BASE_BRANCH $BASE_BRANCH"
echo "::info:: if CURRENT_BRANCH is HEAD_BRANCH and is ahead of BASE_BRANCH, this action creates PR from CURRENT_BRANCH/HEAD_BRANCH against BASE_BRANCH"
echo "::info:: This action helps in keeping two branches in sync by auto creating PR"
check_create_PR_response() {
ERROR="$1"
if [ "$ERROR" != null ]; then
PR_EXISTS=$(echo "${ERROR}" | jq 'select(. | contains("A pull request already exists for"))')
if [ "$PR_EXISTS" != null ]; then
echo "::info:: PR exists from $CURRENT_BRANCH against $BASE_BRANCH"
exit 0
else
echo "::ERROR:: Error in creating PR from $CURRENT_BRANCH against $BASE_BRANCH: $ERROR "
exit 1
fi
fi
}
if [ "$CURRENT_BRANCH" = "$HEAD_BRANCH" ]; then
echo "::info:: creating PR for $CREATE_PR_URL from $CURRENT_BRANCH against $BASE_BRANCH"
GIT_CREATE_PR_RESPONSE=$(
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $GITHUB_TOKEN" \
"$CREATE_PR_URL" \
-d "{\"head\":\"$CURRENT_BRANCH\",\"base\":\"$BASE_BRANCH\", \"title\": \"Merge $CURRENT_BRANCH into $BASE_BRANCH\"}"
)
ERROR_MSG=$(echo "${GIT_CREATE_PR_RESPONSE}" | jq '.errors[0].message')
check_create_PR_response "$ERROR_MSG"
PR_URL=$(echo "${GIT_CREATE_PR_RESPONSE}" | jq '.url'| tr -d \")
echo "::info:: PR created successfully $PR_URL"
CHANGED_FILES=$(echo "${GIT_CREATE_PR_RESPONSE}" | jq '.changed_files')
if [ "$CHANGED_FILES" = 0 ]; then
echo "::debug:: PR has 0 files changes, hence closing the PR $PR_URL"
GIT_CLOSE_PR_RESPONSE=$(
curl \
-X PATCH \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $GITHUB_TOKEN" \
"$PR_URL" \
-d '{"state":"closed", "title": "PR closed as 0 file changes"}'
)
echo "::info:: PR auto closed as $BASE_BRANCH is up-to-date with $CURRENT_BRANCH"
else
echo "::info:: PR created successfully $PR_URL"
fi
fi