Skip to content

Weekly Slack Updates #31

Weekly Slack Updates

Weekly Slack Updates #31

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 }}