This repository has been archived by the owner on Mar 20, 2021. It is now read-only.
forked from stenongithub/gitlab-mirror-and-ci-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·344 lines (303 loc) · 17.3 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/bin/sh
set -u
##################################################################
urlencode() (
i=1
max_i=${#1}
while test $i -le $max_i; do
c="$(expr substr $1 $i 1)"
case $c in
[a-zA-Z0-9.~_-])
printf "$c" ;;
*)
printf '%%%02X' "'$c" ;;
esac
i=$(( i + 1 ))
done
)
##################################################################
##################################################################
#Approved commit sha
approvedcommitsha() (
approved=1
GITHUB_USERNAME=$1
BRANCH=$2
#API returns latest commit first
icommit=-1
while [[ "${approved}" != "0" && "${icommit}" -lt 100 ]]
do
icommit=$(($icommit+1))
commitauthor=$(curl -H "Authorization: token ${SOURCE_PAT}" --silent -H "Accept: application/vnd.github.antiope-preview+json" "https://api.github.com/repos/${GITHUB_REPO}/commits?sha=${BRANCH}&per_page=100" | jq ".[$icommit] | {commitauthor: .author.login}" | jq ".commitauthor")
if [[ $commitauthor == $GITHUB_USERNAME ]]; then approved=0; fi
sha=$(curl -H "Authorization: token ${SOURCE_PAT}" --silent -H "Accept: application/vnd.github.antiope-preview+json" "https://api.github.com/repos/${GITHUB_REPO}/commits?sha=${BRANCH}&per_page=100" | jq ".[$icommit] | {sha: .sha}" | jq ".sha" | sed "s/\\\"/\\,/g" | sed s/\[,\]//g)
ncomments=$(curl -H "Authorization: token ${SOURCE_PAT}" --silent -H "Accept: application/vnd.github.antiope-preview+json" https://api.github.com/repos/${GITHUB_REPO}/commits/$sha/comments | jq length)
icomment=${ncomments}
while [[ "${approved}" != "0" && "${icomment}" -gt 0 ]]
do
icomment=$(($icomment - 1))
commentauthor=$(curl -H "Authorization: token ${SOURCE_PAT}" --silent -H "Accept: application/vnd.github.antiope-preview+json" https://api.github.com/repos/${GITHUB_REPO}/commits/$sha/comments | jq ".[$icomment] | {commentauthor: .user.login}" | jq ".commentauthor")
curl -H "Authorization: token ${SOURCE_PAT}" --silent -H "Accept: application/vnd.github.antiope-preview+json" https://api.github.com/repos/${GITHUB_REPO}/commits/$sha/comments | jq ".[$icomment] | {body: .body}" | grep $APPROVAL_STRING -q
approval_comment=$?
if [[ $commentauthor == $GITHUB_USERNAME && $approval_comment == "0" ]]; then approved=0; fi
done
done
if [[ ${approved} == "0" ]]; then printf "$sha"; else printf "nil"; fi
)
##################################################################
##################################################################
branchexists() (
BRANCH=$1
#Check if branch for which push/pr testing is requested, exists
nbranches=$(curl -H "Authorization: token ${SOURCE_PAT}" --silent -H "Accept: application/vnd.github.antiope-preview+json" "https://api.github.com/repos/${GITHUB_REPO}/branches" | jq length)
branch_exists=1
ibranch=-1
while [[ "${branch_exists}" != "0" && "${ibranch}" -lt "${nbranches}" ]]
do
ibranch=$(($ibranch+1))
temp_branch=$(curl -H "Authorization: token ${SOURCE_PAT}" --silent -H "Accept: application/vnd.github.antiope-preview+json" "https://api.github.com/repos/${GITHUB_REPO}/branches" | jq ".[$ibranch] | {branch: .name}" | jq .branch | sed "s/\\\"/\\,/g" | sed s/\[,\]//g)
if [[ "${temp_branch}" == "${BRANCH}" ]]; then branch_exists=0; fi
done
printf "${branch_exists}"
if [[ "${branch_exists}" != 0 ]]
then
#This error needs to go to MIRROR_REPO and not SOURCE_REPO
echo "Target branch not found, CI job will exit"
curl -d '{"state":"failure", "context": "gitlab-ci"}' -H "Authorization: token ${SOURCE_PAT}" -H "Accept: application/vnd.github.antiope-preview+json" -X POST --silent "https://api.github.com/repos/${GITHUB_REPO}/statuses/${sha}"
exit 1
fi
)
##################################################################
##################################################################
prapproval() (
PR_NUMBER=$1
GITHUB_USERNAME=$2
approved=1
#Decide whether PR needs to be checked based on REPO_EVENT_TYPE
base_repo=$(curl --silent -H "Accept: application/vnd.github.antiope-preview+json" https://api.github.com/repos/${GITHUB_REPO}/pulls/${PR_NUMBER} | jq .base.repo.url)
head_repo=$(curl --silent -H "Accept: application/vnd.github.antiope-preview+json" https://api.github.com/repos/${GITHUB_REPO}/pulls/${PR_NUMBER} | jq .head.repo.url)
if [[ $REPO_EVENT_TYPE == "internal_pr" ]]
then
if [[ $base_repo != $head_repo ]]; then return 1; fi
else
if [[ $base_repo == $head_repo ]]; then return 1; fi
fi
#Find the latest commit date and author
ncommits=$(curl -H "Authorization: token ${SOURCE_PAT}" --silent -H "Accept: application/vnd.github.antiope-preview+json" https://api.github.com/repos/${GITHUB_REPO}/pulls/${PR_NUMBER}/commits | jq length)
ncommits=$(($ncommits - 1))
commitdate=$(curl -H "Authorization: token ${SOURCE_PAT}" --silent -H "Accept: application/vnd.github.antiope-preview+json" https://api.github.com/repos/${GITHUB_REPO}/pulls/${PR_NUMBER}/commits | jq ".[${ncommits}] | {created_at: .commit.author.date}" | jq ".created_at")
commitauthor=$(curl --silent -H "Accept: application/vnd.github.antiope-preview+json" https://api.github.com/repos/${GITHUB_REPO}/pulls/${PR_NUMBER}/commits | jq ".[${ncommits}] | {commit_author: .author.login}" | jq .commit_author)
if [[ $commitauthor == $GITHUB_USERNAME ]]; then approved=0; printf "${commitdate}"; fi
#If commit author is not approved, check comments
if [[ ${approved} != "0" ]]
then
ncomments=$(curl -H "Authorization: token ${SOURCE_PAT}" --silent -H "Accept: application/vnd.github.antiope-preview+json" https://api.github.com/repos/${GITHUB_REPO}/issues/${PR_NUMBER}/comments | jq length)
approval_comment=1
icomment=${ncomments}
while [[ "${approval_comment}" != "0" && "${icomment}" -gt 0 ]]
do
icomment=$(($icomment - 1))
#check comment for string
curl -H "Authorization: token ${SOURCE_PAT}" --silent -H "Accept: application/vnd.github.antiope-preview+json" https://api.github.com/repos/${GITHUB_REPO}/issues/${PR_NUMBER}/comments | jq ".[$icomment] | {body: .body}" | grep $APPROVAL_STRING -q
approval_comment=$?
commentdate=$(curl -H "Authorization: token ${SOURCE_PAT}" --silent -H "Accept: application/vnd.github.antiope-preview+json" https://api.github.com/repos/${GITHUB_REPO}/issues/${PR_NUMBER}/comments | jq ".[${icomment}] | {created_at: .created_at}" | jq ".created_at")
#if string matches check if commenter belongs to the pre-approved list and the comment is newer than the latest commit
if [[ "${approval_comment}" = "0" && ${commentdate} > ${commitdate} ]]
then
commentauthor=$(curl -H "Authorization: token ${SOURCE_PAT}" --silent -H "Accept: application/vnd.github.antiope-preview+json" https://api.github.com/repos/${GITHUB_REPO}/issues/${PR_NUMBER}/comments | jq ".[$icomment] | {commenter: .user.login}" | jq ".commenter")
if [[ $commentauthor == $GITHUB_USERNAME ]]; then approved=0; printf "${commentdate}"; fi
approval_comment=$?
fi
done
fi
)
##################################################################
#TODO: If below preapproved is set to 0, all the checks should be short-circuited.
preapproved=1
DEFAULT_POLL_TIMEOUT=10
POLL_TIMEOUT=${POLL_TIMEOUT:-$DEFAULT_POLL_TIMEOUT}
echo "CI job triggered by event- $REPO_EVENT_TYPE"
#Check if REPO_EVENT_TYPE specified above is supported.
#At present the only difference in internal_pr and fork_pr is how the branch is named on the GITLAB side.
# This is because we dont want 'main' from fork to overwrite 'main' from the upstream repo on the Gitlab side
if [[ ${REPO_EVENT_TYPE} != "push" && ${REPO_EVENT_TYPE} != "internal_pr" && ${REPO_EVENT_TYPE} != "fork_pr" ]]
then
echo "Only PR and Push testing are currently supported. CI will exit"
exit 1
fi
#Retrieve Github username of SOURCE_PAT
#This author is used for approval
GITHUB_USERNAME=$(curl -H "Authorization: token ${SOURCE_PAT}" -H "Accept: application/vnd.github.v3+json" --silent https://api.github.com/user | jq .login)
#TODO: Add a list of required variables for each type of event. The job will fail if any are empty
#In push there is no 'target branch', so we populate both variables with the same name
if [[ "${REPO_EVENT_TYPE}" == "push" ]]; then TARGET_BRANCH=${BRANCH}; fi
branchfound="$(branchexists ${TARGET_BRANCH})"
#Maybe move the below branch check along with the overall variable values check (after that is implemented)
if [[ ${branchfound} != "0" ]]
then
echo "Branch ${TARGET_BRANCH} not found in the repo, CI job will exit"
exit 1
fi
#Maybe move the below pr check along with the overall variable values check (after that is implemented)
if [[ "${REPO_EVENT_TYPE}" = "internal_pr" || "${REPO_EVENT_TYPE}" = "fork_pr" ]]
then
#number of open pr's
npr=$(curl --silent -H "Accept: application/vnd.github.antiope-preview+json" https://api.github.com/repos/${GITHUB_REPO}/pulls | jq length)
if [[ ${npr} == "0" ]]
then
echo "No open PRs, CI will exit."
exit 1
fi
fi
#If PR Number is specified use that or else find the latest acceptable PR
#An acceptable PR is one that has the latest commit from the user $GITHUB_USERNAME or an approval comment by said user.
if [[ $(printenv PR_NUMBER | wc -c) == "0" ]]
then
# Cycle through all PRs
ipr=-1
while [[ "$ipr" -lt "$(($npr-1))" ]]
do
ipr=$(($ipr+1))
echo "line 174: $ipr"
target_PR_NUMBER=$(curl --silent -H "Authorization: token ${SOURCE_PAT}" -H "Accept: application/vnd.github.antiope-preview+json" https://api.github.com/repos/${GITHUB_REPO}/pulls | jq ".[${ipr}] | {PR_NUMBER : .number}" | jq .PR_NUMBER)
echo "${target_PR_NUMBER}"
#Approvaltime is used to find the latest approved action, that PR will be targeted by CI.
#This function only returns PRs where the latest commit is approved.
export temp_approvaltime="$(prapproval ${target_PR_NUMBER} ${GITHUB_USERNAME})"
if [[ ! -z ${temp_approvaltime} ]]
then
if [[ $(printenv approvedtime | wc -c) = 0 ]]
then
export approvedtime=${temp_approvaltime}
PR_NUMBER=${target_PR_NUMBER}
elif [[ ${temp_approvaltime} > ${approvedtime} ]]
then
export approvedtime=${temp_approvaltime}
PR_NUMBER=${target_PR_NUMBER}
fi
fi
done
else
# only check the specified PR.
export approvedtime="$(prapproval ${PR_NUMBER} ${GITHUB_USERNAME})"
fi
if [[ "${REPO_EVENT_TYPE}" = "internal_pr" || "${REPO_EVENT_TYPE}" = "fork_pr" ]]
then
if [[ $(printenv approvedtime | wc -c) == "0" ]]
then
echo "No approval associated with the target PR(s). CI job will exit"
exit 1
fi
fi
#PR already has a valid source at this point (or has exit). Push testing is essentially different in that it is part of the upstream repo.
#Checkout the branches in the VM so that they can pushed to Gitlab
if [ "${REPO_EVENT_TYPE}" = "internal_pr" ]
then
BRANCH=$(curl --silent -H "Authorization: token ${SOURCE_PAT}" -H "Accept: application/vnd.github.antiope-preview+json" https://api.github.com/repos/${GITHUB_REPO}/pulls/${PR_NUMBER} | jq .head.ref | sed "s/\\\"/\\,/g" | sed s/\[,\]//g)
git checkout "${BRANCH}"
sha=$(git rev-parse HEAD)
elif [ "${REPO_EVENT_TYPE}" = "push" ]
then
git checkout "${BRANCH}"
elif [ "${REPO_EVENT_TYPE}" = "fork_pr" ]
then
echo "You are running pull request target, make sure your settings are secure, secrets are accessible."
#Manual change of git
rm -rf * .*
fork_repo=$(curl --silent -H "Accept: application/vnd.github.antiope-preview+json" https://api.github.com/repos/${GITHUB_REPO}/pulls/${PR_NUMBER} | jq .head.repo.clone_url)
fork_repo="${fork_repo:1:${#fork_repo}-2}"
git clone --quiet ${fork_repo} .
GITHUB_HEAD_REF=$(curl --silent -H "Accept: application/vnd.github.antiope-preview+json" https://api.github.com/repos/${GITHUB_REPO}/pulls/${PR_NUMBER} | jq .head.ref)
GITHUB_HEAD_REF="${GITHUB_HEAD_REF:1:${#GITHUB_HEAD_REF}-2}"
git checkout "${GITHUB_HEAD_REF}"
BRANCH=external-pr-${PR_NUMBER}
git branch -m ${BRANCH}
sha=$(git rev-parse HEAD)
fi
branch_uri="$(urlencode ${BRANCH})"
#Approval section
if [ "${REPO_EVENT_TYPE}" = "push" ]
then
#Get the latest commit sha on the target gitlab repository. This is currently not being used but can provide a good sanity check.
base_commitsha=$(curl --header "PRIVATE-TOKEN: $GITLAB_PASSWORD" "https://${GITLAB_HOSTNAME}/api/v4/projects/${GITLAB_PROJECT_ID}/repository/commits?ref_name=${BRANCH}" --silent | jq ".[0] | {id: .id}")
#Run through the recent 100 commits to find the latest than can be cloned
#Like the PR an acceptable commit is one which was commited by the user $GITHUB_USERNAME or has an approval comment by them
sha="$(approvedcommitsha ${GITHUB_USERNAME} ${BRANCH})"
echo "sha: $sha"
if [[ $sha != "nil" ]]
then
#TODO: check if base_commitsha is older than sha before pushing
approved=0
git checkout $sha
fi
fi
if [[ "${approved}" != "0" && "${REPO_EVENT_TYPE}" = "push" ]]
then
echo "Commit author ${commitauthor} not associated with repository. Push testing not allowed. CI will exit"
exit 1
fi
sh -c "git config --global credential.username $GITLAB_USERNAME"
sh -c "git config --global core.askPass /cred-helper.sh"
sh -c "git config --global credential.helper cache"
sh -c "git remote add mirror $*"
sh -c "echo pushing to $BRANCH branch at $(git remote get-url --push mirror)"
#sh -c "git push mirror $branch --force"
sh -c "git push mirror $sha:refs/heads/$BRANCH"
# If the push fails because the target branch is ahead than the push, Pipeline is counted as failed.
push_status=$?
if [[ "${push_status}" != "0" ]]
then
echo "Unable to push to target repository, job will fail."
curl -d '{"state":"failure", "context": "gitlab-ci"}' -H "Authorization: token ${SOURCE_PAT}" -H "Accept: application/vnd.github.antiope-preview+json" -X POST --silent "https://api.github.com/repos/${GITHUB_REPO}/statuses/${sha}"
exit 1
fi
sleep $POLL_TIMEOUT
#TODO: see if there is better way than taking the last pipeline
pipeline_id=$(curl --header "PRIVATE-TOKEN: $GITLAB_PASSWORD" --silent "https://${GITLAB_HOSTNAME}/api/v4/projects/${GITLAB_PROJECT_ID}/repository/commits/${BRANCH}" | jq '.last_pipeline.id')
if [ "${pipeline_id}" = "null" ]
then
echo "pipeline_id is null, so we can't continue."
echo "Response from https://${GITLAB_HOSTNAME}/api/v4/projects/${GITLAB_PROJECT_ID}/repository/commits/${BRANCH} was:"
echo $(curl --header "PRIVATE-TOKEN: $GITLAB_PASSWORD" --silent "https://${GITLAB_HOSTNAME}/api/v4/projects/${GITLAB_PROJECT_ID}/repository/commits/${BRANCH}")
exit 1
fi
echo "Triggered CI for branch ${BRANCH}"
echo "Working with pipeline id #${pipeline_id}"
echo "Poll timeout set to ${POLL_TIMEOUT}"
ci_status="pending"
until [[ "$ci_status" != "pending" && "$ci_status" != "running" ]]
do
sleep $POLL_TIMEOUT
ci_output=$(curl --header "PRIVATE-TOKEN: $GITLAB_PASSWORD" --silent "https://${GITLAB_HOSTNAME}/api/v4/projects/${GITLAB_PROJECT_ID}/pipelines/${pipeline_id}")
ci_status=$(jq -n "$ci_output" | jq -r .status)
ci_web_url=$(jq -n "$ci_output" | jq -r .web_url)
echo "Current pipeline status: ${ci_status}"
if [ "$ci_status" = "running" ]
then
echo "Checking pipeline status..."
curl -d '{"state":"pending", "target_url": "'${ci_web_url}'", "context": "gitlab-ci"}' -H "Authorization: token ${SOURCE_PAT}" -H "Accept: application/vnd.github.antiope-preview+json" -X POST --silent "https://api.github.com/repos/${GITHUB_REPO}/statuses/${sha}" > /dev/null
fi
done
echo "Pipeline finished with status ${ci_status}"
#Delete remote branch if PR
if [[ "${REPO_EVENT_TYPE}" = "internal_pr" || "${REPO_EVENT_TYPE}" = "fork_pr" ]]
then
sh -c "git push mirror --delete ${BRANCH}"
fi
#TODO: change the context from gitlab-ci to something based on a variable
if [ "$ci_status" = "success" ]
then
curl -d '{"state":"success", "target_url": "'${ci_web_url}'", "context": "gitlab-ci"}' -H "Authorization: token ${SOURCE_PAT}" -H "Accept: application/vnd.github.antiope-preview+json" -X POST --silent "https://api.github.com/repos/${GITHUB_REPO}/statuses/${sha}"
exit 0
elif [ "$ci_status" = "manual" ] # do not return non-triggered manual builds as a CI failure
then
curl -d '{"state":"success", "target_url": "'${ci_web_url}'", "context": "gitlab-ci"}' -H "Authorization: token ${SOURCE_PAT}" -H "Accept: application/vnd.github.antiope-preview+json" -X POST --silent "https://api.github.com/repos/${GITHUB_REPO}/statuses/${sha}"
exit 0
elif [ "$ci_status" = "failed" ]
then
curl -d '{"state":"failure", "target_url": "'${ci_web_url}'", "context": "gitlab-ci"}' -H "Authorization: token ${SOURCE_PAT}" -H "Accept: application/vnd.github.antiope-preview+json" -X POST --silent "https://api.github.com/repos/${GITHUB_REPO}/statuses/${sha}"
exit 1
else # no return value, so there's no target URL either
echo "Pipeline ended without a ci_status: https://${GITLAB_HOSTNAME}/api/v4/projects/${GITLAB_PROJECT_ID}/pipelines/${pipeline_id}"
curl -d '{"state":"failure", "context": "gitlab-ci"}' -H "Authorization: token ${SOURCE_PAT}" -H "Accept: application/vnd.github.antiope-preview+json" -X POST --silent "https://api.github.com/repos/${GITHUB_REPO}/statuses/${sha}"
exit 1
fi