|
| 1 | +name: Open Slack Channel |
| 2 | +on: |
| 3 | + workflow_dispatch: |
| 4 | + inputs: |
| 5 | + channel_name: |
| 6 | + description: Name of the public or private channel to create. |
| 7 | + required: true |
| 8 | + type: string |
| 9 | + is_private: |
| 10 | + description: Create a private channel instead of a public one. |
| 11 | + required: false |
| 12 | + type: boolean |
| 13 | + members: |
| 14 | + description: Add members manually to the channel. |
| 15 | + type: array |
| 16 | + required: false |
| 17 | + port_context: |
| 18 | + description: Details of the action and general port_context (blueprint, run ID, entity identifier from Port, etc...). |
| 19 | + required: true |
| 20 | + |
| 21 | +jobs: |
| 22 | + open-slack-channel: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + steps: |
| 25 | + - name: Log Executing Request to Open Channel |
| 26 | + uses: port-labs/port-github-action@v1 |
| 27 | + with: |
| 28 | + clientId: ${{ secrets.PORT_CLIENT_ID }} |
| 29 | + clientSecret: ${{ secrets.PORT_CLIENT_SECRET }} |
| 30 | + baseUrl: https://api.getport.io |
| 31 | + operation: PATCH_RUN |
| 32 | + runId: ${{ fromJson(github.event.inputs.port_context).runId }} |
| 33 | + logMessage: "About to create a conversation channel in slack..." |
| 34 | + |
| 35 | + - name: Create Slack Channel |
| 36 | + id: create_channel |
| 37 | + env: |
| 38 | + SLACK_TOKEN: ${{ secrets.BOT_USER_OAUTH_TOKEN }} |
| 39 | + run: | |
| 40 | + response=$(curl -s -X POST "https://slack.com/api/conversations.create" \ |
| 41 | + -H "Authorization: Bearer $SLACK_TOKEN" \ |
| 42 | + -H "Content-Type: application/json" \ |
| 43 | + --data "{\"name\": \"${{ github.event.inputs.channel_name }}\",\"is_private\": ${{ github.event.inputs.is_private }} }") |
| 44 | +
|
| 45 | + echo "API Response: $response" |
| 46 | +
|
| 47 | + if [[ "$(echo $response | jq -r '.ok')" == "true" ]]; then |
| 48 | + channel_id=$(echo $response | jq -r '.channel.id') |
| 49 | + echo "Channel ID: $channel_id" |
| 50 | + echo "CHANNEL_ID=$channel_id" >> $GITHUB_ENV |
| 51 | +
|
| 52 | + else |
| 53 | + echo "Failed to create Slack channel. Channel ID is null." |
| 54 | + error=$(echo $response | jq -r '.error') |
| 55 | + error_message="${error//_/ }" |
| 56 | + echo "Error: $error_message" |
| 57 | + echo "CREATE_CHANNEL_ERROR=$error_message" >> $GITHUB_ENV |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | +
|
| 61 | + - name: Log If Create Channel Request Fails |
| 62 | + if: failure() |
| 63 | + uses: port-labs/port-github-action@v1 |
| 64 | + with: |
| 65 | + clientId: ${{ secrets.PORT_CLIENT_ID }} |
| 66 | + clientSecret: ${{ secrets.PORT_CLIENT_SECRET }} |
| 67 | + baseUrl: https://api.getport.io |
| 68 | + operation: PATCH_RUN |
| 69 | + runId: ${{ fromJson(github.event.inputs.port_context).runId }} |
| 70 | + logMessage: "Failed to create slack channel: ${{env.CREATE_CHANNEL_ERROR}} ❌" |
| 71 | + |
| 72 | + - name: Log If Create Channel Request is Successful |
| 73 | + uses: port-labs/port-github-action@v1 |
| 74 | + with: |
| 75 | + clientId: ${{ secrets.PORT_CLIENT_ID }} |
| 76 | + clientSecret: ${{ secrets.PORT_CLIENT_SECRET }} |
| 77 | + baseUrl: https://api.getport.io |
| 78 | + operation: PATCH_RUN |
| 79 | + runId: ${{ fromJson(github.event.inputs.port_context).runId }} |
| 80 | + logMessage: "Channel created successfully, channel Id: ${{env.CHANNEL_ID}} ✅" |
| 81 | + |
| 82 | + - name: Checkout code |
| 83 | + uses: actions/checkout@v4 |
| 84 | + |
| 85 | + - name: Add Members to Slack Channel |
| 86 | + id: add_members |
| 87 | + if: success() |
| 88 | + env: |
| 89 | + SLACK_TOKEN: ${{ secrets.BOT_USER_OAUTH_TOKEN }} |
| 90 | + CHANNEL_ID: ${{env.CHANNEL_ID}} |
| 91 | + CLIENT_ID: ${{ secrets.PORT_CLIENT_ID }} |
| 92 | + CLIENT_SECRET: ${{ secrets.PORT_CLIENT_SECRET }} |
| 93 | + RUN_ID: ${{ fromJson(github.event.inputs.port_context).runId }} |
| 94 | + MEMBER_EMAILS: ${{ toJSON(github.event.inputs.members) }} |
| 95 | + run: | |
| 96 | + cd slack |
| 97 | + chmod +x add-members-to-channel.sh |
| 98 | + bash add-members-to-channel.sh "$SLACK_TOKEN" "$CHANNEL_ID" "$CLIENT_ID" "$CLIENT_SECRET" "$RUN_ID" "$MEMBER_EMAILS" |
| 99 | +
|
| 100 | + - name: Log Successful Action |
| 101 | + if: steps.add_members.outcome == 'failure' |
| 102 | + uses: port-labs/port-github-action@v1 |
| 103 | + with: |
| 104 | + clientId: ${{ secrets.PORT_CLIENT_ID }} |
| 105 | + clientSecret: ${{ secrets.PORT_CLIENT_SECRET }} |
| 106 | + baseUrl: https://api.getport.io |
| 107 | + operation: PATCH_RUN |
| 108 | + status: "FAILURE" |
| 109 | + runId: ${{ fromJson(github.event.inputs.port_context).runId }} |
| 110 | + logMessage: "Failed to add members to channel ❌" |
0 commit comments