-
-
Notifications
You must be signed in to change notification settings - Fork 1
71 lines (58 loc) · 2.58 KB
/
manual.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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