forked from WordPress/openverse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dag-sync.sh
executable file
·51 lines (46 loc) · 1.82 KB
/
dag-sync.sh
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
#!/bin/bash
# DAG sync script used in production to update the repo files when invoked.
# This can be run using cron at the desired DAG sync interval.
#
# Inputs:
# - The first and only argument to the script should be the Slack hook URL target
# for the output sync message.
#
# Inspired by https://stackoverflow.com/a/21005490/3277713 via torek CC BY-SA 3.0
set -e
SLACK_URL=$1
# https://stackoverflow.com/a/246128 via Dave Dopson CC BY-SA 4.0
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
cd "$SCRIPT_DIR"
# Update origin
git fetch origin
# Get new commit hash *one commit ahead* of this one
new=$(git rev-list --reverse --topo-order HEAD..origin/main | head -1)
# If there is no new commit hash to move to, nothing has changed, quit early
if [ -z "$new" ]; then
echo "No new commits, nothing has changed, nothing to do."
exit
fi
# Move ahead to this new commit
git reset --hard "$new"
# Verify if have /dags/ in the last commit
have_dag=$(git log -p -1 "$new" --pretty=format: --name-only | grep "catalog/dags/")
# If there is no files under /dags/ folder, no need to notify, quit early
if [ -z "$have_dag" ]; then
echo "No changes to DAGs, nothing to do."
exit
fi
# Pull out the subject from the new commit
subject=$(git log -1 --format='%s')
# Swap the < & > characters for their HTML entities so they aren't
# interpreted as delimiters by Slack
subject=${subject//>/>}
subject=${subject//</<}
if [ -z "$SLACK_URL" ]; then
echo "Slack hook was not supplied! Updates will not be posted"
else
curl "$SLACK_URL" \
-X POST \
-H 'Content-Type: application/json' \
-d '{"text":"Deployed: '"$subject"'","username":"DAG Sync","icon_emoji":":recycle:","blocks":[{"type":"section","text":{"type":"mrkdwn","text":"Deployed: <https://github.com/WordPress/openverse/commit/'"$new"'|'"$subject"'>"}}]}'
fi