Skip to content

Commit

Permalink
More context in slack message (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall authored Nov 15, 2024
1 parent a92ecc8 commit 3c81373
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 20 deletions.
21 changes: 18 additions & 3 deletions .github/workflows/run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,29 @@ jobs:
job_id=$(echo $jobs | jq -r '.jobs[] | select(.runner_name=="${{ runner.name }}") | .id')
echo "job_id=$job_id" >> $GITHUB_OUTPUT
- name: query google
run: ./scripts/download.sh
env:
URL: ${{ github.event.inputs.url }}
RANGE: ${{ github.event.inputs.range }}
ACCESS_TOKEN: ${{ steps.auth.outputs.access_token }}

- name: Notify Slack on Start
run: ./scripts/slack.sh
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
MESSAGE: ":islandora-logo: :hammer_and_wrench: Workbench job started by ${{ github.actor }}. View details: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}/job/${{ steps.get-job-id.outputs.job_id }}#step:12:1"
MESSAGE: |
${{ github.actor }} started workbench ingest for __TITLE__
Items being ingested: __LINE_COUNT__
Google Sheet: __URL__
Workbench execution log: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}/job/${{ steps.get-job-id.outputs.job_id }}#step:12:1
URL: ${{ github.event.inputs.url }}

- name: download and transform google sheet
run: ./scripts/download-transform.sh
- name: transform google sheet
run: ./scripts/transform.sh
env:
URL: ${{ github.event.inputs.url }}
RANGE: ${{ github.event.inputs.range }}
Expand Down
55 changes: 55 additions & 0 deletions scripts/download.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash

set -eou pipefail

regex='^(https:\/\/docs\.google\.com\/spreadsheets\/d\/[a-zA-Z0-9_-]+)'
if [[ "$URL" =~ $regex ]]; then
URL="${BASH_REMATCH[1]}"
else
echo "Invalid URL"
exit 1
fi

# extract sheet ID from https://docs.google.com/spreadsheets/d/foo/edit?gid=0#gid=0
SHEET_ID=$(echo "$URL" | sed -n 's|.*/d/\(.*\)|\1|p')

attempt=0
max_attempts=5
while [[ "$attempt" -lt "$max_attempts" ]]; do
GSHEET_STATUS=$(curl -s \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-o gsheet.json \
-w '%{http_code}' \
"https://sheets.googleapis.com/v4/spreadsheets/$SHEET_ID/values/$RANGE")

TITLE_STATUS=$(curl -s \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-o response.json \
-w '%{http_code}' \
"https://sheets.googleapis.com/v4/spreadsheets/$SHEET_ID?fields=properties(title)")

if [ "$GSHEET_STATUS" -lt 500 ] && [ "$TITLE_STATUS" -lt 500 ]; then
jq -r .properties.title response.json > title.txt
break
fi

delay=$((attempt * 30))
sleep "$delay"
attempt=$(( attempt + 1))
done

files=("gsheet.json" "title.txt")
for file in "${files[@]}"; do
if [[ ! -f "$file" ]]; then
echo "$file does not exist."
exit 1
elif [[ ! -s "$file" ]]; then
echo "$file is empty."
exit 1
fi
contents=$(cat "$file")
if [ "$contents" = "null" ]; then
echo "$file is null."
exit 1
fi
done
18 changes: 18 additions & 0 deletions scripts/slack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

set -eou pipefail

if [ -v URL ]; then
regex='^(https:\/\/docs\.google\.com\/spreadsheets\/d\/[a-zA-Z0-9_-]+)'
if [[ "$URL" =~ $regex ]]; then
URL="${BASH_REMATCH[1]}"
else
echo "Invalid URL"
exit 1
fi

TITLE=$(cat title.txt)
MESSAGE="${MESSAGE//__TITLE__/$TITLE}"
MESSAGE="${MESSAGE//__URL__/$URL\/edit\?gid=0#gid=0}"

LINE_COUNT=$(jq '.values | length' gsheet.json)
LINE_COUNT=$(( LINE_COUNT - 1))
MESSAGE="${MESSAGE//__LINE_COUNT__/$LINE_COUNT}"
fi

escaped_message=$(echo "$MESSAGE" | jq -Rsa .)
curl -s -o /dev/null -XPOST "$SLACK_WEBHOOK_URL" -d '{
"text": '"$escaped_message"'
Expand Down
22 changes: 5 additions & 17 deletions scripts/download-transform.sh → scripts/transform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,15 @@

set -eou pipefail

regex='^(https:\/\/docs\.google\.com\/spreadsheets\/d\/[a-zA-Z0-9_-]+)'
if [[ "$URL" =~ $regex ]]; then
URL="${BASH_REMATCH[1]}"
else
echo "Invalid URL"
exit 1
fi

# extract sheet ID from https://docs.google.com/spreadsheets/d/foo/edit?gid=0#gid=0
SHEET_ID=$(echo "$URL" | sed -n 's|.*/d/\(.*\)|\1|p')
response=$(curl -s \
"https://sheets.googleapis.com/v4/spreadsheets/$SHEET_ID/values/$RANGE" \
-H "Authorization: Bearer $ACCESS_TOKEN")
GSHEET=$(cat gsheet.json)

if echo "$response" | jq -e .values >/dev/null; then
if echo "$GSHEET" | jq -e .values >/dev/null; then
# save as a CSV
echo "$response" | jq .values | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv' | tail -n +2 > source.csv
echo "$GSHEET" | jq .values | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv' | tail -n +2 > source.csv
# and also as JSON in a format check my work expects
echo "$response" | jq -r '.values | map(map(tostring))' > csv.json
echo "$GSHEET" | jq -r '.values | map(map(tostring))' > csv.json
else
echo "Failed to fetch data: $(echo "$response" | jq -r '.error.message')"
echo "Failed to fetch data: $(echo "$GSHEET" | jq -r '.error.message')"
exit 1
fi

Expand Down

0 comments on commit 3c81373

Please sign in to comment.