addSubscriber #2
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: | |
repository_dispatch: | |
types: [addSubscriber] | |
jobs: | |
subscribe: | |
runs-on: ubuntu-latest | |
steps: | |
- 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 | |
id: add_subscriber | |
run: | | |
# Get email from the dispatch event | |
email="${{ github.event.client_payload.email }}" | |
if [[ -z "$email" ]]; then | |
echo "::error::Email is required" | |
exit 1 | |
fi | |
# Validate email format | |
if [[ ! "$email" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then | |
echo "::error::Invalid email format" | |
exit 1 | |
fi | |
# 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 "Initializing new subscribers list" | |
email_list="[]" | |
else | |
email_list=$(echo "$gist_response" | jq -r '.files["subscribers.json"].content | fromjson | .emailList') | |
fi | |
# Check if the email already exists | |
if echo "$email_list" | jq -e --arg email "$email" 'index($email)' > /dev/null; then | |
echo "::error::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]') | |
# Create the JSON payload with metadata | |
payload=$(jq -n \ | |
--argjson list "$updated_email_list" \ | |
--arg timestamp "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \ | |
'{ | |
emailList: $list, | |
lastUpdated: $timestamp, | |
totalSubscribers: ($list | length) | |
}') | |
# Prepare the content for subscribers.json | |
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 | |
} | |
} | |
}") | |
# Check for errors in the response | |
if [[ $(echo "$response" | jq -r '.message // empty') != "" ]]; then | |
echo "::error::Failed to update subscribers list: $(echo "$response" | jq -r '.message')" | |
exit 1 | |
fi | |
# Set output variables | |
echo "status=success" >> $GITHUB_OUTPUT | |
echo "message=Successfully added $email to subscribers list" >> $GITHUB_OUTPUT | |
echo "total_subscribers=$(echo "$updated_email_list" | jq 'length')" >> $GITHUB_OUTPUT | |
- name: Create Issue Comment | |
if: always() | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const outcome = steps.add_subscriber.outcome; | |
const outputs = steps.add_subscriber.outputs; | |
const message = outcome === 'success' | |
? `✅ ${outputs.message}\nTotal subscribers: ${outputs.total_subscribers}` | |
: `❌ Failed to add subscriber: ${steps.add_subscriber.error}`; | |
await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.payload.client_payload.issue_number, | |
body: message | |
}); |