You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.4 KiB

  1. #!/bin/bash
  2. # A naive premoderation script to allow Gitlab CI pipeline on a specific PRs' comment
  3. # Exits with 0, if the pipeline is good to go
  4. # Exits with 1, if the user is not allowed to start pipeline
  5. # Exits with 2, if script is unable to get issue id from CI_BUILD_REF_NAME variable
  6. # Exits with 3, if missing the magic comment in the pipeline to start the pipeline
  7. CURL_ARGS="-fs --retry 4 --retry-delay 5"
  8. MAGIC="${MAGIC:-ci check this}"
  9. # Get PR number from CI_BUILD_REF_NAME
  10. issue=$(echo ${CI_BUILD_REF_NAME} | perl -ne '/^pr-(\d+)-\S+$/ && print $1')
  11. if [ "$issue" = "" ]; then
  12. echo "Unable to get issue id from: $CI_BUILD_REF_NAME"
  13. exit 2
  14. fi
  15. echo "Checking for '$MAGIC' comment in PR $issue"
  16. # Get the user name from the PR comments with the wanted magic incantation casted
  17. user=$(curl ${CURL_ARGS} "https://api.github.com/repos/kubernetes-sigs/kubespray/issues/${issue}/comments" \
  18. | jq -M "map(select(.body | contains (\"$MAGIC\"))) | .[0] .user.login" | tr -d '"')
  19. # Check for the required user group membership to allow (exit 0) or decline (exit >0) the pipeline
  20. if [ "$user" = "" ] || [ "$user" = "null" ]; then
  21. echo "Missing '$MAGIC' comment from one of the OWNERS"
  22. exit 3
  23. else
  24. echo "Found comment from user: $user"
  25. fi
  26. curl ${CURL_ARGS} "https://api.github.com/orgs/kubernetes-sigs/members/${user}"
  27. if [ $? -ne 0 ]; then
  28. echo "User does not have permissions to start CI run"
  29. exit 1
  30. else
  31. echo "$user has allowed CI to start"
  32. fi
  33. exit 0