-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·159 lines (121 loc) · 5.23 KB
/
entrypoint.sh
File metadata and controls
executable file
·159 lines (121 loc) · 5.23 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash
set -e
echo "Integrate Action v1.1.1"
# Workaround until new Actions support neutral strategy
# See how it was before: https://developer.github.com/actions/creating-github-actions/accessing-the-runtime-environment/#exit-codes-and-statuses
NEUTRAL_EXIT_CODE=0
# since https://github.blog/2022-04-12-git-security-vulnerability-announced/
git config --global --add safe.directory /github/workspace
# Skip if not a PR
echo "Checking if issue is a pull request..."
(jq -r ".issue.pull_request.url" "$GITHUB_EVENT_PATH") || exit $NEUTRAL_EXIT_CODE
if [[ "$(jq -r ".action" "$GITHUB_EVENT_PATH")" != "created" ]]; then
echo "This is not a new comment event!"
exit $NEUTRAL_EXIT_CODE
fi
COMMENT_BODY=$(jq -r ".comment.body" "$GITHUB_EVENT_PATH")
if [[ "$COMMENT_BODY" == *"/hotfix"* ]]; then
ACTION_MODE="hotfix"
elif [[ "$COMMENT_BODY" == *"/integrate"* ]]; then
ACTION_MODE="integrate"
else
echo "Comment does not contain /integrate or /hotfix. Skipping."
exit $NEUTRAL_EXIT_CODE
fi
echo "Action mode: $ACTION_MODE"
PR_NUMBER=$(jq -r ".issue.number" "$GITHUB_EVENT_PATH")
echo "Collecting information about PR #$PR_NUMBER of $GITHUB_REPOSITORY..."
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "Set the GITHUB_TOKEN env variable."
exit 1
fi
URI=https://api.github.com
API_HEADER="Accept: application/vnd.github.v3+json"
AUTH_HEADER="Authorization: token $GITHUB_TOKEN"
USER_URL=$(jq -r ".comment.user.url" "$GITHUB_EVENT_PATH")
user_resp=$(curl -X GET -s -H "${API_HEADER}" -H "${AUTH_HEADER}" "${USER_URL}")
USER_FULL_NAME=$(echo "$user_resp" | jq -r ".name")
# add a Thumbs Up reaction to the comment
COMMENTS_URL=$(jq -r ".comment.url" "$GITHUB_EVENT_PATH")
PREVIEW_API_HEADER="Accept: application/vnd.github.squirrel-girl-preview+json"
curl -X POST -s -H "${AUTH_HEADER}" -H "${PREVIEW_API_HEADER}" -d '{ "content": "+1" }' "$COMMENTS_URL/reactions"
if [[ "$USER_FULL_NAME" == "null" ]]; then
echo "USER_RESPONSE: $user_resp"
echo "You must have your full name set up on your GitHub user profile so that the integration can be attributed to you!"
exit 1
fi
PR_URL="${URI}/repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER"
pr_resp=$(curl -X GET -s -H "${AUTH_HEADER}" -H "${API_HEADER}" "${PR_URL}")
BASE_REPO=$(echo "$pr_resp" | jq -r .base.repo.full_name)
BASE_BRANCH=$(echo "$pr_resp" | jq -r .base.ref)
PR_TITLE=$(echo "$pr_resp" | jq -r .title)
if [[ -z "$BASE_BRANCH" ]]; then
echo "Cannot get base branch information for PR #$PR_NUMBER!"
echo "API response: $pr_resp"
exit 1
fi
HEAD_REPO=$(echo "$pr_resp" | jq -r .head.repo.full_name)
HEAD_BRANCH=$(echo "$pr_resp" | jq -r .head.ref)
echo "Base branch for PR #$PR_NUMBER is $BASE_BRANCH"
if [[ "$BASE_REPO" != "$HEAD_REPO" ]]; then
echo "PRs from forks are not supported at the moment."
exit 1
fi
git remote set-url origin https://x-access-token:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY.git
git config --global user.email "action@github.com"
git config --global user.name "GitHub Action"
# Make sure branches are up-to-date
git fetch origin $BASE_BRANCH
git fetch origin $HEAD_BRANCH
# Rebase
git checkout -b $HEAD_BRANCH origin/$HEAD_BRANCH
git rebase origin/$BASE_BRANCH
git push --force-with-lease
HEAD_BRANCH_HEAD=$(git rev-parse HEAD)
echo "(Potentially) Rebased commit hash of HEAD is: $HEAD_BRANCH_HEAD"
# Poll for CI status
while true; do
sleep 10
LAST_STATUS=$(curl -s -H "${AUTH_HEADER}" -H "${API_HEADER}" "${URI}/repos/$GITHUB_REPOSITORY/commits/$HEAD_BRANCH_HEAD/status" | jq -r ".state")
if [[ $LAST_STATUS != "pending" ]]; then
break
fi
echo "Polling for CI build completion..."
done
if [[ $LAST_STATUS != "success" ]]; then
echo "CI did not pass for branch $HEAD_BRANCH and HEAD commit $HEAD_BRANCH_HEAD. Cancelling integration."
exit 1
fi
# Rebase
git checkout $HEAD_BRANCH
git rebase origin/$BASE_BRANCH
git push --force-with-lease
# Hit the merge button
MERGE_COMMIT_TITLE="Merge branch '$HEAD_BRANCH' on behalf of $USER_FULL_NAME"
if [[ "$ACTION_MODE" == "hotfix" ]]; then
MERGE_COMMIT_TITLE="$MERGE_COMMIT_TITLE [skip tests]"
fi
if [[ $ADD_CHANGE_LOGS = "true" ]]; then
COMMENT_TOKEN="(?i)change\\\\s?log:?"
PR_COMMENTS_URL="$URI/repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments"
GITHUB_PR_COMMENTS=$(curl -X GET -s -H "${API_HEADER}" -H "${AUTH_HEADER}" "${PR_COMMENTS_URL}")
MESSAGE=$(echo $GITHUB_PR_COMMENTS | jq ".[].body | select(test(\"$COMMENT_TOKEN\")) | sub(\"$COMMENT_TOKEN\"; \"\")" | tr -d '"')
TRIMMED_MESSAGE=$(echo "$MESSAGE" | sed 's/^[[:space:]]*//')
NEWLINE_MESSAGE=$( sed 's/\\r\\n/\'$'\n''/g' <<< "$TRIMMED_MESSAGE" | sed 's/^/ /' )
MERGE_COMMIT_MESSAGE="\
$PR_TITLE
Change Log
https://github.com/$GITHUB_REPOSITORY/pulls/$PR_NUMBER
${NEWLINE_MESSAGE}"
JSON_STRING=$( jq -n \
--arg title "$MERGE_COMMIT_TITLE" \
--arg message "$MERGE_COMMIT_MESSAGE" \
'{commit_title: $title, commit_message: $message}' )
merge_resp=$(curl -X PUT -s -H "${AUTH_HEADER}" -H "${API_HEADER}" -d "$JSON_STRING" "${PR_URL}/merge")
else
merge_resp=$(curl -X PUT -s -H "${AUTH_HEADER}" -H "${API_HEADER}" -d "{\"commit_title\":\"$MERGE_COMMIT_TITLE\"}" "${PR_URL}/merge")
fi
if [[ $merge_resp != *"Pull Request successfully merged"* ]]; then
echo "Could not merge PR. Error from GitHub: '$merge_resp'"
exit 1
fi