Skip to content

Commit d1ef171

Browse files
committed
Merge branch 'YP-1705-nastroit-prava-sliyanij-dlya-proekta-php-api-client' into 'main'
YP-1705-nastroit-prava-sliyanij-dlya-proekta-php-api-client See merge request ypmn-public/php-api-client!24
2 parents 431ef22 + 43d1b3c commit d1ef171

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

.gitlab-ci.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Main project pipeline
2+
variables:
3+
GIT_STRATEGY: none
4+
5+
stages:
6+
- code-approve-reset
7+
- mr-approve-check
8+
9+
include:
10+
- local: 'ci/code-approve-reset.yml'
11+
- local: 'ci/mr-approve-check.yml'

ci/code-approve-reset.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Removes all previously applied approvers in MR
2+
clear_code_approved:
3+
stage: code-approve-reset
4+
rules:
5+
- if: $CI_MERGE_REQUEST_ID
6+
script:
7+
- 'curl -f -X PUT -H "PRIVATE-TOKEN: $REMOVE_APPROVE_TOKEN" -H "Content-Type: application/json" "http://gtl01.dev.ruo.payudc.net/api/v4/projects/$CI_PROJECT_ID/merge_requests/$CI_MERGE_REQUEST_IID/reset_approvals"'

ci/mr-approve-check.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Blocks the pipeline if MR is not approved by any
2+
## of defined gitlab users (ALLOWED_APPROVERS)
3+
4+
mr-approve-check:
5+
stage: mr-approve-check
6+
needs: ["clear_code_approved"]
7+
tags:
8+
- mr-check
9+
variables:
10+
TARGET_BRANCH: main
11+
script:
12+
- |
13+
# Fetch the merge request ID from the environment variable
14+
# $merge_request_iid variable is set in gitlab webhook payload
15+
if [[ "$CI_PIPELINE_SOURCE" == "trigger" ]]; then
16+
MR_ID=${merge_request_iid}
17+
else
18+
MR_ID=${CI_MERGE_REQUEST_IID}
19+
fi
20+
21+
# Check if MR_ID is set
22+
if [ -z "$MR_ID" ]; then
23+
echo "This job is not running in a merge request context."
24+
exit 0
25+
fi
26+
27+
# Get the list of approvals for the merge request
28+
APPROVALS=$(curl --silent -H "PRIVATE-TOKEN: $REMOVE_APPROVE_TOKEN" -H "Content-Type: application/json" "http://gtl01.dev.ruo.payudc.net/api/v4/projects/$CI_PROJECT_ID/merge_requests/$MR_ID/approvals")
29+
30+
# Define a list of allowed approvers
31+
ALLOWED_APPROVERS=("alexander.viktorchik" "alexey.babak" "roman.zimin")
32+
33+
# Check if any of the allowed users have approved the merge request
34+
APPROVED=false
35+
for USER in "${ALLOWED_APPROVERS[@]}"; do
36+
if echo "$APPROVALS" | grep -q "$USER"; then
37+
APPROVED=true
38+
echo "Merge request approved by allowed user: $USER."
39+
break
40+
fi
41+
done
42+
43+
if [ "$APPROVED" = false ]; then
44+
echo "Merge request not approved by any allowed users. Blocking the merge request."
45+
exit 1
46+
fi
47+
rules:
48+
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $TARGET_BRANCH'

0 commit comments

Comments
 (0)