Add Subscribers #20
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Add Subscribers | |
on: | |
workflow_dispatch: | |
inputs: | |
email: | |
description: 'Email address to subscribe' | |
required: true | |
default: '[email protected]' | |
jobs: | |
subscribe: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Set up environment variables | |
run: | | |
echo "GIST_ID=${{ secrets.GIST_ID }}" >> $GITHUB_ENV | |
echo "GIST_TOKEN=${{ secrets.GIST_TOKEN }}" >> $GITHUB_ENV | |
- name: Add new subscriber | |
run: | | |
email="${{ github.event.inputs.email }}" | |
# Fetch the existing Gist content | |
gist_response=$(curl -s -X GET "https://api.github.com/gists/$GIST_ID" \ | |
-H "Authorization: token $GIST_TOKEN" \ | |
-H "Accept: application/vnd.github.v3+json") | |
# Extract the current email list | |
if [[ $(echo "$gist_response" | jq -r '.files["subscribers.json"] | .content // empty') == "" ]]; then | |
echo "subscribers.json not found or is empty." | |
email_list="[]" | |
else | |
email_list=$(echo "$gist_response" | jq -r '.files["subscribers.json"].content | fromjson | .emailList') | |
fi | |
# Convert existing email list to an array | |
email_array=($(echo "$email_list" | jq -r '.[]')) | |
# Check if the email already exists | |
if [[ " ${email_array[@]} " =~ " $email " ]]; then | |
echo "Email already exists in the notification list." | |
exit 1 | |
fi | |
# Add the new email to the list | |
updated_email_list=$(echo "$email_list" | jq --arg email "$email" '. += [$email]') | |
# Prepare the JSON payload for updating the Gist | |
payload=$(jq -n --argjson list "$updated_email_list" '{ emailList: $list }') | |
# Prepare the content for subscribers.json as a string | |
content_string=$(echo "$payload" | jq -c '.') | |
# Update Gist with the new list | |
response=$(curl -s -X PATCH "https://api.github.com/gists/$GIST_ID" \ | |
-H "Authorization: token $GIST_TOKEN" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
-H "Content-Type: application/json" \ | |
-d "{\"files\": {\"subscribers.json\": {\"content\": \"$content_string\"}}}") | |
# Output response and subscription confirmation | |
echo "$response" | |
if [[ $(echo "$response" | jq -r '.message // empty') == "" ]]; then | |
echo "Thank you for subscribing!" | |
else | |
echo "Error: $(echo "$response" | jq -r '.message')" | |
fi |