executable_git-gitlab 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env bash
  2. set -e
  3. set -o pipefail
  4. PRIVATE_TOKEN="${GITLAB_PRIVATE_TOKEN:-$(pass show majordomo/private/gitlab.intr/tokens/pyhalov)}"
  5. PROJECT_NAME="${GITLAB_PROJECT_NAME:-$(basename "$PWD")}"
  6. PROJECT_GROUP="${GITLAB_GROUP_NAME:-$(basename "$(dirname "$PWD")")}"
  7. PROJECT_ID="${GITLAB_GROUP_ID:-$(curl --header 'Content-Type: application/json' --header "PRIVATE-TOKEN: $PRIVATE_TOKEN" "https://gitlab.intr/api/v4/projects/$PROJECT_GROUP%2F$PROJECT_NAME" | jq .id)}"
  8. ASSIGNEE_USER="${GITLAB_ASSIGNEE_USER:-lyashenko}"
  9. SQUASH="${GITLAB_SQUASH:-false}"
  10. BROWSE="${GITLAB_BROWSE:-true}"
  11. BRANCH="${GITLAB_BRANCH:-$(git branch --show-current)}"
  12. TITLE="${GITLAB_TITLE:-$(git log -1 --format=%s)}"
  13. NOTIFY_SLACK_ENABLE="${GITLAB_NOTIFY_SLACK_ENABLE:-false}"
  14. NOTIFY_SLACK_USERNAME="${GITLAB_NOTIFY_SLACK_USERNAME:-here}"
  15. NOTIFY_SLACK_CHANNEL="${GITLAB_NOTIFY_SLACK_CHANNEL:-#mjd_eng}"
  16. assignee_id()
  17. {
  18. curl --verbose --header "Content-Type: application/json" \
  19. --header "PRIVATE-TOKEN: $PRIVATE_TOKEN" \
  20. "https://gitlab.intr/api/v4/users?active=true" \
  21. | jq --raw-output ".[] | select(.username == \"$1\") | .id"
  22. }
  23. merge_request()
  24. {
  25. curl --verbose --request POST --header 'Content-Type: application/json' --header "PRIVATE-TOKEN: $PRIVATE_TOKEN" "https://gitlab.intr/api/v4/projects/$PROJECT_ID/merge_requests" --data "{\"source_branch\": \"${BRANCH}\", \"target_branch\": \"${GITLAB_TARGET_BRANCH:-master}\", \"title\": \"$TITLE\", \"assignee_id\": \"$ASSIGNEE_ID\", \"remove_source_branch\": \"true\", \"squash\": \"$SQUASH\"}"
  26. }
  27. case "$1" in
  28. merge)
  29. ASSIGNEE_ID="${GITLAB_ASSIGNEE_ID:-$(assignee_id "$ASSIGNEE_USER")}"
  30. web_url="$(merge_request | jq --raw-output .web_url)"
  31. echo "$web_url" | xclip -i -sel p -f | xclip -i -sel c
  32. if [[ "$web_url" == http* ]]
  33. then
  34. if [[ $BROWSE == true ]]
  35. then
  36. "${BROWSER:-firefox}" "$web_url"
  37. fi
  38. if [[ $NOTIFY_SLACK_ENABLE == true ]]
  39. then
  40. ansible-playbook -i localhost, -c local /dev/stdin <<EOF
  41. - hosts: localhost
  42. tasks:
  43. - name: Send notification message via Slack
  44. slack:
  45. channel: '${NOTIFY_SLACK_CHANNEL}'
  46. username: "Олег Пыхалов"
  47. icon_url: "https://ca.slack-edge.com/T2NPT97BM-U02GLBYEFG8-bbeb9604d056-192"
  48. token: '$(pass show majordomo/private/slack.com/token/hr)'
  49. msg: @${NOTIFY_SLACK_USERNAME} ${web_url}
  50. delegate_to: localhost
  51. EOF
  52. fi
  53. else
  54. printf "'%s' is not a HTTP URL.\n" "$web_url"
  55. fi
  56. ;;
  57. repository)
  58. chromium "https://gitlab.intr/${PROJECT_GROUP}/${PROJECT_NAME}/-/settings/repository"
  59. ;;
  60. *)
  61. "${BROWSER:-firefox}" https://gitlab.intr/"$(basename "$(dirname "$PWD")")"/"$(basename "$PWD")"
  62. ;;
  63. esac