|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Assign arguments to variables |
| 4 | +SLACK_TOKEN=$1 |
| 5 | +CHANNEL_ID=$2 |
| 6 | +clientId=$3 |
| 7 | +clientSecret=$4 |
| 8 | +run_id=$5 |
| 9 | +MEMBER_EMAILS_JSON=$6 |
| 10 | + |
| 11 | +# Get the Port access token |
| 12 | +PORT_TOKEN_RESPONSE=$(curl -s -X 'POST' \ |
| 13 | + 'https://api.getport.io/v1/auth/access_token' \ |
| 14 | + -H 'accept: application/json' \ |
| 15 | + -H 'Content-Type: application/json' \ |
| 16 | + -d "{ |
| 17 | + \"clientId\": \"$clientId\", |
| 18 | + \"clientSecret\": \"$clientSecret\" |
| 19 | + }" |
| 20 | + ) |
| 21 | + |
| 22 | +echo $PORT_TOKEN_RESPONSE |
| 23 | +PORT_ACCESS_TOKEN=$(echo $PORT_TOKEN_RESPONSE | jq -r '.accessToken') |
| 24 | + |
| 25 | +# Ensure the access token was obtained successfully |
| 26 | +if [ -z "$PORT_ACCESS_TOKEN" ] || [ "$PORT_ACCESS_TOKEN" == "null" ]; then |
| 27 | + error_message="Failed to obtain Port access token ❌" |
| 28 | + echo $error_message |
| 29 | + report_error "$error_message" |
| 30 | + exit 1 |
| 31 | +fi |
| 32 | + |
| 33 | +# Function to report error |
| 34 | +report_error() { |
| 35 | + local message=$1 |
| 36 | + echo $message |
| 37 | + echo "ADD_MEMBER_TO_CHANNEL_ERROR=$message" >> $GITHUB_ENV |
| 38 | + curl -s -X POST "https://api.getport.io/v1/actions/runs/$run_id/logs" \ |
| 39 | + -H "Content-Type: application/json" \ |
| 40 | + -H "Authorization: Bearer $PORT_ACCESS_TOKEN" \ |
| 41 | + -d "{\"message\": \"$message\"}" |
| 42 | +} |
| 43 | + |
| 44 | +user_ids="" |
| 45 | + |
| 46 | +# Convert MEMBER_EMAILS_JSON to an array |
| 47 | +readarray -t MEMBER_EMAILS < <(echo $MEMBER_EMAILS_JSON | jq -r 'fromjson | .[]') |
| 48 | + |
| 49 | +for email in "${MEMBER_EMAILS[@]}"; do |
| 50 | + user_response=$(curl -s -X GET "https://slack.com/api/users.lookupByEmail?email=$email" \ |
| 51 | + -H "Authorization: Bearer $SLACK_TOKEN") |
| 52 | + |
| 53 | + if [[ "$(echo $user_response | jq -r '.ok')" == "true" ]]; then |
| 54 | + user_id=$(echo $user_response | jq -r '.user.id') |
| 55 | + user_ids+="${user_id}," |
| 56 | + else |
| 57 | + error_message="Failed to retrieve user id for $email: $(echo $user_response | jq -r '.error' | tr '_' ' ') ⚠️" |
| 58 | + report_error "$error_message" |
| 59 | + fi |
| 60 | +done |
| 61 | + |
| 62 | +user_ids=${user_ids%,} |
| 63 | + |
| 64 | +if [[ -n "$user_ids" ]]; then |
| 65 | + invite_response=$(curl -s -X POST "https://slack.com/api/conversations.invite" \ |
| 66 | + -H "Content-Type: application/json" \ |
| 67 | + -H "Authorization: Bearer $SLACK_TOKEN" \ |
| 68 | + --data "{\"channel\":\"$CHANNEL_ID\",\"users\":\"$user_ids\"}") |
| 69 | + |
| 70 | + if [[ "$(echo $invite_response | jq -r '.ok')" == "false" ]]; then |
| 71 | + error_message="Failed to invite users to channel: $(echo $invite_response | jq -r '.error' | tr '_' ' ') ⚠️" |
| 72 | + report_error "$error_message" |
| 73 | + fi |
| 74 | +else |
| 75 | + report_error "No user IDs found to invite." |
| 76 | +fi |
0 commit comments