-
-
Notifications
You must be signed in to change notification settings - Fork 2
203 lines (169 loc) · 6.97 KB
/
weekly-updates.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
name: Weekly Slack Updates
concurrency:
group: notify-weekly-updates
cancel-in-progress: true
on:
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
inputs:
date:
description: "Comparation date"
required: true
default: "1 week ago"
type: choice
options:
- "1 week ago"
- "2 weeks ago"
- "3 weeks ago"
- "4 weeks ago"
- "8 weeks ago"
schedule:
# Weekly updates on Monday at 1:00 PM
- cron: "0 13 * * 1"
jobs:
notify:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 16
- name: Setup Deno
uses: denolib/setup-deno@v2
with:
deno-version: v1.30
- name: Download indexes from last week
run: |
REPOS=(
frontend-reference
devops-reference
backend-reference
)
COMPARE_DATE="${{ github.event.inputs.date }}"
[[ -z "$COMPARE_DATE" ]] && COMPARE_DATE="1 week ago"
i=0
for repo in "${REPOS[@]}"; do
github_url="https://github.com/nanlabs/$repo"
file_path=examples.json
filename=examples
extension=json
filename="$filename-$i.$extension"
git clone "$github_url" repo-$i
cd repo-$i
hash=$(git log --before="${COMPARE_DATE}" --format="%H" -n 1)
cd ..
rm -rf repo-$i
curl -s "https://raw.githubusercontent.com/nanlabs/$repo/$hash/$file_path" -o "$filename"
i=$((i + 1))
done
# Get the list of examples previously downloaded
EXAMPLES=$(ls examples-*.json)
# Generate the JSON file
./tools/readme-generator/main.ts --json examples.json $EXAMPLES > previous-week-examples.json
# Remove the downloaded files
rm examples-*.json
# Log the previous week examples
cat previous-week-examples.json
- name: Download current indexes
run: |
URLS=(
https://raw.githubusercontent.com/nanlabs/frontend-reference/main/examples.json
https://raw.githubusercontent.com/nanlabs/devops-reference/main/examples.json
https://raw.githubusercontent.com/nanlabs/backend-reference/main/examples.json
)
# Download files preventing duplicated names (e.g. examples.json)
i=0
for url in "${URLS[@]}"; do
filename=$(basename "$url")
extension="${filename##*.}"
filename="${filename%.*}"
filename="$filename-$i.$extension"
curl -s "$url" -o "$filename"
i=$((i + 1))
done
# Get the list of examples previously downloaded
EXAMPLES=$(ls examples-*.json)
# Generate the JSON file
./tools/readme-generator/main.ts --json examples.json $EXAMPLES > new-examples.json
# Remove the downloaded files
rm examples-*.json
# Log the new examples
cat new-examples.json
- name: Create a list of the new elements from the JSON!
id: slack-message
# Both JSON files have the following structure:
# [
# {
# name: "Example 1",
# description: "This is an example.",
# url: "URL",
# tags: ["Examples > Category 1 > Subcategory 1"],
# labels: ["label1", "label2"],
# },
# {
# name: "Example 2",
# description: "This is another example.",
# url: "URL",
# tags: ["Examples > Category 1 > Subcategory 1"],
# labels: ["label1", "label2"],
# },
# {
# name: "Example 3",
# description: "This is an example.",
# url: "URL",
# tags: ["Examples > Category 2 > Subcategory 2"],
# labels: ["label1", "label2"],
# },
# ]
#
# We need to list which elements are new, so we need to compare the
# previous week JSON file with the current week JSON file. We can do
# this with jq, which is a command-line JSON processor.
# When doing the compare, for each element treat the name concatenated
# with the tags as the unique identifier. Do it for each tag.
run: |
# Get the list of new elements
NEW_ELEMENTS=$(jq -s '.[0] as $o | .[1] | map(select(.name as $n | $o | map(.name) | index($n) | not))' previous-week-examples.json new-examples.json)
# Log the new elements
echo "$NEW_ELEMENTS"
if [[ "$NEW_ELEMENTS" == "[]" ]]; then
echo "No new elements found!"
exit 0
fi
ORIGINAL_NEW_ELEMENTS_LENGTH=$(echo "$NEW_ELEMENTS" | jq length)
NEW_ELEMENTS_SLICE=$(echo "$NEW_ELEMENTS" | jq '.[0:10]')
SHOULD_SHOW_MORE_BEFORE_FOOTER=$(echo "$ORIGINAL_NEW_ELEMENTS_LENGTH > 10" | bc -l)
# Iterate over the new elements and create a beautiful slack message
# with the new elements as a list containing the name, description and a link.
# The link should be added to the name. Also concatenate the labels after joining
# them with a comma.
SLACK_MESSAGE="<!here> :wave: Hi! Here are the new examples we added to Awesome NaN from our last update:\n"
for row in $(echo "${NEW_ELEMENTS_SLICE}" | jq -r '.[] | @base64'); do
_jq() {
echo "${row}" | base64 --decode | jq -r "${1}"
}
name=$(_jq '.name')
description=$(_jq '.description')
url=$(_jq '.url')
labels=$(_jq '.labels | join(", ")')
SLACK_MESSAGE="$SLACK_MESSAGE\n• <$url|$name>: $description ($labels)"
done
if [[ "$SHOULD_SHOW_MORE_BEFORE_FOOTER" == "1" ]]; then
SLACK_MESSAGE="$SLACK_MESSAGE\n\n:point_right: There are more examples, but we are only showing the first 10. Check the full list <https://github.com/nanlabs/awesome-nan|here>."
fi
# Add a footer to the message
SLACK_MESSAGE="$SLACK_MESSAGE\n\n:wave: If you have any questions, please reach out to the Research and Development team! :heart:"
# Log the slack message
echo "$SLACK_MESSAGE"
# Save the slack message as an output variable
echo "slack_message=$SLACK_MESSAGE" >> $GITHUB_OUTPUT
- name: Send slack message
if: steps.slack-message.outputs.slack_message != ''
run: |
SLACK_USERNAME="Awesome NaN Bot"
curl -X POST -H 'Content-type: application/json' --data "{\"text\":\"$SLACK_MESSAGE\",\"username\":\"$SLACK_USERNAME\",\"link_names\":true}" $SLACK_WEBHOOK
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
SLACK_MESSAGE: ${{ steps.slack-message.outputs.slack_message }}