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.

51 lines
2.1 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. exit_code=0
  10. # Get PR number from CI_BUILD_REF_NAME
  11. issue=$(echo ${CI_BUILD_REF_NAME} | perl -ne '/^pr-(\d+)-\S+$/ && print $1')
  12. if [ "$issue" = "" ]; then
  13. echo "Unable to get issue id from: $CI_BUILD_REF_NAME"
  14. exit 2
  15. fi
  16. echo "Fetching labels from PR $issue"
  17. labels=$(curl ${CURL_ARGS} "https://api.github.com/repos/kubernetes-sigs/kubespray/issues/${issue}?access_token=${GITHUB_TOKEN}" | jq '{labels: .labels}' | jq '.labels[].name' | jq -s '')
  18. labels_to_patch=$(echo -n $labels | jq '. + ["needs-ci-auth"]' | tr -d "\n")
  19. echo "Checking for '$MAGIC' comment in PR $issue"
  20. # Get the user name from the PR comments with the wanted magic incantation casted
  21. user=$(curl ${CURL_ARGS} "https://api.github.com/repos/kubernetes-sigs/kubespray/issues/${issue}/comments" | jq -M "map(select(.body | contains (\"$MAGIC\"))) | .[0] .user.login" | tr -d '"')
  22. # Check for the required user group membership to allow (exit 0) or decline (exit >0) the pipeline
  23. if [ "$user" = "" ] || [ "$user" = "null" ]; then
  24. echo "Missing '$MAGIC' comment from one of the OWNERS"
  25. exit_code=3
  26. else
  27. echo "Found comment from user: $user"
  28. curl ${CURL_ARGS} "https://api.github.com/orgs/kubernetes-sigs/members/${user}"
  29. if [ $? -ne 0 ]; then
  30. echo "User does not have permissions to start CI run"
  31. exit_code=1
  32. else
  33. labels_to_patch=$(echo -n $labels | jq '. - ["needs-ci-auth"]' | tr -d "\n")
  34. exit_code=0
  35. echo "$user has allowed CI to start"
  36. fi
  37. fi
  38. # Patch labels on PR
  39. curl ${CURL_ARGS} --request PATCH "https://api.github.com/repos/kubernetes-sigs/kubespray/issues/${issue}?access_token=${GITHUB_TOKEN}" -H "Content-Type: application/json" -d "{\"labels\": ${labels_to_patch}}"
  40. exit $exit_code