File tree Expand file tree Collapse file tree 3 files changed +66
-0
lines changed Expand file tree Collapse file tree 3 files changed +66
-0
lines changed Original file line number Diff line number Diff line change 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'
Original file line number Diff line number Diff line change 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"'
Original file line number Diff line number Diff line change 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'
You can’t perform that action at this time.
0 commit comments